Coverage Report

Created: 2026-06-09 06:15

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
369M
    (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
4.08G
cmpNodeId(const void *a, const void *b) {
37
4.08G
    const NodeEntry *aa = (const NodeEntry*)a;
38
4.08G
    const NodeEntry *bb = (const NodeEntry*)b;
39
40
    /* Compare hash */
41
4.08G
    if(aa->nodeIdHash < bb->nodeIdHash)
42
1.78G
        return ZIP_CMP_LESS;
43
2.30G
    if(aa->nodeIdHash > bb->nodeIdHash)
44
1.94G
        return ZIP_CMP_MORE;
45
46
    /* Compore nodes in detail */
47
362M
    return (enum ZIP_CMP)UA_NodeId_order(&aa->nodeId, &bb->nodeId);
48
2.30G
}
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
7.81M
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
7.80M
newEntry(UA_NodeClass nodeClass) {
68
7.80M
    size_t size = sizeof(NodeEntry) - sizeof(UA_NodeId);
69
7.80M
    switch(nodeClass) {
70
653k
    case UA_NODECLASS_OBJECT:
71
653k
        size += sizeof(UA_ObjectNode);
72
653k
        break;
73
5.23M
    case UA_NODECLASS_VARIABLE:
74
5.23M
        size += sizeof(UA_VariableNode);
75
5.23M
        break;
76
322k
    case UA_NODECLASS_METHOD:
77
322k
        size += sizeof(UA_MethodNode);
78
322k
        break;
79
478k
    case UA_NODECLASS_OBJECTTYPE:
80
478k
        size += sizeof(UA_ObjectTypeNode);
81
478k
        break;
82
226k
    case UA_NODECLASS_VARIABLETYPE:
83
226k
        size += sizeof(UA_VariableTypeNode);
84
226k
        break;
85
252k
    case UA_NODECLASS_REFERENCETYPE:
86
252k
        size += sizeof(UA_ReferenceTypeNode);
87
252k
        break;
88
635k
    case UA_NODECLASS_DATATYPE:
89
635k
        size += sizeof(UA_DataTypeNode);
90
635k
        break;
91
0
    case UA_NODECLASS_VIEW:
92
0
        size += sizeof(UA_ViewNode);
93
0
        break;
94
0
    default:
95
0
        return NULL;
96
7.80M
    }
97
7.80M
    NodeEntry *entry = (NodeEntry*)UA_calloc(1, size);
98
7.80M
    if(!entry)
99
0
        return NULL;
100
7.80M
    UA_Node *node = (UA_Node*)&entry->nodeId;
101
7.80M
    node->head.nodeClass = nodeClass;
102
7.80M
    return entry;
103
7.80M
}
104
105
static void
106
7.80M
deleteEntry(NodeEntry *entry) {
107
7.80M
    UA_Node_clear((UA_Node*)&entry->nodeId);
108
7.80M
    UA_free(entry);
109
7.80M
}
110
111
static void
112
361M
cleanupEntry(NodeEntry *entry) {
113
361M
    if(entry->refCount > 0)
114
90.9M
        return;
115
271M
    if(entry->deleted) {
116
3.71k
        deleteEntry(entry);
117
3.71k
        return;
118
3.71k
    }
119
271M
    UA_NodeHead *head = (UA_NodeHead*)&entry->nodeId;
120
908M
    for(size_t i = 0; i < head->referencesSize; i++) {
121
637M
        UA_NodeReferenceKind *rk = &head->references[i];
122
637M
        if(rk->targetsSize > 16 && !rk->hasRefTree)
123
113k
            UA_NodeReferenceKind_switch(rk);
124
637M
    }
125
271M
}
126
127
/***********************/
128
/* Interface functions */
129
/***********************/
130
131
/* Not yet inserted into the ZipContext */
132
static UA_Node *
133
7.56M
zipNsNewNode(UA_Nodestore *_, UA_NodeClass nodeClass) {
134
7.56M
    NodeEntry *entry = newEntry(nodeClass);
135
7.56M
    if(!entry)
136
0
        return NULL;
137
7.56M
    return (UA_Node*)&entry->nodeId;
138
7.56M
}
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
362M
             UA_BrowseDirection referenceDirections) {
151
362M
    NodeEntry dummy;
152
362M
    dummy.nodeIdHash = UA_NodeId_hash(nodeId);
153
362M
    dummy.nodeId = *nodeId;
154
362M
    ZipNodestore *zns = (ZipNodestore*)ns;
155
362M
    NodeEntry *entry = ZIP_FIND(NodeTree, &zns->root, &dummy);
156
362M
    if(!entry)
157
330k
        return NULL;
158
361M
    ++entry->refCount;
159
361M
    return (const UA_Node*)&entry->nodeId;
160
362M
}
161
162
static const UA_Node *
163
zipNsGetNodeFromPtr(UA_Nodestore *ns, UA_NodePointer ptr,
164
                    UA_UInt32 attributeMask,
165
                    UA_ReferenceTypeSet references,
166
149M
                    UA_BrowseDirection referenceDirections) {
167
149M
    if(!UA_NodePointer_isLocal(ptr))
168
0
        return NULL;
169
149M
    UA_NodeId id = UA_NodePointer_toNodeId(ptr);
170
149M
    return zipNsGetNode(ns, &id, attributeMask,
171
149M
                        references, referenceDirections);
172
149M
}
173
174
static void
175
361M
zipNsReleaseNode(UA_Nodestore *_, const UA_Node *node) {
176
361M
    if(!node)
177
0
        return;
178
361M
    NodeEntry *entry = container_of(node, NodeEntry, nodeId);
179
361M
    UA_assert(entry->refCount > 0);
180
361M
    --entry->refCount;
181
361M
    cleanupEntry(entry);
182
361M
}
183
184
static UA_StatusCode
185
zipNsGetNodeCopy(UA_Nodestore *ns, const UA_NodeId *nodeId,
186
235k
                 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
235k
    const UA_Node *node =
190
235k
        zipNsGetNode(ns, nodeId, UA_NODEATTRIBUTESMASK_ALL,
191
235k
                     UA_REFERENCETYPESET_ALL, UA_BROWSEDIRECTION_BOTH);
192
235k
    if(!node)
193
0
        return UA_STATUSCODE_BADNODEIDUNKNOWN;
194
195
    /* Create the new entry */
196
235k
    NodeEntry *ne = newEntry(node->head.nodeClass);
197
235k
    if(!ne) {
198
0
        zipNsReleaseNode(ns, node);
199
0
        return UA_STATUSCODE_BADOUTOFMEMORY;
200
0
    }
201
202
    /* Copy the node content */
203
235k
    UA_Node *nnode = (UA_Node*)&ne->nodeId;
204
235k
    UA_StatusCode retval = UA_Node_copy(node, nnode);
205
235k
    zipNsReleaseNode(NULL, node);
206
235k
    if(retval != UA_STATUSCODE_GOOD) {
207
0
        deleteEntry(ne);
208
0
        return retval;
209
0
    }
210
211
235k
    ne->orig = container_of(node, NodeEntry, nodeId);
212
235k
    *outNode = nnode;
213
235k
    return UA_STATUSCODE_GOOD;
214
235k
}
215
216
static UA_StatusCode
217
7.80M
zipNsInsertNode(UA_Nodestore *ns, UA_Node *node, UA_NodeId *addedNodeId) {
218
7.80M
    NodeEntry *entry = container_of(node, NodeEntry, nodeId);
219
7.80M
    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
7.80M
    NodeEntry dummy;
227
7.80M
    memset(&dummy, 0, sizeof(NodeEntry));
228
7.80M
    dummy.nodeId = node->head.nodeId;
229
7.80M
    if(node->head.nodeId.identifierType == UA_NODEIDTYPE_NUMERIC &&
230
7.80M
       node->head.nodeId.identifier.numeric == 0) {
231
235k
        NodeEntry *found;
232
235k
        UA_UInt32 mask = 0x2F;
233
235k
        pcg32_random_t rng;
234
235k
        pcg32_srandom_r(&rng, zns->size, 0);
235
339k
        do {
236
            /* Generate a random NodeId. Favor "easy" NodeIds.
237
             * Always above 50000. */
238
339k
            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
339k
            node->head.nodeId.identifier.numeric = numId;
248
249
            /* Look up the current NodeId */
250
339k
            dummy.nodeId.identifier.numeric = numId;
251
339k
            dummy.nodeIdHash = UA_NodeId_hash(&node->head.nodeId);
252
339k
            found = ZIP_FIND(NodeTree, &zns->root, &dummy);
253
254
339k
            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
104k
                UA_NodeHead *nh = (UA_NodeHead*)&found->nodeId;
260
104k
                pcg32_srandom_r(&rng, rng.state, UA_QualifiedName_hash(&nh->browseName));
261
262
                /* Make the mask less strict when the NodeId already exists */
263
104k
                mask = (mask << 1) | 0x01;
264
104k
            }
265
339k
        } while(found);
266
7.56M
    } else {
267
7.56M
        dummy.nodeIdHash = UA_NodeId_hash(&node->head.nodeId);
268
7.56M
        if(ZIP_FIND(NodeTree, &zns->root, &dummy)) { /* The nodeid exists */
269
0
            deleteEntry(entry);
270
0
            return UA_STATUSCODE_BADNODEIDEXISTS;
271
0
        }
272
7.56M
    }
273
274
    /* Copy the NodeId */
275
7.80M
    if(addedNodeId) {
276
7.80M
        UA_StatusCode retval = UA_NodeId_copy(&node->head.nodeId, addedNodeId);
277
7.80M
        if(retval != UA_STATUSCODE_GOOD) {
278
0
            deleteEntry(entry);
279
0
            return retval;
280
0
        }
281
7.80M
    }
282
283
    /* For new ReferencetypeNodes add to the index map */
284
7.80M
    if(node->head.nodeClass == UA_NODECLASS_REFERENCETYPE) {
285
252k
        UA_ReferenceTypeNode *refNode = &node->referenceTypeNode;
286
252k
        if(zns->referenceTypeCounter >= UA_REFERENCETYPESET_MAX) {
287
0
            deleteEntry(entry);
288
0
            return UA_STATUSCODE_BADINTERNALERROR;
289
0
        }
290
291
252k
        UA_StatusCode retval =
292
252k
            UA_NodeId_copy(&node->head.nodeId,
293
252k
                           &zns->referenceTypeIds[zns->referenceTypeCounter]);
294
252k
        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
252k
        refNode->referenceTypeIndex = zns->referenceTypeCounter;
301
252k
        refNode->subTypes = UA_REFTYPESET(zns->referenceTypeCounter);
302
252k
        zns->referenceTypeCounter++;
303
252k
    }
304
305
    /* Insert the node */
306
7.80M
    entry->nodeIdHash = dummy.nodeIdHash;
307
7.80M
    ZIP_INSERT(NodeTree, &zns->root, entry);
308
7.80M
    zns->size++;
309
7.80M
    return UA_STATUSCODE_GOOD;
310
7.80M
}
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
3.71k
zipNsRemoveNode(UA_Nodestore *ns, const UA_NodeId *nodeId) {
346
3.71k
    ZipNodestore *zns = (ZipNodestore*)ns;
347
3.71k
    NodeEntry dummy;
348
3.71k
    dummy.nodeIdHash = UA_NodeId_hash(nodeId);
349
3.71k
    dummy.nodeId = *nodeId;
350
3.71k
    NodeEntry *entry = ZIP_FIND(NodeTree, &zns->root, &dummy);
351
3.71k
    if(!entry)
352
0
        return UA_STATUSCODE_BADNODEIDUNKNOWN;
353
3.71k
    ZIP_REMOVE(NodeTree, &zns->root, entry);
354
3.71k
    zns->size--;
355
3.71k
    entry->deleted = true;
356
3.71k
    cleanupEntry(entry);
357
3.71k
    return UA_STATUSCODE_GOOD;
358
3.71k
}
359
360
static const UA_NodeId *
361
4.26M
zipNsGetReferenceTypeId(UA_Nodestore *ns, UA_Byte refTypeIndex) {
362
4.26M
    ZipNodestore *zns = (ZipNodestore*)ns;
363
4.26M
    if(refTypeIndex >= zns->referenceTypeCounter)
364
0
        return NULL;
365
4.26M
    return &zns->referenceTypeIds[refTypeIndex];
366
4.26M
}
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
7.79M
deleteNodeVisitor(void *data, NodeEntry *entry) {
392
7.79M
    deleteEntry(entry);
393
7.79M
    return NULL;
394
7.79M
}
395
396
/***********************/
397
/* Nodestore Lifecycle */
398
/***********************/
399
400
static void
401
10.2k
zipNsFree(UA_Nodestore *ns) {
402
10.2k
    ZipNodestore *zns = (ZipNodestore*)ns;
403
10.2k
    ZIP_ITER(NodeTree, &zns->root, deleteNodeVisitor, NULL);
404
405
    /* Clean up the ReferenceTypes index array */
406
262k
    for(size_t i = 0; i < zns->referenceTypeCounter; i++)
407
252k
        UA_NodeId_clear(&zns->referenceTypeIds[i]);
408
409
10.2k
    UA_free(zns);
410
10.2k
}
411
412
UA_Nodestore *
413
10.2k
UA_Nodestore_ZipTree(void) {
414
    /* Allocate and initialize the context */
415
10.2k
    ZipNodestore *zns = (ZipNodestore*)UA_calloc(1, sizeof(ZipNodestore));
416
10.2k
    if(!zns)
417
0
        return NULL;
418
419
10.2k
    ZIP_INIT(&zns->root);
420
10.2k
    zns->referenceTypeCounter = 0;
421
422
    /* Populate the nodestore */
423
10.2k
    zns->ns.free = zipNsFree;
424
10.2k
    zns->ns.newNode = zipNsNewNode;
425
10.2k
    zns->ns.deleteNode = zipNsDeleteNode;
426
10.2k
    zns->ns.getNode = zipNsGetNode;
427
10.2k
    zns->ns.getNodeFromPtr = zipNsGetNodeFromPtr;
428
10.2k
    zns->ns.releaseNode = zipNsReleaseNode;
429
10.2k
    zns->ns.getNodeCopy = zipNsGetNodeCopy;
430
10.2k
    zns->ns.insertNode = zipNsInsertNode;
431
10.2k
    zns->ns.replaceNode = zipNsReplaceNode;
432
10.2k
    zns->ns.removeNode = zipNsRemoveNode;
433
10.2k
    zns->ns.getReferenceTypeId = zipNsGetReferenceTypeId;
434
10.2k
    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
10.2k
    zns->ns.getEditNode =
439
10.2k
        (UA_Node * (*)(UA_Nodestore *ns, const UA_NodeId *nodeId,
440
10.2k
                       UA_UInt32 attributeMask,
441
10.2k
                       UA_ReferenceTypeSet references,
442
10.2k
                       UA_BrowseDirection referenceDirections))zipNsGetNode;
443
10.2k
    zns->ns.getEditNodeFromPtr =
444
10.2k
        (UA_Node * (*)(UA_Nodestore *ns, UA_NodePointer ptr,
445
10.2k
                       UA_UInt32 attributeMask,
446
10.2k
                       UA_ReferenceTypeSet references,
447
10.2k
                       UA_BrowseDirection referenceDirections))zipNsGetNodeFromPtr;
448
449
10.2k
    return &zns->ns;
450
10.2k
}