Commit d216df73 authored by Vivek Das Mohapatra's avatar Vivek Das Mohapatra
Browse files

realloc: Introduce a wrapper for realloc along the same lines as free

We also need to wrap realloc() to dispatch pointers to the correct
free/alloc implementation.
parent af3fd26f
......@@ -42,3 +42,9 @@ _wrapped_free (void *ptr)
if (ptr)
capsule_shim_free( cap, ptr );
}
static void *
_wrapped_realloc (void *ptr, size_t size)
{
return capsule_shim_realloc( cap, ptr, size );
}
......@@ -313,6 +313,10 @@ _capsule_load (const capsule cap,
if( cap->ns->mem->free == NULL )
cap->ns->mem->free = dlsym( ret, "free" );
if( cap->ns->mem->realloc == NULL )
cap->ns->mem->realloc = dlsym( ret, "realloc" );
// =====================================================================
// TODO: failure in the dlopen fixup phase should probably be fatal:
if( ret != NULL && // no errors so far
wrappers != NULL ) // have a dlopen fixup function
......
......@@ -225,7 +225,8 @@ get_capsule_metadata (struct link_map *map, const char *only)
static const capsule_item no_wrapper = { NULL };
const capsule_item int_dlopen_wrapper =
{
"dlopen", (capsule_addr) meta->int_dlopen,
"dlopen",
(capsule_addr) meta->int_dlopen,
(capsule_addr) meta->int_dlopen,
};
......@@ -236,6 +237,13 @@ get_capsule_metadata (struct link_map *map, const char *only)
(capsule_addr) meta->int_free,
};
const capsule_item int_realloc_wrapper =
{
"realloc",
(capsule_addr) meta->int_realloc,
(capsule_addr) meta->int_realloc,
};
cap = xcalloc( 1, sizeof(struct _capsule) );
cap->ns = get_namespace( meta->default_prefix, meta->soname );
DEBUG( DEBUG_CAPSULE,
......@@ -247,12 +255,24 @@ get_capsule_metadata (struct link_map *map, const char *only)
cap->internal_wrappers[ 0 ] = int_dlopen_wrapper;
cap->internal_wrappers[ 1 ] = int_free_wrapper;
cap->internal_wrappers[ 2 ] = int_realloc_wrapper;
for( i = 2; alloc_func[ i ].name != NULL; i++ )
for( i = 3; alloc_func[ i ].name != NULL; i++ )
{
// pre-populated functions that we should skip are
// flagged with an invalid name starting with '-'.
// in a properly laid out file they'll all be at the start of
// alloc_func and we should never see them, but just in case:
if( alloc_func[ i ].name[ 0 ] == '-' )
continue;
capsule_item *capi = &cap->internal_wrappers[ i ];
void **slot = (void **) alloc_func[ i ].real;
// the addresses held in alloc_func are actually the
// addresses of pointers to the real implementations,
// which are populated during capsule_init: we deref
// those here:
capi->name = alloc_func[ i ].name;
capi->shim = capi->real = (capsule_addr) *slot;
}
......
......@@ -327,6 +327,19 @@ static int address_within_main_heap (ElfW(Addr) addr)
return 1;
}
void *
capsule_shim_realloc (const capsule cap, void *ptr, size_t size)
{
if( !ptr || address_within_main_heap( (ElfW(Addr)) ptr ) )
return realloc( ptr, size );
mchunkptr p = mem2chunk( ptr );
if( chunk_is_mmapped( p ) || chunk_is_vanilla( p, ptr ) )
return realloc( ptr, size );
return cap->ns->mem->realloc( ptr, size );
}
void
capsule_shim_free (const capsule cap, void *ptr)
......
......@@ -102,6 +102,7 @@ struct _capsule_metadata
capsule_item *items;
void *(*int_dlopen) (const char *filename, int flag);
void (*int_free) (void *ptr);
void *(*int_realloc) (void *ptr, size_t size);
/*< private >*/
capsule handle;
};
......@@ -267,3 +268,7 @@ char *capsule_get_prefix(const char *dflt, const char *soname);
*/
_CAPSULE_PUBLIC
void capsule_shim_free (const capsule cap, void *ptr);
_CAPSULE_PUBLIC
void * capsule_shim_realloc (const capsule cap, void *ptr, size_t size);
......@@ -296,6 +296,7 @@ capsule_metadata capsule_meta =
.items = relocs,
.int_dlopen = _int_dlopen,
.int_free = _wrapped_free,
.int_realloc = _wrapped_realloc,
};
static void __attribute__ ((constructor)) _capsule_init (void)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment