diff --git a/core/synch.c b/core/synch.c index 566eb5a070a..79124e7b151 100644 --- a/core/synch.c +++ b/core/synch.c @@ -2021,6 +2021,17 @@ detach_on_permanent_stack(bool internal, bool do_cleanup, dr_stats_t *drstats) my_id = get_thread_id(); my_dcontext = get_thread_private_dcontext(); + if (my_dcontext == NULL) { + /* We support detach after just dr_app_setup() with no start. */ + ASSERT(!dynamo_started); + my_tr = thread_lookup(my_id); + ASSERT(my_tr != NULL); + my_dcontext = my_tr->dcontext; + os_process_under_dynamorio_initiate(my_dcontext); + os_process_under_dynamorio_complete(my_dcontext); + dynamo_thread_under_dynamo(my_dcontext); + ASSERT(get_thread_private_dcontext() == my_dcontext); + } ASSERT(my_dcontext != NULL); LOG(GLOBAL, LOG_ALL, 1, "Detach: thread %d starting detach process\n", my_id); diff --git a/suite/tests/api/static_noclient.c b/suite/tests/api/static_noclient.c index 0371f07c104..4091d9e8984 100644 --- a/suite/tests/api/static_noclient.c +++ b/suite/tests/api/static_noclient.c @@ -1,5 +1,5 @@ /* ********************************************************** - * Copyright (c) 2016 Google, Inc. All rights reserved. + * Copyright (c) 2016-2018 Google, Inc. All rights reserved. * **********************************************************/ /* @@ -52,6 +52,20 @@ do_some_work(int seed) int main(int argc, const char *argv[]) { + /* We test using DR IR routines when statically linked. We can't use + * drdecode when statically linked with DR as drdecode relies on symbol + * replacement, so instead we initialize DR and then "detach" to do a full + * cleanup (even without an attach) before starting our regular + * attach+detach testing. + * XXX: When there's a client, this requires a flag to skip the client init + * in this first dr_app_setup(). + */ + dr_app_setup(); + instr_t *instr = XINST_CREATE_return(GLOBAL_DCONTEXT); + assert(instr_is_return(instr)); + instr_destroy(GLOBAL_DCONTEXT, instr); + dr_app_stop_and_cleanup(); + print("pre-DR init\n"); dr_app_setup(); assert(!dr_app_running_under_dynamorio());