core/dynamo.c
/* This routine is called not only at thread initialization,
 * but for every callback, etc. that gets a fresh execution
 * environment!
 */
This routine is called not only at thread initialization,
but for every callback, etc. that gets a fresh execution
environment!
This routine is called not only at thread initialization but for every callback etc . that gets a fresh execution environment!
void
initialize_dynamo_context(dcontext_t *dcontext)
{
    memset(dcontext->upcontext_ptr, 0, sizeof(unprotected_context_t));
    dcontext->initialized = true;
    dcontext->whereami = WHERE_APP;
    dcontext->next_tag = NULL;
    dcontext->native_exec_retval = NULL;
    dcontext->native_exec_retloc = NULL;
    dcontext->native_exec_postsyscall = NULL;
#ifdef RETURN_STACK
    dcontext->top_of_rstack = dcontext->rstack;
#endif
#ifdef X64
    dcontext->x86_mode = false;
#endif
    dcontext->sys_num = 0;
#ifdef WINDOWS
    dcontext->app_errno = 0;
    dcontext->sys_param_base = NULL;
    dcontext->aslr_context.sys_aslr_clobbered = 0;
    dcontext->aslr_context.randomized_section_handle = INVALID_HANDLE_VALUE;
    dcontext->aslr_context.original_image_section_handle = INVALID_HANDLE_VALUE;
    dcontext->aslr_context.original_section_base = ASLR_INVALID_SECTION_BASE;
# ifdef DEBUG
    dcontext->aslr_context.last_app_section_handle = INVALID_HANDLE_VALUE;
# endif
    dcontext->ignore_enterexit = false;
#else
    dcontext->sys_param0 = 0;
    dcontext->sys_param1 = 0;
    dcontext->sys_param2 = 0;
#endif
#ifdef LINUX
    dcontext->signals_pending = false;
#endif
    set_last_exit(dcontext, (linkstub_t *) get_starting_linkstub());
#ifdef NATIVE_RETURN
# ifdef NATIVE_RETURN_CALLDEPTH
    dcontext->call_depth = 0;
# endif
    dcontext->last_retaddr = NULL;
    dcontext->num_calls_in_trace = 0;
#endif
#ifdef PROFILE_RDTSC
    dcontext->start_time = (uint64) 0;
    dcontext->prev_fragment = NULL;
    dcontext->cache_frag_count = (uint64) 0;
    {
        int i;
        for (i=0; i<10; i++) {
            dcontext->cache_time[i] = (uint64) 0;
            dcontext->cache_count[i] = (uint64) 0;
        }
    }
#endif
#ifdef DEBUG
    dcontext->in_opnd_disassemble = false;
#endif
#ifdef WINDOWS
    dcontext->asynch_target = NULL;
    dcontext->valid = true;
#endif
#ifdef HOT_PATCHING_INTERFACE
    dcontext->nudge_thread = false;     
#endif
#ifdef CHECK_RETURNS_SSE2
# ifdef LINUX
    asm("movl $0, %eax");
    asm("pinsrw $7,%eax,%xmm7");
# else
# error NYI
# endif
#endif
}
void initialize_dynamo_context ( dcontext_t * dcontext ) { memset ( dcontext -> upcontext_ptr , 0 , sizeof ( unprotected_context_t ) ) ; dcontext -> initialized = true ; dcontext -> whereami = WHERE_APP ; dcontext -> next_tag = NULL ; dcontext -> native_exec_retval = NULL ; dcontext -> native_exec_retloc = NULL ; dcontext -> native_exec_postsyscall = NULL ; #ifdef RETURN_STACK dcontext -> top_of_rstack = dcontext -> rstack ; #endif #ifdef X64 dcontext -> x86_mode = false ; #endif dcontext -> sys_num = 0 ; #ifdef WINDOWS dcontext -> app_errno = 0 ; dcontext -> sys_param_base = NULL ; dcontext -> aslr_context . sys_aslr_clobbered = 0 ; dcontext -> aslr_context . randomized_section_handle = INVALID_HANDLE_VALUE ; dcontext -> aslr_context . original_image_section_handle = INVALID_HANDLE_VALUE ; dcontext -> aslr_context . original_section_base = ASLR_INVALID_SECTION_BASE ; # ifdef DEBUG dcontext -> aslr_context . last_app_section_handle = INVALID_HANDLE_VALUE ; # endif dcontext -> ignore_enterexit = false ; #else dcontext -> sys_param0 = 0 ; dcontext -> sys_param1 = 0 ; dcontext -> sys_param2 = 0 ; #endif #ifdef LINUX dcontext -> signals_pending = false ; #endif set_last_exit ( dcontext , ( linkstub_t * ) get_starting_linkstub ( ) ) ; #ifdef NATIVE_RETURN # ifdef NATIVE_RETURN_CALLDEPTH dcontext -> call_depth = 0 ; # endif dcontext -> last_retaddr = NULL ; dcontext -> num_calls_in_trace = 0 ; #endif #ifdef PROFILE_RDTSC dcontext -> start_time = ( uint64 ) 0 ; dcontext -> prev_fragment = NULL ; dcontext -> cache_frag_count = ( uint64 ) 0 ; { int i ; for ( i = 0 ; i < 10 ; i ++ ) { dcontext -> cache_time [ i ] = ( uint64 ) 0 ; dcontext -> cache_count [ i ] = ( uint64 ) 0 ; } } #endif #ifdef DEBUG dcontext -> in_opnd_disassemble = false ; #endif #ifdef WINDOWS dcontext -> asynch_target = NULL ; dcontext -> valid = true ; #endif #ifdef HOT_PATCHING_INTERFACE dcontext -> nudge_thread = false ; #endif #ifdef CHECK_RETURNS_SSE2 # ifdef LINUX asm ( " " ) ; asm ( " " ) ; # else # error  NYI # endif #endif }
This routine is called not only at thread initialization,
 but for every callback, etc.
This routine is called not only at thread initialization but for every callback etc .
/* we can't just zero out the whole thing b/c we have persistent state
     * (fields kept across callbacks, like dstack, module-private fields, next & prev, etc.)
     */ /* always initialize aslr_context */ /* note that aslr_context.last_child_padded is preserved across callbacks */ /* all thread-private fields are initialized in dynamo_thread_init
     * or in create_callback_dcontext because they must be initialized differently
     * in those two cases
     */ /* Other pieces of DR -- callback & APC handling, detach -- test
     * asynch_target to determine where the next app pc to execute is
     * stored. Init it to 0 to indicate that this context's most recent
     * syscall was not executed from handle_system_call().
     */ /* next_saved and prev_unused are zeroed out when dcontext is
     * created; we shouldn't zero them here, they may have valid data
     */ /* Fix for case 5367. */ /* initialize sse2 index with 0
     * go ahead and use eax, it's dead (about to return)
     */ /* We don't need to initialize dcontext->coarse_exit as it is only
     * read when last_exit indicates a coarse exit, which sets the fields.
     */