Skip to content

Commit 26b92d5

Browse files
authored
Fixing edge cases in the mock-bpa EID and EID Pattern decoders (#74)
* Fixing edge cases in the mock-bpa EID and EID Pattern decoders * Avoid extreme logging for large test runs
1 parent a3aee2f commit 26b92d5

25 files changed

Lines changed: 393 additions & 68 deletions

.github/workflows/fuzzing.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,16 @@ jobs:
8787
run: >
8888
./build.sh prep
8989
-DBUILD_DOCS_API=OFF -DBUILD_DOCS_MAN=OFF
90-
-DBUILD_UNITTEST=OFF -DBUILD_FUZZING=ON -DBUILD_COVERAGE=ON
90+
-DBUILD_MOCK_BPA=OFF -DBUILD_UNITTEST=OFF
91+
-DBUILD_FUZZING=ON -DBUILD_COVERAGE=ON
9192
- name: Build
9293
run: ./build.sh
9394
- name: Install
9495
run: ./build.sh install
9596
- name: Test
9697
run: ./build.sh check
9798
- name: Report corpus
98-
run: for FN in $(find test/*cbor*corpus -type f); do echo $FN; cborseq2diag.rb <$FN; done
99+
run: for FN in $(find test/*cbor*corpus -type f); do echo $FN; cborseq2diag.rb <$FN || true; done
99100
- name: Collect coverage
100101
run: ./build.sh coverage
101102
- name: Archive coverage

CMakeGraphVizOptions.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
set(GRAPHVIZ_GENERATE_PER_TARGET TRUE)
2-
set(GRAPHVIZ_IGNORE_TARGETS test_* unity gcov)
2+
set(GRAPHVIZ_CUSTOM_TARGETS FALSE)
3+
set(GRAPHVIZ_IGNORE_TARGETS "^test_.+" "^fuzz_.+" "^unity$" "^gcov$" "^m$" "\\.so$")

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ option(BUILD_LIB "Build the library itself" ON)
2525
option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
2626
option(BUILD_DOCS_API "Enable API documentation building" OFF)
2727
option(BUILD_DOCS_MAN "Enable manpage building" OFF)
28+
option(BUILD_MOCK_BPA "Enable building the Mock BPA executable" ON)
2829
option(BUILD_UNITTEST "Enable building unit tests" ON)
2930
option(TEST_MEMCHECK "Enable test runtime memory checking" ON)
3031
option(BUILD_COVERAGE "Enable runtime coverage logging and reporting" OFF)

cmake/Findlibfuzzer.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ if(NOT CMAKE_C_COMPILER_ID MATCHES "Clang"
99
message(FATAL "Can only fuzz with clang compiler")
1010
endif()
1111

12+
# Options for all compilation units
13+
add_compile_options(
14+
-fsanitize=fuzzer-no-link
15+
)
16+
1217
function(add_fuzz_test)
1318
set(options OPTIONAL )
1419
set(oneValueArgs TARGET MAIN_NAME RUNS_COUNT)

src/BPSecLib_Private.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,32 @@ void BSL_openlog(void);
186186
*/
187187
void BSL_closelog(void);
188188

189+
/** Interpret a text name as a severity level.
190+
*
191+
* @param[out] severity The associated severity level.
192+
* @param[in] name The text name, which is case insensitive.
193+
* @return Zero if successful.
194+
*/
195+
int BSL_LogGetSeverity(int *severity, const char *name);
196+
197+
/** Set the least severity enabled for logging.
198+
* Other events will be dropped by the logging facility.
199+
* This function is multi-thread safe.
200+
*
201+
* @param severity The severity from a subset of the POSIX syslog values.
202+
* @sa BSL_log_is_enabled_for()
203+
*/
204+
void BSL_LogSetLeastSeverity(int severity);
205+
206+
/** Determine if a particular severity is being logged.
207+
* This function is multi-thread safe.
208+
*
209+
* @param severity The severity from a subset of the POSIX syslog values.
210+
* @return True if the severity level will be logged.
211+
* @sa BSL_log_set_least_severity()
212+
*/
213+
bool BSL_LogIsEnabledFor(int severity);
214+
189215
/** Log an event.
190216
*
191217
* @param severity The severity from a subset of the POSIX syslog values.

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ target_link_libraries(bsl_dynamic PUBLIC bsl_front)
148148
target_link_libraries(bsl_dynamic PUBLIC MLIB::mlib)
149149
target_link_libraries(bsl_dynamic PUBLIC QCBOR::qcbor)
150150

151-
152151
add_subdirectory(mock_bpa)
152+
153153
find_program(CLANG_FORMAT "clang-format")
154154
if(CLANG_FORMAT)
155155
# Define which files to format

src/backend/HostInterface.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,11 @@ int BSL_Host_GetSecSrcEID(BSL_HostEID_t *eid)
159159
return HostDescriptorTable.get_host_eid_fn(HostDescriptorTable.user_data, eid);
160160
}
161161

162-
int BSL_HostEID_EncodeToCBOR(const BSL_HostEID_t *eid, void *user_data)
162+
int BSL_HostEID_EncodeToCBOR(const BSL_HostEID_t *eid, void *encoder)
163163
{
164164
CHK_ARG_NONNULL(eid);
165-
CHK_ARG_NONNULL(user_data);
166-
return HostDescriptorTable.eid_to_cbor(user_data, eid);
165+
CHK_ARG_NONNULL(encoder);
166+
return HostDescriptorTable.eid_to_cbor(encoder, eid);
167167
}
168168

169169
int BSL_HostEID_DecodeFromCBOR(BSL_HostEID_t *eid, void *decoder)
@@ -181,8 +181,7 @@ int BSL_HostEID_DecodeFromText(BSL_HostEID_t *eid, const char *text)
181181
CHK_ARG_NONNULL(eid);
182182
CHK_ARG_NONNULL(text);
183183

184-
// Basic sanity check, may need to remove.
185-
CHK_PRECONDITION(strlen(text) < 100);
184+
CHK_PRECONDITION(eid->handle != NULL);
186185
CHK_PRECONDITION(HostDescriptorTable.eid_from_text != NULL);
187186

188187
return HostDescriptorTable.eid_from_text(eid, text, HostDescriptorTable.user_data);
@@ -205,7 +204,6 @@ int BSL_HostEIDPattern_DecodeFromText(BSL_HostEIDPattern_t *pat, const char *tex
205204
{
206205
CHK_ARG_NONNULL(pat);
207206
CHK_ARG_NONNULL(text);
208-
CHK_ARG_EXPR(strlen(text) < 100);
209207
CHK_PRECONDITION(HostDescriptorTable.eidpat_from_text != NULL);
210208
return HostDescriptorTable.eidpat_from_text(pat, text, HostDescriptorTable.user_data);
211209
}

src/backend/LoggingStderr.c

Lines changed: 110 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
#include <m-buffer.h>
3737
#include <m-string.h>
38+
#include <m-atomic.h>
3839

3940
/// Number of events to buffer to I/O thread
4041
#define BSL_LOG_QUEUE_SIZE 100
@@ -82,14 +83,48 @@ static void BSL_LogEvent_event_deinit(BSL_LogEvent_event_t *obj)
8283
string_clear(obj->context);
8384
}
8485

86+
static void BSL_LogEvent_event_init_set(BSL_LogEvent_event_t *obj, const BSL_LogEvent_event_t *src)
87+
{
88+
obj->thread = src->thread;
89+
obj->timestamp = src->timestamp;
90+
obj->severity = src->severity;
91+
string_init_set(obj->context, src->context);
92+
string_init_set(obj->message, src->message);
93+
}
94+
95+
static void BSL_LogEvent_event_init_move(BSL_LogEvent_event_t *obj, BSL_LogEvent_event_t *src)
96+
{
97+
obj->thread = src->thread;
98+
obj->timestamp = src->timestamp;
99+
obj->severity = src->severity;
100+
string_init_move(obj->context, src->context);
101+
string_init_move(obj->message, src->message);
102+
}
103+
104+
static void BSL_LogEvent_event_set(BSL_LogEvent_event_t *obj, const BSL_LogEvent_event_t *src)
105+
{
106+
obj->thread = src->thread;
107+
obj->timestamp = src->timestamp;
108+
obj->severity = src->severity;
109+
string_set(obj->context, src->context);
110+
string_set(obj->message, src->message);
111+
}
112+
85113
/// OPLIST for BSL_LogEvent_event_t
86-
#define M_OPL_BSL_LogEvent_event_t() (INIT(API_2(BSL_LogEvent_event_init)), CLEAR(API_2(BSL_LogEvent_event_deinit)))
114+
#define M_OPL_BSL_LogEvent_event_t() \
115+
(INIT(API_2(BSL_LogEvent_event_init)), INIT_SET(API_6(BSL_LogEvent_event_init_set)), \
116+
INIT_MOVE(API_6(BSL_LogEvent_event_init_move)), SET(API_6(BSL_LogEvent_event_set)), \
117+
CLEAR(API_2(BSL_LogEvent_event_deinit)))
87118

88119
// NOLINTBEGIN
89120
/// @cond Doxygen_Suppress
90-
M_BUFFER_DEF(BSL_LogEvent_queue, BSL_LogEvent_event_t, BSL_LOG_QUEUE_SIZE, M_BUFFER_THREAD_SAFE | M_BUFFER_BLOCKING)
121+
M_BUFFER_DEF(BSL_LogEvent_queue, BSL_LogEvent_event_t, BSL_LOG_QUEUE_SIZE,
122+
M_BUFFER_THREAD_SAFE | M_BUFFER_BLOCKING | M_BUFFER_PUSH_INIT_POP_MOVE)
91123
/// @endcond
92124

125+
/// Shared least severity
126+
static atomic_int least_severity = LOG_DEBUG;
127+
93128
/// Shared safe queue
94129
static BSL_LogEvent_queue_t event_queue;
95130
/// Sink thread ID
@@ -156,17 +191,19 @@ static void write_log(const BSL_LogEvent_event_t *event)
156191

157192
static void *work_sink(void *arg _U_)
158193
{
159-
while (true)
194+
bool running = true;
195+
while (running)
160196
{
161197
BSL_LogEvent_event_t event;
162198
BSL_LogEvent_queue_pop(&event, event_queue);
163199
if (string_empty_p(event.message))
164200
{
165-
BSL_LogEvent_event_deinit(&event);
166-
break;
201+
running = false;
202+
}
203+
else
204+
{
205+
write_log(&event);
167206
}
168-
169-
write_log(&event);
170207
BSL_LogEvent_event_deinit(&event);
171208
}
172209
return NULL;
@@ -198,6 +235,7 @@ void BSL_closelog(void)
198235
BSL_LogEvent_event_t event;
199236
BSL_LogEvent_event_init(&event);
200237
BSL_LogEvent_queue_push(event_queue, event);
238+
BSL_LogEvent_event_deinit(&event);
201239

202240
int res = pthread_join(thr_sink, NULL);
203241
if (res)
@@ -214,12 +252,59 @@ void BSL_closelog(void)
214252
{
215253
atomic_store(&thr_valid, false);
216254
}
255+
256+
// no consumer after join above
257+
BSL_LogEvent_queue_clear(event_queue);
258+
}
259+
260+
int BSL_LogGetSeverity(int *severity, const char *name)
261+
{
262+
CHKERR1(severity)
263+
CHKERR1(name)
264+
265+
for (size_t ix = 0; ix < sizeof(sev_names) / sizeof(const char *); ++ix)
266+
{
267+
if (!sev_names[ix])
268+
{
269+
continue;
270+
}
271+
if (strcasecmp(sev_names[ix], name) == 0)
272+
{
273+
*severity = (int)ix;
274+
return 0;
275+
}
276+
}
277+
return 2;
278+
}
279+
280+
void BSL_LogSetLeastSeverity(int severity)
281+
{
282+
if ((severity < 0) || (severity > LOG_DEBUG))
283+
{
284+
return;
285+
}
286+
287+
atomic_store(&least_severity, severity);
288+
}
289+
290+
bool BSL_LogIsEnabledFor(int severity)
291+
{
292+
if ((severity < 0) || (severity > LOG_DEBUG))
293+
{
294+
return false;
295+
}
296+
297+
const int limit = atomic_load(&least_severity);
298+
// lower severity has higher define value
299+
const bool enabled = (limit >= severity);
300+
301+
return enabled;
217302
}
218303

219304
// NOLINTBEGIN
220305
void BSL_LogEvent(int severity, const char *filename, int lineno, const char *funcname, const char *format, ...)
221306
{
222-
if ((severity < 0) || (severity > LOG_DEBUG))
307+
if (!BSL_LogIsEnabledFor(severity))
223308
{
224309
return;
225310
}
@@ -251,28 +336,25 @@ void BSL_LogEvent(int severity, const char *filename, int lineno, const char *fu
251336
va_end(val);
252337
}
253338

254-
if (string_empty_p(event.message))
255-
{
256-
// ignore empty messages
257-
BSL_LogEvent_event_deinit(&event);
258-
return;
259-
}
260-
261-
if (atomic_load(&thr_valid))
262-
{
263-
BSL_LogEvent_queue_push(event_queue, event);
264-
}
265-
else
339+
// ignore empty messages
340+
if (!string_empty_p(event.message))
266341
{
267-
BSL_LogEvent_event_t manual;
268-
BSL_LogEvent_event_init(&manual);
269-
manual.severity = LOG_CRIT;
270-
string_set_str(manual.message, "BSL_LogEvent() called before BSL_openlog()");
271-
write_log(&manual);
272-
BSL_LogEvent_event_deinit(&manual);
342+
if (atomic_load(&thr_valid))
343+
{
344+
BSL_LogEvent_queue_push(event_queue, event);
345+
}
346+
else
347+
{
348+
BSL_LogEvent_event_t manual;
349+
BSL_LogEvent_event_init(&manual);
350+
manual.severity = LOG_CRIT;
351+
string_set_str(manual.message, "BSL_LogEvent() called before BSL_openlog()");
352+
write_log(&manual);
353+
BSL_LogEvent_event_deinit(&manual);
273354

274-
write_log(&event);
275-
BSL_LogEvent_event_deinit(&event);
355+
write_log(&event);
356+
}
276357
}
358+
BSL_LogEvent_event_deinit(&event);
277359
}
278360
// NOLINTEND

src/mock_bpa/CMakeLists.txt

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,9 @@ target_link_libraries(bsl_mock_bpa PUBLIC MLIB::mlib)
6161
target_link_libraries(bsl_mock_bpa PUBLIC QCBOR::qcbor)
6262
target_link_libraries(bsl_mock_bpa PUBLIC Jansson::Jansson)
6363
target_link_libraries(bsl_mock_bpa PUBLIC bsl_front bsl_dynamic bsl_default_sc bsl_sample_pp)
64-
65-
add_executable(bsl-mock-bpa)
66-
target_sources(bsl-mock-bpa PRIVATE mock_bpa.c)
67-
target_link_libraries(bsl-mock-bpa PUBLIC bsl_mock_bpa bsl_crypto)
68-
target_link_libraries(bsl-mock-bpa PUBLIC Threads::Threads)
69-
7064
# Installation config
7165
install(
72-
TARGETS bsl_mock_bpa bsl-mock-bpa
66+
TARGETS bsl_mock_bpa
7367
PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/bsl"
7468
RUNTIME
7569
COMPONENT test
@@ -79,3 +73,18 @@ install(
7973
ARCHIVE
8074
COMPONENT devel
8175
)
76+
77+
if(BUILD_MOCK_BPA)
78+
add_executable(bsl-mock-bpa)
79+
target_sources(bsl-mock-bpa PRIVATE mock_bpa.c)
80+
target_link_libraries(bsl-mock-bpa PUBLIC bsl_mock_bpa bsl_crypto)
81+
target_link_libraries(bsl-mock-bpa PUBLIC Threads::Threads)
82+
83+
# Installation config
84+
install(
85+
TARGETS bsl-mock-bpa
86+
PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/bsl"
87+
RUNTIME
88+
COMPONENT test
89+
)
90+
endif(BUILD_MOCK_BPA)

0 commit comments

Comments
 (0)