-
-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AddressSanitizer support to fibers. (#2975)
- Loading branch information
1 parent
4b101ba
commit e25c4ee
Showing
7 changed files
with
100 additions
and
3 deletions.
There are no files selected for viewing
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
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
Submodule druntime
updated
3 files
+47 −0 | src/core/thread.d | |
+36 −0 | src/ldc/sanitizer_common.d | |
+119 −0 | src/ldc/sanitizers_optionally_linked.d |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// AddressSanitizer: Test stack overflow detection of an array on a fiber's local stack. | ||
|
||
// REQUIRES: ASan, RTSupportsSanitizers | ||
|
||
// RUN: %ldc -g -fsanitize=address %s -of=%t%exe && not %t%exe 2>&1 | FileCheck %s | ||
// RUN: %ldc -g -fsanitize=address %s -of=%t%exe -d-version=BAD_AFTER_YIELD && not %t%exe 2>&1 | FileCheck %s | ||
|
||
import core.thread; | ||
|
||
// Note: the ordering of `foo` and `prefoo` is intentional to ease FileCheck checking line numbers, | ||
// because of the order in which ASan reports the stack buffer overflow. | ||
|
||
void foo(int* ptr) | ||
{ | ||
version (BAD_AFTER_YIELD) | ||
Fiber.yield(); | ||
|
||
// CHECK: stack-buffer-overflow | ||
// CHECK: WRITE of size 4 | ||
// CHECK-NEXT: #0 {{.*}} in {{.*foo.*}} {{.*}}asan_fiber.d:[[@LINE+1]] | ||
ptr[10] = 1; | ||
|
||
} | ||
|
||
// CHECK-NOT: wild pointer | ||
// CHECK: Address {{.*}} is located in stack of | ||
// CHECK-NEXT: #0 {{.*}} in {{.*prefoo.*}} {{.*}}asan_fiber.d:[[@LINE+1]] | ||
void prefoo() | ||
{ | ||
int[10] a; | ||
foo(&a[0]); | ||
} | ||
|
||
void main() | ||
{ | ||
auto fib = new Fiber(&prefoo); | ||
fib.call(); | ||
version (BAD_AFTER_YIELD) | ||
fib.call(); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// AddressSanitizer: Test stack overflow detection inside a fiber of an array on main's stack. | ||
|
||
// REQUIRES: ASan, RTSupportsSanitizers | ||
|
||
// RUN: %ldc -g -fsanitize=address %s -of=%t1%exe && not %t1%exe 2>&1 | FileCheck %s | ||
// RUN: %ldc -g -fsanitize=address %s -of=%t22%exe -d-version=BAD_AFTER_YIELD && not %t22%exe 2>&1 | FileCheck %s | ||
|
||
// Test with fake stack enabled | ||
// RUN: env %env_asan_opts=detect_stack_use_after_return=true not %t1%exe 2>&1 | FileCheck %s --check-prefix=FAKESTACK | ||
// RUN: env %env_asan_opts=detect_stack_use_after_return=true not %t22%exe 2>&1 | FileCheck %s --check-prefix=FAKESTACK | ||
|
||
import core.thread; | ||
|
||
void foo(int* arr) | ||
{ | ||
version (BAD_AFTER_YIELD) | ||
Fiber.yield(); | ||
|
||
// CHECK: stack-buffer-overflow | ||
// CHECK: WRITE of size 4 | ||
// CHECK-NEXT: #0 {{.*}} in {{.*foo.*}} {{.*}}asan_fiber_main.d:[[@LINE+1]] | ||
arr[10] = 1; // out-of-bounds write | ||
} | ||
|
||
// Without fake stack, ASan only keeps track of the current stack and thus reports | ||
// the bad memory location as a "wild pointer". | ||
// But with fake stack enabled we get something much better: | ||
// FAKESTACK: Address {{.*}} is located in stack of | ||
// FAKESTACK: #0 {{.*}} in {{.*main.*}} {{.*}}asan_fiber_main.d:[[@LINE+1]] | ||
void main() | ||
{ | ||
int[10] a; | ||
int b; | ||
|
||
// Use an extra variable instead of passing `&a[0]` directly to `foo`. | ||
// This is to keep `a` on the stack: `ptr` may be heap allocated because | ||
// it is used in the lambda (delegate). | ||
int* ptr = &a[0]; | ||
auto fib = new Fiber(() => foo(ptr)); | ||
fib.call(); | ||
version (BAD_AFTER_YIELD) | ||
fib.call(); | ||
} |
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