Add __asan_mach_override_ptr_custom, which allows to inject a custom memory allocator into mach_override_ptr().

llvm-svn: 148115
This commit is contained in:
Alexander Potapenko 2012-01-13 15:31:37 +00:00
parent 553c208d22
commit 9301db4baa
2 changed files with 53 additions and 7 deletions

View File

@ -114,6 +114,14 @@ allocateBranchIsland(
freeBranchIsland( freeBranchIsland(
BranchIsland *island ) __attribute__((visibility("hidden"))); BranchIsland *island ) __attribute__((visibility("hidden")));
mach_error_t
defaultIslandMalloc(
void **ptr, size_t unused_size, void *hint) __attribute__((visibility("hidden")));
mach_error_t
defaultIslandFree(
void *ptr) __attribute__((visibility("hidden")));
#if defined(__ppc__) || defined(__POWERPC__) #if defined(__ppc__) || defined(__POWERPC__)
mach_error_t mach_error_t
setBranchIslandTarget( setBranchIslandTarget(
@ -175,11 +183,37 @@ mach_error_t makeIslandExecutable(void *address) {
} }
#endif #endif
mach_error_t
defaultIslandMalloc(
void **ptr, size_t unused_size, void *hint) {
return allocateBranchIsland( (BranchIsland**)ptr, kAllocateHigh, hint );
}
mach_error_t
defaultIslandFree(
void *ptr) {
return freeBranchIsland(ptr);
}
mach_error_t mach_error_t
__asan_mach_override_ptr( __asan_mach_override_ptr(
void *originalFunctionAddress, void *originalFunctionAddress,
const void *overrideFunctionAddress, const void *overrideFunctionAddress,
void **originalFunctionReentryIsland ) void **originalFunctionReentryIsland )
{
return __asan_mach_override_ptr_custom(originalFunctionAddress,
overrideFunctionAddress,
originalFunctionReentryIsland,
defaultIslandMalloc,
defaultIslandFree);
}
mach_error_t
__asan_mach_override_ptr_custom(
void *originalFunctionAddress,
const void *overrideFunctionAddress,
void **originalFunctionReentryIsland,
island_malloc *alloc,
island_free *dealloc)
{ {
assert( originalFunctionAddress ); assert( originalFunctionAddress );
assert( overrideFunctionAddress ); assert( overrideFunctionAddress );
@ -277,9 +311,8 @@ __asan_mach_override_ptr(
// Allocate and target the escape island to the overriding function. // Allocate and target the escape island to the overriding function.
BranchIsland *escapeIsland = NULL; BranchIsland *escapeIsland = NULL;
if( !err ) if( !err )
err = allocateBranchIsland( &escapeIsland, kAllocateHigh, originalFunctionAddress ); err = alloc( (void**)&escapeIsland, sizeof(BranchIsland), originalFunctionAddress );
if (err) fprintf(stderr, "err = %x %s:%d\n", err, __FILE__, __LINE__); if ( err ) fprintf(stderr, "err = %x %s:%d\n", err, __FILE__, __LINE__);
#if defined(__ppc__) || defined(__POWERPC__) #if defined(__ppc__) || defined(__POWERPC__)
if( !err ) if( !err )
@ -319,7 +352,7 @@ __asan_mach_override_ptr(
// technically our original function. // technically our original function.
BranchIsland *reentryIsland = NULL; BranchIsland *reentryIsland = NULL;
if( !err && originalFunctionReentryIsland ) { if( !err && originalFunctionReentryIsland ) {
err = allocateBranchIsland( &reentryIsland, kAllocateHigh, escapeIsland); err = alloc( (void**)&reentryIsland, sizeof(BranchIsland), escapeIsland);
if( !err ) if( !err )
*originalFunctionReentryIsland = reentryIsland; *originalFunctionReentryIsland = reentryIsland;
} }
@ -383,9 +416,9 @@ __asan_mach_override_ptr(
// Clean up on error. // Clean up on error.
if( err ) { if( err ) {
if( reentryIsland ) if( reentryIsland )
freeBranchIsland( reentryIsland ); dealloc( reentryIsland );
if( escapeIsland ) if( escapeIsland )
freeBranchIsland( escapeIsland ); dealloc( escapeIsland );
} }
#ifdef DEBUG_DISASM #ifdef DEBUG_DISASM

View File

@ -85,6 +85,19 @@ __asan_mach_override_ptr(
const void *overrideFunctionAddress, const void *overrideFunctionAddress,
void **originalFunctionReentryIsland ); void **originalFunctionReentryIsland );
// Allow to use custom allocation and deallocation routines with mach_override_ptr().
// This should help to speed up the things on x86_64.
typedef mach_error_t island_malloc( void **ptr, size_t size, void *hint );
typedef mach_error_t island_free( void *ptr );
mach_error_t
__asan_mach_override_ptr_custom(
void *originalFunctionAddress,
const void *overrideFunctionAddress,
void **originalFunctionReentryIsland,
island_malloc *alloc,
island_free *dealloc );
/************************************************************************************//** /************************************************************************************//**