From f85db6ed5e5fe7eaec1026dfebd3f291eb91f9d1 Mon Sep 17 00:00:00 2001 From: Joshua Stone Date: Fri, 13 Mar 2026 15:24:45 -0400 Subject: [PATCH 01/17] Remove #defines and add host descriptor callbacks for mem mgmt --- src/BPSecLib_Private.h | 5 +++++ src/BPSecLib_Public.h | 13 +++++++++++++ src/BSLConfig.h.in | 24 ------------------------ src/backend/HostInterface.c | 27 +++++++++++++++++++++++++++ src/mock_bpa/agent.c | 5 +++++ 5 files changed, 50 insertions(+), 24 deletions(-) diff --git a/src/BPSecLib_Private.h b/src/BPSecLib_Private.h index 4dc4ee5a..63804cd2 100644 --- a/src/BPSecLib_Private.h +++ b/src/BPSecLib_Private.h @@ -425,6 +425,11 @@ int BSL_HostEIDPattern_DecodeFromText(BSL_HostEIDPattern_t *pat, const char *tex */ bool BSL_HostEIDPattern_IsMatch(const BSL_HostEIDPattern_t *pat, const BSL_HostEID_t *eid); +void *BSL_MALLOC(size_t size); +void *BSL_REALLOC(void *ptr, size_t size); +void *BSL_CALLOC(size_t nmemb, size_t size); +void BSL_FREE(void *ptr); + /** Block types using IANA-assigned code points from @cite iana:bundle. */ typedef enum diff --git a/src/BPSecLib_Public.h b/src/BPSecLib_Public.h index 831b94a2..0db4335f 100644 --- a/src/BPSecLib_Public.h +++ b/src/BPSecLib_Public.h @@ -350,6 +350,19 @@ typedef struct /// @brief Host BPA function that returns true if the given EID matched an EID pattern. bool (*eidpat_match)(const BSL_HostEIDPattern_t *pat, const BSL_HostEID_t *eid, void *user_data); + + /// @brief Dynamic memory allocation callback. Returns valid heap pointer on success, NULL on failure. + void *(*malloc_cb)(size_t size); + + /// @brief Dynamic memory re-allocation callback. Returns valid heap pointer on success, NULL on failure. + void *(*realloc_cb)(void *ptr, size_t size); + + /// @brief Contiguous dynamic memory allocation callback. Returns valid 0-initialized heap pointer on success, NULL on failure. + void *(*calloc_cb)(size_t nmemb, size_t size); + + /// @brief Free dynamic memory allocation callback. + void (*free_cb)(void* ptr); + } BSL_HostDescriptors_t; /** Set the BPA descriptor (callbacks) for this process. diff --git a/src/BSLConfig.h.in b/src/BSLConfig.h.in index 0bdb33c1..241d1dcd 100644 --- a/src/BSLConfig.h.in +++ b/src/BSLConfig.h.in @@ -53,30 +53,6 @@ extern "C" { */ const char * bsl_version(void); -#ifndef BSL_MALLOC -/** Uses the same function signature as C99 malloc(). - */ -#define BSL_MALLOC malloc -#endif /* BSL_MALLOC */ - -#ifndef BSL_REALLOC -/** Uses the same function signature as C99 realloc(). - */ -#define BSL_REALLOC realloc -#endif /* BSL_REALLOC */ - -#ifndef BSL_FREE -/** Uses the same function signature as C99 free(). - */ -#define BSL_FREE free -#endif /* BSL_FREE */ - -#ifndef BSL_CALLOC -/** Uses the same function signature as C99 calloc(). - */ -#define BSL_CALLOC calloc -#endif /* BSL_CALLOC */ - /** Force the use of M_ prefixed macros for M*LIB */ //#define M_USE_SMALL_NAME 0 diff --git a/src/backend/HostInterface.c b/src/backend/HostInterface.c index cf8ff3e9..4d6d3d4f 100644 --- a/src/backend/HostInterface.c +++ b/src/backend/HostInterface.c @@ -48,6 +48,13 @@ int BSL_HostDescriptors_Set(BSL_HostDescriptors_t desc) CHK_PRECONDITION(desc.eidpat_deinit); CHK_PRECONDITION(desc.eidpat_from_text); CHK_PRECONDITION(desc.eidpat_match); + + // Dyanmic mem callbacks + CHK_PRECONDITION(desc.malloc_cb); + CHK_PRECONDITION(desc.realloc_cb); + CHK_PRECONDITION(desc.calloc_cb); + CHK_PRECONDITION(desc.free_cb); + // GCOV_EXCL_STOP HostDescriptorTable = desc; @@ -224,3 +231,23 @@ bool BSL_HostEIDPattern_IsMatch(const BSL_HostEIDPattern_t *pat, const BSL_HostE ASSERT_PRECONDITION(HostDescriptorTable.eidpat_match); return HostDescriptorTable.eidpat_match(pat, eid, HostDescriptorTable.user_data); } + +void *BSL_MALLOC(size_t size) +{ + return HostDescriptorTable.malloc_cb(size); +} + +void *BSL_REALLOC(void *ptr, size_t size) +{ + return HostDescriptorTable.realloc_cb(ptr, size); +} + +void *BSL_CALLOC(size_t nmemb, size_t size) +{ + return HostDescriptorTable.calloc_cb(nmemb, size); +} + +void BSL_FREE(void *ptr) +{ + HostDescriptorTable.free_cb(ptr); +} \ No newline at end of file diff --git a/src/mock_bpa/agent.c b/src/mock_bpa/agent.c index 32c8f141..3a8f1b0e 100644 --- a/src/mock_bpa/agent.c +++ b/src/mock_bpa/agent.c @@ -406,6 +406,11 @@ BSL_HostDescriptors_t MockBPA_Agent_Descriptors(MockBPA_Agent_t *agent) .eidpat_deinit = mock_bpa_eidpat_deinit, .eidpat_from_text = mock_bpa_eidpat_from_text, .eidpat_match = mock_bpa_eidpat_match, + + .malloc_cb = malloc, + .realloc_cb = realloc, + .calloc_cb = calloc, + .free_cb = free, }; return bpa; } From 19f5fb5774a5604caf0d77a88f7509d9d531f20b Mon Sep 17 00:00:00 2001 From: Joshua Stone Date: Fri, 13 Mar 2026 15:29:54 -0400 Subject: [PATCH 02/17] Add docs to bsl private mem mgmt fns --- src/BPSecLib_Private.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/BPSecLib_Private.h b/src/BPSecLib_Private.h index 2cefffe2..d2760222 100644 --- a/src/BPSecLib_Private.h +++ b/src/BPSecLib_Private.h @@ -425,9 +425,25 @@ int BSL_HostEIDPattern_DecodeFromText(BSL_HostEIDPattern_t *pat, const char *tex */ bool BSL_HostEIDPattern_IsMatch(const BSL_HostEIDPattern_t *pat, const BSL_HostEID_t *eid); +/// @brief Dynamic memory allocation +/// @param size size of allocation +/// @return valid heap pointer void *BSL_MALLOC(size_t size); + +/// @brief Dynamic memory reallocation +/// @param ptr exisitng dynamic memory pointer +/// @param size new allocation size +/// @return valid heap pointer void *BSL_REALLOC(void *ptr, size_t size); + +/// @brief Contiguous dynamic memory allocation +/// @param nmemb number of members to allocate +/// @param size size of each member +/// @return valid heap pointer void *BSL_CALLOC(size_t nmemb, size_t size); + +/// @brief Free dynamically allocated memory +/// @param ptr pointer to memory to free void BSL_FREE(void *ptr); /** Block types using IANA-assigned code points from @cite iana:bundle. From 900912b35e8c3ef63ba531232841ed8a97437de3 Mon Sep 17 00:00:00 2001 From: Joshua Stone Date: Sat, 14 Mar 2026 15:04:05 -0400 Subject: [PATCH 03/17] Modify unit tests to set host desc first; add defualt libc --- src/backend/HostInterface.c | 23 +++++++++++++++++++---- src/mock_bpa/agent.c | 5 ----- src/mock_bpa/mock_bpa.c | 13 +++++++------ test/fuzz_dynamic_asb_cbor.cpp | 2 +- test/fuzz_mock_bpa_bpv7_cbor.cpp | 2 +- test/fuzz_mock_bpa_eid_cbor.cpp | 2 +- test/fuzz_mock_bpa_eid_uri.cpp | 2 +- test/fuzz_mock_bpa_eidpat_text.cpp | 2 +- test/test_AbsSecBlock.c | 4 ++-- test/test_BackendPolicyProvider.c | 4 ++-- test/test_BackendSecurityContext.c | 4 ++-- test/test_CryptoInterface.c | 2 ++ test/test_DefaultSecurityContext.c | 4 ++-- test/test_MockBPA_Codecs.c | 4 ++-- test/test_MockBPA_EID.c | 4 ++-- test/test_PublicInterfaceImpl.c | 4 ++-- test/test_SamplePolicyProvider.c | 4 ++-- test/test_mock_bpa_ctr.c | 4 ++-- 18 files changed, 51 insertions(+), 38 deletions(-) diff --git a/src/backend/HostInterface.c b/src/backend/HostInterface.c index e5ccd978..bf658e99 100644 --- a/src/backend/HostInterface.c +++ b/src/backend/HostInterface.c @@ -50,10 +50,25 @@ int BSL_HostDescriptors_Set(BSL_HostDescriptors_t desc) CHK_PRECONDITION(desc.eidpat_match); // Dyanmic mem callbacks - CHK_PRECONDITION(desc.malloc_cb); - CHK_PRECONDITION(desc.realloc_cb); - CHK_PRECONDITION(desc.calloc_cb); - CHK_PRECONDITION(desc.free_cb); + if (NULL == desc.malloc_cb) + { + desc.malloc_cb = malloc; + } + + if (NULL == desc.realloc_cb) + { + desc.realloc_cb = realloc; + } + + if (NULL == desc.calloc_cb) + { + desc.calloc_cb = calloc; + } + + if (NULL == desc.free_cb) + { + desc.free_cb = free; + } // GCOV_EXCL_STOP diff --git a/src/mock_bpa/agent.c b/src/mock_bpa/agent.c index 28586115..a3cc5daf 100644 --- a/src/mock_bpa/agent.c +++ b/src/mock_bpa/agent.c @@ -406,11 +406,6 @@ BSL_HostDescriptors_t MockBPA_Agent_Descriptors(MockBPA_Agent_t *agent) .eidpat_deinit = mock_bpa_eidpat_deinit, .eidpat_from_text = mock_bpa_eidpat_from_text, .eidpat_match = mock_bpa_eidpat_match, - - .malloc_cb = malloc, - .realloc_cb = realloc, - .calloc_cb = calloc, - .free_cb = free, }; return bpa; } diff --git a/src/mock_bpa/mock_bpa.c b/src/mock_bpa/mock_bpa.c index b15f225b..69b501ab 100644 --- a/src/mock_bpa/mock_bpa.c +++ b/src/mock_bpa/mock_bpa.c @@ -104,8 +104,13 @@ static void show_usage(const char *argv0) int main(int argc, char **argv) { - BSL_openlog(); int retval = 0; + if (BSL_HostDescriptors_Set(MockBPA_Agent_Descriptors(&agent))) + { + retval = 2; + } + + BSL_openlog(); int res; BSL_CryptoInit(); @@ -121,11 +126,7 @@ int main(int argc, char **argv) sigaction(SIGINT, &stopper, NULL); sigaction(SIGTERM, &stopper, NULL); } - // always run these steps - if (BSL_HostDescriptors_Set(MockBPA_Agent_Descriptors(&agent))) - { - retval = 2; - } + BSL_HostEID_Init(&app_eid); BSL_HostEID_Init(&sec_eid); diff --git a/test/fuzz_dynamic_asb_cbor.cpp b/test/fuzz_dynamic_asb_cbor.cpp index fa9f37d8..a2ae0914 100644 --- a/test/fuzz_dynamic_asb_cbor.cpp +++ b/test/fuzz_dynamic_asb_cbor.cpp @@ -34,9 +34,9 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); extern "C" int LLVMFuzzerInitialize(int *argc _U_, char ***argv _U_) { + BSL_HostDescriptors_Set(MockBPA_Agent_Descriptors(NULL)); BSL_openlog(); BSL_LogSetLeastSeverity(LOG_CRIT); - BSL_HostDescriptors_Set(MockBPA_Agent_Descriptors(NULL)); return 0; } diff --git a/test/fuzz_mock_bpa_bpv7_cbor.cpp b/test/fuzz_mock_bpa_bpv7_cbor.cpp index 5c98bfa2..0074f44e 100644 --- a/test/fuzz_mock_bpa_bpv7_cbor.cpp +++ b/test/fuzz_mock_bpa_bpv7_cbor.cpp @@ -34,9 +34,9 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); extern "C" int LLVMFuzzerInitialize(int *argc _U_, char ***argv _U_) { + BSL_HostDescriptors_Set(MockBPA_Agent_Descriptors(NULL)); BSL_openlog(); BSL_LogSetLeastSeverity(LOG_CRIT); - BSL_HostDescriptors_Set(MockBPA_Agent_Descriptors(NULL)); return 0; } diff --git a/test/fuzz_mock_bpa_eid_cbor.cpp b/test/fuzz_mock_bpa_eid_cbor.cpp index 4687dd03..d3ec526f 100644 --- a/test/fuzz_mock_bpa_eid_cbor.cpp +++ b/test/fuzz_mock_bpa_eid_cbor.cpp @@ -34,9 +34,9 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); extern "C" int LLVMFuzzerInitialize(int *argc _U_, char ***argv _U_) { + BSL_HostDescriptors_Set(MockBPA_Agent_Descriptors(NULL)); BSL_openlog(); BSL_LogSetLeastSeverity(LOG_CRIT); - BSL_HostDescriptors_Set(MockBPA_Agent_Descriptors(NULL)); return 0; } diff --git a/test/fuzz_mock_bpa_eid_uri.cpp b/test/fuzz_mock_bpa_eid_uri.cpp index 9319bf2a..2a2a9d0f 100644 --- a/test/fuzz_mock_bpa_eid_uri.cpp +++ b/test/fuzz_mock_bpa_eid_uri.cpp @@ -36,9 +36,9 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); extern "C" int LLVMFuzzerInitialize(int *argc _U_, char ***argv _U_) { + BSL_HostDescriptors_Set(MockBPA_Agent_Descriptors(NULL)); BSL_openlog(); BSL_LogSetLeastSeverity(LOG_CRIT); - BSL_HostDescriptors_Set(MockBPA_Agent_Descriptors(NULL)); return 0; } diff --git a/test/fuzz_mock_bpa_eidpat_text.cpp b/test/fuzz_mock_bpa_eidpat_text.cpp index ad81007a..5668807a 100644 --- a/test/fuzz_mock_bpa_eidpat_text.cpp +++ b/test/fuzz_mock_bpa_eidpat_text.cpp @@ -36,9 +36,9 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); extern "C" int LLVMFuzzerInitialize(int *argc _U_, char ***argv _U_) { + BSL_HostDescriptors_Set(MockBPA_Agent_Descriptors(NULL)); BSL_openlog(); BSL_LogSetLeastSeverity(LOG_CRIT); - BSL_HostDescriptors_Set(MockBPA_Agent_Descriptors(NULL)); return 0; } diff --git a/test/test_AbsSecBlock.c b/test/test_AbsSecBlock.c index fbfed4d3..145e2cfb 100644 --- a/test/test_AbsSecBlock.c +++ b/test/test_AbsSecBlock.c @@ -36,14 +36,14 @@ void suiteSetUp(void) { - BSL_openlog(); TEST_ASSERT_EQUAL_INT(0, BSL_HostDescriptors_Set(MockBPA_Agent_Descriptors(NULL))); + BSL_openlog(); } int suiteTearDown(int failures) { - BSL_HostDescriptors_Clear(); BSL_closelog(); + BSL_HostDescriptors_Clear(); return failures; } diff --git a/test/test_BackendPolicyProvider.c b/test/test_BackendPolicyProvider.c index 737c0f70..d396bc6b 100644 --- a/test/test_BackendPolicyProvider.c +++ b/test/test_BackendPolicyProvider.c @@ -44,14 +44,14 @@ static BSL_TestContext_t LocalTestCtx; void suiteSetUp(void) { - BSL_openlog(); TEST_ASSERT_EQUAL_INT(0, BSL_HostDescriptors_Set(MockBPA_Agent_Descriptors(NULL))); + BSL_openlog(); } int suiteTearDown(int failures) { - BSL_HostDescriptors_Clear(); BSL_closelog(); + BSL_HostDescriptors_Clear(); return failures; } diff --git a/test/test_BackendSecurityContext.c b/test/test_BackendSecurityContext.c index 5f36992f..0920353c 100644 --- a/test/test_BackendSecurityContext.c +++ b/test/test_BackendSecurityContext.c @@ -46,14 +46,14 @@ static BSL_TestContext_t LocalTestCtx; void suiteSetUp(void) { - BSL_openlog(); TEST_ASSERT_EQUAL_INT(0, BSL_HostDescriptors_Set(MockBPA_Agent_Descriptors(NULL))); + BSL_openlog(); } int suiteTearDown(int failures) { - BSL_HostDescriptors_Clear(); BSL_closelog(); + BSL_HostDescriptors_Clear(); return failures; } diff --git a/test/test_CryptoInterface.c b/test/test_CryptoInterface.c index 184d6e71..a7da4c6a 100644 --- a/test/test_CryptoInterface.c +++ b/test/test_CryptoInterface.c @@ -30,6 +30,7 @@ #include #include +#include #include "bsl_test_utils.h" @@ -197,6 +198,7 @@ static uint8_t test_256[32] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, void suiteSetUp(void) { + TEST_ASSERT_EQUAL_INT(0, BSL_HostDescriptors_Set(MockBPA_Agent_Descriptors(NULL))); BSL_openlog(); } diff --git a/test/test_DefaultSecurityContext.c b/test/test_DefaultSecurityContext.c index b378308f..87ba4f22 100644 --- a/test/test_DefaultSecurityContext.c +++ b/test/test_DefaultSecurityContext.c @@ -51,14 +51,14 @@ static BSL_TestContext_t LocalTestCtx; void suiteSetUp(void) { - BSL_openlog(); TEST_ASSERT_EQUAL_INT(0, BSL_HostDescriptors_Set(MockBPA_Agent_Descriptors(NULL))); + BSL_openlog(); } int suiteTearDown(int failures) { - BSL_HostDescriptors_Clear(); BSL_closelog(); + BSL_HostDescriptors_Clear(); return failures; } diff --git a/test/test_MockBPA_Codecs.c b/test/test_MockBPA_Codecs.c index 30a9ddf2..4897bcfa 100644 --- a/test/test_MockBPA_Codecs.c +++ b/test/test_MockBPA_Codecs.c @@ -50,14 +50,14 @@ static void printencoded(const uint8_t *pEncoded, size_t nLen) void suiteSetUp(void) { - BSL_openlog(); TEST_ASSERT_EQUAL_INT(0, BSL_HostDescriptors_Set(MockBPA_Agent_Descriptors(NULL))); + BSL_openlog(); } int suiteTearDown(int failures) { - BSL_HostDescriptors_Clear(); BSL_closelog(); + BSL_HostDescriptors_Clear(); return failures; } diff --git a/test/test_MockBPA_EID.c b/test/test_MockBPA_EID.c index 4bbac41b..b9c58d07 100644 --- a/test/test_MockBPA_EID.c +++ b/test/test_MockBPA_EID.c @@ -29,14 +29,14 @@ void suiteSetUp(void) { - BSL_openlog(); TEST_ASSERT_EQUAL_INT(0, BSL_HostDescriptors_Set(MockBPA_Agent_Descriptors(NULL))); + BSL_openlog(); } int suiteTearDown(int failures) { - BSL_HostDescriptors_Clear(); BSL_closelog(); + BSL_HostDescriptors_Clear(); return failures; } diff --git a/test/test_PublicInterfaceImpl.c b/test/test_PublicInterfaceImpl.c index b6168214..e62ab03c 100644 --- a/test/test_PublicInterfaceImpl.c +++ b/test/test_PublicInterfaceImpl.c @@ -59,14 +59,14 @@ static BSL_TestPublInterfaceCtx_t ctx = { 0 }; void suiteSetUp(void) { - BSL_openlog(); TEST_ASSERT_EQUAL_INT(0, BSL_HostDescriptors_Set(MockBPA_Agent_Descriptors(NULL))); + BSL_openlog(); } int suiteTearDown(int failures) { - BSL_HostDescriptors_Clear(); BSL_closelog(); + BSL_HostDescriptors_Clear(); return failures; } diff --git a/test/test_SamplePolicyProvider.c b/test/test_SamplePolicyProvider.c index 58f88884..b37c97ed 100644 --- a/test/test_SamplePolicyProvider.c +++ b/test/test_SamplePolicyProvider.c @@ -40,14 +40,14 @@ static BSL_TestContext_t LocalTestCtx; void suiteSetUp(void) { - BSL_openlog(); TEST_ASSERT_EQUAL_INT(0, BSL_HostDescriptors_Set(MockBPA_Agent_Descriptors(NULL))); + BSL_openlog(); } int suiteTearDown(int failures) { - BSL_HostDescriptors_Clear(); BSL_closelog(); + BSL_HostDescriptors_Clear(); return failures; } diff --git a/test/test_mock_bpa_ctr.c b/test/test_mock_bpa_ctr.c index 4f6fe8a1..53f35195 100644 --- a/test/test_mock_bpa_ctr.c +++ b/test/test_mock_bpa_ctr.c @@ -27,14 +27,14 @@ void suiteSetUp(void) { - BSL_openlog(); TEST_ASSERT_EQUAL_INT(0, BSL_HostDescriptors_Set(MockBPA_Agent_Descriptors(NULL))); + BSL_openlog(); } int suiteTearDown(int failures) { - BSL_HostDescriptors_Clear(); BSL_closelog(); + BSL_HostDescriptors_Clear(); return failures; } From e4d27b9de15236b3b5f5ee91bc1c17be2b631918 Mon Sep 17 00:00:00 2001 From: Joshua Stone Date: Sat, 14 Mar 2026 15:11:59 -0400 Subject: [PATCH 04/17] Add docs to host desc struct for dynamic mem cbs --- src/BPSecLib_Public.h | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/BPSecLib_Public.h b/src/BPSecLib_Public.h index fe13d098..775a55ef 100644 --- a/src/BPSecLib_Public.h +++ b/src/BPSecLib_Public.h @@ -351,16 +351,30 @@ typedef struct /// @brief Host BPA function that returns true if the given EID matched an EID pattern. bool (*eidpat_match)(const BSL_HostEIDPattern_t *pat, const BSL_HostEID_t *eid, void *user_data); - /// @brief Dynamic memory allocation callback. Returns valid heap pointer on success, NULL on failure. + /** (Optionally set) Dynamic memory allocation callback. + * Defaults to libc malloc if unset. + * + * @return valid heap pointer on success, NULL on failure. + */ void *(*malloc_cb)(size_t size); - /// @brief Dynamic memory re-allocation callback. Returns valid heap pointer on success, NULL on failure. + /** (Optionally set) Dynamic memory re-allocation callback. + * Defaults to libc realloc if unset. + * + * @return valid heap pointer on success, NULL on failure. + */ void *(*realloc_cb)(void *ptr, size_t size); - /// @brief Contiguous dynamic memory allocation callback. Returns valid 0-initialized heap pointer on success, NULL on failure. + /** (Optionally set) Contiguous dynamic memory allocation callback. + * Defaults to libc calloc if unset + * + * @return valid 0-initialized heap pointer on success, NULL on failure. + */ void *(*calloc_cb)(size_t nmemb, size_t size); - /// @brief Free dynamic memory allocation callback. + /** (Optionally set) Free dynamic memory allocation callback. + * Defaults to libc free if unset. + */ void (*free_cb)(void* ptr); } BSL_HostDescriptors_t; From 96f78b20a0cb5b8c7ee27ef03480bcbc9f7d1295 Mon Sep 17 00:00:00 2001 From: Joshua Stone Date: Tue, 17 Mar 2026 02:35:49 -0400 Subject: [PATCH 05/17] Add test for dyn mem cbs --- test/CMakeLists.txt | 4 + test/test_DynamicMemCbs.c | 221 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 225 insertions(+) create mode 100644 test/test_DynamicMemCbs.c diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index c20fa3cd..6de4a060 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -89,6 +89,10 @@ if(BUILD_UNITTEST AND NOT BUILD_FUZZING) # Exercises the MockBPA using the publicly exposed BSL front end add_unity_test(SOURCE test_PublicInterfaceImpl.c) target_link_libraries(test_PublicInterfaceImpl PUBLIC ${LIB_ORDERED}) + + # Test user-specified dynamic mem mgmt callbacks + add_unity_test(SOURCE test_DynamicMemCbs.c) + target_link_libraries(test_DynamicMemCbs PUBLIC ${LIB_ORDERED}) endif(BUILD_UNITTEST AND NOT BUILD_FUZZING) if(BUILD_FUZZING) diff --git a/test/test_DynamicMemCbs.c b/test/test_DynamicMemCbs.c new file mode 100644 index 00000000..c3c32f5f --- /dev/null +++ b/test/test_DynamicMemCbs.c @@ -0,0 +1,221 @@ +/* + * Copyright (c) 2025-2026 The Johns Hopkins University Applied Physics + * Laboratory LLC. + * + * This file is part of the Bundle Protocol Security Library (BSL). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * This work was performed for the Jet Propulsion Laboratory, California + * Institute of Technology, sponsored by the United States Government under + * the prime contract 80NM0018D0004 between the Caltech and NASA under + * subcontract 1700763. + */ +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "bsl_test_utils.h" + +static int malloc_cnt = 0; +static int realloc_cnt = 0; +static int calloc_cnt = 0; +static int free_cnt = 0; + +static BSL_SecParam_t param_aes_variant_128; +static BSL_SecParam_t param_use_wrap_key; +static BSL_SecParam_t param_test_bcb_key_correct; + +static BSL_TestContext_t LocalTestCtx = { 0 }; +static BSL_SecurityActionSet_t action_set = { 0 }; + +static void *malloc_test(size_t size) +{ + malloc_cnt++; + return malloc(size); +} + +static void *realloc_test(void *ptr, size_t size) +{ + realloc_cnt++; + return realloc(ptr, size); +} + +static void *calloc_test(size_t nmemb, size_t size) +{ + calloc_cnt++; + return calloc(nmemb, size); +} + +static void free_test(void *ptr) +{ + free_cnt++; + free(ptr); +} + +void suiteSetUp(void) +{ + BSL_HostDescriptors_t host_desc = MockBPA_Agent_Descriptors(NULL); + host_desc.malloc_cb = malloc_test; + host_desc.realloc_cb = realloc_test; + host_desc.calloc_cb = calloc_test; + host_desc.free_cb = free_test; + + TEST_ASSERT_EQUAL_INT(0, BSL_HostDescriptors_Set(host_desc)); + BSL_openlog(); +} + +int suiteTearDown(int failures) +{ + BSL_closelog(); + BSL_HostDescriptors_Clear(); + return failures; +} + +// manually call this to control dynamic mem callback tracking for test +void _setUp(void) +{ + setenv("BSL_TEST_LOCAL_IPN_EID", "ipn:2.1", 1); + memset(&LocalTestCtx, 0, sizeof(LocalTestCtx)); + TEST_ASSERT_EQUAL(0, BSL_API_InitLib(&LocalTestCtx.bsl)); + mock_bpa_ctr_init(&LocalTestCtx.mock_bpa_ctr); + memset(&action_set, 0, sizeof(action_set)); + + BSL_CryptoInit(); + + BSL_SecParam_Init(¶m_aes_variant_128); + BSL_SecParam_Init(¶m_use_wrap_key); + BSL_SecParam_Init(¶m_test_bcb_key_correct); + + /// Register the policy provider with some rules + BSL_PolicyDesc_t policy_desc = { 0 }; + policy_desc.user_data = BSL_CALLOC(1, sizeof(BSLP_PolicyProvider_t)); + policy_desc.query_fn = BSLP_QueryPolicy; + policy_desc.deinit_fn = BSLP_Deinit; + policy_desc.finalize_fn = BSLP_FinalizePolicy; + TEST_ASSERT_EQUAL(0, BSL_API_RegisterPolicyProvider(&LocalTestCtx.bsl, BSL_SAMPLE_PP_ID, policy_desc)); + + BSLP_PolicyProvider_t *policy = BSL_PolicyDict_get(LocalTestCtx.bsl.policy_reg, BSL_SAMPLE_PP_ID)->user_data; + + policy->pp_id = 1; + + BSL_SecParam_InitInt64(¶m_aes_variant_128, RFC9173_BCB_SECPARAM_AESVARIANT, RFC9173_BCB_AES_VARIANT_A128GCM); + BSL_SecParam_InitInt64(¶m_use_wrap_key, BSL_SECPARAM_USE_KEY_WRAP, 1); + BSL_SecParam_InitTextstr(¶m_test_bcb_key_correct, BSL_SECPARAM_TYPE_KEY_ID, RFC9173_EXAMPLE_A2_KEY); + + // BSL_32 + BSLP_PolicyPredicate_t *predicate_bsl_32a = &policy->predicates[policy->predicate_count++]; + BSLP_PolicyPredicate_Init(predicate_bsl_32a, BSL_POLICYLOCATION_CLOUT, BSL_TestUtils_GetEidPatternFromText("*:**"), + BSL_TestUtils_GetEidPatternFromText("*:**"), + BSL_TestUtils_GetEidPatternFromText("ipn:*.3.2")); + BSLP_PolicyRule_t *rule_bsl_32a = &policy->rules[policy->rule_count++]; + BSLP_PolicyRule_Init(rule_bsl_32a, "SOURCE BCB OVER PAYLOAD AT CLOUT FILTER(DEST=ipn:3.2)", predicate_bsl_32a, 2, + BSL_SECROLE_SOURCE, BSL_SECBLOCKTYPE_BCB, BSL_BLOCK_TYPE_PAYLOAD, BSL_POLICYACTION_DROP_BLOCK); + BSLP_PolicyRule_CopyParam(rule_bsl_32a, ¶m_test_bcb_key_correct); + BSLP_PolicyRule_CopyParam(rule_bsl_32a, ¶m_aes_variant_128); + BSLP_PolicyRule_CopyParam(rule_bsl_32a, ¶m_use_wrap_key); + + BSLP_PolicyPredicate_t *predicate_bsl_32b = &policy->predicates[policy->predicate_count++]; + BSLP_PolicyPredicate_Init(predicate_bsl_32b, BSL_POLICYLOCATION_CLOUT, BSL_TestUtils_GetEidPatternFromText("*:**"), + BSL_TestUtils_GetEidPatternFromText("*:**"), + BSL_TestUtils_GetEidPatternFromText("ipn:*.3.2")); + BSLP_PolicyRule_t *rule_bsl_32b = &policy->rules[policy->rule_count++]; + BSLP_PolicyRule_Init(rule_bsl_32b, "SOURCE BCB OVER BIB AT CLOUT FILTER(DEST=ipn:3.2)", predicate_bsl_32b, 2, + BSL_SECROLE_SOURCE, BSL_SECBLOCKTYPE_BCB, BSL_BLOCK_TYPE_BIB, BSL_POLICYACTION_DROP_BLOCK); + BSLP_PolicyRule_CopyParam(rule_bsl_32b, ¶m_test_bcb_key_correct); + BSLP_PolicyRule_CopyParam(rule_bsl_32b, ¶m_aes_variant_128); + BSLP_PolicyRule_CopyParam(rule_bsl_32b, ¶m_use_wrap_key); + + /// Register the Security Context + BSL_TestUtils_SetupDefaultSecurityContext(&LocalTestCtx.bsl); +} + +// manually call this to control dynamic mem callback tracking for test +void _tearDown(void) +{ + BSL_SecurityActionSet_Deinit(&action_set); + mock_bpa_ctr_deinit(&LocalTestCtx.mock_bpa_ctr); + BSL_CryptoDeinit(); + TEST_ASSERT_EQUAL(0, BSL_API_DeinitLib(&LocalTestCtx.bsl)); + + BSL_SecParam_Deinit(¶m_aes_variant_128); + BSL_SecParam_Deinit(¶m_use_wrap_key); + BSL_SecParam_Deinit(¶m_test_bcb_key_correct); +} + +// Test BSL 32 with user-defined dyn mem cbs +void test_dyn_mem_cbs_BSL_32(void) +{ + _setUp(); + + BSL_PrimaryBlock_t primary_block; + BSL_SecurityResponseSet_t response_set = { 0 }; + BSL_CanonicalBlock_t res_blk; + int query_result = -1; + int apply_result = -1; + + TEST_ASSERT_EQUAL(0, + BSL_TestUtils_LoadBundleFromCBOR(&LocalTestCtx, RFC9173_TestVectors_AppendixA1.cbor_bundle_bib)); + + int res = BSL_TestUtils_ModifyEIDs(&LocalTestCtx.mock_bpa_ctr.bundle_ref, NULL, "ipn:3.2", NULL); + BSL_LOG_INFO("EID MODIFICATION RESULT: %d", res); + + BSL_BundleCtx_GetBundleMetadata(&LocalTestCtx.mock_bpa_ctr.bundle_ref, &primary_block); + TEST_ASSERT_EQUAL(2, primary_block.block_count); + + TEST_ASSERT_EQUAL(0, BSL_BundleCtx_GetBlockMetadata(&LocalTestCtx.mock_bpa_ctr.bundle_ref, 2, &res_blk)); + TEST_ASSERT_EQUAL(11, res_blk.type_code); // should be a bib already + + query_result = BSL_API_QuerySecurity(&LocalTestCtx.bsl, &action_set, &LocalTestCtx.mock_bpa_ctr.bundle_ref, + BSL_POLICYLOCATION_CLOUT); + TEST_ASSERT_EQUAL(0, query_result); + TEST_ASSERT_EQUAL(2, BSL_SecurityActionSet_CountOperations(&action_set)); + + apply_result = + BSL_API_ApplySecurity(&LocalTestCtx.bsl, &response_set, &LocalTestCtx.mock_bpa_ctr.bundle_ref, &action_set); + TEST_ASSERT_EQUAL(0, apply_result); + + for (size_t i = 0; i < BSL_SecurityActionSet_CountActions(&action_set); i++) + { + const BSL_SecurityAction_t *act = BSL_SecurityActionSet_GetActionAtIndex(&action_set, i); + for (size_t j = 0; j < BSL_SecurityAction_CountSecOpers(act); j++) + { + TEST_ASSERT_EQUAL(BSL_SECOP_CONCLUSION_SUCCESS, + BSL_SecOper_GetConclusion(BSL_SecurityAction_GetSecOperAtIndex(act, j))); + } + } + BSL_PrimaryBlock_deinit(&primary_block); + + BSL_BundleCtx_GetBundleMetadata(&LocalTestCtx.mock_bpa_ctr.bundle_ref, &primary_block); + + TEST_ASSERT_EQUAL(4, primary_block.block_count); + TEST_ASSERT_EQUAL(0, BSL_BundleCtx_GetBlockMetadata(&LocalTestCtx.mock_bpa_ctr.bundle_ref, 2, &res_blk)); + TEST_ASSERT_EQUAL(11, res_blk.type_code); + TEST_ASSERT_EQUAL(0, BSL_BundleCtx_GetBlockMetadata(&LocalTestCtx.mock_bpa_ctr.bundle_ref, 3, &res_blk)); + TEST_ASSERT_EQUAL(12, res_blk.type_code); + TEST_ASSERT_EQUAL(0, BSL_BundleCtx_GetBlockMetadata(&LocalTestCtx.mock_bpa_ctr.bundle_ref, 4, &res_blk)); + TEST_ASSERT_EQUAL(12, res_blk.type_code); + + BSL_PrimaryBlock_deinit(&primary_block); + + _tearDown(); + + TEST_ASSERT_NOT_EQUAL(0, malloc_cnt); + TEST_ASSERT_NOT_EQUAL(0, realloc_cnt); + TEST_ASSERT_NOT_EQUAL(0, calloc_cnt); + TEST_ASSERT_NOT_EQUAL(0, free_cnt); +} From 7a98081bf22faca3300a9613384179e6a2f8198a Mon Sep 17 00:00:00 2001 From: Joshua Stone Date: Tue, 17 Mar 2026 10:30:46 -0400 Subject: [PATCH 06/17] MockBPA dyn mem patch --- src/backend/HostInterface.c | 2 +- src/mock_bpa/mock_bpa.c | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/backend/HostInterface.c b/src/backend/HostInterface.c index bf658e99..04ae316e 100644 --- a/src/backend/HostInterface.c +++ b/src/backend/HostInterface.c @@ -265,4 +265,4 @@ void *BSL_CALLOC(size_t nmemb, size_t size) void BSL_FREE(void *ptr) { HostDescriptorTable.free_cb(ptr); -} \ No newline at end of file +} diff --git a/src/mock_bpa/mock_bpa.c b/src/mock_bpa/mock_bpa.c index 69b501ab..718c0965 100644 --- a/src/mock_bpa/mock_bpa.c +++ b/src/mock_bpa/mock_bpa.c @@ -104,14 +104,14 @@ static void show_usage(const char *argv0) int main(int argc, char **argv) { - int retval = 0; if (BSL_HostDescriptors_Set(MockBPA_Agent_Descriptors(&agent))) { - retval = 2; + return 2; } BSL_openlog(); int res; + int retval = 0; BSL_CryptoInit(); if ((res = MockBPA_Agent_Init(&agent))) @@ -253,8 +253,8 @@ int main(int argc, char **argv) BSL_HostEID_Deinit(&sec_eid); BSL_HostEID_Deinit(&app_eid); - BSL_HostDescriptors_Clear(); BSL_CryptoDeinit(); BSL_closelog(); + BSL_HostDescriptors_Clear(); return retval; } From 07c903a8917be3376737e9bf5065cccaadb83ce9 Mon Sep 17 00:00:00 2001 From: Joshua Stone Date: Tue, 17 Mar 2026 11:18:13 -0400 Subject: [PATCH 07/17] diff fix --- src/mock_bpa/mock_bpa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mock_bpa/mock_bpa.c b/src/mock_bpa/mock_bpa.c index 718c0965..0a8aa5e6 100644 --- a/src/mock_bpa/mock_bpa.c +++ b/src/mock_bpa/mock_bpa.c @@ -110,8 +110,8 @@ int main(int argc, char **argv) } BSL_openlog(); - int res; int retval = 0; + int res; BSL_CryptoInit(); if ((res = MockBPA_Agent_Init(&agent))) From 9ef4bb8550075a0266575705481684a0360f4a1d Mon Sep 17 00:00:00 2001 From: Joshua Stone Date: Tue, 17 Mar 2026 11:31:37 -0400 Subject: [PATCH 08/17] apply format ubuntu --- src/BPSecLib_Public.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/BPSecLib_Public.h b/src/BPSecLib_Public.h index 775a55ef..e8a32fc5 100644 --- a/src/BPSecLib_Public.h +++ b/src/BPSecLib_Public.h @@ -351,31 +351,31 @@ typedef struct /// @brief Host BPA function that returns true if the given EID matched an EID pattern. bool (*eidpat_match)(const BSL_HostEIDPattern_t *pat, const BSL_HostEID_t *eid, void *user_data); - /** (Optionally set) Dynamic memory allocation callback. + /** (Optionally set) Dynamic memory allocation callback. * Defaults to libc malloc if unset. - * + * * @return valid heap pointer on success, NULL on failure. */ void *(*malloc_cb)(size_t size); - /** (Optionally set) Dynamic memory re-allocation callback. + /** (Optionally set) Dynamic memory re-allocation callback. * Defaults to libc realloc if unset. - * + * * @return valid heap pointer on success, NULL on failure. */ void *(*realloc_cb)(void *ptr, size_t size); /** (Optionally set) Contiguous dynamic memory allocation callback. * Defaults to libc calloc if unset - * + * * @return valid 0-initialized heap pointer on success, NULL on failure. - */ + */ void *(*calloc_cb)(size_t nmemb, size_t size); - /** (Optionally set) Free dynamic memory allocation callback. + /** (Optionally set) Free dynamic memory allocation callback. * Defaults to libc free if unset. */ - void (*free_cb)(void* ptr); + void (*free_cb)(void *ptr); } BSL_HostDescriptors_t; From 10af76edf60b02c3115f6f06e0cb2b79cce6ba3e Mon Sep 17 00:00:00 2001 From: Joshua Stone Date: Tue, 17 Mar 2026 11:52:37 -0400 Subject: [PATCH 09/17] Spelling fix --- docs/api/dictionary.txt | 1 + src/BPSecLib_Private.h | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/api/dictionary.txt b/docs/api/dictionary.txt index 6c4a4860..a12897b4 100644 --- a/docs/api/dictionary.txt +++ b/docs/api/dictionary.txt @@ -123,6 +123,7 @@ JSON JWK KEK len +libc LibCtx lifecycle Lifecycles diff --git a/src/BPSecLib_Private.h b/src/BPSecLib_Private.h index 01859d9b..001fce19 100644 --- a/src/BPSecLib_Private.h +++ b/src/BPSecLib_Private.h @@ -431,7 +431,7 @@ bool BSL_HostEIDPattern_IsMatch(const BSL_HostEIDPattern_t *pat, const BSL_HostE void *BSL_MALLOC(size_t size); /// @brief Dynamic memory reallocation -/// @param ptr exisitng dynamic memory pointer +/// @param ptr existing dynamic memory pointer /// @param size new allocation size /// @return valid heap pointer void *BSL_REALLOC(void *ptr, size_t size); From 145f11249b958d8cd6b3bab91bb51db56756740c Mon Sep 17 00:00:00 2001 From: Joshua Stone Date: Wed, 18 Mar 2026 13:31:17 -0400 Subject: [PATCH 10/17] 133 Move dyn mem bcbs to seperate struct --- src/BPSecLib_Public.h | 54 ++++++++++++++++++++----------------- src/backend/HostInterface.c | 36 +++++++++++-------------- test/test_DynamicMemCbs.c | 12 ++++++--- 3 files changed, 52 insertions(+), 50 deletions(-) diff --git a/src/BPSecLib_Public.h b/src/BPSecLib_Public.h index e8a32fc5..c598514e 100644 --- a/src/BPSecLib_Public.h +++ b/src/BPSecLib_Public.h @@ -260,6 +260,33 @@ typedef struct BSL_CanonicalBlock_s size_t btsd_len; ///< Length in bytes of the BTSD accessible through sequential APIs } BSL_CanonicalBlock_t; +/** Dynamic memory callback descriptors used by Dynamic BPA descriptor. +*/ +typedef struct +{ + /** Dynamic memory allocation callback. + * + * @return valid heap pointer on success, NULL on failure. + */ + void *(*malloc_cb)(size_t size); + + /** Dynamic memory re-allocation callback. + * + * @return valid heap pointer on success, NULL on failure. + */ + void *(*realloc_cb)(void *ptr, size_t size); + + /** Contiguous dynamic memory allocation callback. + * + * @return valid 0-initialized heap pointer on success, NULL on failure. + */ + void *(*calloc_cb)(size_t nmemb, size_t size); + + /** Free dynamic memory allocation callback. + */ + void (*free_cb)(void* ptr); +} BSL_DynMemHostDescriptors_t; + /** Dynamic BPA descriptor. */ typedef struct @@ -351,31 +378,8 @@ typedef struct /// @brief Host BPA function that returns true if the given EID matched an EID pattern. bool (*eidpat_match)(const BSL_HostEIDPattern_t *pat, const BSL_HostEID_t *eid, void *user_data); - /** (Optionally set) Dynamic memory allocation callback. - * Defaults to libc malloc if unset. - * - * @return valid heap pointer on success, NULL on failure. - */ - void *(*malloc_cb)(size_t size); - - /** (Optionally set) Dynamic memory re-allocation callback. - * Defaults to libc realloc if unset. - * - * @return valid heap pointer on success, NULL on failure. - */ - void *(*realloc_cb)(void *ptr, size_t size); - - /** (Optionally set) Contiguous dynamic memory allocation callback. - * Defaults to libc calloc if unset - * - * @return valid 0-initialized heap pointer on success, NULL on failure. - */ - void *(*calloc_cb)(size_t nmemb, size_t size); - - /** (Optionally set) Free dynamic memory allocation callback. - * Defaults to libc free if unset. - */ - void (*free_cb)(void *ptr); + /// @brief Optionally set dynamic memory management callbacks. Defaults to libc calls if unset. + BSL_DynMemHostDescriptors_t dyn_mem_desc; } BSL_HostDescriptors_t; diff --git a/src/backend/HostInterface.c b/src/backend/HostInterface.c index 04ae316e..a8b87514 100644 --- a/src/backend/HostInterface.c +++ b/src/backend/HostInterface.c @@ -29,6 +29,14 @@ // NOLINTNEXTLINE static BSL_HostDescriptors_t HostDescriptorTable = { 0 }; +static BSL_DynMemHostDescriptors_t defaultDynMemCbs = +{ + .malloc_cb = malloc, + .realloc_cb = realloc, + .calloc_cb = calloc, + .free_cb = free, +}; + int BSL_HostDescriptors_Set(BSL_HostDescriptors_t desc) { // GCOV_EXCL_START @@ -50,24 +58,10 @@ int BSL_HostDescriptors_Set(BSL_HostDescriptors_t desc) CHK_PRECONDITION(desc.eidpat_match); // Dyanmic mem callbacks - if (NULL == desc.malloc_cb) - { - desc.malloc_cb = malloc; - } - - if (NULL == desc.realloc_cb) - { - desc.realloc_cb = realloc; - } - - if (NULL == desc.calloc_cb) - { - desc.calloc_cb = calloc; - } - - if (NULL == desc.free_cb) + if (NULL == desc.dyn_mem_desc.malloc_cb || NULL == desc.dyn_mem_desc.realloc_cb || + NULL == desc.dyn_mem_desc.calloc_cb || NULL == desc.dyn_mem_desc.free_cb) { - desc.free_cb = free; + desc.dyn_mem_desc = defaultDynMemCbs; } // GCOV_EXCL_STOP @@ -249,20 +243,20 @@ bool BSL_HostEIDPattern_IsMatch(const BSL_HostEIDPattern_t *pat, const BSL_HostE void *BSL_MALLOC(size_t size) { - return HostDescriptorTable.malloc_cb(size); + return HostDescriptorTable.dyn_mem_desc.malloc_cb(size); } void *BSL_REALLOC(void *ptr, size_t size) { - return HostDescriptorTable.realloc_cb(ptr, size); + return HostDescriptorTable.dyn_mem_desc.realloc_cb(ptr, size); } void *BSL_CALLOC(size_t nmemb, size_t size) { - return HostDescriptorTable.calloc_cb(nmemb, size); + return HostDescriptorTable.dyn_mem_desc.calloc_cb(nmemb, size); } void BSL_FREE(void *ptr) { - HostDescriptorTable.free_cb(ptr); + HostDescriptorTable.dyn_mem_desc.free_cb(ptr); } diff --git a/test/test_DynamicMemCbs.c b/test/test_DynamicMemCbs.c index c3c32f5f..01381dda 100644 --- a/test/test_DynamicMemCbs.c +++ b/test/test_DynamicMemCbs.c @@ -70,10 +70,14 @@ static void free_test(void *ptr) void suiteSetUp(void) { BSL_HostDescriptors_t host_desc = MockBPA_Agent_Descriptors(NULL); - host_desc.malloc_cb = malloc_test; - host_desc.realloc_cb = realloc_test; - host_desc.calloc_cb = calloc_test; - host_desc.free_cb = free_test; + BSL_DynMemHostDescriptors_t dyn_mem_desc = + { + .malloc_cb = malloc_test, + .realloc_cb = realloc_test, + .calloc_cb = calloc_test, + .free_cb = free_test, + }; + host_desc.dyn_mem_desc = dyn_mem_desc; TEST_ASSERT_EQUAL_INT(0, BSL_HostDescriptors_Set(host_desc)); BSL_openlog(); From 83addc3cc08dd083bd4610f270b46b9ac8eb25c6 Mon Sep 17 00:00:00 2001 From: Joshua Stone Date: Wed, 18 Mar 2026 13:35:33 -0400 Subject: [PATCH 11/17] Rename BSL dynamic mem fns to lowercase --- docs/api/10-bsl-developers.md | 6 +-- src/BPSecLib_Private.h | 8 ++-- src/BSLConfig.h.in | 8 ++-- src/CryptoInterface.h | 8 ++-- src/Data.c | 6 +-- src/backend/HostInterface.c | 8 ++-- src/backend/PublicInterfaceImpl.c | 6 +-- src/backend/SecurityContext.c | 4 +- src/backend/UtilDefs_SeqReadWrite.c | 4 +- src/crypto/CryptoInterface.c | 16 +++---- src/mock_bpa/agent.c | 48 +++++++++---------- src/mock_bpa/bundle.c | 2 +- src/mock_bpa/ctr.c | 4 +- src/mock_bpa/decode.c | 4 +- src/mock_bpa/eid.c | 4 +- src/mock_bpa/eidpat.c | 4 +- src/mock_bpa/policy_params.c | 28 +++++------ src/policy_provider/SamplePolicyProvider.c | 16 +++---- src/security_context/BCB_AES_GCM.c | 30 ++++++------ src/security_context/BIB_HMAC_SHA2.c | 10 ++-- test/bsl_test_utils.c | 20 ++++---- test/fuzz_dynamic_asb_cbor.cpp | 4 +- test/test_AbsSecBlock.c | 8 ++-- test/test_BackendPolicyProvider.c | 6 +-- test/test_BackendSecurityContext.c | 56 +++++++++++----------- test/test_CryptoInterface.c | 6 +-- test/test_DefaultSecurityContext.c | 20 ++++---- test/test_DynamicMemCbs.c | 2 +- test/test_MockBPA_Codecs.c | 10 ++-- test/test_PublicInterfaceImpl.c | 2 +- 30 files changed, 179 insertions(+), 179 deletions(-) diff --git a/docs/api/10-bsl-developers.md b/docs/api/10-bsl-developers.md index 373a8fee..ffd6d910 100644 --- a/docs/api/10-bsl-developers.md +++ b/docs/api/10-bsl-developers.md @@ -217,9 +217,9 @@ This section contains references to commonly used macros defined for the BSL When heap memory is needed at BSL runtime, the following macros are used and have the same signature and semantics as the corresponding C99 functions indicated below. -- [BSL_MALLOC](@ref BSL_MALLOC) as `malloc()` -- [BSL_REALLOC](@ref BSL_REALLOC) as `realloc()` -- [BSL_FREE](@ref BSL_FREE) as `free()` +- [BSL_malloc](@ref BSL_malloc) as `malloc()` +- [BSL_realloc](@ref BSL_realloc) as `realloc()` +- [BSL_free](@ref BSL_free) as `free()` ## Error Checking Handler Macros diff --git a/src/BPSecLib_Private.h b/src/BPSecLib_Private.h index 001fce19..ff625295 100644 --- a/src/BPSecLib_Private.h +++ b/src/BPSecLib_Private.h @@ -428,23 +428,23 @@ bool BSL_HostEIDPattern_IsMatch(const BSL_HostEIDPattern_t *pat, const BSL_HostE /// @brief Dynamic memory allocation /// @param size size of allocation /// @return valid heap pointer -void *BSL_MALLOC(size_t size); +void *BSL_malloc(size_t size); /// @brief Dynamic memory reallocation /// @param ptr existing dynamic memory pointer /// @param size new allocation size /// @return valid heap pointer -void *BSL_REALLOC(void *ptr, size_t size); +void *BSL_realloc(void *ptr, size_t size); /// @brief Contiguous dynamic memory allocation /// @param nmemb number of members to allocate /// @param size size of each member /// @return valid heap pointer -void *BSL_CALLOC(size_t nmemb, size_t size); +void *BSL_calloc(size_t nmemb, size_t size); /// @brief Free dynamically allocated memory /// @param ptr pointer to memory to free -void BSL_FREE(void *ptr); +void BSL_free(void *ptr); /** Block types using IANA-assigned code points from @cite iana:bundle. */ diff --git a/src/BSLConfig.h.in b/src/BSLConfig.h.in index 241d1dcd..9ddb35fd 100644 --- a/src/BSLConfig.h.in +++ b/src/BSLConfig.h.in @@ -61,25 +61,25 @@ const char * bsl_version(void); /** Define to override value/struct allocation. * See m-core.h for details. */ -#define M_MEMORY_ALLOC(type) ((type *) BSL_MALLOC(sizeof(type))) +#define M_MEMORY_ALLOC(type) ((type *) BSL_malloc(sizeof(type))) #undef M_MEMORY_DEL /** Define to override value/struct deallocation. * See m-core.h for details. */ -#define M_MEMORY_DEL(ptr) BSL_FREE(ptr) +#define M_MEMORY_DEL(ptr) BSL_free(ptr) #undef M_MEMORY_REALLOC /** Define to override array allocation. * See m-core.h for details. */ -#define M_MEMORY_REALLOC(type, ptr, n) (M_UNLIKELY((n) > SIZE_MAX / sizeof(type)) ? (type *) NULL : (type *) BSL_REALLOC((ptr), (n)*sizeof (type))) +#define M_MEMORY_REALLOC(type, ptr, n) (M_UNLIKELY((n) > SIZE_MAX / sizeof(type)) ? (type *) NULL : (type *) BSL_realloc((ptr), (n)*sizeof (type))) #undef M_MEMORY_FREE /** Define to override array deallocation. * See m-core.h for details. */ -#define M_MEMORY_FREE(ptr) BSL_FREE(ptr) +#define M_MEMORY_FREE(ptr) BSL_free(ptr) #ifdef __cplusplus } // extern C diff --git a/src/CryptoInterface.h b/src/CryptoInterface.h index 7bbd1425..8468b9de 100644 --- a/src/CryptoInterface.h +++ b/src/CryptoInterface.h @@ -215,7 +215,7 @@ int BSL_AuthCtx_Deinit(BSL_AuthCtx_t *hmac_ctx); /** * Deinit and free generated key handle * @param[in] keyhandle key handle to clear. - * Key handle assumed to be generated, not present in key registry, and allocated with ::BSL_MALLOC(). + * Key handle assumed to be generated, not present in key registry, and allocated with ::BSL_malloc(). * @returns 0 if successfully cleared key handle */ int BSL_Crypto_ClearGeneratedKeyHandle(void *keyhandle); @@ -226,7 +226,7 @@ int BSL_Crypto_ClearGeneratedKeyHandle(void *keyhandle); * @param[in] kek_handle key encryption key handle (encryption key) * @param[in] cek_handle content encryption key handle (encryption data) * @param[in,out] wrapped_key output wrapped key (ciphertext) bytes - * @param[in,out] wrapped_key_handle output wrapped key (ciphertext) handle, allocated with ::BSL_MALLOC(). Set to NULL + * @param[in,out] wrapped_key_handle output wrapped key (ciphertext) handle, allocated with ::BSL_malloc(). Set to NULL * if handle not needed. */ int BSL_Crypto_WrapKey(void *kek_handle, void *cek_handle, BSL_Data_t *wrapped_key, void **wrapped_key_handle); @@ -236,7 +236,7 @@ int BSL_Crypto_WrapKey(void *kek_handle, void *cek_handle, BSL_Data_t *wrapped_k * CEK size expected to match size of KEK * @param[in] kek_handle key encryption key handle (decryption key) * @param[in] wrapped_key input wrapped key (ciphertext) bytes - * @param[in,out] cek_handle output content encryption key (plaintext) handle, allocated with ::BSL_MALLOC() + * @param[in,out] cek_handle output content encryption key (plaintext) handle, allocated with ::BSL_malloc() */ int BSL_Crypto_UnwrapKey(void *kek_handle, BSL_Data_t *wrapped_key, void **cek_handle); @@ -326,7 +326,7 @@ int BSL_Cipher_Deinit(BSL_Cipher_t *cipher_ctx); /** * Generate a new cryptographic key * @param[in] key_length length of new key. Should be 16 or 32 - * @param[in, out] key_out pointer to pointer for new key handle, allocated with ::BSL_MALLOC() + * @param[in, out] key_out pointer to pointer for new key handle, allocated with ::BSL_malloc() */ int BSL_Crypto_GenKey(size_t key_length, void **key_out); diff --git a/src/Data.c b/src/Data.c index b477615e..a3ee2670 100644 --- a/src/Data.c +++ b/src/Data.c @@ -42,7 +42,7 @@ static void bsl_data_int_free(BSL_Data_t *data) if (data->owned && data->ptr) { - BSL_FREE(data->ptr); + BSL_free(data->ptr); } } @@ -59,7 +59,7 @@ int BSL_Data_InitBuffer(BSL_Data_t *data, size_t bytelen) CHK_ARG_EXPR(bytelen > 0); bsl_data_int_reset(data); - data->ptr = BSL_MALLOC(bytelen); + data->ptr = BSL_malloc(bytelen); data->len = bytelen; data->owned = true; memset(data->ptr, 0, bytelen); @@ -145,7 +145,7 @@ int BSL_Data_Resize(BSL_Data_t *data, size_t len) { data->ptr = NULL; } - BSL_DataPtr_t got = BSL_REALLOC(data->ptr, len); + BSL_DataPtr_t got = BSL_realloc(data->ptr, len); if (UNLIKELY(!got)) { bsl_data_int_reset(data); diff --git a/src/backend/HostInterface.c b/src/backend/HostInterface.c index a8b87514..0ac1d642 100644 --- a/src/backend/HostInterface.c +++ b/src/backend/HostInterface.c @@ -241,22 +241,22 @@ bool BSL_HostEIDPattern_IsMatch(const BSL_HostEIDPattern_t *pat, const BSL_HostE return HostDescriptorTable.eidpat_match(pat, eid, HostDescriptorTable.user_data); } -void *BSL_MALLOC(size_t size) +void *BSL_malloc(size_t size) { return HostDescriptorTable.dyn_mem_desc.malloc_cb(size); } -void *BSL_REALLOC(void *ptr, size_t size) +void *BSL_realloc(void *ptr, size_t size) { return HostDescriptorTable.dyn_mem_desc.realloc_cb(ptr, size); } -void *BSL_CALLOC(size_t nmemb, size_t size) +void *BSL_calloc(size_t nmemb, size_t size) { return HostDescriptorTable.dyn_mem_desc.calloc_cb(nmemb, size); } -void BSL_FREE(void *ptr) +void BSL_free(void *ptr) { HostDescriptorTable.dyn_mem_desc.free_cb(ptr); } diff --git a/src/backend/PublicInterfaceImpl.c b/src/backend/PublicInterfaceImpl.c index 43dddb0f..3588eb5c 100644 --- a/src/backend/PublicInterfaceImpl.c +++ b/src/backend/PublicInterfaceImpl.c @@ -91,7 +91,7 @@ void BSL_PrimaryBlock_deinit(BSL_PrimaryBlock_t *obj) { ASSERT_ARG_NONNULL(obj); - BSL_FREE(obj->block_numbers); + BSL_free(obj->block_numbers); obj->block_numbers = NULL; BSL_Data_Deinit(&obj->encoded); @@ -174,7 +174,7 @@ int BSL_API_QuerySecurity(const BSL_LibCtx_t *bsl, BSL_SecurityActionSet_t *outp BSL_SeqReader_Get(btsd_read, btsd_copy.ptr, &btsd_copy.len); BSL_SeqReader_Destroy(btsd_read); - BSL_AbsSecBlock_t *abs_sec_block = BSL_CALLOC(1, BSL_AbsSecBlock_Sizeof()); + BSL_AbsSecBlock_t *abs_sec_block = BSL_calloc(1, BSL_AbsSecBlock_Sizeof()); BSL_AbsSecBlock_InitEmpty(abs_sec_block); if (BSL_AbsSecBlock_DecodeFromCBOR(abs_sec_block, &btsd_copy) == 0) { @@ -189,7 +189,7 @@ int BSL_API_QuerySecurity(const BSL_LibCtx_t *bsl, BSL_SecurityActionSet_t *outp BSL_SecOper_SetReasonCode(sec_oper, BSL_REASONCODE_BLOCK_UNINTELLIGIBLE); } BSL_AbsSecBlock_Deinit(abs_sec_block); - BSL_FREE(abs_sec_block); + BSL_free(abs_sec_block); BSL_Data_Deinit(&btsd_copy); } diff --git a/src/backend/SecurityContext.c b/src/backend/SecurityContext.c index 0b9324e0..572d8a07 100644 --- a/src/backend/SecurityContext.c +++ b/src/backend/SecurityContext.c @@ -481,7 +481,7 @@ int BSL_SecCtx_ExecutePolicyActionSet(BSL_LibCtx_t *lib, BSL_SecurityResponseSet * - BCB will be a special case, since it actively manipulates the BTSD * */ - BSL_SecOutcome_t *outcome = BSL_CALLOC(1, BSL_SecOutcome_Sizeof()); + BSL_SecOutcome_t *outcome = BSL_calloc(1, BSL_SecOutcome_Sizeof()); BSL_SecActionList_it_t act_it; for (BSL_SecActionList_it(act_it, action_set->actions); !BSL_SecActionList_end_p(act_it); @@ -535,7 +535,7 @@ int BSL_SecCtx_ExecutePolicyActionSet(BSL_LibCtx_t *lib, BSL_SecurityResponseSet BSL_SecurityResponseSet_AppendResult(output_response, errcode, sec_oper->policy_action); } } - BSL_FREE(outcome); + BSL_free(outcome); return BSL_SUCCESS; } diff --git a/src/backend/UtilDefs_SeqReadWrite.c b/src/backend/UtilDefs_SeqReadWrite.c index 278a79dc..3ccfe71a 100644 --- a/src/backend/UtilDefs_SeqReadWrite.c +++ b/src/backend/UtilDefs_SeqReadWrite.c @@ -39,7 +39,7 @@ int BSL_SeqReader_Destroy(BSL_SeqReader_t *obj) CHK_ARG_NONNULL(obj->deinit); obj->deinit(obj->user_data); - BSL_FREE(obj); + BSL_free(obj); return BSL_SUCCESS; } @@ -59,7 +59,7 @@ int BSL_SeqWriter_Destroy(BSL_SeqWriter_t *obj) CHK_ARG_NONNULL(obj->deinit); obj->deinit(obj->user_data); - BSL_FREE(obj); + BSL_free(obj); return BSL_SUCCESS; } diff --git a/src/crypto/CryptoInterface.c b/src/crypto/CryptoInterface.c index e6a102be..0afe5219 100644 --- a/src/crypto/CryptoInterface.c +++ b/src/crypto/CryptoInterface.c @@ -124,7 +124,7 @@ int BSL_Crypto_ClearGeneratedKeyHandle(void *keyhandle) BSL_CryptoKey_t *key = (BSL_CryptoKey_t *)keyhandle; BSL_CryptoKey_Deinit(key); - BSL_FREE(key); + BSL_free(key); return BSL_SUCCESS; } @@ -164,7 +164,7 @@ int BSL_Crypto_UnwrapKey(void *kek_handle, BSL_Data_t *wrapped_key, void **cek_h return BSL_ERR_SECURITY_CONTEXT_CRYPTO_FAILED; } - BSL_CryptoKey_t *cek = BSL_MALLOC(sizeof(BSL_CryptoKey_t)); + BSL_CryptoKey_t *cek = BSL_malloc(sizeof(BSL_CryptoKey_t)); if (cek == NULL) { return BSL_ERR_SECURITY_CONTEXT_CRYPTO_FAILED; @@ -180,7 +180,7 @@ int BSL_Crypto_UnwrapKey(void *kek_handle, BSL_Data_t *wrapped_key, void **cek_h if (dec_result != 1) { BSL_CryptoKey_Deinit(cek); - BSL_FREE(cek); + BSL_free(cek); return BSL_ERR_SECURITY_CONTEXT_CRYPTO_FAILED; } EVP_CIPHER_CTX_set_padding(ctx, 0); @@ -193,7 +193,7 @@ int BSL_Crypto_UnwrapKey(void *kek_handle, BSL_Data_t *wrapped_key, void **cek_h BSL_LOG_ERR("EVP_DecryptUpdate: %s", ERR_error_string(ERR_get_error(), NULL)); EVP_CIPHER_CTX_free(ctx); BSL_CryptoKey_Deinit(cek); - BSL_FREE(cek); + BSL_free(cek); return BSL_ERR_SECURITY_CONTEXT_CRYPTO_FAILED; } @@ -207,7 +207,7 @@ int BSL_Crypto_UnwrapKey(void *kek_handle, BSL_Data_t *wrapped_key, void **cek_h BSL_LOG_ERR("Failed DecryptFinal: %s", ERR_error_string(ERR_get_error(), NULL)); EVP_CIPHER_CTX_free(ctx); BSL_CryptoKey_Deinit(cek); - BSL_FREE(cek); + BSL_free(cek); return BSL_ERR_SECURITY_CONTEXT_CRYPTO_FAILED; } @@ -223,7 +223,7 @@ int BSL_Crypto_UnwrapKey(void *kek_handle, BSL_Data_t *wrapped_key, void **cek_h if (res != 1) { BSL_CryptoKey_Deinit(cek); - BSL_FREE(cek); + BSL_free(cek); return BSL_ERR_SECURITY_CONTEXT_CRYPTO_FAILED; } @@ -322,7 +322,7 @@ int BSL_Crypto_WrapKey(void *kek_handle, void *cek_handle, BSL_Data_t *wrapped_k int res = EVP_PKEY_keygen_init(pctx); CHK_PROPERTY(res == 1); - BSL_CryptoKey_t *new_wrapped_key_handle = BSL_MALLOC(sizeof(BSL_CryptoKey_t)); + BSL_CryptoKey_t *new_wrapped_key_handle = BSL_malloc(sizeof(BSL_CryptoKey_t)); BSL_CryptoKey_Init(new_wrapped_key_handle); new_wrapped_key_handle->pkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, wrapped_key->ptr, wrapped_key->len); BSL_Data_Init(&new_wrapped_key_handle->raw); @@ -630,7 +630,7 @@ int BSL_Crypto_GenKey(size_t key_length, void **key_out) CHK_ARG_NONNULL(key_out); CHK_ARG_EXPR(key_length == 16 || key_length == 32); - BSL_CryptoKey_t *new_key = BSL_MALLOC(sizeof(BSL_CryptoKey_t)); + BSL_CryptoKey_t *new_key = BSL_malloc(sizeof(BSL_CryptoKey_t)); CHK_PROPERTY(new_key); BSL_CryptoKey_Init(new_key); diff --git a/src/mock_bpa/agent.c b/src/mock_bpa/agent.c index a3cc5daf..4ff3c2cd 100644 --- a/src/mock_bpa/agent.c +++ b/src/mock_bpa/agent.c @@ -75,7 +75,7 @@ int MockBPA_GetBundleMetadata(const BSL_BundleRef_t *bundle_ref, BSL_PrimaryBloc result_primary_block->block_count = MockBPA_BlockList_size(bundle->blocks); - result_primary_block->block_numbers = BSL_CALLOC(result_primary_block->block_count, sizeof(uint64_t)); + result_primary_block->block_numbers = BSL_calloc(result_primary_block->block_count, sizeof(uint64_t)); if (!result_primary_block->block_numbers) { return -2; @@ -136,12 +136,12 @@ int MockBPA_ReallocBTSD(BSL_BundleRef_t *bundle_ref, uint64_t block_num, size_t if (found_block->btsd == NULL) { - found_block->btsd = BSL_CALLOC(1, bytesize); + found_block->btsd = BSL_calloc(1, bytesize); found_block->btsd_len = bytesize; } else { - found_block->btsd = BSL_REALLOC(found_block->btsd, bytesize); + found_block->btsd = BSL_realloc(found_block->btsd, bytesize); found_block->btsd_len = bytesize; } @@ -185,7 +185,7 @@ static void MockBPA_ReadBTSD_Deinit(void *user_data) fclose(obj->file); // buffer is external data, no cleanup - BSL_FREE(obj); + BSL_free(obj); } static struct BSL_SeqReader_s *MockBPA_ReadBTSD(const BSL_BundleRef_t *bundle_ref, uint64_t block_num) @@ -198,7 +198,7 @@ static struct BSL_SeqReader_s *MockBPA_ReadBTSD(const BSL_BundleRef_t *bundle_re } MockBPA_CanonicalBlock_t *found_block = *found_ptr; - struct MockBPA_BTSD_Data_s *obj = BSL_CALLOC(1, sizeof(struct MockBPA_BTSD_Data_s)); + struct MockBPA_BTSD_Data_s *obj = BSL_calloc(1, sizeof(struct MockBPA_BTSD_Data_s)); if (!obj) { return NULL; @@ -208,10 +208,10 @@ static struct BSL_SeqReader_s *MockBPA_ReadBTSD(const BSL_BundleRef_t *bundle_re obj->size = found_block->btsd_len; obj->file = fmemopen(obj->ptr, obj->size, "rb"); - BSL_SeqReader_t *reader = BSL_CALLOC(1, sizeof(BSL_SeqReader_t)); + BSL_SeqReader_t *reader = BSL_calloc(1, sizeof(BSL_SeqReader_t)); if (!reader) { - BSL_FREE(obj); + BSL_free(obj); return NULL; } reader->user_data = obj; @@ -247,11 +247,11 @@ static void MockBPA_WriteBTSD_Deinit(void *user_data) BSL_LOG_DEBUG("closed block %p with size %zu", obj->block, obj->size); // now write-back the BTSD - BSL_FREE(obj->block->btsd); + BSL_free(obj->block->btsd); obj->block->btsd = obj->ptr; obj->block->btsd_len = obj->size; - BSL_FREE(obj); + BSL_free(obj); } static struct BSL_SeqWriter_s *MockBPA_WriteBTSD(BSL_BundleRef_t *bundle_ref, uint64_t block_num, size_t total_size) @@ -265,7 +265,7 @@ static struct BSL_SeqWriter_s *MockBPA_WriteBTSD(BSL_BundleRef_t *bundle_ref, ui MockBPA_CanonicalBlock_t *found_block = *found_ptr; BSL_LOG_DEBUG("opened block %p for size %zu", found_block, total_size); - struct MockBPA_BTSD_Data_s *obj = BSL_CALLOC(1, sizeof(struct MockBPA_BTSD_Data_s)); + struct MockBPA_BTSD_Data_s *obj = BSL_calloc(1, sizeof(struct MockBPA_BTSD_Data_s)); if (!obj) { return NULL; @@ -276,11 +276,11 @@ static struct BSL_SeqWriter_s *MockBPA_WriteBTSD(BSL_BundleRef_t *bundle_ref, ui obj->size = 0; obj->file = open_memstream(&obj->ptr, &obj->size); - BSL_SeqWriter_t *writer = BSL_CALLOC(1, sizeof(BSL_SeqWriter_t)); + BSL_SeqWriter_t *writer = BSL_calloc(1, sizeof(BSL_SeqWriter_t)); if (!writer) { - BSL_FREE(obj->ptr); - BSL_FREE(obj); + BSL_free(obj->ptr); + BSL_free(obj); return NULL; } writer->user_data = obj; @@ -356,7 +356,7 @@ int MockBPA_RemoveBlock(BSL_BundleRef_t *bundle_ref, uint64_t block_num) } // Deinit and clear the target block for removal - BSL_FREE(found_block->btsd); + BSL_free(found_block->btsd); MockBPA_BlockByNum_erase(bundle->blocks_num, block_num); MockBPA_BlockList_remove(bundle->blocks, bit); @@ -444,7 +444,7 @@ int MockBPA_Agent_Init(MockBPA_Agent_t *agent) { MockBPA_Agent_BSL_Ctx_t *ctx = ctxs[ix]; - ctx->bsl = BSL_CALLOC(1, BSL_LibCtx_Sizeof()); + ctx->bsl = BSL_calloc(1, BSL_LibCtx_Sizeof()); if (BSL_API_InitLib(ctx->bsl)) { BSL_LOG_ERR("Failed BSL_API_InitLib()"); @@ -468,7 +468,7 @@ int MockBPA_Agent_Init(MockBPA_Agent_t *agent) } // TODO find a better way to deal with this { - agent->appin.policy = BSL_CALLOC(1, sizeof(BSLP_PolicyProvider_t)); + agent->appin.policy = BSL_calloc(1, sizeof(BSLP_PolicyProvider_t)); agent->appin.policy->pp_id = 1; BSL_PolicyDesc_t policy_callbacks = (BSL_PolicyDesc_t) { .deinit_fn = BSLP_Deinit, .query_fn = BSLP_QueryPolicy, @@ -477,7 +477,7 @@ int MockBPA_Agent_Init(MockBPA_Agent_t *agent) ASSERT_PROPERTY(BSL_SUCCESS == BSL_API_RegisterPolicyProvider(agent->appin.bsl, 1, policy_callbacks)); } { - agent->appout.policy = BSL_CALLOC(1, sizeof(BSLP_PolicyProvider_t)); + agent->appout.policy = BSL_calloc(1, sizeof(BSLP_PolicyProvider_t)); agent->appout.policy->pp_id = 1; BSL_PolicyDesc_t policy_callbacks = (BSL_PolicyDesc_t) { .deinit_fn = BSLP_Deinit, .query_fn = BSLP_QueryPolicy, @@ -486,7 +486,7 @@ int MockBPA_Agent_Init(MockBPA_Agent_t *agent) ASSERT_PROPERTY(BSL_SUCCESS == BSL_API_RegisterPolicyProvider(agent->appout.bsl, 1, policy_callbacks)); } { - agent->clin.policy = BSL_CALLOC(1, sizeof(BSLP_PolicyProvider_t)); + agent->clin.policy = BSL_calloc(1, sizeof(BSLP_PolicyProvider_t)); agent->clin.policy->pp_id = 1; BSL_PolicyDesc_t policy_callbacks = (BSL_PolicyDesc_t) { .deinit_fn = BSLP_Deinit, .query_fn = BSLP_QueryPolicy, @@ -495,7 +495,7 @@ int MockBPA_Agent_Init(MockBPA_Agent_t *agent) ASSERT_PROPERTY(BSL_SUCCESS == BSL_API_RegisterPolicyProvider(agent->clin.bsl, 1, policy_callbacks)); } { - agent->clout.policy = BSL_CALLOC(1, sizeof(BSLP_PolicyProvider_t)); + agent->clout.policy = BSL_calloc(1, sizeof(BSLP_PolicyProvider_t)); agent->clout.policy->pp_id = 1; BSL_PolicyDesc_t policy_callbacks = (BSL_PolicyDesc_t) { .deinit_fn = BSLP_Deinit, .query_fn = BSLP_QueryPolicy, @@ -534,7 +534,7 @@ void MockBPA_Agent_Deinit(MockBPA_Agent_t *agent) { BSL_LOG_ERR("Failed BSL_API_DeinitLib()"); } - BSL_FREE(ctx->bsl); + BSL_free(ctx->bsl); ctx->bsl = NULL; } @@ -648,8 +648,8 @@ static int MockBPA_Agent_process(MockBPA_Agent_t *agent, MockBPA_Agent_BSL_Ctx_t return 2; } - BSL_SecurityActionSet_t *malloced_action_set = BSL_CALLOC(1, BSL_SecurityActionSet_Sizeof()); - BSL_SecurityResponseSet_t *malloced_response_set = BSL_CALLOC(1, BSL_SecurityResponseSet_Sizeof()); + BSL_SecurityActionSet_t *malloced_action_set = BSL_calloc(1, BSL_SecurityActionSet_Sizeof()); + BSL_SecurityResponseSet_t *malloced_response_set = BSL_calloc(1, BSL_SecurityResponseSet_Sizeof()); BSL_BundleRef_t bundle_ref = { .data = bundle }; BSL_LOG_INFO("calling BSL_API_QuerySecurity"); @@ -677,8 +677,8 @@ static int MockBPA_Agent_process(MockBPA_Agent_t *agent, MockBPA_Agent_BSL_Ctx_t MockBPA_Agent_DumpTelemetry(agent); BSL_SecurityActionSet_Deinit(malloced_action_set); - BSL_FREE(malloced_action_set); - BSL_FREE(malloced_response_set); + BSL_free(malloced_action_set); + BSL_free(malloced_response_set); BSL_LOG_INFO("result code %d", returncode); return returncode; } diff --git a/src/mock_bpa/bundle.c b/src/mock_bpa/bundle.c index b73ef7a5..b9cefc29 100644 --- a/src/mock_bpa/bundle.c +++ b/src/mock_bpa/bundle.c @@ -49,7 +49,7 @@ int MockBPA_Bundle_Deinit(MockBPA_Bundle_t *bundle) { MockBPA_CanonicalBlock_t *blk = MockBPA_BlockList_ref(bit); BSL_LOG_DEBUG("freeing block number %" PRIu64, blk->blk_num); - BSL_FREE(blk->btsd); + BSL_free(blk->btsd); } MockBPA_BlockList_clear(bundle->blocks); diff --git a/src/mock_bpa/ctr.c b/src/mock_bpa/ctr.c index d17a3b39..550dd1c7 100644 --- a/src/mock_bpa/ctr.c +++ b/src/mock_bpa/ctr.c @@ -33,7 +33,7 @@ void mock_bpa_ctr_init(mock_bpa_ctr_t *ctr) BSL_Data_Init(&(ctr->encoded)); - ctr->bundle = BSL_CALLOC(1, sizeof(MockBPA_Bundle_t)); + ctr->bundle = BSL_calloc(1, sizeof(MockBPA_Bundle_t)); MockBPA_Bundle_Init(ctr->bundle); ctr->bundle_ref.data = ctr->bundle; @@ -60,7 +60,7 @@ void mock_bpa_ctr_deinit(mock_bpa_ctr_t *ctr) if (ctr->bundle) { MockBPA_Bundle_Deinit(ctr->bundle); - BSL_FREE(ctr->bundle); + BSL_free(ctr->bundle); } } diff --git a/src/mock_bpa/decode.c b/src/mock_bpa/decode.c index ff429f7b..254173ca 100644 --- a/src/mock_bpa/decode.c +++ b/src/mock_bpa/decode.c @@ -254,14 +254,14 @@ int bsl_mock_decode_canonical(QCBORDecodeContext *dec, MockBPA_CanonicalBlock_t { if (blk->btsd) { - BSL_FREE(blk->btsd); + BSL_free(blk->btsd); blk->btsd = NULL; } blk->btsd_len = view.len; if (blk->btsd_len > 0) { - blk->btsd = BSL_MALLOC(view.len); + blk->btsd = BSL_malloc(view.len); // GCOV_EXCL_START ASSERT_ARG_NONNULL(blk->btsd); // GCOV_EXCL_STOP diff --git a/src/mock_bpa/eid.c b/src/mock_bpa/eid.c index 94052ab3..2f751727 100644 --- a/src/mock_bpa/eid.c +++ b/src/mock_bpa/eid.c @@ -57,7 +57,7 @@ int MockBPA_EID_Init(void *user_data _U_, BSL_HostEID_t *eid) { BSL_CHKERR1(eid); memset(eid, 0, sizeof(BSL_HostEID_t)); - eid->handle = BSL_MALLOC(sizeof(bsl_mock_eid_t)); + eid->handle = BSL_malloc(sizeof(bsl_mock_eid_t)); if (!(eid->handle)) { return -2; @@ -72,7 +72,7 @@ void MockBPA_EID_Deinit(void *user_data _U_, BSL_HostEID_t *eid) if (eid->handle) { bsl_mock_eid_deinit(eid->handle); - BSL_FREE(eid->handle); + BSL_free(eid->handle); } memset(eid, 0, sizeof(BSL_HostEID_t)); } diff --git a/src/mock_bpa/eidpat.c b/src/mock_bpa/eidpat.c index 2d54df8d..e1b7e068 100644 --- a/src/mock_bpa/eidpat.c +++ b/src/mock_bpa/eidpat.c @@ -389,7 +389,7 @@ int mock_bpa_eidpat_init(BSL_HostEIDPattern_t *pat, void *user_data _U_) BSL_CHKERR1(pat); // GCOV_EXCL_LINE memset(pat, 0, sizeof(BSL_HostEIDPattern_t)); - pat->handle = BSL_MALLOC(sizeof(bsl_mock_eidpat_t)); + pat->handle = BSL_malloc(sizeof(bsl_mock_eidpat_t)); if (!(pat->handle)) { return 2; // GCOV_EXCL_LINE @@ -416,7 +416,7 @@ void mock_bpa_eidpat_deinit(BSL_HostEIDPattern_t *pat, void *user_data _U_) if (pat->handle) { bsl_mock_eidpat_deinit(pat->handle); - BSL_FREE(pat->handle); + BSL_free(pat->handle); } memset(pat, 0, sizeof(BSL_HostEIDPattern_t)); } diff --git a/src/mock_bpa/policy_params.c b/src/mock_bpa/policy_params.c index f51662b6..2ebd69a0 100644 --- a/src/mock_bpa/policy_params.c +++ b/src/mock_bpa/policy_params.c @@ -29,13 +29,13 @@ void mock_bpa_policy_params_init(mock_bpa_policy_params_t *params, int policy_num) { - params->param_integ_scope_flag = BSL_CALLOC(1, BSL_SecParam_Sizeof()); - params->param_sha_variant = BSL_CALLOC(1, BSL_SecParam_Sizeof()); - params->param_aad_scope_flag = BSL_CALLOC(1, BSL_SecParam_Sizeof()); - params->param_init_vector = BSL_CALLOC(1, BSL_SecParam_Sizeof()); - params->param_aes_variant = BSL_CALLOC(1, BSL_SecParam_Sizeof()); - params->param_test_key = BSL_CALLOC(1, BSL_SecParam_Sizeof()); - params->param_use_wrapped_key = BSL_CALLOC(1, BSL_SecParam_Sizeof()); + params->param_integ_scope_flag = BSL_calloc(1, BSL_SecParam_Sizeof()); + params->param_sha_variant = BSL_calloc(1, BSL_SecParam_Sizeof()); + params->param_aad_scope_flag = BSL_calloc(1, BSL_SecParam_Sizeof()); + params->param_init_vector = BSL_calloc(1, BSL_SecParam_Sizeof()); + params->param_aes_variant = BSL_calloc(1, BSL_SecParam_Sizeof()); + params->param_test_key = BSL_calloc(1, BSL_SecParam_Sizeof()); + params->param_use_wrapped_key = BSL_calloc(1, BSL_SecParam_Sizeof()); params->active = true; @@ -45,19 +45,19 @@ void mock_bpa_policy_params_init(mock_bpa_policy_params_t *params, int policy_nu void mock_bpa_policy_params_deinit(mock_bpa_policy_params_t *params, int policy_num) { BSL_SecParam_Deinit(params->param_integ_scope_flag); - BSL_FREE(params->param_integ_scope_flag); + BSL_free(params->param_integ_scope_flag); BSL_SecParam_Deinit(params->param_sha_variant); - BSL_FREE(params->param_sha_variant); + BSL_free(params->param_sha_variant); BSL_SecParam_Deinit(params->param_aad_scope_flag); - BSL_FREE(params->param_aad_scope_flag); + BSL_free(params->param_aad_scope_flag); BSL_SecParam_Deinit(params->param_init_vector); - BSL_FREE(params->param_init_vector); + BSL_free(params->param_init_vector); BSL_SecParam_Deinit(params->param_aes_variant); - BSL_FREE(params->param_aes_variant); + BSL_free(params->param_aes_variant); BSL_SecParam_Deinit(params->param_test_key); - BSL_FREE(params->param_test_key); + BSL_free(params->param_test_key); BSL_SecParam_Deinit(params->param_use_wrapped_key); - BSL_FREE(params->param_use_wrapped_key); + BSL_free(params->param_use_wrapped_key); params->active = false; diff --git a/src/policy_provider/SamplePolicyProvider.c b/src/policy_provider/SamplePolicyProvider.c index 0e635485..8d6658c7 100644 --- a/src/policy_provider/SamplePolicyProvider.c +++ b/src/policy_provider/SamplePolicyProvider.c @@ -164,7 +164,7 @@ int BSLP_QueryPolicy(const void *user_data, BSL_SecurityActionSet_t *output_acti return BSL_ERR_HOST_CALLBACK_FAILED; } - BSL_SecurityAction_t *action = BSL_CALLOC(1, BSL_SecurityAction_Sizeof()); + BSL_SecurityAction_t *action = BSL_calloc(1, BSL_SecurityAction_Sizeof()); BSL_SecurityAction_Init(action); BSLP_SecOperPtrList_t secops; @@ -195,14 +195,14 @@ int BSLP_QueryPolicy(const void *user_data, BSL_SecurityActionSet_t *output_acti continue; } - BSL_SecOper_t *sec_oper = BSL_CALLOC(1, BSL_SecOper_Sizeof()); + BSL_SecOper_t *sec_oper = BSL_calloc(1, BSL_SecOper_Sizeof()); BSL_SecOper_Init(sec_oper); if (BSLP_PolicyRule_EvaluateAsSecOper(rule, sec_oper, bundle, location) < 0) { BSL_LOG_WARNING("SecOp evaluate failed"); BSL_SecurityAction_IncrError(action); BSL_SecOper_Deinit(sec_oper); - BSL_FREE(sec_oper); + BSL_free(sec_oper); continue; } @@ -279,13 +279,13 @@ int BSLP_QueryPolicy(const void *user_data, BSL_SecurityActionSet_t *output_acti { BSL_SecOper_t **secop = BSLP_SecOperPtrList_get(secops, i); BSL_SecurityAction_AppendSecOper(action, *secop); - BSL_FREE(*secop); + BSL_free(*secop); } BSLP_SecOperPtrList_clear(secops); BSL_SecurityActionSet_AppendAction(output_action_set, action); BSL_SecurityAction_Deinit(action); - BSL_FREE(action); + BSL_free(action); CHK_POSTCONDITION(BSL_SecurityActionSet_IsConsistent(output_action_set)); return (int)BSL_SecurityActionSet_CountErrors(output_action_set); @@ -370,7 +370,7 @@ void BSLP_Deinit(void *user_data) BSLP_PolicyPredicate_Deinit(&self->predicates[index]); } memset(self, 0, sizeof(*self)); - BSL_FREE(user_data); + BSL_free(user_data); } void BSLP_PolicyPredicate_Init(BSLP_PolicyPredicate_t *self, BSL_PolicyLocation_e location, @@ -422,7 +422,7 @@ int BSLP_PolicyRule_Init(BSLP_PolicyRule_t *self, const char *desc, BSLP_PolicyP memset(self, 0, sizeof(*self)); size_t desc_sz = strnlen(desc, BSLP_POLICYPREDICATE_ARRAY_CAPACITY); - self->description = BSL_MALLOC(desc_sz + 1); + self->description = BSL_malloc(desc_sz + 1); strncpy(self->description, desc, desc_sz); self->description[desc_sz] = '\0'; @@ -442,7 +442,7 @@ void BSLP_PolicyRule_Deinit(BSLP_PolicyRule_t *self) { ASSERT_ARG_EXPR(BSLP_PolicyRule_IsConsistent(self)); BSL_LOG_INFO("BSLP_PolicyRule_Deinit: %s, nparams=%zu", self->description, BSLB_SecParamList_size(self->params)); - BSL_FREE(self->description); + BSL_free(self->description); BSLB_SecParamList_clear(self->params); memset(self, 0, sizeof(*self)); } diff --git a/src/security_context/BCB_AES_GCM.c b/src/security_context/BCB_AES_GCM.c index 6eae1e20..e232467e 100644 --- a/src/security_context/BCB_AES_GCM.c +++ b/src/security_context/BCB_AES_GCM.c @@ -651,14 +651,14 @@ int BSLX_BCB_Execute(BSL_LibCtx_t *lib _U_, BSL_BundleRef_t *bundle, const BSL_S // If present, append it to the result. if (bcb_context.authtag.len > 0) { - BSL_SecResult_t *auth_tag = BSL_CALLOC(1, BSL_SecResult_Sizeof()); + BSL_SecResult_t *auth_tag = BSL_calloc(1, BSL_SecResult_Sizeof()); if (BSL_SUCCESS != BSL_SecResult_InitFull(auth_tag, RFC9173_BCB_RESULTID_AUTHTAG, RFC9173_CONTEXTID_BCB_AES_GCM, BSL_SecOper_GetTargetBlockNum(sec_oper), &bcb_context.authtag)) { BSL_LOG_ERR("Failed to append BCB auth tag"); BSL_SecResult_Deinit(auth_tag); - BSL_FREE(auth_tag); + BSL_free(auth_tag); BSLX_BCB_Deinit(&bcb_context); return BSL_ERR_SECURITY_CONTEXT_FAILED; } @@ -668,17 +668,17 @@ int BSLX_BCB_Execute(BSL_LibCtx_t *lib _U_, BSL_BundleRef_t *bundle, const BSL_S BSL_SecOutcome_AppendResult(sec_outcome, auth_tag); } BSL_SecResult_Deinit(auth_tag); - BSL_FREE(auth_tag); + BSL_free(auth_tag); } if (bcb_context.iv.len > 0) { - BSL_SecParam_t *iv_param = BSL_CALLOC(1, BSL_SecParam_Sizeof()); + BSL_SecParam_t *iv_param = BSL_calloc(1, BSL_SecParam_Sizeof()); if (BSL_SUCCESS != BSL_SecParam_InitBytestr(iv_param, RFC9173_BCB_SECPARAM_IV, bcb_context.iv)) { BSL_LOG_ERR("Failed to append BCB source IV"); BSL_SecParam_Deinit(iv_param); - BSL_FREE(iv_param); + BSL_free(iv_param); BSLX_BCB_Deinit(&bcb_context); return BSL_ERR_SECURITY_CONTEXT_FAILED; } @@ -688,15 +688,15 @@ int BSLX_BCB_Execute(BSL_LibCtx_t *lib _U_, BSL_BundleRef_t *bundle, const BSL_S BSL_SecOutcome_AppendParam(sec_outcome, iv_param); } BSL_SecParam_Deinit(iv_param); - BSL_FREE(iv_param); + BSL_free(iv_param); } - BSL_SecParam_t *aes_param = BSL_CALLOC(1, BSL_SecParam_Sizeof()); + BSL_SecParam_t *aes_param = BSL_calloc(1, BSL_SecParam_Sizeof()); if (BSL_SUCCESS != BSL_SecParam_InitInt64(aes_param, RFC9173_BCB_SECPARAM_AESVARIANT, bcb_context.aes_variant)) { BSL_LOG_ERR("Failed to append BCB AES param"); BSL_SecParam_Deinit(aes_param); - BSL_FREE(aes_param); + BSL_free(aes_param); BSLX_BCB_Deinit(&bcb_context); return BSL_ERR_SECURITY_CONTEXT_FAILED; } @@ -706,18 +706,18 @@ int BSLX_BCB_Execute(BSL_LibCtx_t *lib _U_, BSL_BundleRef_t *bundle, const BSL_S BSL_SecOutcome_AppendParam(sec_outcome, aes_param); } BSL_SecParam_Deinit(aes_param); - BSL_FREE(aes_param); + BSL_free(aes_param); if (bcb_context.wrapped_key.len > 0) { - BSL_SecParam_t *aes_wrapped_key_param = BSL_CALLOC(1, BSL_SecParam_Sizeof()); + BSL_SecParam_t *aes_wrapped_key_param = BSL_calloc(1, BSL_SecParam_Sizeof()); if (BSL_SUCCESS != BSL_SecParam_InitBytestr(aes_wrapped_key_param, RFC9173_BCB_SECPARAM_WRAPPEDKEY, bcb_context.wrapped_key)) { BSL_LOG_ERR("Failed to append BCB wrapped key param"); BSL_SecParam_Deinit(aes_wrapped_key_param); - BSL_FREE(aes_wrapped_key_param); + BSL_free(aes_wrapped_key_param); BSLX_BCB_Deinit(&bcb_context); return BSL_ERR_SECURITY_CONTEXT_FAILED; } @@ -727,15 +727,15 @@ int BSLX_BCB_Execute(BSL_LibCtx_t *lib _U_, BSL_BundleRef_t *bundle, const BSL_S BSL_SecOutcome_AppendParam(sec_outcome, aes_wrapped_key_param); } BSL_SecParam_Deinit(aes_wrapped_key_param); - BSL_FREE(aes_wrapped_key_param); + BSL_free(aes_wrapped_key_param); } - BSL_SecParam_t *scope_flag_param = BSL_CALLOC(1, BSL_SecParam_Sizeof()); + BSL_SecParam_t *scope_flag_param = BSL_calloc(1, BSL_SecParam_Sizeof()); if (BSL_SUCCESS != BSL_SecParam_InitInt64(scope_flag_param, RFC9173_BCB_SECPARAM_AADSCOPE, bcb_context.aad_scope)) { BSL_LOG_ERR("Failed to append BCB scope flag param"); BSL_SecParam_Deinit(scope_flag_param); - BSL_FREE(scope_flag_param); + BSL_free(scope_flag_param); BSLX_BCB_Deinit(&bcb_context); return BSL_ERR_SECURITY_CONTEXT_FAILED; } @@ -745,7 +745,7 @@ int BSLX_BCB_Execute(BSL_LibCtx_t *lib _U_, BSL_BundleRef_t *bundle, const BSL_S BSL_SecOutcome_AppendParam(sec_outcome, scope_flag_param); } BSL_SecParam_Deinit(scope_flag_param); - BSL_FREE(scope_flag_param); + BSL_free(scope_flag_param); BSLX_BCB_Deinit(&bcb_context); return BSL_SUCCESS; diff --git a/src/security_context/BIB_HMAC_SHA2.c b/src/security_context/BIB_HMAC_SHA2.c index e60acf3c..cb951640 100644 --- a/src/security_context/BIB_HMAC_SHA2.c +++ b/src/security_context/BIB_HMAC_SHA2.c @@ -515,23 +515,23 @@ int BSLX_BIB_Execute(BSL_LibCtx_t *lib, BSL_BundleRef_t *bundle, const BSL_SecOp } { - BSL_SecResult_t *bib_result = BSL_CALLOC(1, BSL_SecResult_Sizeof()); + BSL_SecResult_t *bib_result = BSL_calloc(1, BSL_SecResult_Sizeof()); BSL_SecResult_InitFull(bib_result, RFC9173_BIB_RESULTID_HMAC, RFC9173_CONTEXTID_BIB_HMAC_SHA2, BSL_SecOper_GetTargetBlockNum(sec_oper), &bib_context.hmac_result_val); BSL_SecOutcome_AppendResult(sec_outcome, bib_result); BSL_SecResult_Deinit(bib_result); - BSL_FREE(bib_result); + BSL_free(bib_result); } if (bib_context.wrapped_key.len > 0) { - BSL_SecParam_t *wrapped_key_param = BSL_CALLOC(1, BSL_SecParam_Sizeof()); + BSL_SecParam_t *wrapped_key_param = BSL_calloc(1, BSL_SecParam_Sizeof()); if (BSL_SUCCESS != BSL_SecParam_InitBytestr(wrapped_key_param, RFC9173_BIB_PARAMID_WRAPPED_KEY, bib_context.wrapped_key)) { BSL_LOG_ERR("Failed to append BIB wrapped key param"); BSL_SecParam_Deinit(wrapped_key_param); - BSL_FREE(wrapped_key_param); + BSL_free(wrapped_key_param); BSLX_BIB_Deinit(&bib_context); return BSL_ERR_SECURITY_CONTEXT_FAILED; } @@ -541,7 +541,7 @@ int BSLX_BIB_Execute(BSL_LibCtx_t *lib, BSL_BundleRef_t *bundle, const BSL_SecOp BSL_SecOutcome_AppendParam(sec_outcome, wrapped_key_param); } BSL_SecParam_Deinit(wrapped_key_param); - BSL_FREE(wrapped_key_param); + BSL_free(wrapped_key_param); } BSLX_BIB_Deinit(&bib_context); diff --git a/test/bsl_test_utils.c b/test/bsl_test_utils.c index f246e27b..a6c5b21f 100644 --- a/test/bsl_test_utils.c +++ b/test/bsl_test_utils.c @@ -144,22 +144,22 @@ void BSL_TestUtils_InitBCB_Appendix2(BCBTestContext *context, BSL_SecRole_e role BSL_SecurityActionSet_t *BSL_TestUtils_InitMallocBIBActionSet(BIBTestContext *bib_context) { - BSL_SecurityActionSet_t *action_set = BSL_CALLOC(1, sizeof(BSL_SecurityActionSet_t)); + BSL_SecurityActionSet_t *action_set = BSL_calloc(1, sizeof(BSL_SecurityActionSet_t)); BSL_SecurityActionSet_Init(action_set); - BSL_SecurityAction_t *act = BSL_CALLOC(1, sizeof(BSL_SecurityAction_t)); + BSL_SecurityAction_t *act = BSL_calloc(1, sizeof(BSL_SecurityAction_t)); BSL_SecurityAction_Init(act); BSL_SecurityAction_AppendSecOper(act, &bib_context->sec_oper); // ensure consistent context state BSL_SecOper_Init(&bib_context->sec_oper); BSL_SecurityActionSet_AppendAction(action_set, act); BSL_SecurityAction_Deinit(act); - BSL_FREE(act); + BSL_free(act); return action_set; } BSL_SecurityResponseSet_t *BSL_TestUtils_MallocEmptyPolicyResponse(void) { - return BSL_CALLOC(1, BSL_SecurityResponseSet_Sizeof()); + return BSL_calloc(1, BSL_SecurityResponseSet_Sizeof()); } int rfc9173_byte_gen_fn_a1(unsigned char *buf, int len) @@ -490,12 +490,12 @@ static void BSL_TestUtils_ReadBTSD_Deinit(void *user_data) fclose(obj->file); // buffer is external data, no cleanup - BSL_FREE(obj); + BSL_free(obj); } BSL_SeqReader_t *BSL_TestUtils_FlatReader(const void *buf, size_t bufsize) { - struct BSL_TestUtils_Flat_Data_s *obj = BSL_CALLOC(1, sizeof(struct BSL_TestUtils_Flat_Data_s)); + struct BSL_TestUtils_Flat_Data_s *obj = BSL_calloc(1, sizeof(struct BSL_TestUtils_Flat_Data_s)); ASSERT_PROPERTY(obj); obj->origbuf = NULL; obj->origsize = NULL; @@ -503,7 +503,7 @@ BSL_SeqReader_t *BSL_TestUtils_FlatReader(const void *buf, size_t bufsize) obj->size = bufsize; obj->file = fmemopen(obj->ptr, obj->size, "rb"); - BSL_SeqReader_t *reader = BSL_MALLOC(sizeof(BSL_SeqReader_t)); + BSL_SeqReader_t *reader = BSL_malloc(sizeof(BSL_SeqReader_t)); ASSERT_PROPERTY(reader); reader->user_data = obj; reader->read = BSL_TestUtils_ReadBTSD_Read; @@ -548,12 +548,12 @@ static void BSL_TestUtils_WriteBTSD_Deinit(void *user_data) *obj->origsize = obj->size; } - BSL_FREE(obj); + BSL_free(obj); } BSL_SeqWriter_t *BSL_TestUtils_FlatWriter(void **buf, size_t *bufsize) { - struct BSL_TestUtils_Flat_Data_s *obj = BSL_CALLOC(1, sizeof(struct BSL_TestUtils_Flat_Data_s)); + struct BSL_TestUtils_Flat_Data_s *obj = BSL_calloc(1, sizeof(struct BSL_TestUtils_Flat_Data_s)); ASSERT_PROPERTY(obj); // double-buffer for this write obj->origbuf = buf; @@ -562,7 +562,7 @@ BSL_SeqWriter_t *BSL_TestUtils_FlatWriter(void **buf, size_t *bufsize) obj->size = 0; obj->file = open_memstream(&obj->ptr, &obj->size); - BSL_SeqWriter_t *writer = BSL_MALLOC(sizeof(BSL_SeqWriter_t)); + BSL_SeqWriter_t *writer = BSL_malloc(sizeof(BSL_SeqWriter_t)); ASSERT_PROPERTY(writer); writer->user_data = obj; writer->write = BSL_TestUtils_WriteBTSD_Write; diff --git a/test/fuzz_dynamic_asb_cbor.cpp b/test/fuzz_dynamic_asb_cbor.cpp index a2ae0914..9a42379a 100644 --- a/test/fuzz_dynamic_asb_cbor.cpp +++ b/test/fuzz_dynamic_asb_cbor.cpp @@ -44,7 +44,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { int retval = 0; - BSL_AbsSecBlock_t *asb = (BSL_AbsSecBlock_t *)BSL_MALLOC(BSL_AbsSecBlock_Sizeof()); + BSL_AbsSecBlock_t *asb = (BSL_AbsSecBlock_t *)BSL_malloc(BSL_AbsSecBlock_Sizeof()); BSL_AbsSecBlock_InitEmpty(asb); { @@ -90,7 +90,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) BSL_Data_Deinit(&out_data); BSL_AbsSecBlock_Deinit(asb); - BSL_FREE(asb); + BSL_free(asb); return retval; } diff --git a/test/test_AbsSecBlock.c b/test/test_AbsSecBlock.c index 145e2cfb..72e9506c 100644 --- a/test/test_AbsSecBlock.c +++ b/test/test_AbsSecBlock.c @@ -52,7 +52,7 @@ void TestASBDecodeEncodeClosure(uint8_t *asb_cbor, size_t asb_cbor_bytelen, int6 { BSL_Data_t asb_cbor_data; BSL_Data_InitView(&asb_cbor_data, asb_cbor_bytelen, asb_cbor); - BSL_AbsSecBlock_t *asb = BSL_CALLOC(1, BSL_AbsSecBlock_Sizeof()); + BSL_AbsSecBlock_t *asb = BSL_calloc(1, BSL_AbsSecBlock_Sizeof()); BSL_AbsSecBlock_InitEmpty(asb); const int decode_result = BSL_AbsSecBlock_DecodeFromCBOR(asb, &asb_cbor_data); @@ -82,7 +82,7 @@ void TestASBDecodeEncodeClosure(uint8_t *asb_cbor, size_t asb_cbor_bytelen, int6 BSL_Data_Deinit(&encoded_cbor); BSL_AbsSecBlock_Deinit(asb); - BSL_FREE(asb); + BSL_free(asb); } // See: https://www.rfc-editor.org/rfc/rfc9173.html#name-abstract-security-block-2 @@ -146,14 +146,14 @@ void test_AbsSecBlock_Decode_failure(const char *hexdata) string_clear(in_text); } - BSL_AbsSecBlock_t *asb = BSL_CALLOC(1, BSL_AbsSecBlock_Sizeof()); + BSL_AbsSecBlock_t *asb = BSL_calloc(1, BSL_AbsSecBlock_Sizeof()); BSL_AbsSecBlock_InitEmpty(asb); const int decode_result = BSL_AbsSecBlock_DecodeFromCBOR(asb, &in_data); TEST_ASSERT_EQUAL_INT(BSL_ERR_DECODING, decode_result); BSL_AbsSecBlock_Deinit(asb); - BSL_FREE(asb); + BSL_free(asb); BSL_Data_Deinit(&in_data); } diff --git a/test/test_BackendPolicyProvider.c b/test/test_BackendPolicyProvider.c index d396bc6b..f01d4785 100644 --- a/test/test_BackendPolicyProvider.c +++ b/test/test_BackendPolicyProvider.c @@ -62,7 +62,7 @@ void setUp(void) TEST_ASSERT_EQUAL(0, BSL_API_InitLib(&LocalTestCtx.bsl)); BSL_PolicyDesc_t policy_desc = { 0 }; - policy_desc.user_data = BSL_CALLOC(1, sizeof(BSLP_PolicyProvider_t)); + policy_desc.user_data = BSL_calloc(1, sizeof(BSLP_PolicyProvider_t)); policy_desc.query_fn = BSLP_QueryPolicy; policy_desc.finalize_fn = BSLP_FinalizePolicy; policy_desc.deinit_fn = BSLP_Deinit; @@ -172,7 +172,7 @@ void test_PolicyProvider_Inspect_RFC9173_BIB(void) void test_MultiplePolicyProviders(void) { BSL_PolicyDesc_t policy_desc_2 = { 0 }; - policy_desc_2.user_data = BSL_CALLOC(1, sizeof(BSLP_PolicyProvider_t)); + policy_desc_2.user_data = BSL_calloc(1, sizeof(BSLP_PolicyProvider_t)); policy_desc_2.query_fn = BSLP_QueryPolicy; policy_desc_2.finalize_fn = BSLP_FinalizePolicy; policy_desc_2.deinit_fn = BSLP_Deinit; @@ -244,5 +244,5 @@ void test_MultiplePolicyProviders(void) &LocalTestCtx.mock_bpa_ctr.bundle_ref, response_set)); BSL_SecurityActionSet_Deinit(&action_set); - BSL_FREE(response_set); + BSL_free(response_set); } diff --git a/test/test_BackendSecurityContext.c b/test/test_BackendSecurityContext.c index 0920353c..40cbbd35 100644 --- a/test/test_BackendSecurityContext.c +++ b/test/test_BackendSecurityContext.c @@ -117,9 +117,9 @@ void test_SecurityContext_BIB_Source(void) (BSL_TestUtils_IsB16StrEqualTo(RFC9173_TestVectors_AppendixA1.cbor_bundle_bib, mock_bpa_ctr->encoded)); BSL_SecurityResponseSet_Deinit(malloced_responseset); - BSL_FREE(malloced_responseset); + BSL_free(malloced_responseset); BSL_SecurityActionSet_Deinit(malloced_actionset); - BSL_FREE(malloced_actionset); + BSL_free(malloced_actionset); BIBTestContext_Deinit(&bib_test_context); TEST_ASSERT_TRUE(is_expected); @@ -158,9 +158,9 @@ void test_SecurityContext_BIB_Verifier(void) (BSL_TestUtils_IsB16StrEqualTo(RFC9173_TestVectors_AppendixA1.cbor_bundle_bib, mock_bpa_ctr->encoded)); BSL_SecurityActionSet_Deinit(malloced_actionset); - BSL_FREE(malloced_actionset); + BSL_free(malloced_actionset); BSL_SecurityResponseSet_Deinit(malloced_responseset); - BSL_FREE(malloced_responseset); + BSL_free(malloced_responseset); BIBTestContext_Deinit(&bib_test_context); TEST_ASSERT_TRUE(is_match); @@ -207,9 +207,9 @@ void test_SecurityContext_BIB_Verifier_Failure(void) BSL_SECOP_CONCLUSION_FAILURE); BSL_SecurityActionSet_Deinit(malloced_actionset); - BSL_FREE(malloced_actionset); + BSL_free(malloced_actionset); BSL_SecurityResponseSet_Deinit(malloced_responseset); - BSL_FREE(malloced_responseset); + BSL_free(malloced_responseset); BIBTestContext_Deinit(&bib_test_context); } @@ -258,9 +258,9 @@ void test_SecurityContext_BIB_Acceptor(void) cleanup: BSL_SecurityActionSet_Deinit(malloced_actionset); - BSL_FREE(malloced_actionset); + BSL_free(malloced_actionset); BSL_SecurityResponseSet_Deinit(malloced_responseset); - BSL_FREE(malloced_responseset); + BSL_free(malloced_responseset); BIBTestContext_Deinit(&bib_test_context); TEST_ASSERT_EQUAL(0, sec_context_result); @@ -316,10 +316,10 @@ void test_RFC9173_AppendixA_Example3_Acceptor(void) BSL_SecOper_AppendParam(&bcb_oper, &bcb_context.param_test_key_id); BSL_SecOper_AppendParam(&bcb_oper, &bcb_context.use_key_wrap); - BSL_SecurityActionSet_t *malloced_actionset = BSL_CALLOC(1, BSL_SecurityActionSet_Sizeof()); + BSL_SecurityActionSet_t *malloced_actionset = BSL_calloc(1, BSL_SecurityActionSet_Sizeof()); BSL_SecurityActionSet_Init(malloced_actionset); - BSL_SecurityAction_t *malloced_action = BSL_CALLOC(1, BSL_SecurityAction_Sizeof()); + BSL_SecurityAction_t *malloced_action = BSL_calloc(1, BSL_SecurityAction_Sizeof()); BSL_SecurityAction_Init(malloced_action); BSL_SecurityAction_AppendSecOper(malloced_action, &bib_oper_ext_block); BSL_SecurityAction_AppendSecOper(malloced_action, &bcb_oper); @@ -334,11 +334,11 @@ void test_RFC9173_AppendixA_Example3_Acceptor(void) TEST_ASSERT_EQUAL(BSL_SUCCESS, exec_result); BSL_SecurityAction_Deinit(malloced_action); - BSL_FREE(malloced_action); + BSL_free(malloced_action); BSL_SecurityActionSet_Deinit(malloced_actionset); - BSL_FREE(malloced_actionset); + BSL_free(malloced_actionset); BSL_SecurityResponseSet_Deinit(malloced_responseset); - BSL_FREE(malloced_responseset); + BSL_free(malloced_responseset); BSL_PrimaryBlock_deinit(&primary_block); BIBTestContext_Deinit(&bib_context); BCBTestContext_Deinit(&bcb_context); @@ -400,10 +400,10 @@ void test_RFC9173_AppendixA_Example3_Source(void) BSL_SecOper_AppendParam(&bcb_oper, &bcb_context.param_aes_variant); BSL_SecOper_AppendParam(&bcb_oper, &bcb_context.use_key_wrap); - BSL_SecurityActionSet_t *malloced_actionset = BSL_CALLOC(1, BSL_SecurityActionSet_Sizeof()); + BSL_SecurityActionSet_t *malloced_actionset = BSL_calloc(1, BSL_SecurityActionSet_Sizeof()); BSL_SecurityActionSet_Init(malloced_actionset); - BSL_SecurityAction_t *malloced_action = BSL_CALLOC(1, BSL_SecurityAction_Sizeof()); + BSL_SecurityAction_t *malloced_action = BSL_calloc(1, BSL_SecurityAction_Sizeof()); BSL_SecurityAction_Init(malloced_action); BSL_SecurityAction_AppendSecOper(malloced_action, &bib_oper_primary); BSL_SecurityAction_AppendSecOper(malloced_action, &bib_oper_ext_block); @@ -430,11 +430,11 @@ void test_RFC9173_AppendixA_Example3_Source(void) BSL_SecurityResponseSet_Deinit(malloced_responseset); BSL_SecurityAction_Deinit(malloced_action); - BSL_FREE(malloced_action); + BSL_free(malloced_action); BSL_SecurityActionSet_Deinit(malloced_actionset); - BSL_FREE(malloced_actionset); + BSL_free(malloced_actionset); BSL_SecurityResponseSet_Deinit(malloced_responseset); - BSL_FREE(malloced_responseset); + BSL_free(malloced_responseset); BSL_PrimaryBlock_deinit(&primary_block); BIBTestContext_Deinit(&bib_context); BCBTestContext_Deinit(&bcb_context); @@ -510,10 +510,10 @@ void test_RFC9173_AppendixA_Example4_Acceptor(void) BSL_SecOper_AppendParam(&bib_oper_payload, &bib_context.param_scope_flags); BSL_SecOper_AppendParam(&bib_oper_payload, &bib_context.use_key_wrap); - BSL_SecurityActionSet_t *malloced_actionset = BSL_CALLOC(1, BSL_SecurityActionSet_Sizeof()); + BSL_SecurityActionSet_t *malloced_actionset = BSL_calloc(1, BSL_SecurityActionSet_Sizeof()); BSL_SecurityActionSet_Init(malloced_actionset); - BSL_SecurityAction_t *malloced_action = BSL_CALLOC(1, BSL_SecurityAction_Sizeof()); + BSL_SecurityAction_t *malloced_action = BSL_calloc(1, BSL_SecurityAction_Sizeof()); BSL_SecurityAction_Init(malloced_action); BSL_SecurityAction_AppendSecOper(malloced_action, &bcb_op_tgt_payload); BSL_SecurityAction_AppendSecOper(malloced_action, &bcb_op_tgt_bib); @@ -536,11 +536,11 @@ void test_RFC9173_AppendixA_Example4_Acceptor(void) TEST_ASSERT_TRUE(BSL_TestUtils_IsB16StrEqualTo(expected_processed_bundle, mock_bpa_ctr->encoded)); BSL_SecurityAction_Deinit(malloced_action); - BSL_FREE(malloced_action); + BSL_free(malloced_action); BSL_SecurityActionSet_Deinit(malloced_actionset); - BSL_FREE(malloced_actionset); + BSL_free(malloced_actionset); BSL_SecurityResponseSet_Deinit(malloced_responseset); - BSL_FREE(malloced_responseset); + BSL_free(malloced_responseset); BSL_PrimaryBlock_deinit(&primary_block); BIBTestContext_Deinit(&bib_context); BCBTestContext_Deinit(&bcb_context); @@ -603,10 +603,10 @@ void test_RFC9173_AppendixA_Example4_Source(void) BSL_SecOper_AppendParam(&bcb_op_tgt_bib, &bcb_context.param_aes_variant); BSL_SecOper_AppendParam(&bcb_op_tgt_bib, &bcb_context.use_key_wrap); - BSL_SecurityActionSet_t *malloced_actionset = BSL_CALLOC(1, BSL_SecurityActionSet_Sizeof()); + BSL_SecurityActionSet_t *malloced_actionset = BSL_calloc(1, BSL_SecurityActionSet_Sizeof()); BSL_SecurityActionSet_Init(malloced_actionset); - BSL_SecurityAction_t *malloced_action = BSL_CALLOC(1, BSL_SecurityAction_Sizeof()); + BSL_SecurityAction_t *malloced_action = BSL_calloc(1, BSL_SecurityAction_Sizeof()); BSL_SecurityAction_Init(malloced_action); BSL_SecurityAction_AppendSecOper(malloced_action, &bib_oper_payload); BSL_SecurityAction_AppendSecOper(malloced_action, &bcb_op_tgt_payload); @@ -626,11 +626,11 @@ void test_RFC9173_AppendixA_Example4_Source(void) TEST_ASSERT_TRUE(primary_block.block_count <= 4); BSL_SecurityAction_Deinit(malloced_action); - BSL_FREE(malloced_action); + BSL_free(malloced_action); BSL_SecurityActionSet_Deinit(malloced_actionset); - BSL_FREE(malloced_actionset); + BSL_free(malloced_actionset); BSL_SecurityResponseSet_Deinit(malloced_responseset); - BSL_FREE(malloced_responseset); + BSL_free(malloced_responseset); BSL_PrimaryBlock_deinit(&primary_block); BIBTestContext_Deinit(&bib_context); BCBTestContext_Deinit(&bcb_context); diff --git a/test/test_CryptoInterface.c b/test/test_CryptoInterface.c index a7da4c6a..bbc70482 100644 --- a/test/test_CryptoInterface.c +++ b/test/test_CryptoInterface.c @@ -288,7 +288,7 @@ void test_SeqWriter_flat(void) const uint8_t expect[] = { 0x01, 0x02, 0x03, 0x01, 0x02 }; TEST_ASSERT_EQUAL_MEMORY(expect, dest, sizeof(expect)); - BSL_FREE(dest); + BSL_free(dest); } // test vectors from RFC 4231 @@ -470,7 +470,7 @@ void test_encrypt(const char *plaintext_in, const char *keyid) res = BSL_Cipher_Deinit(&ctx); TEST_ASSERT_EQUAL(0, res); - BSL_FREE(ciphertext); + BSL_free(ciphertext); } /** @@ -541,7 +541,7 @@ void test_decrypt(const char *plaintext_in, const char *keyid) TEST_ASSERT_EQUAL(0, res); TEST_ASSERT_EQUAL(0, BSL_SeqReader_Destroy(reader)); - BSL_FREE(plaintext); + BSL_free(plaintext); } TEST_RANGE(<6, 18, 1>) diff --git a/test/test_DefaultSecurityContext.c b/test/test_DefaultSecurityContext.c index 87ba4f22..4107c29d 100644 --- a/test/test_DefaultSecurityContext.c +++ b/test/test_DefaultSecurityContext.c @@ -106,7 +106,7 @@ void test_RFC9173_AppendixA_Example1_BIB_Source(void) BIBTestContext_Init(&bib_test_context); BSL_TestUtils_InitBIB_AppendixA1(&bib_test_context, BSL_SECROLE_SOURCE, RFC9173_EXAMPLE_A1_KEY); - BSL_SecOutcome_t *sec_outcome = BSL_CALLOC(1, BSL_SecOutcome_Sizeof()); + BSL_SecOutcome_t *sec_outcome = BSL_calloc(1, BSL_SecOutcome_Sizeof()); BSL_SecOutcome_Init(sec_outcome, &bib_test_context.sec_oper, BSL_SecOutcome_Sizeof()); /// Confirm running BIB as source executes without error @@ -132,7 +132,7 @@ void test_RFC9173_AppendixA_Example1_BIB_Source(void) } BSL_SecOutcome_Deinit(sec_outcome); - BSL_FREE(sec_outcome); + BSL_free(sec_outcome); BIBTestContext_Deinit(&bib_test_context); } @@ -168,7 +168,7 @@ void test_RFC9173_AppendixA_Example2_BCB_Source(void) BCBTestContext_Init(&bcb_test_context); BSL_TestUtils_InitBCB_Appendix2(&bcb_test_context, BSL_SECROLE_SOURCE); - BSL_SecOutcome_t *outcome = BSL_CALLOC(1, BSL_SecOutcome_Sizeof()); + BSL_SecOutcome_t *outcome = BSL_calloc(1, BSL_SecOutcome_Sizeof()); BSL_SecOutcome_Init(outcome, &bcb_test_context.sec_oper, 10000); // Execute BCB as source, confirm result is 0 (success) @@ -205,7 +205,7 @@ void test_RFC9173_AppendixA_Example2_BCB_Source(void) TEST_ASSERT_EQUAL_MEMORY(ApxA2_Ciphertext, target_block->btsd, sizeof(ApxA2_Ciphertext)); BSL_SecOutcome_Deinit(outcome); - BSL_FREE(outcome); + BSL_free(outcome); BCBTestContext_Deinit(&bcb_test_context); } @@ -219,7 +219,7 @@ void test_RFC9173_AppendixA_Example2_BCB_Acceptor(void) BCBTestContext_Init(&bcb_test_context); BSL_TestUtils_InitBCB_Appendix2(&bcb_test_context, BSL_SECROLE_ACCEPTOR); - BSL_SecOutcome_t *outcome = BSL_CALLOC(1, BSL_SecOutcome_Sizeof()); + BSL_SecOutcome_t *outcome = BSL_calloc(1, BSL_SecOutcome_Sizeof()); BSL_SecOutcome_Init(outcome, &bcb_test_context.sec_oper, 10000); /// Confirm that BCB executes with SUCCESS @@ -246,7 +246,7 @@ void test_RFC9173_AppendixA_Example2_BCB_Acceptor(void) TEST_ASSERT_EQUAL_MEMORY(ApxA2_PayloadData, target_block->btsd, sizeof(ApxA2_PayloadData)); BSL_SecOutcome_Deinit(outcome); - BSL_FREE(outcome); + BSL_free(outcome); BCBTestContext_Deinit(&bcb_test_context); } @@ -311,7 +311,7 @@ void test_sec_source_keywrap(bool wrap, bool bib) 0, BSL_TestUtils_LoadBundleFromCBOR(&LocalTestCtx, RFC9173_TestVectors_AppendixA1.cbor_bundle_original)); mock_bpa_ctr_t *mock_bpa_ctr = &LocalTestCtx.mock_bpa_ctr; - BSL_SecOutcome_t *sec_outcome = BSL_CALLOC(1, BSL_SecOutcome_Sizeof()); + BSL_SecOutcome_t *sec_outcome = BSL_calloc(1, BSL_SecOutcome_Sizeof()); const BSL_SecResult_t *result; BIBTestContext bibcontext; BCBTestContext bcbcontext; @@ -459,7 +459,7 @@ void test_sec_source_keywrap(bool wrap, bool bib) BSL_Data_Deinit(&cek_data); BSL_Data_Deinit(&kek_data); BSL_Data_Deinit(&wrapped_key_data); - BSL_FREE(sec_outcome); + BSL_free(sec_outcome); BIBTestContext_Deinit(&bibcontext); BCBTestContext_Deinit(&bcbcontext); } @@ -526,7 +526,7 @@ void test_sec_accept_keyunwrap(bool bib) } mock_bpa_ctr_t *mock_bpa_ctr = &LocalTestCtx.mock_bpa_ctr; - BSL_SecOutcome_t *sec_outcome = BSL_CALLOC(1, BSL_SecOutcome_Sizeof()); + BSL_SecOutcome_t *sec_outcome = BSL_calloc(1, BSL_SecOutcome_Sizeof()); BIBTestContext bibcontext; BCBTestContext bcbcontext; BIBTestContext_Init(&bibcontext); @@ -615,7 +615,7 @@ void test_sec_accept_keyunwrap(bool bib) BSL_Data_Deinit(&wrapped_key_data); BSL_Data_Deinit(&result_data); BSL_Data_Deinit(&iv_data); - BSL_FREE(sec_outcome); + BSL_free(sec_outcome); BIBTestContext_Deinit(&bibcontext); BCBTestContext_Deinit(&bcbcontext); } diff --git a/test/test_DynamicMemCbs.c b/test/test_DynamicMemCbs.c index 01381dda..57c31c9e 100644 --- a/test/test_DynamicMemCbs.c +++ b/test/test_DynamicMemCbs.c @@ -107,7 +107,7 @@ void _setUp(void) /// Register the policy provider with some rules BSL_PolicyDesc_t policy_desc = { 0 }; - policy_desc.user_data = BSL_CALLOC(1, sizeof(BSLP_PolicyProvider_t)); + policy_desc.user_data = BSL_calloc(1, sizeof(BSLP_PolicyProvider_t)); policy_desc.query_fn = BSLP_QueryPolicy; policy_desc.deinit_fn = BSLP_Deinit; policy_desc.finalize_fn = BSLP_FinalizePolicy; diff --git a/test/test_MockBPA_Codecs.c b/test/test_MockBPA_Codecs.c index 4897bcfa..7c0f821e 100644 --- a/test/test_MockBPA_Codecs.c +++ b/test/test_MockBPA_Codecs.c @@ -66,7 +66,7 @@ void setUp(void) TEST_ASSERT_EQUAL(0, BSL_API_InitLib(&bsl)); buf.len = 10000; - buf.ptr = BSL_MALLOC(buf.len); + buf.ptr = BSL_malloc(buf.len); TEST_ASSERT_NOT_NULL(buf.ptr); QCBOREncode_Init(&encoder, buf); } @@ -75,7 +75,7 @@ void tearDown(void) { if (buf.ptr) { - BSL_FREE(buf.ptr); + BSL_free(buf.ptr); } buf = NULLUsefulBuf; @@ -191,7 +191,7 @@ void test_bsl_mock_encode_canonical(uint64_t crc_type, const char *expecthex) blk.blk_num = 45; blk.flags = 0; blk.crc_type = crc_type; - blk.btsd = BSL_MALLOC(dummy_size); + blk.btsd = BSL_malloc(dummy_size); blk.btsd_len = dummy_size; memcpy(blk.btsd, dummy_btsd, dummy_size); @@ -205,7 +205,7 @@ void test_bsl_mock_encode_canonical(uint64_t crc_type, const char *expecthex) TEST_ASSERT_EQUAL_INT(expect_data.len, encoded.len); TEST_ASSERT_EQUAL_MEMORY(expect_data.ptr, encoded.ptr, expect_data.len); - BSL_FREE(blk.btsd); + BSL_free(blk.btsd); BSL_Data_Deinit(&expect_data); string_clear(expect_text); } @@ -237,7 +237,7 @@ void test_bsl_mock_encode_bundle(void) blk->blk_num = 45; blk->flags = 0; blk->crc_type = 0; - blk->btsd = BSL_CALLOC(1, dummy_size); + blk->btsd = BSL_calloc(1, dummy_size); blk->btsd_len = dummy_size; memcpy(blk->btsd, dummy_btsd, dummy_size); } diff --git a/test/test_PublicInterfaceImpl.c b/test/test_PublicInterfaceImpl.c index e62ab03c..cf0c3b51 100644 --- a/test/test_PublicInterfaceImpl.c +++ b/test/test_PublicInterfaceImpl.c @@ -128,7 +128,7 @@ void setUp(void) /// Register the policy provider with some rules BSL_PolicyDesc_t policy_desc = { 0 }; - policy_desc.user_data = BSL_CALLOC(1, sizeof(BSLP_PolicyProvider_t)); + policy_desc.user_data = BSL_calloc(1, sizeof(BSLP_PolicyProvider_t)); policy_desc.query_fn = BSLP_QueryPolicy; policy_desc.deinit_fn = BSLP_Deinit; policy_desc.finalize_fn = BSLP_FinalizePolicy; From f09147814adb43051d9a031a3fd645461100fab8 Mon Sep 17 00:00:00 2001 From: Joshua Stone Date: Wed, 18 Mar 2026 13:35:59 -0400 Subject: [PATCH 12/17] apply format ubuntu --- src/BPSecLib_Public.h | 18 +++++++++--------- src/backend/HostInterface.c | 13 ++++++------- test/test_DynamicMemCbs.c | 13 ++++++------- 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/BPSecLib_Public.h b/src/BPSecLib_Public.h index c598514e..f32608d8 100644 --- a/src/BPSecLib_Public.h +++ b/src/BPSecLib_Public.h @@ -261,30 +261,30 @@ typedef struct BSL_CanonicalBlock_s } BSL_CanonicalBlock_t; /** Dynamic memory callback descriptors used by Dynamic BPA descriptor. -*/ + */ typedef struct { - /** Dynamic memory allocation callback. - * + /** Dynamic memory allocation callback. + * * @return valid heap pointer on success, NULL on failure. */ void *(*malloc_cb)(size_t size); - /** Dynamic memory re-allocation callback. - * + /** Dynamic memory re-allocation callback. + * * @return valid heap pointer on success, NULL on failure. */ void *(*realloc_cb)(void *ptr, size_t size); /** Contiguous dynamic memory allocation callback. - * + * * @return valid 0-initialized heap pointer on success, NULL on failure. - */ + */ void *(*calloc_cb)(size_t nmemb, size_t size); - /** Free dynamic memory allocation callback. + /** Free dynamic memory allocation callback. */ - void (*free_cb)(void* ptr); + void (*free_cb)(void *ptr); } BSL_DynMemHostDescriptors_t; /** Dynamic BPA descriptor. diff --git a/src/backend/HostInterface.c b/src/backend/HostInterface.c index 0ac1d642..1d27af55 100644 --- a/src/backend/HostInterface.c +++ b/src/backend/HostInterface.c @@ -29,12 +29,11 @@ // NOLINTNEXTLINE static BSL_HostDescriptors_t HostDescriptorTable = { 0 }; -static BSL_DynMemHostDescriptors_t defaultDynMemCbs = -{ - .malloc_cb = malloc, +static BSL_DynMemHostDescriptors_t defaultDynMemCbs = { + .malloc_cb = malloc, .realloc_cb = realloc, - .calloc_cb = calloc, - .free_cb = free, + .calloc_cb = calloc, + .free_cb = free, }; int BSL_HostDescriptors_Set(BSL_HostDescriptors_t desc) @@ -58,8 +57,8 @@ int BSL_HostDescriptors_Set(BSL_HostDescriptors_t desc) CHK_PRECONDITION(desc.eidpat_match); // Dyanmic mem callbacks - if (NULL == desc.dyn_mem_desc.malloc_cb || NULL == desc.dyn_mem_desc.realloc_cb || - NULL == desc.dyn_mem_desc.calloc_cb || NULL == desc.dyn_mem_desc.free_cb) + if (NULL == desc.dyn_mem_desc.malloc_cb || NULL == desc.dyn_mem_desc.realloc_cb + || NULL == desc.dyn_mem_desc.calloc_cb || NULL == desc.dyn_mem_desc.free_cb) { desc.dyn_mem_desc = defaultDynMemCbs; } diff --git a/test/test_DynamicMemCbs.c b/test/test_DynamicMemCbs.c index 57c31c9e..75f26b37 100644 --- a/test/test_DynamicMemCbs.c +++ b/test/test_DynamicMemCbs.c @@ -69,13 +69,12 @@ static void free_test(void *ptr) void suiteSetUp(void) { - BSL_HostDescriptors_t host_desc = MockBPA_Agent_Descriptors(NULL); - BSL_DynMemHostDescriptors_t dyn_mem_desc = - { - .malloc_cb = malloc_test, - .realloc_cb = realloc_test, - .calloc_cb = calloc_test, - .free_cb = free_test, + BSL_HostDescriptors_t host_desc = MockBPA_Agent_Descriptors(NULL); + BSL_DynMemHostDescriptors_t dyn_mem_desc = { + .malloc_cb = malloc_test, + .realloc_cb = realloc_test, + .calloc_cb = calloc_test, + .free_cb = free_test, }; host_desc.dyn_mem_desc = dyn_mem_desc; From 23b6ebe1cb3ad009065743f89a727deec46debbd Mon Sep 17 00:00:00 2001 From: Joshua Stone Date: Wed, 18 Mar 2026 14:09:46 -0400 Subject: [PATCH 13/17] 133 refactor setter --- src/backend/HostInterface.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/backend/HostInterface.c b/src/backend/HostInterface.c index 1d27af55..35b415ec 100644 --- a/src/backend/HostInterface.c +++ b/src/backend/HostInterface.c @@ -56,13 +56,18 @@ int BSL_HostDescriptors_Set(BSL_HostDescriptors_t desc) CHK_PRECONDITION(desc.eidpat_from_text); CHK_PRECONDITION(desc.eidpat_match); - // Dyanmic mem callbacks - if (NULL == desc.dyn_mem_desc.malloc_cb || NULL == desc.dyn_mem_desc.realloc_cb - || NULL == desc.dyn_mem_desc.calloc_cb || NULL == desc.dyn_mem_desc.free_cb) + // If all callbacks are unset/NULL, use default + if (NULL == desc.dyn_mem_desc.malloc_cb && NULL == desc.dyn_mem_desc.realloc_cb + && NULL == desc.dyn_mem_desc.calloc_cb && NULL == desc.dyn_mem_desc.free_cb) { desc.dyn_mem_desc = defaultDynMemCbs; } - + // otherwiese, if any one are unset, return error + else if (NULL == desc.dyn_mem_desc.malloc_cb || NULL == desc.dyn_mem_desc.realloc_cb + || NULL == desc.dyn_mem_desc.calloc_cb || NULL == desc.dyn_mem_desc.free_cb) + { + return BSL_ERR_ARG_NULL; + } // GCOV_EXCL_STOP HostDescriptorTable = desc; From 3051693a5378da99685db34d7c926d110ef7d653 Mon Sep 17 00:00:00 2001 From: Joshua Stone Date: Wed, 18 Mar 2026 14:10:03 -0400 Subject: [PATCH 14/17] apply format ubuntu --- src/backend/HostInterface.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/backend/HostInterface.c b/src/backend/HostInterface.c index 35b415ec..3b2271c0 100644 --- a/src/backend/HostInterface.c +++ b/src/backend/HostInterface.c @@ -63,8 +63,8 @@ int BSL_HostDescriptors_Set(BSL_HostDescriptors_t desc) desc.dyn_mem_desc = defaultDynMemCbs; } // otherwiese, if any one are unset, return error - else if (NULL == desc.dyn_mem_desc.malloc_cb || NULL == desc.dyn_mem_desc.realloc_cb - || NULL == desc.dyn_mem_desc.calloc_cb || NULL == desc.dyn_mem_desc.free_cb) + else if (NULL == desc.dyn_mem_desc.malloc_cb || NULL == desc.dyn_mem_desc.realloc_cb + || NULL == desc.dyn_mem_desc.calloc_cb || NULL == desc.dyn_mem_desc.free_cb) { return BSL_ERR_ARG_NULL; } From c16440c6ac82a40b03177a8179da2ced8017410e Mon Sep 17 00:00:00 2001 From: Brian Sipos Date: Wed, 18 Mar 2026 20:17:40 -0400 Subject: [PATCH 15/17] Moved libc defaults into a macro. Fixed API docs. --- docs/api/10-bsl-developers.md | 15 +++++++++------ src/BPSecLib_Public.h | 12 ++++++++++++ src/backend/HostInterface.c | 12 +++--------- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/docs/api/10-bsl-developers.md b/docs/api/10-bsl-developers.md index ffd6d910..ea284960 100644 --- a/docs/api/10-bsl-developers.md +++ b/docs/api/10-bsl-developers.md @@ -209,18 +209,21 @@ After its de-initialization the members of the struct will no longer have well d To help with troubleshooting, de-initialization should set pointers set to NULL and other values to a well-defined state. One option is to use `memset()` to zeroize the entire struct. -# Macros - -This section contains references to commonly used macros defined for the BSL +## Memory Management Functions -## Memory Management Macros - -When heap memory is needed at BSL runtime, the following macros are used and have the same signature and semantics as the corresponding C99 functions indicated below. +When heap memory is needed at BSL runtime, the following functions are used and have the same signature and semantics as the corresponding C99 functions indicated below. - [BSL_malloc](@ref BSL_malloc) as `malloc()` - [BSL_realloc](@ref BSL_realloc) as `realloc()` +- [BSL_cealloc](@ref BSL_cealloc) as `cealloc()` - [BSL_free](@ref BSL_free) as `free()` +These can be modified using the @ref BSL_DynMemHostDescriptors_t interface. + +# Macros + +This section contains references to commonly used macros defined for the BSL + ## Error Checking Handler Macros To help with the error reporting conventions above, the following macros can be used to simplify function precondition checking. diff --git a/src/BPSecLib_Public.h b/src/BPSecLib_Public.h index f32608d8..6bd387f5 100644 --- a/src/BPSecLib_Public.h +++ b/src/BPSecLib_Public.h @@ -261,6 +261,9 @@ typedef struct BSL_CanonicalBlock_s } BSL_CanonicalBlock_t; /** Dynamic memory callback descriptors used by Dynamic BPA descriptor. + * + * These are meant to be used as part of ::BSL_HostDescriptors_t for + * registering host callbacks. */ typedef struct { @@ -287,6 +290,15 @@ typedef struct void (*free_cb)(void *ptr); } BSL_DynMemHostDescriptors_t; +/// Default heap functions from libc +#define BSL_DynMemHostDescriptors_DEFAULT \ + { \ + .malloc_cb = malloc, \ + .realloc_cb = realloc, \ + .calloc_cb = calloc, \ + .free_cb = free, \ + } + /** Dynamic BPA descriptor. */ typedef struct diff --git a/src/backend/HostInterface.c b/src/backend/HostInterface.c index 3b2271c0..aceb5d34 100644 --- a/src/backend/HostInterface.c +++ b/src/backend/HostInterface.c @@ -27,14 +27,8 @@ #include "UtilDefs_SeqReadWrite.h" // NOLINTNEXTLINE -static BSL_HostDescriptors_t HostDescriptorTable = { 0 }; - -static BSL_DynMemHostDescriptors_t defaultDynMemCbs = { - .malloc_cb = malloc, - .realloc_cb = realloc, - .calloc_cb = calloc, - .free_cb = free, -}; +/// Initialized to library default +static BSL_HostDescriptors_t HostDescriptorTable = { .dyn_mem_desc = BSL_DynMemHostDescriptors_DEFAULT }; int BSL_HostDescriptors_Set(BSL_HostDescriptors_t desc) { @@ -60,7 +54,7 @@ int BSL_HostDescriptors_Set(BSL_HostDescriptors_t desc) if (NULL == desc.dyn_mem_desc.malloc_cb && NULL == desc.dyn_mem_desc.realloc_cb && NULL == desc.dyn_mem_desc.calloc_cb && NULL == desc.dyn_mem_desc.free_cb) { - desc.dyn_mem_desc = defaultDynMemCbs; + desc.dyn_mem_desc = (BSL_DynMemHostDescriptors_t)BSL_DynMemHostDescriptors_DEFAULT; } // otherwiese, if any one are unset, return error else if (NULL == desc.dyn_mem_desc.malloc_cb || NULL == desc.dyn_mem_desc.realloc_cb From 3e55dd540832175526e71d924641c5e6f163c84f Mon Sep 17 00:00:00 2001 From: Brian Sipos Date: Wed, 18 Mar 2026 20:18:29 -0400 Subject: [PATCH 16/17] format --- src/BPSecLib_Public.h | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/BPSecLib_Public.h b/src/BPSecLib_Public.h index 6bd387f5..698141f2 100644 --- a/src/BPSecLib_Public.h +++ b/src/BPSecLib_Public.h @@ -291,12 +291,9 @@ typedef struct } BSL_DynMemHostDescriptors_t; /// Default heap functions from libc -#define BSL_DynMemHostDescriptors_DEFAULT \ - { \ - .malloc_cb = malloc, \ - .realloc_cb = realloc, \ - .calloc_cb = calloc, \ - .free_cb = free, \ +#define BSL_DynMemHostDescriptors_DEFAULT \ + { \ + .malloc_cb = malloc, .realloc_cb = realloc, .calloc_cb = calloc, .free_cb = free, \ } /** Dynamic BPA descriptor. From 665d92e6ad5626f00bb593bfbcf3a726fcb686d6 Mon Sep 17 00:00:00 2001 From: Brian Sipos Date: Wed, 18 Mar 2026 20:29:05 -0400 Subject: [PATCH 17/17] spelling --- docs/api/10-bsl-developers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api/10-bsl-developers.md b/docs/api/10-bsl-developers.md index ea284960..38228a7b 100644 --- a/docs/api/10-bsl-developers.md +++ b/docs/api/10-bsl-developers.md @@ -215,7 +215,7 @@ When heap memory is needed at BSL runtime, the following functions are used and - [BSL_malloc](@ref BSL_malloc) as `malloc()` - [BSL_realloc](@ref BSL_realloc) as `realloc()` -- [BSL_cealloc](@ref BSL_cealloc) as `cealloc()` +- [BSL_calloc](@ref BSL_calloc) as `calloc()` - [BSL_free](@ref BSL_free) as `free()` These can be modified using the @ref BSL_DynMemHostDescriptors_t interface.