Add custom memory allocation

Thanks to Basile Starynkevitch for the suggestion and initial patch.
Thanks to Jonathan Landis and Deron Meranda for showing how this can
be utilized for implementing secure memory operations.
This commit is contained in:
Petri Lehtinen
2011-02-17 10:10:53 +02:00
parent dd7dd414f0
commit 4be9e9e7fe
14 changed files with 293 additions and 58 deletions

7
test/.gitignore vendored
View File

@@ -1,13 +1,14 @@
logs
bin/json_process
suites/api/test_array
suites/api/test_equal
suites/api/test_copy
suites/api/test_cpp
suites/api/test_dump
suites/api/test_equal
suites/api/test_load
suites/api/test_memory_funcs
suites/api/test_number
suites/api/test_object
suites/api/test_simple
suites/api/test_cpp
suites/api/test_pack
suites/api/test_simple
suites/api/test_unpack

View File

@@ -2,24 +2,26 @@ EXTRA_DIST = run
check_PROGRAMS = \
test_array \
test_equal \
test_copy \
test_dump \
test_equal \
test_load \
test_simple \
test_memory_funcs \
test_number \
test_object \
test_pack \
test_simple \
test_unpack
test_array_SOURCES = test_array.c util.h
test_copy_SOURCES = test_copy.c util.h
test_dump_SOURCES = test_dump.c util.h
test_load_SOURCES = test_load.c util.h
test_simple_SOURCES = test_simple.c util.h
test_memory_funcs_SOURCES = test_memory_funcs.c util.h
test_number_SOURCES = test_number.c util.h
test_object_SOURCES = test_object.c util.h
test_pack_SOURCES = test_pack.c util.h
test_simple_SOURCES = test_simple.c util.h
test_unpack_SOURCES = test_unpack.c util.h
AM_CPPFLAGS = -I$(top_srcdir)/src

View File

@@ -59,6 +59,7 @@ json_vpack_ex
json_unpack
json_unpack_ex
json_vunpack_ex
json_set_alloc_funcs
EOF
# The list of functions are not exported in the library because they

View File

@@ -0,0 +1,84 @@
#include <string.h>
#include <jansson.h>
#include "util.h"
static int malloc_called = 0;
static int free_called = 0;
/* helper */
static void create_and_free_complex_object()
{
json_t *obj;
obj = json_pack("{s:i,s:n,s:b,s:b,s:{s:s},s:[i,i,i]",
"foo", 42,
"bar",
"baz", 1,
"qux", 0,
"alice", "bar", "baz",
"bob", 9, 8, 7);
json_decref(obj);
}
static void *my_malloc(size_t size)
{
malloc_called += 1;
return malloc(size);
}
static void my_free(void *ptr)
{
free_called += 1;
free(ptr);
}
static void test_simple()
{
json_set_alloc_funcs(my_malloc, my_free);
create_and_free_complex_object();
if(malloc_called != 27 || free_called != 27)
fail("Custom allocation failed");
}
/*
Test the secure memory functions code given in the API reference
documentation, but by using plain memset instead of
guaranteed_memset().
*/
static void *secure_malloc(size_t size)
{
/* Store the memory area size in the beginning of the block */
void *ptr = malloc(size + 8);
*((size_t *)ptr) = size;
return ptr + 8;
}
static void secure_free(void *ptr)
{
size_t size;
ptr -= 8;
size = *((size_t *)ptr);
/*guaranteed_*/memset(ptr, 0, size);
free(ptr);
}
static void test_secure_funcs(void)
{
json_set_alloc_funcs(secure_malloc, secure_free);
create_and_free_complex_object();
}
int main()
{
test_simple();
test_secure_funcs();
return 0;
}