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

[sanitizer_common] Fix TgKill on Solaris #98000

Merged
merged 1 commit into from
Jul 16, 2024

Conversation

rorth
Copy link
Collaborator

@rorth rorth commented Jul 8, 2024

While working on safestack on Solaris, I noticed that the TgKill implementation is wrong here: TgKill is supposed to return -1 on error, while thr_kill returns errno instead. This patch compensates for that.

This went unnoticed so far since TgKill has been unused.

Tested on amd64-pc-solaris2.11 and sparcv9-sun-solaris2.11 together with a subsequent patch to make safestack actually work on Solaris.

While working on safestack on Solaris, I noticed that the `TgKill`
implementation is wrong here: `TgKill` is supposed to return `-1` on error,
while `thr_kill` returns `errno` instead.  This patch compensates for that.

This went unnoticed so far since `TgKill` has been unused.

Tested on `amd64-pc-solaris2.11` and `sparcv9-sun-solaris2.11` together
with a subsequent patch to make safestack actually work on Solaris.
@llvmbot
Copy link
Collaborator

llvmbot commented Jul 8, 2024

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

Author: Rainer Orth (rorth)

Changes

While working on safestack on Solaris, I noticed that the TgKill implementation is wrong here: TgKill is supposed to return -1 on error, while thr_kill returns errno instead. This patch compensates for that.

This went unnoticed so far since TgKill has been unused.

Tested on amd64-pc-solaris2.11 and sparcv9-sun-solaris2.11 together with a subsequent patch to make safestack actually work on Solaris.


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

1 Files Affected:

  • (modified) compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp (+3-1)
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
index 12df3ef73da4bc..eec9d652630f95 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
@@ -563,7 +563,9 @@ int TgKill(pid_t pid, tid_t tid, int sig) {
   return internal_syscall(SYSCALL(thr_kill2), pid, tid, sig);
 #    elif SANITIZER_SOLARIS
   (void)pid;
-  return thr_kill(tid, sig);
+  errno = thr_kill(tid, sig);
+  // TgKill is expected to return -1 on error, not an errno.
+  return errno != 0 ? -1 : 0;
 #    endif
 }
 #  endif

rorth added a commit to rorth/llvm-project that referenced this pull request Jul 11, 2024
safestack currently uses its own portability layer in
`safestack_platform.h`.  This is both wasteful and leads to errors easily
avoided by using the well-tested functions in `sanitizer_common` instead.
This is particularly notable when ([safestack] Support multilib testing
E.g. all Linux/i386 tests `FAIL`ed with
```
safestack CHECK failed: /vol/llvm/src/llvm-project/local/compiler-rt/lib/safestack/safestack.cpp:95 MAP_FAILED != addr
```
because the safestack `mmap` implementation doesn't work there.

Thus this patch switches safestack to use the `sanitizer_common` code
instead.  It's mostly obvious, with the exception of `TgKill`:

- The `sanitizer_common` Linux `TgKill` implementation is wrong: while
  safestack used `syscall(SYS_tgkill)`, `sanitizer_common` has
  `internal_syscall(SYSCALL(tgkill))` instead.  However, the latter (based
  on using the `syscall` insn directly on x86_64) returns `errno` instead
  of the expeccted `-1`.  Besides, `TgKill` had to be moved to
  `sanitizer_linux_libcdep.cpp`, otherwise the
  `Sanitizer-x86_64-Test-Nolibc` test failed to link with an undefined
  reference to `syscall`.
- On Solaris, a similar issue with `thr_kill` went unnoticed before because
  `sanitizer_common` `TgKill` wasn't used.  ([sanitizer_common] Fix TgKill
  on Solaris)[llvm#98000] was merged
  into this patch.
- On 32-bit Linux/sparc, `pthread-cleanup.c` would `FAIL` because a `tid_t`
  (`uint64_t`) `tid` arg was passed to `syscall(SYS_tgkill)` while `tid` is
  actually a `pid_t` (`int`).

Tested on `amd64-pc-solaris2.11`, `sparcv9-sun-solaris2.11`,
`x86_64-pc-solaris2.11`, and `sparc64-unknown-linux-gnu`.
@rorth rorth merged commit 009e176 into llvm:main Jul 16, 2024
10 checks passed
sayhaan pushed a commit to sayhaan/llvm-project that referenced this pull request Jul 16, 2024
Summary:
While working on safestack on Solaris, I noticed that the `TgKill`
implementation is wrong here: `TgKill` is supposed to return `-1` on
error, while `thr_kill` returns `errno` instead. This patch compensates
for that.

This went unnoticed so far since `TgKill` has been unused.

Tested on `amd64-pc-solaris2.11` and `sparcv9-sun-solaris2.11` together
with a subsequent patch to make safestack actually work on Solaris.

Test Plan: 

Reviewers: 

Subscribers: 

Tasks: 

Tags: 


Differential Revision: https://phabricator.intern.facebook.com/D59822428
yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
Summary:
While working on safestack on Solaris, I noticed that the `TgKill`
implementation is wrong here: `TgKill` is supposed to return `-1` on
error, while `thr_kill` returns `errno` instead. This patch compensates
for that.

This went unnoticed so far since `TgKill` has been unused.

Tested on `amd64-pc-solaris2.11` and `sparcv9-sun-solaris2.11` together
with a subsequent patch to make safestack actually work on Solaris.

Test Plan: 

Reviewers: 

Subscribers: 

Tasks: 

Tags: 


Differential Revision: https://phabricator.intern.facebook.com/D60251717
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