Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why does the option -fno-sanitize-address-use-after-scope not take effect? #77047

Closed
Zhenhang1213 opened this issue Jan 5, 2024 · 7 comments
Closed
Labels
clang:codegen question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

Comments

@Zhenhang1213
Copy link

Zhenhang1213 commented Jan 5, 2024

void foo(int *x) {
  *x = 0;
}

int main() {
  int x;
  foo(&x);
  return x;
}

https://godbolt.org/z/Yof6d6EYb

@EugeneZelenko EugeneZelenko added question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead! clang:codegen and removed new issue labels Jan 5, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Jan 5, 2024

@llvm/issue-subscribers-clang-codegen

Author: None (Zhenhang1213)

`void foo(int *x) { *x = 0; }

int main() {
int x;
foo(&x);
return x;
}`
https://godbolt.org/z/9Mdexxd6x

@Zhenhang1213 Zhenhang1213 changed the title Why does the option -fno-sanitize-address-use-after-scope not take effect? Why does the option '-fno-sanitize-address-use-after-scope' not take effect? Jan 5, 2024
@Zhenhang1213 Zhenhang1213 changed the title Why does the option '-fno-sanitize-address-use-after-scope' not take effect? Why does the option -fno-sanitize-address-use-after-scope not take effect? Jan 5, 2024
@shafik
Copy link
Collaborator

shafik commented Jan 5, 2024

Your question is not clear, can you explain in more detail what you are asking.

@Zhenhang1213
Copy link
Author

Your question is not clear, can you explain in more detail what you are asking.

I found clang using this option, the assembly was still the call asan_report_load function,but gcc doesn‘t

@MaskRay
Copy link
Member

MaskRay commented Jan 7, 2024

Neither asan-use-after-scope nor asan-use-after-return controls whether accesses to a local variable x (AllocaInst) should be instrumented.
However, StackSafetyAnalysis can remove the unneeded instrumentation for return x. I have created #77210 to enable StackSafetyAnalysis by default.

godbolt.org/z/Yof6d6EYb

For future issues, consider adding the exact clang command line.

@Zhenhang1213
Copy link
Author

Neither asan-use-after-scope nor asan-use-after-return controls whether accesses to a local variable x (AllocaInst) should be instrumented. However, StackSafetyAnalysis can remove the unneeded instrumentation for return x. I have created #77210 to enable StackSafetyAnalysis by default.

godbolt.org/z/Yof6d6EYb

For future issues, consider adding the exact clang command line.

Neither asan-use-after-scope nor asan-use-after-return controls whether accesses to a local variable x (AllocaInst) should be instrumented. However, StackSafetyAnalysis can remove the unneeded instrumentation for return x. I have created #77210 to enable StackSafetyAnalysis by default.

godbolt.org/z/Yof6d6EYb

For future issues, consider adding the exact clang command line.

ok,but I have another question, so what scenario is those options generally used for? Don't they detect the return value ?

@MaskRay
Copy link
Member

MaskRay commented Jan 10, 2024

Now StackSafetyAnalysis is the default, this variable in question is no longer instrumented.


I think neither -fsanitize-address-stack-use-after-scope nor -fsanitize-address-stack-use-after-return affects whether a stack variable should be instrumented,
but they are responsible for certain extra instructions for stack instrumentation.


If -fsanitize-address-stack-use-after-scope (default) is enabled, when a variable gets out of scope, its shadow memory is filled with 0xf8 (kAsanStackUseAfterScopeMagic).
Accessing the variable will lead to a stack-use-after-scope error.

-fsanitize-address-use-after-return= accepts one of the following values:

  • runtime (default): instrumented code checks a global variable __asan_option_detect_stack_use_after_return to decide whether a fake stack frame is used.
  • always: instrumented code unconditionally creates a fake stack frame. This saves code size.
  • never: don't detect use-after-return

When the instrumentation decides to create a fake stack frame, it allocates one using __asan_stack_malloc_{0..10}(uptr ptr, uptr size).
The runtime function may return nullptr, in which case a fake stack frame is unavailable, and alloca will be used to allocate the local stack frame; otherwise, stack variables and associated redzones are allocated on the fake stack frame.
never removes the extra code.


I am curious why GCC -fsanitize-address-use-after-scope instruments the variable when optimizations are enabled.
It seems that this case can be optimized out as well.

@MaskRay MaskRay closed this as completed Jan 10, 2024
@Zhenhang1213
Copy link
Author

Now StackSafetyAnalysis is the default, this variable in question is no longer instrumented.

I think neither -fsanitize-address-stack-use-after-scope nor -fsanitize-address-stack-use-after-return affects whether a stack variable should be instrumented, but they are responsible for certain extra instructions for stack instrumentation.

If -fsanitize-address-stack-use-after-scope (default) is enabled, when a variable gets out of scope, its shadow memory is filled with 0xf8 (kAsanStackUseAfterScopeMagic). Accessing the variable will lead to a stack-use-after-scope error.

-fsanitize-address-use-after-return= accepts one of the following values:

  • runtime (default): instrumented code checks a global variable __asan_option_detect_stack_use_after_return to decide whether a fake stack frame is used.
  • always: instrumented code unconditionally creates a fake stack frame. This saves code size.
  • never: don't detect use-after-return

When the instrumentation decides to create a fake stack frame, it allocates one using __asan_stack_malloc_{0..10}(uptr ptr, uptr size). The runtime function may return nullptr, in which case a fake stack frame is unavailable, and alloca will be used to allocate the local stack frame; otherwise, stack variables and associated redzones are allocated on the fake stack frame. never removes the extra code.

I am curious why GCC -fsanitize-address-use-after-scope instruments the variable when optimizations are enabled. It seems that this case can be optimized out as well.

I get it, but I find disable __asan_option_detect_stack_use_after_return doesnot work in gcc or clang。Asan still reports __asan_report_load4。
https://godbolt.org/z/Yzx9EM11e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!
Projects
None yet
Development

No branches or pull requests

5 participants