Skip to content

Commit 9aad997

Browse files
committedJun 26, 2019
[WebAssembly] Implement Address Sanitizer for Emscripten
Summary: This diff enables address sanitizer on Emscripten. On Emscripten, real memory starts at the value passed to --global-base. All memory before this is used as shadow memory, and thus the shadow mapping function is simply dividing by 8. Reviewers: tlively, aheejin, sbc100 Reviewed By: sbc100 Subscribers: dschuff, sbc100, jgravelle-google, hiraditya, sunfish, cfe-commits, llvm-commits Tags: #clang, #llvm Differential Revision: https://reviews.llvm.org/D63742 llvm-svn: 364468
1 parent e15dc95 commit 9aad997

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed
 

Diff for: ‎clang/lib/Driver/ToolChains/WebAssembly.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ void WebAssembly::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
238238
SanitizerMask WebAssembly::getSupportedSanitizers() const {
239239
SanitizerMask Res = ToolChain::getSupportedSanitizers();
240240
if (getTriple().isOSEmscripten()) {
241-
Res |= SanitizerKind::Vptr | SanitizerKind::Leak;
241+
Res |= SanitizerKind::Vptr | SanitizerKind::Leak | SanitizerKind::Address;
242242
}
243243
return Res;
244244
}

Diff for: ‎clang/test/Driver/wasm-toolchain.c

+4
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,7 @@
6060
// RUN: --sysroot=/foo %s -pthread -mno-atomics 2>&1 \
6161
// RUN: | FileCheck -check-prefix=PTHREAD_NO_ATOMICS %s
6262
// PTHREAD_NO_ATOMICS: invalid argument '-pthread' not allowed with '-mno-atomics'
63+
64+
// RUN: %clang %s -### -fsanitize=address -target wasm32-unknown-emscripten 2>&1 | FileCheck -check-prefix=CHECK-ASAN-EMSCRIPTEN %s
65+
// CHECK-ASAN-EMSCRIPTEN: "-fsanitize=address"
66+
// CHECK-ASAN-EMSCRIPTEN: "-fsanitize-address-globals-dead-stripping"

Diff for: ‎llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ static const uint64_t kNetBSD_ShadowOffset64 = 1ULL << 46;
109109
static const uint64_t kNetBSDKasan_ShadowOffset64 = 0xdfff900000000000;
110110
static const uint64_t kPS4CPU_ShadowOffset64 = 1ULL << 40;
111111
static const uint64_t kWindowsShadowOffset32 = 3ULL << 28;
112+
static const uint64_t kEmscriptenShadowOffset = 0;
112113

113114
static const uint64_t kMyriadShadowScale = 5;
114115
static const uint64_t kMyriadMemoryOffset32 = 0x80000000ULL;
@@ -433,6 +434,7 @@ static ShadowMapping getShadowMapping(Triple &TargetTriple, int LongSize,
433434
bool IsWindows = TargetTriple.isOSWindows();
434435
bool IsFuchsia = TargetTriple.isOSFuchsia();
435436
bool IsMyriad = TargetTriple.getVendor() == llvm::Triple::Myriad;
437+
bool IsEmscripten = TargetTriple.isOSEmscripten();
436438

437439
ShadowMapping Mapping;
438440

@@ -454,6 +456,8 @@ static ShadowMapping getShadowMapping(Triple &TargetTriple, int LongSize,
454456
Mapping.Offset = kDynamicShadowSentinel;
455457
else if (IsWindows)
456458
Mapping.Offset = kWindowsShadowOffset32;
459+
else if (IsEmscripten)
460+
Mapping.Offset = kEmscriptenShadowOffset;
457461
else if (IsMyriad) {
458462
uint64_t ShadowOffset = (kMyriadMemoryOffset32 + kMyriadMemorySize32 -
459463
(kMyriadMemorySize32 >> Mapping.Scale));

0 commit comments

Comments
 (0)
Please sign in to comment.