Coverage Report

Created: 2026-06-30 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/open62541_15/plugins/ua_nodestore_ziptree.c
Line
Count
Source
1
/* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
2
 * See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
3
 *
4
 *    Copyright 2014-2018 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
5
 *    Copyright 2017 (c) Julian Grothoff
6
 *    Copyright 2017 (c) Stefan Profanter, fortiss GmbH
7
 */
8
9
#include <open62541/server.h>
10
#include <open62541/plugin/nodestore.h>
11
#include <open62541/plugin/nodestore_default.h>
12
#include "ziptree.h"
13
#include "pcg_basic.h"
14
15
#ifndef container_of
16
#define container_of(ptr, type, member) \
17
567M
    (type *)((uintptr_t)ptr - offsetof(type,member))
18
#endif
19
20
struct NodeEntry;
21
typedef struct NodeEntry NodeEntry;
22
23
struct NodeEntry {
24
    ZIP_ENTRY(NodeEntry) zipfields;
25
    UA_UInt32 nodeIdHash;
26
    UA_UInt16 refCount; /* How many consumers have a reference to the node? */
27
    UA_Boolean deleted; /* Node was marked as deleted and can be deleted when refCount == 0 */
28
    NodeEntry *orig;    /* If a copy is made to replace a node, track that we
29
                         * replace only the node from which the copy was made.
30
                         * Important for concurrent operations. */
31
    UA_NodeId nodeId; /* This is actually a UA_Node that also starts with a NodeId */
32
};
33
34
/* Absolute ordering for NodeIds */
35
static enum ZIP_CMP
36
6.34G
cmpNodeId(const void *a, const void *b) {
37
6.34G
    const NodeEntry *aa = (const NodeEntry*)a;
38
6.34G
    const NodeEntry *bb = (const NodeEntry*)b;
39
40
    /* Compare hash */
41
6.34G
    if(aa->nodeIdHash < bb->nodeIdHash)
42
2.74G
        return ZIP_CMP_LESS;
43
3.59G
    if(aa->nodeIdHash > bb->nodeIdHash)
44
3.03G
        return ZIP_CMP_MORE;
45
46
    /* Compore nodes in detail */
47
555M
    return (enum ZIP_CMP)UA_NodeId_order(&aa->nodeId, &bb->nodeId);
48
3.59G
}
49
50
ZIP_HEAD(NodeTree, NodeEntry);
51
typedef struct NodeTree NodeTree;
52
53
typedef struct {
54
    UA_Nodestore ns;
55
56
    NodeTree root;
57
    size_t size;
58
59
    /* Maps ReferenceTypeIndex to the NodeId of the ReferenceType */
60
    UA_NodeId referenceTypeIds[UA_REFERENCETYPESET_MAX];
61
    UA_Byte referenceTypeCounter;
62
} ZipNodestore;
63
64
12.0M
ZIP_FUNCTIONS(NodeTree, NodeEntry, zipfields, NodeEntry, zipfields, cmpNodeId)
ua_nodestore_ziptree.c:NodeTree_ZIP_ITER
Line
Count
Source
64
ZIP_FUNCTIONS(NodeTree, NodeEntry, zipfields, NodeEntry, zipfields, cmpNodeId)
ua_nodestore_ziptree.c:NodeTree_ZIP_INSERT
Line
Count
Source
64
ZIP_FUNCTIONS(NodeTree, NodeEntry, zipfields, NodeEntry, zipfields, cmpNodeId)
ua_nodestore_ziptree.c:NodeTree_ZIP_REMOVE
Line
Count
Source
64
ZIP_FUNCTIONS(NodeTree, NodeEntry, zipfields, NodeEntry, zipfields, cmpNodeId)
65
66
static NodeEntry *
67
11.7M
newEntry(UA_NodeClass nodeClass) {
68
11.7M
    size_t size = sizeof(NodeEntry) - sizeof(UA_NodeId);
69
11.7M
    switch(nodeClass) {
70
970k
    case UA_NODECLASS_OBJECT:
71
970k
        size += sizeof(UA_ObjectNode);
72
970k
        break;
73
7.98M
    case UA_NODECLASS_VARIABLE:
74
7.98M
        size += sizeof(UA_VariableNode);
75
7.98M
        break;
76
476k
    case UA_NODECLASS_METHOD:
77
476k
        size += sizeof(UA_MethodNode);
78
476k
        break;
79
708k
    case UA_NODECLASS_OBJECTTYPE:
80
708k
        size += sizeof(UA_ObjectTypeNode);
81
708k
        break;
82
334k
    case UA_NODECLASS_VARIABLETYPE:
83
334k
        size += sizeof(UA_VariableTypeNode);
84
334k
        break;
85
373k
    case UA_NODECLASS_REFERENCETYPE:
86
373k
        size += sizeof(UA_ReferenceTypeNode);
87
373k
        break;
88
939k
    case UA_NODECLASS_DATATYPE:
89
939k
        size += sizeof(UA_DataTypeNode);
90
939k
        break;
91
0
    case UA_NODECLASS_VIEW:
92
0
        size += sizeof(UA_ViewNode);
93
0
        break;
94
0
    default:
95
0
        return NULL;
96
11.7M
    }
97
11.7M
    NodeEntry *entry = (NodeEntry*)UA_calloc(1, size);
98
11.7M
    if(!entry)
99
0
        return NULL;
100
11.7M
    UA_Node *node = (UA_Node*)&entry->nodeId;
101
11.7M
    node->head.nodeClass = nodeClass;
102
11.7M
    return entry;
103
11.7M
}
104
105
static void
106
11.7M
deleteEntry(NodeEntry *entry) {
107
11.7M
    UA_Node_clear((UA_Node*)&entry->nodeId);
108
11.7M
    UA_free(entry);
109
11.7M
}
110
111
static void
112
554M
cleanupEntry(NodeEntry *entry) {
113
554M
    if(entry->refCount > 0)
114
138M
        return;
115
416M
    if(entry->deleted) {
116
250k
        deleteEntry(entry);
117
250k
        return;
118
250k
    }
119
416M
    UA_NodeHead *head = (UA_NodeHead*)&entry->nodeId;
120
1.40G
    for(size_t i = 0; i < head->referencesSize; i++) {
121
985M
        UA_NodeReferenceKind *rk = &head->references[i];
122
985M
        if(rk->targetsSize > 16 && !rk->hasRefTree)
123
172k
            UA_NodeReferenceKind_switch(rk);
124
985M
    }
125
416M
}
126
127
/***********************/
128
/* Interface functions */
129
/***********************/
130
131
/* Not yet inserted into the ZipContext */
132
static UA_Node *
133
11.1M
zipNsNewNode(UA_Nodestore *_, UA_NodeClass nodeClass) {
134
11.1M
    NodeEntry *entry = newEntry(nodeClass);
135
11.1M
    if(!entry)
136
0
        return NULL;
137
11.1M
    return (UA_Node*)&entry->nodeId;
138
11.1M
}
139
140
/* Not yet inserted into the ZipContext */
141
static void
142
0
zipNsDeleteNode(UA_Nodestore *_, UA_Node *node) {
143
0
    deleteEntry(container_of(node, NodeEntry, nodeId));
144
0
}
145
146
static const UA_Node *
147
zipNsGetNode(UA_Nodestore *ns, const UA_NodeId *nodeId,
148
             UA_UInt32 attributeMask,
149
             UA_ReferenceTypeSet references,
150
555M
             UA_BrowseDirection referenceDirections) {
151
555M
    NodeEntry dummy;
152
555M
    dummy.nodeIdHash = UA_NodeId_hash(nodeId);
153
555M
    dummy.nodeId = *nodeId;
154
555M
    ZipNodestore *zns = (ZipNodestore*)ns;
155
555M
    NodeEntry *entry = ZIP_FIND(NodeTree, &zns->root, &dummy);
156
555M
    if(!entry)
157
489k
        return NULL;
158
554M
    ++entry->refCount;
159
554M
    return (const UA_Node*)&entry->nodeId;
160
555M
}
161
162
static const UA_Node *
163
zipNsGetNodeFromPtr(UA_Nodestore *ns, UA_NodePointer ptr,
164
                    UA_UInt32 attributeMask,
165
                    UA_ReferenceTypeSet references,
166
227M
                    UA_BrowseDirection referenceDirections) {
167
227M
    if(!UA_NodePointer_isLocal(ptr))
168
0
        return NULL;
169
227M
    UA_NodeId id = UA_NodePointer_toNodeId(ptr);
170
227M
    return zipNsGetNode(ns, &id, attributeMask,
171
227M
                        references, referenceDirections);
172
227M
}
173
174
static void
175
554M
zipNsReleaseNode(UA_Nodestore *_, const UA_Node *node) {
176
554M
    if(!node)
177
0
        return;
178
554M
    NodeEntry *entry = container_of(node, NodeEntry, nodeId);
179
554M
    UA_assert(entry->refCount > 0);
180
554M
    --entry->refCount;
181
554M
    cleanupEntry(entry);
182
554M
}
183
184
static UA_StatusCode
185
zipNsGetNodeCopy(UA_Nodestore *ns, const UA_NodeId *nodeId,
186
589k
                 UA_Node **outNode) {
187
    /* Get the node (with all attributes and references, the mask and refs are
188
       currently noy evaluated within the plugin.) */
189
589k
    const UA_Node *node =
190
589k
        zipNsGetNode(ns, nodeId, UA_NODEATTRIBUTESMASK_ALL,
191
589k
                     UA_REFERENCETYPESET_ALL, UA_BROWSEDIRECTION_BOTH);
192
589k
    if(!node)
193
0
        return UA_STATUSCODE_BADNODEIDUNKNOWN;
194
195
    /* Create the new entry */
196
589k
    NodeEntry *ne = newEntry(node->head.nodeClass);
197
589k
    if(!ne) {
198
0
        zipNsReleaseNode(ns, node);
199
0
        return UA_STATUSCODE_BADOUTOFMEMORY;
200
0
    }
201
202
    /* Copy the node content */
203
589k
    UA_Node *nnode = (UA_Node*)&ne->nodeId;
204
589k
    UA_StatusCode retval = UA_Node_copy(node, nnode);
205
589k
    zipNsReleaseNode(NULL, node);
206
589k
    if(retval != UA_STATUSCODE_GOOD) {
207
0
        deleteEntry(ne);
208
0
        return retval;
209
0
    }
210
211
589k
    ne->orig = container_of(node, NodeEntry, nodeId);
212
589k
    *outNode = nnode;
213
589k
    return UA_STATUSCODE_GOOD;
214
589k
}
215
216
static UA_StatusCode
217
11.7M
zipNsInsertNode(UA_Nodestore *ns, UA_Node *node, UA_NodeId *addedNodeId) {
218
11.7M
    NodeEntry *entry = container_of(node, NodeEntry, nodeId);
219
11.7M
    ZipNodestore *zns = (ZipNodestore*)ns;
220
221
    /* Ensure that the NodeId is unique by testing their presence. If the NodeId
222
     * is ns=xx;i=0, then the numeric identifier is replaced with a random
223
     * unused int32. It is ensured that the created identifiers are stable after
224
     * a server restart (assuming that Nodes are created in the same order and
225
     * with the same BrowseName). */
226
11.7M
    NodeEntry dummy;
227
11.7M
    memset(&dummy, 0, sizeof(NodeEntry));
228
11.7M
    dummy.nodeId = node->head.nodeId;
229
11.7M
    if(node->head.nodeId.identifierType == UA_NODEIDTYPE_NUMERIC &&
230
11.7M
       node->head.nodeId.identifier.numeric == 0) {
231
589k
        NodeEntry *found;
232
589k
        UA_UInt32 mask = 0x2F;
233
589k
        pcg32_random_t rng;
234
589k
        pcg32_srandom_r(&rng, zns->size, 0);
235
1.51M
        do {
236
            /* Generate a random NodeId. Favor "easy" NodeIds.
237
             * Always above 50000. */
238
1.51M
            UA_UInt32 numId = (pcg32_random_r(&rng) & mask) + 50000;
239
240
#if SIZE_MAX <= UA_UINT32_MAX
241
            /* The compressed "immediate" representation of nodes does not
242
             * support the full range on 32bit systems. Generate smaller
243
             * identifiers as they can be stored more compactly. */
244
            if(numId >= (0x01 << 24))
245
                numId = numId % (0x01 << 24);
246
#endif
247
1.51M
            node->head.nodeId.identifier.numeric = numId;
248
249
            /* Look up the current NodeId */
250
1.51M
            dummy.nodeId.identifier.numeric = numId;
251
1.51M
            dummy.nodeIdHash = UA_NodeId_hash(&node->head.nodeId);
252
1.51M
            found = ZIP_FIND(NodeTree, &zns->root, &dummy);
253
254
1.51M
            if(found) {
255
                /* Reseed the rng using the browseName of the existing node.
256
                 * This ensures that different information models end up with
257
                 * different NodeId sequences, but still stable after a
258
                 * restart. */
259
922k
                UA_NodeHead *nh = (UA_NodeHead*)&found->nodeId;
260
922k
                pcg32_srandom_r(&rng, rng.state, UA_QualifiedName_hash(&nh->browseName));
261
262
                /* Make the mask less strict when the NodeId already exists */
263
922k
                mask = (mask << 1) | 0x01;
264
922k
            }
265
1.51M
        } while(found);
266
11.1M
    } else {
267
11.1M
        dummy.nodeIdHash = UA_NodeId_hash(&node->head.nodeId);
268
11.1M
        if(ZIP_FIND(NodeTree, &zns->root, &dummy)) { /* The nodeid exists */
269
0
            deleteEntry(entry);
270
0
            return UA_STATUSCODE_BADNODEIDEXISTS;
271
0
        }
272
11.1M
    }
273
274
    /* Copy the NodeId */
275
11.7M
    if(addedNodeId) {
276
11.7M
        UA_StatusCode retval = UA_NodeId_copy(&node->head.nodeId, addedNodeId);
277
11.7M
        if(retval != UA_STATUSCODE_GOOD) {
278
0
            deleteEntry(entry);
279
0
            return retval;
280
0
        }
281
11.7M
    }
282
283
    /* For new ReferencetypeNodes add to the index map */
284
11.7M
    if(node->head.nodeClass == UA_NODECLASS_REFERENCETYPE) {
285
373k
        UA_ReferenceTypeNode *refNode = &node->referenceTypeNode;
286
373k
        if(zns->referenceTypeCounter >= UA_REFERENCETYPESET_MAX) {
287
0
            deleteEntry(entry);
288
0
            return UA_STATUSCODE_BADINTERNALERROR;
289
0
        }
290
291
373k
        UA_StatusCode retval =
292
373k
            UA_NodeId_copy(&node->head.nodeId,
293
373k
                           &zns->referenceTypeIds[zns->referenceTypeCounter]);
294
373k
        if(retval != UA_STATUSCODE_GOOD) {
295
0
            deleteEntry(entry);
296
0
            return UA_STATUSCODE_BADINTERNALERROR;
297
0
        }
298
299
        /* Assign the ReferenceTypeIndex to the new ReferenceTypeNode */
300
373k
        refNode->referenceTypeIndex = zns->referenceTypeCounter;
301
373k
        refNode->subTypes = UA_REFTYPESET(zns->referenceTypeCounter);
302
373k
        zns->referenceTypeCounter++;
303
373k
    }
304
305
    /* Insert the node */
306
11.7M
    entry->nodeIdHash = dummy.nodeIdHash;
307
11.7M
    ZIP_INSERT(NodeTree, &zns->root, entry);
308
11.7M
    zns->size++;
309
11.7M
    return UA_STATUSCODE_GOOD;
310
11.7M
}
311
312
static UA_StatusCode
313
0
zipNsReplaceNode(UA_Nodestore *ns, UA_Node *node) {
314
    /* Find the node (the mask and refs are not evaluated yet by the plugin)*/
315
0
    const UA_Node *oldNode =
316
0
        zipNsGetNode(ns, &node->head.nodeId, UA_NODEATTRIBUTESMASK_ALL,
317
0
                     UA_REFERENCETYPESET_ALL, UA_BROWSEDIRECTION_BOTH);
318
0
    if(!oldNode) {
319
0
        deleteEntry(container_of(node, NodeEntry, nodeId));
320
0
        return UA_STATUSCODE_BADNODEIDUNKNOWN;
321
0
    }
322
323
    /* Test if the copy is current */
324
0
    NodeEntry *entry = container_of(node, NodeEntry, nodeId);
325
0
    NodeEntry *oldEntry = container_of(oldNode, NodeEntry, nodeId);
326
0
    if(oldEntry != entry->orig) {
327
        /* The node was already updated since the copy was made */
328
0
        deleteEntry(entry);
329
0
        zipNsReleaseNode(NULL, oldNode);
330
0
        return UA_STATUSCODE_BADINTERNALERROR;
331
0
    }
332
333
    /* Replace */
334
0
    ZipNodestore *zns = (ZipNodestore*)ns;
335
0
    ZIP_REMOVE(NodeTree, &zns->root, oldEntry);
336
0
    entry->nodeIdHash = oldEntry->nodeIdHash;
337
0
    ZIP_INSERT(NodeTree, &zns->root, entry);
338
0
    oldEntry->deleted = true;
339
340
0
    zipNsReleaseNode(NULL, oldNode);
341
0
    return UA_STATUSCODE_GOOD;
342
0
}
343
344
static UA_StatusCode
345
250k
zipNsRemoveNode(UA_Nodestore *ns, const UA_NodeId *nodeId) {
346
250k
    ZipNodestore *zns = (ZipNodestore*)ns;
347
250k
    NodeEntry dummy;
348
250k
    dummy.nodeIdHash = UA_NodeId_hash(nodeId);
349
250k
    dummy.nodeId = *nodeId;
350
250k
    NodeEntry *entry = ZIP_FIND(NodeTree, &zns->root, &dummy);
351
250k
    if(!entry)
352
0
        return UA_STATUSCODE_BADNODEIDUNKNOWN;
353
250k
    ZIP_REMOVE(NodeTree, &zns->root, entry);
354
250k
    zns->size--;
355
250k
    entry->deleted = true;
356
250k
    cleanupEntry(entry);
357
250k
    return UA_STATUSCODE_GOOD;
358
250k
}
359
360
static const UA_NodeId *
361
7.51M
zipNsGetReferenceTypeId(UA_Nodestore *ns, UA_Byte refTypeIndex) {
362
7.51M
    ZipNodestore *zns = (ZipNodestore*)ns;
363
7.51M
    if(refTypeIndex >= zns->referenceTypeCounter)
364
0
        return NULL;
365
7.51M
    return &zns->referenceTypeIds[refTypeIndex];
366
7.51M
}
367
368
struct VisitorData {
369
    UA_NodestoreVisitor visitor;
370
    void *visitorContext;
371
};
372
373
static void *
374
0
nodeVisitor(void *data, NodeEntry *entry) {
375
0
    struct VisitorData *d = (struct VisitorData*)data;
376
0
    d->visitor(d->visitorContext, (UA_Node*)&entry->nodeId);
377
0
    return NULL;
378
0
}
379
380
static void
381
zipNsIterate(UA_Nodestore *ns, UA_NodestoreVisitor visitor,
382
0
             void *visitorCtx) {
383
0
    struct VisitorData d;
384
0
    d.visitor = visitor;
385
0
    d.visitorContext = visitorCtx;
386
0
    ZipNodestore *zns = (ZipNodestore*)ns;
387
0
    ZIP_ITER(NodeTree, &zns->root, nodeVisitor, &d);
388
0
}
389
390
static void *
391
11.5M
deleteNodeVisitor(void *data, NodeEntry *entry) {
392
11.5M
    deleteEntry(entry);
393
11.5M
    return NULL;
394
11.5M
}
395
396
/***********************/
397
/* Nodestore Lifecycle */
398
/***********************/
399
400
static void
401
14.4k
zipNsFree(UA_Nodestore *ns) {
402
14.4k
    ZipNodestore *zns = (ZipNodestore*)ns;
403
14.4k
    ZIP_ITER(NodeTree, &zns->root, deleteNodeVisitor, NULL);
404
405
    /* Clean up the ReferenceTypes index array */
406
387k
    for(size_t i = 0; i < zns->referenceTypeCounter; i++)
407
373k
        UA_NodeId_clear(&zns->referenceTypeIds[i]);
408
409
14.4k
    UA_free(zns);
410
14.4k
}
411
412
UA_Nodestore *
413
14.4k
UA_Nodestore_ZipTree(void) {
414
    /* Allocate and initialize the context */
415
14.4k
    ZipNodestore *zns = (ZipNodestore*)UA_calloc(1, sizeof(ZipNodestore));
416
14.4k
    if(!zns)
417
0
        return NULL;
418
419
14.4k
    ZIP_INIT(&zns->root);
420
14.4k
    zns->referenceTypeCounter = 0;
421
422
    /* Populate the nodestore */
423
14.4k
    zns->ns.free = zipNsFree;
424
14.4k
    zns->ns.newNode = zipNsNewNode;
425
14.4k
    zns->ns.deleteNode = zipNsDeleteNode;
426
14.4k
    zns->ns.getNode = zipNsGetNode;
427
14.4k
    zns->ns.getNodeFromPtr = zipNsGetNodeFromPtr;
428
14.4k
    zns->ns.releaseNode = zipNsReleaseNode;
429
14.4k
    zns->ns.getNodeCopy = zipNsGetNodeCopy;
430
14.4k
    zns->ns.insertNode = zipNsInsertNode;
431
14.4k
    zns->ns.replaceNode = zipNsReplaceNode;
432
14.4k
    zns->ns.removeNode = zipNsRemoveNode;
433
14.4k
    zns->ns.getReferenceTypeId = zipNsGetReferenceTypeId;
434
14.4k
    zns->ns.iterate = zipNsIterate;
435
436
    /* All nodes are stored in RAM. Changes are made in-situ. GetEditNode is
437
     * identical to GetNode -- but the Node pointer is non-const. */
438
14.4k
    zns->ns.getEditNode =
439
14.4k
        (UA_Node * (*)(UA_Nodestore *ns, const UA_NodeId *nodeId,
440
14.4k
                       UA_UInt32 attributeMask,
441
14.4k
                       UA_ReferenceTypeSet references,
442
14.4k
                       UA_BrowseDirection referenceDirections))zipNsGetNode;
443
14.4k
    zns->ns.getEditNodeFromPtr =
444
14.4k
        (UA_Node * (*)(UA_Nodestore *ns, UA_NodePointer ptr,
445
14.4k
                       UA_UInt32 attributeMask,
446
14.4k
                       UA_ReferenceTypeSet references,
447
14.4k
                       UA_BrowseDirection referenceDirections))zipNsGetNodeFromPtr;
448
449
14.4k
    return &zns->ns;
450
14.4k
}