On 06/30/2017 04:03 PM, Michael Matz wrote: > So you need to find some other solution of setting up the stack for ASAN. > And it'd be best if that solution doesn't require inserting code inside > the above sequence of parameter setup instructions, and you certainly > can't call any functions inside that sequence. It might mean that you > can't track the static chain place or the nonlocal goto save area. You > also don't track the parameter stack slots, right?
Hi. Hopefully following patch will fix that. I returned to the first version and saved/restored static_chain register before/after __asan_stack_malloc. Patch can bootstrap on ppc64le-redhat-linux and survives regression tests. Thoughts? Martin
>From b285e7cb1d7f3e35981dec951121db58ce152b3b Mon Sep 17 00:00:00 2001 From: marxin <mli...@suse.cz> Date: Thu, 13 Jul 2017 13:37:47 +0200 Subject: [PATCH] Move static chain and non-local goto init after NOTE_INSN_FUNCTION_BEG gcc/ChangeLog: 2017-06-27 Martin Liska <mli...@suse.cz> PR sanitize/81186 * function.c (expand_function_start): Move static chain and non-local goto init after NOTE_INSN_FUNCTION_BEG. * asan.c (asan_emit_stack_protection): Preserve static chain register if we call __asan_stack_malloc_N. gcc/testsuite/ChangeLog: 2017-06-27 Martin Liska <mli...@suse.cz> PR sanitize/81186 * gcc.dg/asan/pr81186.c: New test. --- gcc/asan.c | 12 ++++++++++++ gcc/function.c | 18 +++++++++--------- gcc/testsuite/gcc.dg/asan/pr81186.c | 18 ++++++++++++++++++ 3 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/asan/pr81186.c diff --git a/gcc/asan.c b/gcc/asan.c index 89c2731e8cd..9cc1d21c1fb 100644 --- a/gcc/asan.c +++ b/gcc/asan.c @@ -1340,6 +1340,16 @@ asan_emit_stack_protection (rtx base, rtx pbase, unsigned int alignb, emit_cmp_and_jump_insns (ret, const0_rtx, EQ, NULL_RTX, VOIDmode, 0, lab, profile_probability::very_likely ()); + /* Preserve static chain register in order to not have it clobbered in + __asan_stack_malloc_N function. */ + rtx chain = targetm.calls.static_chain (current_function_decl, true); + rtx saved_chain; + if (chain) + { + saved_chain = gen_reg_rtx (Pmode); + emit_move_insn (saved_chain, chain); + } + snprintf (buf, sizeof buf, "__asan_stack_malloc_%d", use_after_return_class); ret = init_one_libfunc (buf); @@ -1347,6 +1357,8 @@ asan_emit_stack_protection (rtx base, rtx pbase, unsigned int alignb, GEN_INT (asan_frame_size + base_align_bias), TYPE_MODE (pointer_sized_int_node)); + if (chain) + emit_move_insn (chain, saved_chain); /* __asan_stack_malloc_[n] returns a pointer to fake stack if succeeded and NULL otherwise. Check RET value is NULL here and jump over the BASE reassignment in this case. Otherwise, reassign BASE to RET. */ diff --git a/gcc/function.c b/gcc/function.c index f625489205b..5e8a56099a5 100644 --- a/gcc/function.c +++ b/gcc/function.c @@ -5220,6 +5220,14 @@ expand_function_start (tree subr) In some cases this requires emitting insns. */ assign_parms (subr); + /* The following was moved from init_function_start. + The move is supposed to make sdb output more accurate. */ + /* Indicate the beginning of the function body, + as opposed to parm setup. */ + rtx_note *b = emit_note (NOTE_INSN_FUNCTION_BEG); + + gcc_assert (NOTE_P (get_last_insn ())); + /* If function gets a static chain arg, store it. */ if (cfun->static_chain_decl) { @@ -5284,15 +5292,7 @@ expand_function_start (tree subr) update_nonlocal_goto_save_area (); } - /* The following was moved from init_function_start. - The move is supposed to make sdb output more accurate. */ - /* Indicate the beginning of the function body, - as opposed to parm setup. */ - emit_note (NOTE_INSN_FUNCTION_BEG); - - gcc_assert (NOTE_P (get_last_insn ())); - - parm_birth_insn = get_last_insn (); + parm_birth_insn = b; if (crtl->profile) { diff --git a/gcc/testsuite/gcc.dg/asan/pr81186.c b/gcc/testsuite/gcc.dg/asan/pr81186.c new file mode 100644 index 00000000000..7f0f672ca40 --- /dev/null +++ b/gcc/testsuite/gcc.dg/asan/pr81186.c @@ -0,0 +1,18 @@ +/* PR sanitizer/81186 */ +/* { dg-do run } */ + +int +main () +{ + __label__ l; + void f () + { + int a[123]; + + goto l; + } + + f (); +l: + return 0; +} -- 2.13.2