-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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] Pre-commit disabled test for fork #75257
Merged
vitalybuka
merged 7 commits into
main
from
users/vitalybuka/spr/sanitizer-pre-commit-disabled-test-for-fork
Dec 13, 2023
Merged
[sanitizer] Pre-commit disabled test for fork #75257
vitalybuka
merged 7 commits into
main
from
users/vitalybuka/spr/sanitizer-pre-commit-disabled-test-for-fork
Dec 13, 2023
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Created using spr 1.3.4
@llvm/pr-subscribers-compiler-rt-sanitizer Author: Vitaly Buka (vitalybuka) ChangesFull diff: https://github.com/llvm/llvm-project/pull/75257.diff 2 Files Affected:
diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/fork_threaded.cpp b/compiler-rt/test/sanitizer_common/TestCases/Posix/fork_threaded.cpp
new file mode 100644
index 00000000000000..d5c60bbb7c9430
--- /dev/null
+++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/fork_threaded.cpp
@@ -0,0 +1,86 @@
+// RUN: %clangxx -O0 %s -o %t && %env_tool_opts=die_after_fork=0 %run %t
+
+// UNSUPPORTED: *
+
+// Forking in multithread environment is unsupported. However we already have
+// some workarounds, and will add more, so this is the test.
+
+#include <assert.h>
+#include <pthread.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#include "sanitizer_common/sanitizer_specific.h"
+
+static const size_t kBufferSize = 1 << 20;
+
+static void *background(void *arg) { return nullptr; }
+
+pthread_barrier_t bar;
+
+void CanDeadLock() {
+ // Don't bother with leaks, we try to trigger allocator or lsan deadlock.
+ __lsan::ScopedDisabler disable;
+ char *volatile p = new char[10];
+ __lsan_do_recoverable_leak_check();
+ delete[] p;
+}
+
+// Prevent stack buffer cleanup by instrumentation.
+#define NOSAN __attribute__((no_sanitize("address", "hwaddress", "memory")))
+
+NOSAN static void *inparent(void *arg) {
+ fprintf(stderr, "inparent %d\n", gettid());
+
+ char t[kBufferSize];
+ make_mem_bad(t, sizeof(t));
+
+ pthread_barrier_wait(&bar);
+
+ for (;;)
+ CanDeadLock();
+
+ return 0;
+}
+
+NOSAN static void *inchild(void *arg) {
+ char t[kBufferSize];
+ check_mem_is_good(t, sizeof(t));
+ CanDeadLock();
+ return 0;
+}
+
+int main(void) {
+ pid_t pid;
+
+ pthread_barrier_init(&bar, nullptr, 2);
+ pthread_t thread_id;
+ while (pthread_create(&thread_id, 0, &inparent, 0) != 0) {
+ }
+ pthread_barrier_wait(&bar);
+
+ pid = fork();
+ switch (pid) {
+ case -1:
+ perror("fork");
+ return -1;
+ case 0:
+ while (pthread_create(&thread_id, 0, &inchild, 0) != 0) {
+ }
+ break;
+ default: {
+ fprintf(stderr, "fork %d\n", pid);
+ int status;
+ while (waitpid(-1, &status, __WALL) != pid) {
+ }
+ assert(WIFEXITED(status) && WEXITSTATUS(status) == 0);
+ break;
+ }
+ }
+
+ return 0;
+}
\ No newline at end of file
diff --git a/compiler-rt/test/sanitizer_common/sanitizer_specific.h b/compiler-rt/test/sanitizer_common/sanitizer_specific.h
index 1a802020cfd668..963d91cb305f60 100644
--- a/compiler-rt/test/sanitizer_common/sanitizer_specific.h
+++ b/compiler-rt/test/sanitizer_common/sanitizer_specific.h
@@ -1,6 +1,12 @@
#ifndef __SANITIZER_COMMON_SANITIZER_SPECIFIC_H__
#define __SANITIZER_COMMON_SANITIZER_SPECIFIC_H__
+#include <sanitizer/lsan_interface.h>
+
+__attribute__((weak)) int __lsan_do_recoverable_leak_check() { return 0; }
+__attribute__((weak)) void __lsan_disable(void) {}
+__attribute__((weak)) void __lsan_enable(void) {}
+
#ifndef __has_feature
# define __has_feature(x) 0
#endif
@@ -10,6 +16,8 @@
static void check_mem_is_good(void *p, size_t s) {
__msan_check_mem_is_initialized(p, s);
}
+static void make_mem_good(void *p, size_t s) { __msan_unpoison(p, s); }
+static void make_mem_bad(void *p, size_t s) { __msan_poison(p, s); }
#elif __has_feature(address_sanitizer)
# include <sanitizer/asan_interface.h>
# include <stdlib.h>
@@ -17,8 +25,16 @@ static void check_mem_is_good(void *p, size_t s) {
if (__asan_region_is_poisoned(p, s))
abort();
}
+static void make_mem_good(void *p, size_t s) {
+ __asan_unpoison_memory_region(p, s);
+}
+static void make_mem_bad(void *p, size_t s) {
+ __asan_poison_memory_region(p, s);
+}
#else
static void check_mem_is_good(void *p, size_t s) {}
+static void make_mem_good(void *p, size_t s) {}
+static void make_mem_bad(void *p, size_t s) {}
#endif
#endif // __SANITIZER_COMMON_SANITIZER_SPECIFIC_H__
\ No newline at end of file
|
fmayer
reviewed
Dec 13, 2023
compiler-rt/test/sanitizer_common/TestCases/Posix/fork_threaded.cpp
Outdated
Show resolved
Hide resolved
fmayer
reviewed
Dec 13, 2023
compiler-rt/test/sanitizer_common/TestCases/Posix/fork_threaded.cpp
Outdated
Show resolved
Hide resolved
compiler-rt/test/sanitizer_common/TestCases/Posix/fork_threaded.cpp
Outdated
Show resolved
Hide resolved
fmayer
approved these changes
Dec 13, 2023
Created using spr 1.3.4
Created using spr 1.3.4
vitalybuka
deleted the
users/vitalybuka/spr/sanitizer-pre-commit-disabled-test-for-fork
branch
December 13, 2023 04:00
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.