Skip to content

[sanitizer_common] Fix GetArgsAndEnv on Linux/sparc64 #109109

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

Merged

Conversation

rorth
Copy link
Collaborator

@rorth rorth commented Sep 18, 2024

When ASan testing is enabled on SPARC as per PR #107405, the

  AddressSanitizer-sparc-linux :: TestCases/Posix/print_cmdline.cpp

test FAILs. Either ASAN_OPTIONS=print_cmdline=true yielded binary garbage in the Command: output or just an empty string.

It turns out one needs to apply an offset to __libc_stack_end to get at the actual argc/argv, as described in glibc's sysdeps/sparc/sparc{32,64}/dl-machine.h (DL_STACK_END).

This patch does this, fixing the test.

Tested on sparc64-unknown-linux-gnu.

When ASan testing is enabled on SPARC as per PR llvm#107405, the
```
  AddressSanitizer-sparc-linux :: TestCases/Posix/print_cmdline.cpp
```
test `FAIL`s.  Either `ASAN_OPTIONS=print_cmdline=true` yielded binary
garbage in the `Command:` output or just an empty string.

It turns out one needs to apply an offset to `__libc_stack_end` to get at
the actual `argc`/`argv`, as described in `glibc`'s
`sysdeps/sparc/sparc{32,64}/dl-machine.h` (`DL_STACK_END`).

This patch does this, fixing the test.

Tested on `sparc64-unknown-linux-gnu`.
@llvmbot
Copy link
Member

llvmbot commented Sep 18, 2024

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Rainer Orth (rorth)

Changes

When ASan testing is enabled on SPARC as per PR #107405, the

  AddressSanitizer-sparc-linux :: TestCases/Posix/print_cmdline.cpp

test FAILs. Either ASAN_OPTIONS=print_cmdline=true yielded binary garbage in the Command: output or just an empty string.

It turns out one needs to apply an offset to __libc_stack_end to get at the actual argc/argv, as described in glibc's sysdeps/sparc/sparc{32,64}/dl-machine.h (DL_STACK_END).

This patch does this, fixing the test.

Tested on sparc64-unknown-linux-gnu.


Full diff: https://github.com/llvm/llvm-project/pull/109109.diff

1 Files Affected:

  • (modified) compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp (+5)
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
index 6359f4348e3c48..2faba3353d13dc 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
@@ -725,6 +725,11 @@ static void GetArgsAndEnv(char ***argv, char ***envp) {
 #    if !SANITIZER_GO
   if (&__libc_stack_end) {
     uptr *stack_end = (uptr *)__libc_stack_end;
+    // Linux/sparc64 needs an adjustment, cf. glibc
+    // sysdeps/sparc/sparc{32,64}/dl-machine.h (DL_STACK_END).
+#      if SANITIZER_LINUX && defined(__sparc__)
+    stack_end = &stack_end[16];
+#      endif
     // Normally argc can be obtained from *stack_end, however, on ARM glibc's
     // _start clobbers it:
     // https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/arm/start.S;hb=refs/heads/release/2.31/master#l75

@rorth rorth merged commit 4a264c5 into llvm:main Sep 24, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants