[gcc(refs/users/aoliva/heads/testme)] regard the address of auto vars and consts as invariant
Alexandre Oliva
aoliva@gcc.gnu.org
Tue Jan 26 01:59:29 GMT 2021
https://gcc.gnu.org/g:9b4c26c7bf32d0aa23fdf49f85106c2ca5effc85
commit 9b4c26c7bf32d0aa23fdf49f85106c2ca5effc85
Author: Alexandre Oliva <oliva@adacore.com>
Date: Mon Jan 25 20:25:36 2021 -0300
regard the address of auto vars and consts as invariant
Ada makes extensive use of nested functions, which turn 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. asan_expand_mark_ifn will reject
an ADDR_EXPRs if it's split out from the call into an SSA_NAMEs.
Taking the address of a member of FRAME within a nested function was
not regarded as a gimple val: while introducing FRAME variables,
current_function_decl pointed to the outermost function, even while
processing a nested function, so decl_address_invariant_p, checking
that the context of the variable is current_function_decl, returned
false for such ADDR_EXPRs.
This patch changes decl_address_invariant_p to disregard
current_function_decl, and regard all automatic variables as having
invariant addresses, regardless of nesting. This may initially
include references to variables in other nesting levels, but once they
become references to enclosing frames, the indirection makes them
non-gimple_vals. As long as poisoning and unpoisoning calls doesn't
kick in for variables in other frames, this shouldn't be a problem.
for gcc/ChangeLog
* tree.c (decl_address_invariant_p): Accept auto variables and
constants.
for gcc/testsuite/ChangeLog
* gcc.dg/asan/nested-1.c: New.
Diff:
---
gcc/testsuite/gcc.dg/asan/nested-1.c | 24 ++++++++++++++++++++++++
gcc/tree.c | 5 ++---
2 files changed, 26 insertions(+), 3 deletions(-)
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();
+}
diff --git a/gcc/tree.c b/gcc/tree.c
index 287e5001dc3..3de3085f42c 100644
--- a/gcc/tree.c
+++ b/gcc/tree.c
@@ -3590,14 +3590,13 @@ decl_address_invariant_p (const_tree op)
case VAR_DECL:
if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
|| DECL_THREAD_LOCAL_P (op)
- || DECL_CONTEXT (op) == current_function_decl
- || decl_function_context (op) == current_function_decl)
+ || auto_var_p (op))
return true;
break;
case CONST_DECL:
if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
- || decl_function_context (op) == current_function_decl)
+ || auto_var_p (op))
return true;
break;
More information about the Gcc-cvs
mailing list