Coverage Report

Created: 2026-05-16 06:54

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