Coverage Report

Created: 2026-08-31 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/open62541_15/tests/fuzz/fuzz_binary_message.cc
Line
Count
Source
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
 *
5
 *    Copyright 2019 (c) fortiss (Author: Stefan Profanter)
6
 */
7
8
#include <open62541/plugin/log_stdout.h>
9
#include <open62541/server_config_default.h>
10
#include <open62541/types.h>
11
12
#include "ua_server_internal.h"
13
#include "custom_memory_manager.h"
14
#include "testing_networklayers.h"
15
16
#define RECEIVE_BUFFER_SIZE 65535
17
18
static void *
19
753
_removeServerComponent(void *application, UA_ServerComponent *sc) {
20
753
    UA_assert(sc->state == UA_LIFECYCLESTATE_STOPPED);
21
753
    sc->clear(sc);
22
753
    UA_free(sc);
23
753
    return NULL;
24
753
}
25
26
/*
27
** Main entry point.  The fuzzer invokes this function with each
28
** fuzzed input.
29
*/
30
extern "C" int
31
255
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
32
255
    if(size <= 4)
33
4
        return 0;
34
35
    /* Keep setup and teardown outside allocation-failure fuzzing. */
36
251
    UA_memoryManager_setLimit((unsigned long long)-1);
37
38
    /* less debug output */
39
251
    UA_ServerConfig initialConfig;
40
251
    memset(&initialConfig, 0, sizeof(UA_ServerConfig));
41
251
    UA_StatusCode retval = UA_ServerConfig_setDefault(&initialConfig);
42
251
    initialConfig.allowEmptyVariables = UA_RULEHANDLING_ACCEPT;
43
251
    if(retval != UA_STATUSCODE_GOOD) {
44
0
        UA_ServerConfig_clean(&initialConfig);
45
0
        UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
46
0
                     "Could not generate the server config");
47
0
        return 0;
48
0
    }
49
50
251
    UA_Server *server = UA_Server_newWithConfig(&initialConfig);
51
251
    if(!server) {
52
0
        UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
53
0
                     "Could not create server instance using UA_Server_new");
54
0
        return 0;
55
0
    }
56
57
    // we need to copy the message because it will be freed in the processing function
58
251
    UA_ByteString msg = UA_BYTESTRING_NULL;
59
251
    retval = UA_ByteString_allocBuffer(&msg, size);
60
251
    if(retval != UA_STATUSCODE_GOOD) {
61
0
        UA_Server_delete(server);
62
0
        UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
63
0
                     "Could not allocate message buffer");
64
0
        return 0;
65
0
    }
66
251
    memcpy(msg.data, data, size);
67
68
    /* Remove all remaining server components (must be all stopped) */
69
251
    lockServer(server);
70
251
    ZIP_ITER(UA_ServerComponentTree, &server->serverComponents,
71
251
             _removeServerComponent, server);
72
251
    ZIP_INIT(&server->serverComponents);
73
251
    unlockServer(server);
74
75
251
    UA_ServerComponent *bpm = UA_BinaryProtocolManager_new(server);
76
251
    addServerComponent(server, bpm, NULL);
77
78
251
    UA_ConnectionManager *cm = TestConnectionManager_new("tcp", NULL);
79
80
    /* Register a listening socket first. New active connections inherit its
81
     * context and replace it with their SecureChannel on the first callback. */
82
251
    void *listenCtx = NULL;
83
251
    serverNetworkCallback(cm, 1, bpm,
84
251
                          &listenCtx, UA_CONNECTIONSTATE_ESTABLISHED,
85
251
                          &UA_KEYVALUEMAP_NULL, UA_BYTESTRING_NULL);
86
87
251
    void *connectionCtx = listenCtx;
88
251
    serverNetworkCallback(cm, 2, bpm,
89
251
                          &connectionCtx, UA_CONNECTIONSTATE_ESTABLISHED,
90
251
                          &UA_KEYVALUEMAP_NULL, msg);
91
92
    /* Remove both connections before freeing the testing ConnectionManager. */
93
251
    serverNetworkCallback(cm, 2, bpm,
94
251
                          &connectionCtx, UA_CONNECTIONSTATE_CLOSING,
95
251
                          &UA_KEYVALUEMAP_NULL, UA_BYTESTRING_NULL);
96
251
    serverNetworkCallback(cm, 1, bpm,
97
251
                          &listenCtx, UA_CONNECTIONSTATE_CLOSING,
98
251
                          &UA_KEYVALUEMAP_NULL, UA_BYTESTRING_NULL);
99
251
    cm->eventSource.free(&cm->eventSource);
100
101
    // if we got an invalid chunk, the message is not deleted, so delete it here
102
251
    UA_ByteString_clear(&msg);
103
251
    UA_Server_delete(server);
104
251
    return 0;
105
251
}