static void wrap_unwindtest_addr(OUT app_pc *addr, const char *name, const module_data_t *mod) { bool ok; *addr = (app_pc) dr_get_proc_address(mod->handle, name); CHECK(*addr != NULL, "cannot find lib export"); ok = drwrap_wrap(*addr, wrap_unwindtest_pre, wrap_unwindtest_post); CHECK(ok, "wrap unwindtest failed"); CHECK(drwrap_is_wrapped(*addr, wrap_unwindtest_pre, wrap_unwindtest_post), "drwrap_is_wrapped query failed"); }
static void wrap_addr(OUT app_pc *addr, const char *name, const module_data_t *mod, bool pre, bool post) { bool ok; *addr = (app_pc) dr_get_proc_address(mod->handle, name); CHECK(*addr != NULL, "cannot find lib export"); ok = drwrap_wrap(*addr, pre ? wrap_pre : NULL, post ? wrap_post : NULL); CHECK(ok, "wrap failed"); CHECK(drwrap_is_wrapped(*addr, pre ? wrap_pre : NULL, post ? wrap_post : NULL), "drwrap_is_wrapped query failed"); }
static void module_load_event(void *drcontext, const module_data_t *mod, bool loaded) { app_pc towrap = (app_pc)dr_get_proc_address(mod->handle, "SSL_write"); if (towrap != NULL) { bool ok = drwrap_wrap(towrap, wrap_pre_SSL_write, NULL); if (!ok) { dr_fprintf(STDERR, "Couldn’t wrap SSL_write\n"); DR_ASSERT(ok); } } towrap = (app_pc)dr_get_proc_address(mod->handle, "SSL_read"); if (towrap != NULL) { bool ok = drwrap_wrap(towrap, wrap_pre_SSL_read, wrap_post_SSL_read); if (!ok) { dr_fprintf(STDERR, "Couldn’t wrap SSL_read\n"); DR_ASSERT(ok); } } towrap = (app_pc)dr_get_proc_address(mod->handle, "gnutls_record_send"); if (towrap != NULL) { bool ok = drwrap_wrap(towrap, wrap_pre_SSL_write, NULL); if (!ok) { dr_fprintf(STDERR, "Couldn’t wrap gnutls_record_send\n"); DR_ASSERT(ok); } } towrap = (app_pc)dr_get_proc_address(mod->handle, "gnutls_record_recv"); if (towrap != NULL) { bool ok = drwrap_wrap(towrap, wrap_pre_SSL_read, wrap_post_SSL_read); if (!ok) { dr_fprintf(STDERR, "Couldn’t wrap gnutls_record_recv\n"); DR_ASSERT(ok); } } }
/* * Helper function for bulk use of drwrap. */ static void try_wrap_fn(const module_data_t *module, const char *name, prewrapper_t pre, postwrapper_t post, bool *done) { if (*done) return; size_t offset; drsym_error_t status = drsym_lookup_symbol( module->full_path, name, &offset, DRSYM_DEFAULT_FLAGS); if (status == DRSYM_SUCCESS) { app_pc notify_fn = module->start + offset; bool ok = drwrap_wrap(notify_fn, pre, post); DR_ASSERT(ok); *done = true; } }
void funcwrap_module_load(void * drcontext, module_data_t * module, bool loaded){ module_t * md = md_lookup_module(head, module->full_path); int i = 0; app_pc address; if (md != NULL){ for (int i = 1; i <= md->bbs[0].start_addr; i++){ address = md->bbs[i].start_addr + module->start; DEBUG_PRINT("funcwrap: %s module %x function wrapping\n", md->module, address); drwrap_wrap(address, pre_func_cb, post_func_cb); } } }
static void module_load_event(void *drcontext, const module_data_t *mod, bool loaded) { app_pc towrap = (app_pc) dr_get_proc_address(mod->start, IF_WINDOWS_ELSE("HeapAlloc", "malloc")); if (towrap != NULL) { #ifdef SHOW_RESULTS bool ok = #endif drwrap_wrap(towrap, wrap_pre, wrap_post); #ifdef SHOW_RESULTS if (ok) dr_fprintf(STDERR, "<wrapped HeapAlloc @"PFX"\n", towrap); else { /* We expect this w/ forwarded exports (e.g., on win7 both * kernel32!HeapAlloc and kernelbase!HeapAlloc forward to * the same routine in ntdll.dll) */ dr_fprintf(STDERR, "<FAILED to wrap HeapAlloc @"PFX": already wrapped?\n", towrap); } #endif } }
/** Installs wrapper for a given symbol. */ static void wrapSymbol( const char* sym, module_data_t* module, size_t offset, struct demangle_component* qual, struct demangle_component* info ) { app_pc pc = module->start + offset; if( offset == 0 || pc == 0 ) { dr_printf( "Skipping symbol %s\n", sym ); return; } size_t size; char* className = cplus_demangle_print( DMGL_NO_OPTS, qual, 64, &size ); hashtable_lock( &wraps ); struct wrap* wrap = ( struct wrap* )hashtable_lookup( &wraps, (void*)pc ); if( wrap == NULL ) { // Not wrapped yet. if( !drwrap_wrap( pc, &wrapperPre, NULL ) ) { dr_printf( "drwrap_wrap(%s %s of kind %i = +%p = %p) failed\n", className, info->type == DEMANGLE_COMPONENT_CTOR ? "ctor" : "dtor", info->type == DEMANGLE_COMPONENT_CTOR ? info->u.s_ctor.kind : info->u.s_dtor.kind, (void*)offset, (void*)pc ); exit( 1 ); } wrap = alloc_wrap(); memset( wrap, 0, sizeof( struct wrap ) ); wrap->address = pc; hashtable_add( &wraps, (void*)pc, wrap ); } else { // Already wrapped - do nothing. } char* eventType = "?"; if( info->type == DEMANGLE_COMPONENT_CTOR ) { switch( info->u.s_ctor.kind ) { case gnu_v3_complete_object_allocating_ctor: wrap->flags |= FLAG_ALLOCATING_CONSTRUCTOR; eventType = "A"; break; case gnu_v3_complete_object_ctor: wrap->flags |= FLAG_COMPLETE_OBJECT_CONSTRUCTOR; eventType = "C"; break; case gnu_v3_base_object_ctor: wrap->flags |= FLAG_BASE_OBJECT_CONSTRUCTOR; eventType = "B"; break; } } else { switch( info->u.s_dtor.kind ) { case gnu_v3_deleting_dtor: wrap->flags |= FLAG_DEALLOCATING_DESTRUCTOR; eventType ="a"; break; case gnu_v3_complete_object_dtor: wrap->flags |= FLAG_COMPLETE_OBJECT_DESTRUCTOR; eventType = "c"; break; case gnu_v3_base_object_dtor: wrap->flags |= FLAG_BASE_OBJECT_DESTRUCTOR; eventType = "b"; break; } } hashtable_unlock( &wraps ); dr_mutex_lock( outMutex ); dr_fprintf( outFile, "%s %p %s\n", eventType, (void*)pc, className ); dr_mutex_unlock( outMutex ); free( className ); }