Skip to content

Commit b481035

Browse files
133 Add dynamic memory mgmt callbacks to host descriptors (#141)
* Remove #defines and add host descriptor callbacks for mem mgmt * Add docs to bsl private mem mgmt fns * Modify unit tests to set host desc first; add defualt libc * Add docs to host desc struct for dynamic mem cbs * Add test for dyn mem cbs * MockBPA dyn mem patch * diff fix * apply format ubuntu * Spelling fix * 133 Move dyn mem bcbs to seperate struct * Rename BSL dynamic mem fns to lowercase * apply format ubuntu * 133 refactor setter * apply format ubuntu * Moved libc defaults into a macro. Fixed API docs. * format * spelling --------- Co-authored-by: Brian Sipos <brian.sipos@jhuapl.edu>
1 parent d0b7a5c commit b481035

41 files changed

Lines changed: 534 additions & 228 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/api/10-bsl-developers.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,17 +209,20 @@ After its de-initialization the members of the struct will no longer have well d
209209

210210
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.
211211

212-
# Macros
212+
## Memory Management Functions
213213

214-
This section contains references to commonly used macros defined for the BSL
214+
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.
215215

216-
## Memory Management Macros
216+
- [BSL_malloc](@ref BSL_malloc) as `malloc()`
217+
- [BSL_realloc](@ref BSL_realloc) as `realloc()`
218+
- [BSL_calloc](@ref BSL_calloc) as `calloc()`
219+
- [BSL_free](@ref BSL_free) as `free()`
217220

218-
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.
221+
These can be modified using the @ref BSL_DynMemHostDescriptors_t interface.
219222

220-
- [BSL_MALLOC](@ref BSL_MALLOC) as `malloc()`
221-
- [BSL_REALLOC](@ref BSL_REALLOC) as `realloc()`
222-
- [BSL_FREE](@ref BSL_FREE) as `free()`
223+
# Macros
224+
225+
This section contains references to commonly used macros defined for the BSL
223226

224227
## Error Checking Handler Macros
225228

docs/api/dictionary.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ JSON
123123
JWK
124124
KEK
125125
len
126+
libc
126127
LibCtx
127128
lifecycle
128129
Lifecycles

src/BPSecLib_Private.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,27 @@ int BSL_HostEIDPattern_DecodeFromText(BSL_HostEIDPattern_t *pat, const char *tex
425425
*/
426426
bool BSL_HostEIDPattern_IsMatch(const BSL_HostEIDPattern_t *pat, const BSL_HostEID_t *eid);
427427

428+
/// @brief Dynamic memory allocation
429+
/// @param size size of allocation
430+
/// @return valid heap pointer
431+
void *BSL_malloc(size_t size);
432+
433+
/// @brief Dynamic memory reallocation
434+
/// @param ptr existing dynamic memory pointer
435+
/// @param size new allocation size
436+
/// @return valid heap pointer
437+
void *BSL_realloc(void *ptr, size_t size);
438+
439+
/// @brief Contiguous dynamic memory allocation
440+
/// @param nmemb number of members to allocate
441+
/// @param size size of each member
442+
/// @return valid heap pointer
443+
void *BSL_calloc(size_t nmemb, size_t size);
444+
445+
/// @brief Free dynamically allocated memory
446+
/// @param ptr pointer to memory to free
447+
void BSL_free(void *ptr);
448+
428449
/** Block types using IANA-assigned code points from @cite iana:bundle.
429450
*/
430451
typedef enum

src/BPSecLib_Public.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,42 @@ typedef struct BSL_CanonicalBlock_s
260260
size_t btsd_len; ///< Length in bytes of the BTSD accessible through sequential APIs
261261
} BSL_CanonicalBlock_t;
262262

263+
/** Dynamic memory callback descriptors used by Dynamic BPA descriptor.
264+
*
265+
* These are meant to be used as part of ::BSL_HostDescriptors_t for
266+
* registering host callbacks.
267+
*/
268+
typedef struct
269+
{
270+
/** Dynamic memory allocation callback.
271+
*
272+
* @return valid heap pointer on success, NULL on failure.
273+
*/
274+
void *(*malloc_cb)(size_t size);
275+
276+
/** Dynamic memory re-allocation callback.
277+
*
278+
* @return valid heap pointer on success, NULL on failure.
279+
*/
280+
void *(*realloc_cb)(void *ptr, size_t size);
281+
282+
/** Contiguous dynamic memory allocation callback.
283+
*
284+
* @return valid 0-initialized heap pointer on success, NULL on failure.
285+
*/
286+
void *(*calloc_cb)(size_t nmemb, size_t size);
287+
288+
/** Free dynamic memory allocation callback.
289+
*/
290+
void (*free_cb)(void *ptr);
291+
} BSL_DynMemHostDescriptors_t;
292+
293+
/// Default heap functions from libc
294+
#define BSL_DynMemHostDescriptors_DEFAULT \
295+
{ \
296+
.malloc_cb = malloc, .realloc_cb = realloc, .calloc_cb = calloc, .free_cb = free, \
297+
}
298+
263299
/** Dynamic BPA descriptor.
264300
*/
265301
typedef struct
@@ -350,6 +386,10 @@ typedef struct
350386

351387
/// @brief Host BPA function that returns true if the given EID matched an EID pattern.
352388
bool (*eidpat_match)(const BSL_HostEIDPattern_t *pat, const BSL_HostEID_t *eid, void *user_data);
389+
390+
/// @brief Optionally set dynamic memory management callbacks. Defaults to libc calls if unset.
391+
BSL_DynMemHostDescriptors_t dyn_mem_desc;
392+
353393
} BSL_HostDescriptors_t;
354394

355395
/** Set the BPA descriptor (callbacks) for this process.

src/BSLConfig.h.in

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -53,30 +53,6 @@ extern "C" {
5353
*/
5454
const char * bsl_version(void);
5555

56-
#ifndef BSL_MALLOC
57-
/** Uses the same function signature as C99 malloc().
58-
*/
59-
#define BSL_MALLOC malloc
60-
#endif /* BSL_MALLOC */
61-
62-
#ifndef BSL_REALLOC
63-
/** Uses the same function signature as C99 realloc().
64-
*/
65-
#define BSL_REALLOC realloc
66-
#endif /* BSL_REALLOC */
67-
68-
#ifndef BSL_FREE
69-
/** Uses the same function signature as C99 free().
70-
*/
71-
#define BSL_FREE free
72-
#endif /* BSL_FREE */
73-
74-
#ifndef BSL_CALLOC
75-
/** Uses the same function signature as C99 calloc().
76-
*/
77-
#define BSL_CALLOC calloc
78-
#endif /* BSL_CALLOC */
79-
8056
/** Force the use of M_ prefixed macros for M*LIB
8157
*/
8258
//#define M_USE_SMALL_NAME 0
@@ -85,25 +61,25 @@ const char * bsl_version(void);
8561
/** Define to override value/struct allocation.
8662
* See m-core.h for details.
8763
*/
88-
#define M_MEMORY_ALLOC(type) ((type *) BSL_MALLOC(sizeof(type)))
64+
#define M_MEMORY_ALLOC(type) ((type *) BSL_malloc(sizeof(type)))
8965

9066
#undef M_MEMORY_DEL
9167
/** Define to override value/struct deallocation.
9268
* See m-core.h for details.
9369
*/
94-
#define M_MEMORY_DEL(ptr) BSL_FREE(ptr)
70+
#define M_MEMORY_DEL(ptr) BSL_free(ptr)
9571

9672
#undef M_MEMORY_REALLOC
9773
/** Define to override array allocation.
9874
* See m-core.h for details.
9975
*/
100-
#define M_MEMORY_REALLOC(type, ptr, n) (M_UNLIKELY((n) > SIZE_MAX / sizeof(type)) ? (type *) NULL : (type *) BSL_REALLOC((ptr), (n)*sizeof (type)))
76+
#define M_MEMORY_REALLOC(type, ptr, n) (M_UNLIKELY((n) > SIZE_MAX / sizeof(type)) ? (type *) NULL : (type *) BSL_realloc((ptr), (n)*sizeof (type)))
10177

10278
#undef M_MEMORY_FREE
10379
/** Define to override array deallocation.
10480
* See m-core.h for details.
10581
*/
106-
#define M_MEMORY_FREE(ptr) BSL_FREE(ptr)
82+
#define M_MEMORY_FREE(ptr) BSL_free(ptr)
10783

10884
#ifdef __cplusplus
10985
} // extern C

src/CryptoInterface.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ int BSL_AuthCtx_Deinit(BSL_AuthCtx_t *hmac_ctx);
215215
/**
216216
* Deinit and free generated key handle
217217
* @param[in] keyhandle key handle to clear.
218-
* Key handle assumed to be generated, not present in key registry, and allocated with ::BSL_MALLOC().
218+
* Key handle assumed to be generated, not present in key registry, and allocated with ::BSL_malloc().
219219
* @returns 0 if successfully cleared key handle
220220
*/
221221
int BSL_Crypto_ClearGeneratedKeyHandle(void *keyhandle);
@@ -226,7 +226,7 @@ int BSL_Crypto_ClearGeneratedKeyHandle(void *keyhandle);
226226
* @param[in] kek_handle key encryption key handle (encryption key)
227227
* @param[in] cek_handle content encryption key handle (encryption data)
228228
* @param[in,out] wrapped_key output wrapped key (ciphertext) bytes
229-
* @param[in,out] wrapped_key_handle output wrapped key (ciphertext) handle, allocated with ::BSL_MALLOC(). Set to NULL
229+
* @param[in,out] wrapped_key_handle output wrapped key (ciphertext) handle, allocated with ::BSL_malloc(). Set to NULL
230230
* if handle not needed.
231231
*/
232232
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
236236
* CEK size expected to match size of KEK
237237
* @param[in] kek_handle key encryption key handle (decryption key)
238238
* @param[in] wrapped_key input wrapped key (ciphertext) bytes
239-
* @param[in,out] cek_handle output content encryption key (plaintext) handle, allocated with ::BSL_MALLOC()
239+
* @param[in,out] cek_handle output content encryption key (plaintext) handle, allocated with ::BSL_malloc()
240240
*/
241241
int BSL_Crypto_UnwrapKey(void *kek_handle, BSL_Data_t *wrapped_key, void **cek_handle);
242242

@@ -326,7 +326,7 @@ int BSL_Cipher_Deinit(BSL_Cipher_t *cipher_ctx);
326326
/**
327327
* Generate a new cryptographic key
328328
* @param[in] key_length length of new key. Should be 16 or 32
329-
* @param[in, out] key_out pointer to pointer for new key handle, allocated with ::BSL_MALLOC()
329+
* @param[in, out] key_out pointer to pointer for new key handle, allocated with ::BSL_malloc()
330330
*/
331331
int BSL_Crypto_GenKey(size_t key_length, void **key_out);
332332

src/Data.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static void bsl_data_int_free(BSL_Data_t *data)
4242

4343
if (data->owned && data->ptr)
4444
{
45-
BSL_FREE(data->ptr);
45+
BSL_free(data->ptr);
4646
}
4747
}
4848

@@ -59,7 +59,7 @@ int BSL_Data_InitBuffer(BSL_Data_t *data, size_t bytelen)
5959
CHK_ARG_EXPR(bytelen > 0);
6060

6161
bsl_data_int_reset(data);
62-
data->ptr = BSL_MALLOC(bytelen);
62+
data->ptr = BSL_malloc(bytelen);
6363
data->len = bytelen;
6464
data->owned = true;
6565
memset(data->ptr, 0, bytelen);
@@ -145,7 +145,7 @@ int BSL_Data_Resize(BSL_Data_t *data, size_t len)
145145
{
146146
data->ptr = NULL;
147147
}
148-
BSL_DataPtr_t got = BSL_REALLOC(data->ptr, len);
148+
BSL_DataPtr_t got = BSL_realloc(data->ptr, len);
149149
if (UNLIKELY(!got))
150150
{
151151
bsl_data_int_reset(data);

src/backend/HostInterface.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
#include "UtilDefs_SeqReadWrite.h"
2828

2929
// NOLINTNEXTLINE
30-
static BSL_HostDescriptors_t HostDescriptorTable = { 0 };
30+
/// Initialized to library default
31+
static BSL_HostDescriptors_t HostDescriptorTable = { .dyn_mem_desc = BSL_DynMemHostDescriptors_DEFAULT };
3132

3233
int BSL_HostDescriptors_Set(BSL_HostDescriptors_t desc)
3334
{
@@ -48,6 +49,19 @@ int BSL_HostDescriptors_Set(BSL_HostDescriptors_t desc)
4849
CHK_PRECONDITION(desc.eidpat_deinit);
4950
CHK_PRECONDITION(desc.eidpat_from_text);
5051
CHK_PRECONDITION(desc.eidpat_match);
52+
53+
// If all callbacks are unset/NULL, use default
54+
if (NULL == desc.dyn_mem_desc.malloc_cb && NULL == desc.dyn_mem_desc.realloc_cb
55+
&& NULL == desc.dyn_mem_desc.calloc_cb && NULL == desc.dyn_mem_desc.free_cb)
56+
{
57+
desc.dyn_mem_desc = (BSL_DynMemHostDescriptors_t)BSL_DynMemHostDescriptors_DEFAULT;
58+
}
59+
// otherwiese, if any one are unset, return error
60+
else if (NULL == desc.dyn_mem_desc.malloc_cb || NULL == desc.dyn_mem_desc.realloc_cb
61+
|| NULL == desc.dyn_mem_desc.calloc_cb || NULL == desc.dyn_mem_desc.free_cb)
62+
{
63+
return BSL_ERR_ARG_NULL;
64+
}
5165
// GCOV_EXCL_STOP
5266

5367
HostDescriptorTable = desc;
@@ -224,3 +238,23 @@ bool BSL_HostEIDPattern_IsMatch(const BSL_HostEIDPattern_t *pat, const BSL_HostE
224238
ASSERT_PRECONDITION(HostDescriptorTable.eidpat_match);
225239
return HostDescriptorTable.eidpat_match(pat, eid, HostDescriptorTable.user_data);
226240
}
241+
242+
void *BSL_malloc(size_t size)
243+
{
244+
return HostDescriptorTable.dyn_mem_desc.malloc_cb(size);
245+
}
246+
247+
void *BSL_realloc(void *ptr, size_t size)
248+
{
249+
return HostDescriptorTable.dyn_mem_desc.realloc_cb(ptr, size);
250+
}
251+
252+
void *BSL_calloc(size_t nmemb, size_t size)
253+
{
254+
return HostDescriptorTable.dyn_mem_desc.calloc_cb(nmemb, size);
255+
}
256+
257+
void BSL_free(void *ptr)
258+
{
259+
HostDescriptorTable.dyn_mem_desc.free_cb(ptr);
260+
}

src/backend/PublicInterfaceImpl.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void BSL_PrimaryBlock_deinit(BSL_PrimaryBlock_t *obj)
9191
{
9292
ASSERT_ARG_NONNULL(obj);
9393

94-
BSL_FREE(obj->block_numbers);
94+
BSL_free(obj->block_numbers);
9595
obj->block_numbers = NULL;
9696

9797
BSL_Data_Deinit(&obj->encoded);
@@ -174,7 +174,7 @@ int BSL_API_QuerySecurity(const BSL_LibCtx_t *bsl, BSL_SecurityActionSet_t *outp
174174
BSL_SeqReader_Get(btsd_read, btsd_copy.ptr, &btsd_copy.len);
175175
BSL_SeqReader_Destroy(btsd_read);
176176

177-
BSL_AbsSecBlock_t *abs_sec_block = BSL_CALLOC(1, BSL_AbsSecBlock_Sizeof());
177+
BSL_AbsSecBlock_t *abs_sec_block = BSL_calloc(1, BSL_AbsSecBlock_Sizeof());
178178
BSL_AbsSecBlock_InitEmpty(abs_sec_block);
179179
if (BSL_AbsSecBlock_DecodeFromCBOR(abs_sec_block, &btsd_copy) == 0)
180180
{
@@ -189,7 +189,7 @@ int BSL_API_QuerySecurity(const BSL_LibCtx_t *bsl, BSL_SecurityActionSet_t *outp
189189
BSL_SecOper_SetReasonCode(sec_oper, BSL_REASONCODE_BLOCK_UNINTELLIGIBLE);
190190
}
191191
BSL_AbsSecBlock_Deinit(abs_sec_block);
192-
BSL_FREE(abs_sec_block);
192+
BSL_free(abs_sec_block);
193193

194194
BSL_Data_Deinit(&btsd_copy);
195195
}

src/backend/SecurityContext.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ int BSL_SecCtx_ExecutePolicyActionSet(BSL_LibCtx_t *lib, BSL_SecurityResponseSet
481481
* - BCB will be a special case, since it actively manipulates the BTSD
482482
*
483483
*/
484-
BSL_SecOutcome_t *outcome = BSL_CALLOC(1, BSL_SecOutcome_Sizeof());
484+
BSL_SecOutcome_t *outcome = BSL_calloc(1, BSL_SecOutcome_Sizeof());
485485

486486
BSL_SecActionList_it_t act_it;
487487
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
535535
BSL_SecurityResponseSet_AppendResult(output_response, errcode, sec_oper->policy_action);
536536
}
537537
}
538-
BSL_FREE(outcome);
538+
BSL_free(outcome);
539539

540540
return BSL_SUCCESS;
541541
}

0 commit comments

Comments
 (0)