Skip to content

Commit

Permalink
i#95 Linux detach: generalize call_switch_stack
Browse files Browse the repository at this point in the history
Generalizes call_switch_stack() to take any void * argument to its target
function: it does not need to be a dcontext_t.  This is in preparation of
using other types for detach code.

Review-URL: https://codereview.appspot.com/303560043
  • Loading branch information
derekbruening committed Sep 13, 2016
1 parent 6d08225 commit c7524ac
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 24 deletions.
4 changes: 2 additions & 2 deletions core/arch/aarch64/aarch64.asm
Expand Up @@ -69,9 +69,9 @@ GLOBAL_LABEL(cpuid_supported:)
ret
END_FUNC(cpuid_supported)

/* void call_switch_stack(dcontext_t *dcontext, // REG_X0
/* void call_switch_stack(void *func_arg, // REG_X0
* byte *stack, // REG_X1
* void (*func)(dcontext_t *), // REG_X2
* void (*func)(void *arg), // REG_X2
* void *mutex_to_free, // REG_X3
* bool return_on_return) // REG_W4
*/
Expand Down
4 changes: 2 additions & 2 deletions core/arch/arch_exports.h
Expand Up @@ -1087,12 +1087,12 @@ byte *get_app_sysenter_addr(void);
/* in [x86/arm].asm */
/* Calls the specified function 'func' after switching to the stack 'stack'. If we're
* currently on the initstack 'mutex_to_free' should be passed so we release the
* initstack lock. The supplied 'dcontext' will be passed as an argument to 'func'.
* initstack lock. The supplied 'func_arg' will be passed as an argument to 'func'.
* If 'func' returns then 'return_on_return' is checked. If set we swap back stacks and
* return to the caller. If not set then it's assumed that func wasn't supposed to
* return and we go to an error routine unexpected_return() below.
*/
void call_switch_stack(dcontext_t *dcontext, byte *stack, void (*func) (dcontext_t *),
void call_switch_stack(void *func_arg, byte *stack, void (*func) (void *arg),
void *mutex_to_free, bool return_on_return);
# if defined (WINDOWS) && !defined(X64)
DYNAMORIO_EXPORT int64
Expand Down
4 changes: 2 additions & 2 deletions core/arch/arm/arm.asm
Expand Up @@ -86,9 +86,9 @@ GLOBAL_LABEL(cpuid_supported:)
bx lr
END_FUNC(cpuid_supported)

/* void call_switch_stack(dcontext_t *dcontext, // REG_R0
/* void call_switch_stack(void *func_arg, // REG_R0
* byte *stack, // REG_R1
* void (*func)(dcontext_t *), // REG_R2
* void (*func)(void *arg), // REG_R2
* void *mutex_to_free, // REG_R3
* bool return_on_return) // [REG_SP]
*/
Expand Down
6 changes: 3 additions & 3 deletions core/arch/x86/x86.asm
Expand Up @@ -247,9 +247,9 @@ GLOBAL_LABEL(get_pic_xdi:)
END_FUNC(get_pic_xdi)
#endif

/* void call_switch_stack(dcontext_t *dcontext, // 1*ARG_SZ+XAX
/* void call_switch_stack(void *func_arg, // 1*ARG_SZ+XAX
* byte *stack, // 2*ARG_SZ+XAX
* void (*func)(dcontext_t *), // 3*ARG_SZ+XAX
* void (*func)(void *arg), // 3*ARG_SZ+XAX
* void *mutex_to_free, // 4*ARG_SZ+XAX
* bool return_on_return) // 5*ARG_SZ+XAX
*/
Expand Down Expand Up @@ -286,7 +286,7 @@ GLOBAL_LABEL(call_switch_stack:)
mov IF_X64_ELSE(r12, REG_XDI), REG_XSP
/* set up for call */
mov REG_XDX, [3*ARG_SZ + REG_XAX] /* func */
mov REG_XCX, [1*ARG_SZ + REG_XAX] /* dcontext */
mov REG_XCX, [1*ARG_SZ + REG_XAX] /* func_arg */
mov REG_XSP, [2*ARG_SZ + REG_XAX] /* stack */
cmp PTRSZ [4*ARG_SZ + REG_XAX], 0 /* mutex_to_free */
je call_dispatch_alt_stack_no_free
Expand Down
14 changes: 7 additions & 7 deletions core/arch/x86_code.c
@@ -1,5 +1,5 @@
/* **********************************************************
* Copyright (c) 2013-2015 Google, Inc. All rights reserved.
* Copyright (c) 2013-2016 Google, Inc. All rights reserved.
* Copyright (c) 2001-2010 VMware, Inc. All rights reserved.
* **********************************************************/

Expand Down Expand Up @@ -111,7 +111,7 @@ dynamo_start(priv_mcontext_t *mc)
});

/* Swap stacks so dispatch is invoked outside the application. */
call_switch_stack(dcontext, dcontext->dstack, dispatch,
call_switch_stack(dcontext, dcontext->dstack, (void(*)(void*))dispatch,
NULL/*not on initstack*/, true/*return on error*/);
/* In release builds, this will simply return and continue native
* execution. That's better than calling unexpected_return() which
Expand Down Expand Up @@ -218,7 +218,7 @@ auto_setup(ptr_uint_t appstack)
* then. We do so now.
*/
IF_WINDOWS(os_swap_context(dcontext, false/*to priv*/, DR_STATE_STACK_BOUNDS));
call_switch_stack(dcontext, dcontext->dstack, dispatch,
call_switch_stack(dcontext, dcontext->dstack, (void(*)(void*))dispatch,
NULL/*not on initstack*/, false/*shouldn't return*/);
ASSERT_NOT_REACHED();
}
Expand Down Expand Up @@ -303,7 +303,7 @@ new_thread_setup(priv_mcontext_t *mc)
thread_starting(dcontext);
dcontext->next_tag = next_tag;

call_switch_stack(dcontext, dcontext->dstack, dispatch,
call_switch_stack(dcontext, dcontext->dstack, (void(*)(void*))dispatch,
NULL/*not on initstack*/, false/*shouldn't return*/);
ASSERT_NOT_REACHED();
}
Expand Down Expand Up @@ -353,7 +353,7 @@ new_bsdthread_setup(priv_mcontext_t *mc)
*(reg_t*)(mc->xsp + sizeof(reg_t)) = (reg_t) func_arg;
# endif

call_switch_stack(dcontext, dcontext->dstack, dispatch,
call_switch_stack(dcontext, dcontext->dstack, (void(*)(void*))dispatch,
NULL/*not on initstack*/, false/*shouldn't return*/);
ASSERT_NOT_REACHED();
}
Expand Down Expand Up @@ -400,7 +400,7 @@ nt_continue_setup(priv_mcontext_t *mc)
/* We came straight from fcache, so swap to priv now (i#25) */
IF_WINDOWS(swap_peb_pointer(dcontext, true/*to priv*/));

call_switch_stack(dcontext, dcontext->dstack, dispatch,
call_switch_stack(dcontext, dcontext->dstack, (void(*)(void*))dispatch,
NULL/*not on initstack*/, false/*shouldn't return*/);
ASSERT_NOT_REACHED();
}
Expand Down Expand Up @@ -477,7 +477,7 @@ test_call_switch_stack(dcontext_t *dc)
static_dc = dc;
print_file(STDERR, "testing asm call_switch_stack\n");
memset(test_stack, CONST_BYTE, sizeof(test_stack));
call_switch_stack(dc, stack_ptr, test_func,
call_switch_stack(dc, stack_ptr, (void(*)(void*))test_func,
NULL, true /* should return */);
}

Expand Down
2 changes: 1 addition & 1 deletion core/dispatch.c
Expand Up @@ -2188,7 +2188,7 @@ transfer_to_dispatch(dcontext_t *dcontext, priv_mcontext_t *mc, bool full_DR_sta
* what may have been there before, for both new dcontext and reuse dcontext
* options.
*/
call_switch_stack(dcontext, dcontext->dstack, dispatch,
call_switch_stack(dcontext, dcontext->dstack, (void(*)(void*))dispatch,
using_initstack ? &initstack_mutex : NULL,
false/*do not return on error*/);
ASSERT_NOT_REACHED();
Expand Down
4 changes: 2 additions & 2 deletions core/native_exec.c
@@ -1,5 +1,5 @@
/* **********************************************************
* Copyright (c) 2012-2015 Google, Inc. All rights reserved.
* Copyright (c) 2012-2016 Google, Inc. All rights reserved.
* **********************************************************/

/*
Expand Down Expand Up @@ -346,7 +346,7 @@ back_from_native_common(dcontext_t *dcontext, priv_mcontext_t *mc, app_pc target
__FUNCTION__, dcontext->next_tag, cur_esp, mc->xsp);
});

call_switch_stack(dcontext, dcontext->dstack, dispatch,
call_switch_stack(dcontext, dcontext->dstack, (void(*)(void*))dispatch,
NULL/*not on initstack*/, false/*shouldn't return*/);
ASSERT_NOT_REACHED();
}
Expand Down
3 changes: 2 additions & 1 deletion core/nudge.c
Expand Up @@ -171,7 +171,8 @@ nudge_thread_cleanup(dcontext_t *dcontext, bool exit_process, uint exit_code)
dcontext->nudge_terminate_process = true;
dcontext->nudge_exit_code = exit_code;
}
call_switch_stack(dcontext, dcontext->dstack, nudge_terminate_on_dstack,
call_switch_stack(dcontext, dcontext->dstack,
(void(*)(void*))nudge_terminate_on_dstack,
NULL /* not on initstack */, false /* don't return */);
} else {
/* Already on dstack or nudge creator will free app stack. */
Expand Down
2 changes: 1 addition & 1 deletion core/unix/os.c
Expand Up @@ -9294,7 +9294,7 @@ os_thread_take_over(priv_mcontext_t *mc)
});

/* Start interpreting from the signal context. */
call_switch_stack(dcontext, dcontext->dstack, dispatch,
call_switch_stack(dcontext, dcontext->dstack, (void(*)(void*))dispatch,
NULL/*not on initstack*/, false/*shouldn't return*/);
ASSERT_NOT_REACHED();
}
Expand Down
4 changes: 1 addition & 3 deletions core/unix/signal.c
Expand Up @@ -4817,7 +4817,7 @@ terminate_via_kill_from_anywhere(dcontext_t *dcontext, int sig)
/* We can't clean up our sigstack properly when we're on it
* (i#1160) so we terminate on the dstack.
*/
call_switch_stack(dcontext, dcontext->dstack, terminate_via_kill,
call_switch_stack(dcontext, dcontext->dstack, (void(*)(void*))terminate_via_kill,
NULL/*!initstack */, false/*no return */);
} else {
terminate_via_kill(dcontext);
Expand Down Expand Up @@ -6067,8 +6067,6 @@ handle_post_alarm(dcontext_t *dcontext, bool success, unsigned int sec)
return;
}

/***************************************************************************/

/* Returns whether to pass on to app */
static bool
handle_suspend_signal(dcontext_t *dcontext, kernel_ucontext_t *ucxt)
Expand Down

0 comments on commit c7524ac

Please sign in to comment.