public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc(refs/users/aoliva/heads/testme)] follow SSA defs for asan base
@ 2021-01-21 17:06 Alexandre Oliva
  0 siblings, 0 replies; 2+ messages in thread
From: Alexandre Oliva @ 2021-01-21 17:06 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:ae89cf14c34df439bf4cb79c24152b405a9a7123

commit ae89cf14c34df439bf4cb79c24152b405a9a7123
Author: Alexandre Oliva <oliva@adacore.com>
Date:   Thu Jan 21 14:05:07 2021 -0300

    follow SSA defs for asan base
    
    Ada makes extensive use of nested functions, that turns all automatic
    variables of the enclosing function that are used in nested ones into
    members of an artificial FRAME record type.
    
    The address of a local variable is usually passed to asan marking
    functions without using a temporary.  Taking the address of a member
    of FRAME within a nested function, however, is not regarded as a
    gimple val: while introducing FRAME variables, current_function_decl
    is always the outermost function, even while processing a nested
    function, so decl_address_invariant_p returns false for such
    ADDR_EXPRs.  So, as automatic variables are moved into FRAME, any asan
    call that marks such a variable has its ADDR_EXPR replaced with a
    SSA_NAME set to the ADDR_EXPR of the FRAME member.
    
    asan_expand_mark_ifn was not prepared to deal with ADDR_EXPRs split
    out into SSA_NAMEs.  This patch deals with such cases.
    
    [It does NOT deal with PHI nodes and whatnot.  I'm not even sure it
    should.  Maybe we want the ADDR_EXPR to be a gimple val instead.]
    
    
    for  gcc/ChangeLog
    
            * asan.c (asan_expand_mark_ifn): Follow SSA_NAME defs for
            an ADDR_EXPR base.
    
    for  gcc/testsuite/ChangeLog
    
            * gcc.dg/asan/nested-1.c: New.

Diff:
---
 gcc/asan.c                           | 21 +++++++++++++++++++++
 gcc/testsuite/gcc.dg/asan/nested-1.c | 24 ++++++++++++++++++++++++
 2 files changed, 45 insertions(+)

diff --git a/gcc/asan.c b/gcc/asan.c
index 89ecd99b182..2d2fb97098b 100644
--- a/gcc/asan.c
+++ b/gcc/asan.c
@@ -3629,6 +3629,27 @@ asan_expand_mark_ifn (gimple_stmt_iterator *iter)
   bool is_poison = ((asan_mark_flags)flag) == ASAN_MARK_POISON;
 
   tree base = gimple_call_arg (g, 1);
+  while (TREE_CODE (base) == SSA_NAME)
+    {
+      gimple *def = SSA_NAME_DEF_STMT (base);
+      if (!def)
+	break;
+
+      if (!is_gimple_assign (def))
+	break;
+
+      if (!SINGLE_SSA_TREE_OPERAND (def, SSA_OP_DEF))
+	break;
+
+      if (gimple_num_ops (def) != 2)
+	break;
+
+      if (gimple_expr_code (def) == ADDR_EXPR
+	  || gimple_expr_code (def) == SSA_NAME)
+	base = gimple_assign_rhs1 (def);
+      else
+	break;
+    }
   gcc_checking_assert (TREE_CODE (base) == ADDR_EXPR);
   tree decl = TREE_OPERAND (base, 0);
 
diff --git a/gcc/testsuite/gcc.dg/asan/nested-1.c b/gcc/testsuite/gcc.dg/asan/nested-1.c
new file mode 100644
index 00000000000..87e84209807
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/asan/nested-1.c
@@ -0,0 +1,24 @@
+/* { dg-do compile } */
+/* { dg-options "-fsanitize=address" } */
+
+int f(int i) {
+  auto int h() {
+    int r;
+    int *p;
+
+    {
+      int x[3];
+
+      auto int g() {
+	return x[i];
+      }
+
+      p = &r;
+      *p = g();
+    }
+
+    return *p;
+  }
+
+  return h();
+}


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [gcc(refs/users/aoliva/heads/testme)] follow SSA defs for asan base
@ 2021-01-20 10:26 Alexandre Oliva
  0 siblings, 0 replies; 2+ messages in thread
From: Alexandre Oliva @ 2021-01-20 10:26 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:cd2e357501ff934051c5fa32a2f3eb530a52f68d

commit cd2e357501ff934051c5fa32a2f3eb530a52f68d
Author: Alexandre Oliva <oliva@adacore.com>
Date:   Wed Jan 20 04:20:18 2021 -0300

    follow SSA defs for asan base
    
    Ada makes extensive use of nested functions, that turns all automatic
    variables of the enclosing function that are used in nested ones into
    members of an artificial FRAME record type.
    
    The address of a local variable is usually passed to asan marking
    functions without using a temporary.  Taking the address of a member
    of FRAME, however, is not a gimple val, so, as automatic variables are
    moved into FRAME, any asan call that marks such a variable has its
    ADDR_EXPR replaced with a SSA_NAME set to the ADDR_EXPR of the FRAME
    member.
    
    asan_expand_mark_ifn was not prepared to deal with ADDR_EXPRs split
    out into SSA_NAMEs.  This patch deals with such cases.
    
    
    for  gcc/ChangeLog
    
            * asan.c (asan_expand_mark_ifn): Follow SSA_NAME defs for
            an ADDR_EXPR base.

Diff:
---
 gcc/asan.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/gcc/asan.c b/gcc/asan.c
index 89ecd99b182..2d2fb97098b 100644
--- a/gcc/asan.c
+++ b/gcc/asan.c
@@ -3629,6 +3629,27 @@ asan_expand_mark_ifn (gimple_stmt_iterator *iter)
   bool is_poison = ((asan_mark_flags)flag) == ASAN_MARK_POISON;
 
   tree base = gimple_call_arg (g, 1);
+  while (TREE_CODE (base) == SSA_NAME)
+    {
+      gimple *def = SSA_NAME_DEF_STMT (base);
+      if (!def)
+	break;
+
+      if (!is_gimple_assign (def))
+	break;
+
+      if (!SINGLE_SSA_TREE_OPERAND (def, SSA_OP_DEF))
+	break;
+
+      if (gimple_num_ops (def) != 2)
+	break;
+
+      if (gimple_expr_code (def) == ADDR_EXPR
+	  || gimple_expr_code (def) == SSA_NAME)
+	base = gimple_assign_rhs1 (def);
+      else
+	break;
+    }
   gcc_checking_assert (TREE_CODE (base) == ADDR_EXPR);
   tree decl = TREE_OPERAND (base, 0);


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-01-21 17:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-21 17:06 [gcc(refs/users/aoliva/heads/testme)] follow SSA defs for asan base Alexandre Oliva
  -- strict thread matches above, loose matches on Subject: below --
2021-01-20 10:26 Alexandre Oliva

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).