Coverage Report

Created: 2025-07-01 06:54

/src/S2OPC/tests/PubSub/fuzzing/fuzz_sub.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Licensed to Systerel under one or more contributor license
3
 * agreements. See the NOTICE file distributed with this work
4
 * for additional information regarding copyright ownership.
5
 * Systerel licenses this file to you under the Apache
6
 * License, Version 2.0 (the "License"); you may not use this
7
 * file except in compliance with the License. You may obtain
8
 * a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied.  See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19
20
#include <stddef.h>
21
#include <stdint.h>
22
#include <string.h>
23
24
#include "sopc_assert.h"
25
#include "sopc_helper_endianness_cfg.h"
26
#include "sopc_network_layer.h"
27
#include "sopc_reader_layer.h"
28
29
2
#define BUFFER_INITIAL_LEN (4096)
30
2
#define BUFFER_MAX_LEN (1u << 30)
31
32
static const uint32_t subGroupVersion = 963852;
33
static const uint32_t subGroupId = 1245;
34
35
static SOPC_PubSubConfiguration* configuration = NULL;
36
static SOPC_PubSubConnection* subConnection = NULL;
37
38
static SOPC_Buffer* sopc_buffer = NULL;
39
40
static void setupConnection(void)
41
2
{
42
2
    SOPC_FieldMetaData* meta = NULL;
43
2
    SOPC_ReaderGroup* subReader = NULL;
44
2
    SOPC_DataSetReader* dsReader = NULL;
45
2
    configuration = SOPC_PubSubConfiguration_Create();
46
2
    SOPC_ASSERT(NULL != configuration);
47
    // "udp_pub_test"
48
2
    SOPC_PubSubConfiguration_Allocate_SubConnection_Array(configuration, 1);
49
2
    subConnection = SOPC_PubSubConfiguration_Get_SubConnection_At(configuration, 0);
50
51
2
    SOPC_PubSubConnection_Allocate_ReaderGroup_Array(subConnection, 2);
52
2
    subReader = SOPC_PubSubConnection_Get_ReaderGroup_At(subConnection, 0);
53
54
2
    SOPC_ReaderGroup_Set_SecurityMode(subReader, SOPC_SecurityMode_None);
55
2
    SOPC_ReaderGroup_Allocate_DataSetReader_Array(subReader, 1);
56
2
    dsReader = SOPC_ReaderGroup_Get_DataSetReader_At(subReader, 0);
57
2
    SOPC_ReaderGroup_Set_GroupId(subReader, (uint16_t) subGroupId);
58
2
    SOPC_ReaderGroup_Set_GroupVersion(subReader, subGroupVersion);
59
2
    SOPC_ReaderGroup_Set_PublisherId_UInteger(subReader, 15300);
60
61
2
    SOPC_DataSetReader_Set_DataSetWriterId(dsReader, 123);
62
63
2
    SOPC_DataSetReader_Allocate_FieldMetaData_Array(dsReader, SOPC_TargetVariablesDataType, 5);
64
2
    SOPC_PubSub_ArrayDimension arrDimension = {.valueRank = -1, .arrayDimensions = NULL};
65
    // Var 1
66
2
    meta = SOPC_DataSetReader_Get_FieldMetaData_At(dsReader, 0);
67
2
    SOPC_ASSERT(NULL != meta);
68
2
    SOPC_FieldMetaData_ArrayDimension_Move(meta, &arrDimension);
69
2
    SOPC_FieldMetaData_Set_BuiltinType(meta, SOPC_UInt32_Id);
70
    // Var 2
71
2
    meta = SOPC_DataSetReader_Get_FieldMetaData_At(dsReader, 1);
72
2
    SOPC_ASSERT(NULL != meta);
73
2
    SOPC_FieldMetaData_ArrayDimension_Move(meta, &arrDimension);
74
2
    SOPC_FieldMetaData_Set_BuiltinType(meta, SOPC_Byte_Id);
75
    // Var 3
76
2
    meta = SOPC_DataSetReader_Get_FieldMetaData_At(dsReader, 2);
77
2
    SOPC_ASSERT(NULL != meta);
78
2
    SOPC_FieldMetaData_ArrayDimension_Move(meta, &arrDimension);
79
2
    SOPC_FieldMetaData_Set_BuiltinType(meta, SOPC_UInt16_Id);
80
    // Var 4
81
2
    meta = SOPC_DataSetReader_Get_FieldMetaData_At(dsReader, 3);
82
2
    SOPC_ASSERT(NULL != meta);
83
2
    SOPC_FieldMetaData_ArrayDimension_Move(meta, &arrDimension);
84
2
    SOPC_FieldMetaData_Set_BuiltinType(meta, SOPC_DateTime_Id);
85
    // Var 5
86
2
    meta = SOPC_DataSetReader_Get_FieldMetaData_At(dsReader, 4);
87
2
    SOPC_ASSERT(NULL != meta);
88
2
    SOPC_FieldMetaData_ArrayDimension_Move(meta, &arrDimension);
89
2
    SOPC_FieldMetaData_Set_BuiltinType(meta, SOPC_UInt32_Id);
90
91
    // Configuration for "udp_pub_conf_test"
92
2
    subReader = SOPC_PubSubConnection_Get_ReaderGroup_At(subConnection, 1);
93
94
2
    SOPC_ReaderGroup_Set_SecurityMode(subReader, SOPC_SecurityMode_None);
95
2
    SOPC_ReaderGroup_Allocate_DataSetReader_Array(subReader, 1);
96
2
    dsReader = SOPC_ReaderGroup_Get_DataSetReader_At(subReader, 0);
97
2
    SOPC_ReaderGroup_Set_GroupId(subReader, (uint16_t) 45612);
98
2
    SOPC_ReaderGroup_Set_GroupVersion(subReader, 123456);
99
    //    SOPC_ReaderGroup_Set_PublisherId_UInteger(subReader, 15300);
100
101
2
    SOPC_DataSetReader_Set_DataSetWriterId(dsReader, 12);
102
103
2
    SOPC_DataSetReader_Allocate_FieldMetaData_Array(dsReader, SOPC_TargetVariablesDataType, 4);
104
    // Var 1
105
2
    meta = SOPC_DataSetReader_Get_FieldMetaData_At(dsReader, 0);
106
2
    SOPC_ASSERT(NULL != meta);
107
2
    SOPC_FieldMetaData_ArrayDimension_Move(meta, &arrDimension);
108
2
    SOPC_FieldMetaData_Set_BuiltinType(meta, SOPC_UInt16_Id);
109
    // Var 2
110
2
    meta = SOPC_DataSetReader_Get_FieldMetaData_At(dsReader, 1);
111
2
    SOPC_ASSERT(NULL != meta);
112
2
    SOPC_FieldMetaData_ArrayDimension_Move(meta, &arrDimension);
113
2
    SOPC_FieldMetaData_Set_BuiltinType(meta, SOPC_DateTime_Id);
114
    // Var 3
115
2
    meta = SOPC_DataSetReader_Get_FieldMetaData_At(dsReader, 2);
116
2
    SOPC_ASSERT(NULL != meta);
117
2
    SOPC_FieldMetaData_ArrayDimension_Move(meta, &arrDimension);
118
2
    SOPC_FieldMetaData_Set_BuiltinType(meta, SOPC_UInt32_Id);
119
    // Var 4
120
2
    meta = SOPC_DataSetReader_Get_FieldMetaData_At(dsReader, 3);
121
2
    SOPC_ASSERT(NULL != meta);
122
2
    SOPC_FieldMetaData_ArrayDimension_Move(meta, &arrDimension);
123
2
    SOPC_FieldMetaData_Set_BuiltinType(meta, SOPC_String_Id);
124
2
}
125
126
int LLVMFuzzerInitialize(int* argc, char*** argv)
127
2
{
128
2
    (void) argc;
129
2
    (void) argv;
130
2
    setupConnection();
131
2
    SOPC_Helper_Endianness_Check();
132
2
    sopc_buffer = SOPC_Buffer_CreateResizable(BUFFER_INITIAL_LEN, BUFFER_MAX_LEN);
133
2
    SOPC_ASSERT(sopc_buffer != NULL);
134
135
2
    return 0;
136
2
}
137
138
int LLVMFuzzerTestOneInput(const uint8_t* buf, size_t len)
139
7.55k
{
140
7.55k
    if (len == 0 || len > UINT32_MAX)
141
0
    {
142
0
        return 0;
143
0
    }
144
145
    /* FIXME: Avoid messages with String PublisherId which are known to raise an assert in the subscriber code */
146
7.55k
    if ((len >= 2) && (buf[0] & 0x80) && ((buf[1] & 0x07) == DataSet_LL_PubId_String_Id))
147
1
    {
148
1
        return 0;
149
1
    }
150
151
7.55k
    SOPC_Buffer_SetPosition(sopc_buffer, 0);
152
7.55k
    SOPC_ReturnStatus status = SOPC_Buffer_Write(sopc_buffer, buf, (uint32_t) len);
153
7.55k
    SOPC_ASSERT(SOPC_STATUS_OK == status);
154
7.55k
    sopc_buffer->length = sopc_buffer->position;
155
7.55k
    SOPC_Buffer_SetPosition(sopc_buffer, 0);
156
157
7.55k
    SOPC_ASSERT(NULL != subConnection);
158
7.55k
    const SOPC_UADP_NetworkMessage_Reader_Configuration readerConf = {
159
7.55k
        .pGetSecurity_Func = NULL,
160
7.55k
        .callbacks = SOPC_Reader_NetworkMessage_Default_Readers,
161
7.55k
        .checkDataSetMessageSN_Func = NULL,
162
7.55k
        .updateTimeout_Func = NULL,
163
7.55k
        .targetVariable_Func = NULL,
164
7.55k
        .targetConfig = NULL};
165
166
7.55k
    SOPC_UADP_NetworkMessage* uadp_nm = NULL;
167
7.55k
    SOPC_UADP_NetworkMessage_Decode(sopc_buffer, &readerConf, subConnection, &uadp_nm);
168
169
7.55k
    if (NULL != uadp_nm)
170
57
        SOPC_UADP_NetworkMessage_Delete(uadp_nm);
171
172
7.55k
    return 0;
173
7.55k
}