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

Fix ThreadSanitizer support by removing detaching of main thread upon program termination #3572

Merged
merged 1 commit into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion runtime/druntime
1 change: 1 addition & 0 deletions tests/lit.site.cfg.in
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ config.ldc_host_arch = "@LLVM_NATIVE_ARCH@"
config.ldc_with_lld = @LDC_WITH_LLD@
config.spirv_enabled = @LLVM_SPIRV_FOUND@
config.rt_supports_sanitizers = @RT_SUPPORT_SANITIZERS@
config.shared_rt_libs_only = "@BUILD_SHARED_LIBS@" == "ON"

config.name = 'LDC'

Expand Down
8 changes: 5 additions & 3 deletions tests/sanitizers/lit.local.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import os
import platform
import re

# Add "ASan" and "Fuzzer" feature if the runtime library is available.
# Add "ASan", "TSan" and "Fuzzer" feature if the runtime library is available.
sys = platform.system()
for file in os.listdir(config.ldc2_lib_dir):
m = re.match('.*asan.*', file)
if m is not None:
config.available_features.add('ASan')
continue
# FreeBSD TSan needs https://reviews.llvm.org/D85292
if platform.system() != 'FreeBSD':
# FreeBSD TSan needs https://reviews.llvm.org/D85292,
# Linux TSan currently only works with static druntime.
if (sys != 'FreeBSD') and not (sys == 'Linux' and config.shared_rt_libs_only):
m = re.match('.*tsan.*', file)
if m is not None:
config.available_features.add('TSan')
Expand Down
5 changes: 1 addition & 4 deletions tests/sanitizers/tsan_noerror.d
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// Test that a simple program passes ThreadSanitizer without error

// REQUIRES: TSan

// XFAIL: *
// Druntime does not yet work with ThreadSanitizer.
// See Github issue 3519 (https://github.com/ldc-developers/ldc/issues/3519)
// REQUIRES: atleast_llvm800

// RUN: %ldc -fsanitize=thread %s -of=%t%exe
// RUN: %t%exe
Expand Down
20 changes: 8 additions & 12 deletions tests/sanitizers/tsan_tiny_race.d
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
// Test that ThreadSanitizer+LDC works on a very basic testcase.
// Note that -betterC is used, to avoid relying on druntime for this test.

// REQUIRES: TSan
// REQUIRES: atleast_llvm800

// RUN: %ldc -betterC -g -fsanitize=thread %s -of=%t%exe
// RUN: %ldc -g -fsanitize=thread %s -of=%t%exe
// RUN: %deflake 20 %t%exe | FileCheck %s

// CHECK: WARNING: ThreadSanitizer: data race

import core.sys.posix.pthread;
import core.thread;

shared int global;

extern(C)
void *thread1(void *x) {
void thread1() {
barrier_wait(&barrier);
// CHECK-DAG: thread1{{.*}}[[@LINE+1]]
// CHECK-DAG: 7thread1{{.*}}[[@LINE+1]]
global = 42;
return x;
}

extern(C)
int main() {
barrier_init(&barrier, 2);
pthread_t t;
pthread_create(&t, null, &thread1, null);
// CHECK-DAG: main{{.*}}[[@LINE+1]]
auto t = new Thread(&thread1);
t.start();
// CHECK-DAG: _Dmain{{.*}}[[@LINE+1]]
global = 43;
barrier_wait(&barrier);
pthread_join(t, null);
t.join();
return global;
}

Expand Down
23 changes: 10 additions & 13 deletions tests/sanitizers/tsan_tiny_race_TLS.d
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
// Test that ThreadSanitizer+LDC works on a very basic testcase.
// Note that -betterC is used, to avoid relying on druntime for this test.

// REQUIRES: TSan
// REQUIRES: atleast_llvm800

// RUN: %ldc -betterC -g -fsanitize=thread %s -of=%t%exe
// RUN: %ldc -g -fsanitize=thread %s -of=%t%exe
// RUN: %deflake 20 %t%exe | FileCheck %s

// CHECK: WARNING: ThreadSanitizer: data race

import core.sys.posix.pthread;
import core.thread;

extern(C)
void *thread1(void *x) {
void thread1(ref int x) nothrow {
barrier_wait(&barrier);
// CHECK-DAG: thread1{{.*}}[[@LINE+1]]
*cast(int*)x = 42;
return x;
// CHECK-DAG: 7thread1{{.*}}[[@LINE+1]]
x = 42;
}

extern(C)
int main() {
int tls_variable;
barrier_init(&barrier, 2);
pthread_t t;
pthread_create(&t, null, &thread1, &tls_variable);
// CHECK-DAG: main{{.*}}[[@LINE+1]]
auto tid = createLowLevelThread(() { thread1(tls_variable); });
// CHECK-DAG: _Dmain{{.*}}[[@LINE+1]]
tls_variable = 43;
barrier_wait(&barrier);
pthread_join(t, null);
joinLowLevelThread(tid);
return 0;
}

Expand All @@ -45,6 +40,8 @@ alias __c_unsigned = uint;
// Default instance of the barrier, but a test can declare more manually.
__gshared invisible_barrier_t barrier;

nothrow:

extern (C) {
// These functions reside inside the tsan library.
void __tsan_testonly_barrier_init(invisible_barrier_t *barrier, __c_unsigned count);
Expand Down