-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[clang] Support ASan on WASI #139014
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
base: main
Are you sure you want to change the base?
[clang] Support ASan on WASI #139014
Conversation
This change makes `-fsanitize=address` available for WASI target by replicating what we do for Emscripten because they share the same memory model.
@llvm/pr-subscribers-backend-webassembly @llvm/pr-subscribers-clang-driver Author: Yuta Saito (kateinoigakukun) ChangesI'm working on porting ASan to Wasm/WASI targets, and this is the first part of the change sets. I'll post runtime changes separately. This change makes Full diff: https://github.com/llvm/llvm-project/pull/139014.diff 2 Files Affected:
diff --git a/clang/lib/Driver/ToolChains/WebAssembly.cpp b/clang/lib/Driver/ToolChains/WebAssembly.cpp
index cd12f2ae5a6de..9fcc33728c1ad 100644
--- a/clang/lib/Driver/ToolChains/WebAssembly.cpp
+++ b/clang/lib/Driver/ToolChains/WebAssembly.cpp
@@ -545,8 +545,13 @@ void WebAssembly::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
SanitizerMask WebAssembly::getSupportedSanitizers() const {
SanitizerMask Res = ToolChain::getSupportedSanitizers();
if (getTriple().isOSEmscripten()) {
- Res |= SanitizerKind::Vptr | SanitizerKind::Leak | SanitizerKind::Address;
+ Res |= SanitizerKind::Vptr | SanitizerKind::Leak;
}
+
+ if (getTriple().isOSEmscripten() || getTriple().isOSWASI()) {
+ Res |= SanitizerKind::Address;
+ }
+
// -fsanitize=function places two words before the function label, which are
// -unsupported.
Res &= ~SanitizerKind::Function;
diff --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
index c1dba77c3532b..840a5e3f31dfd 100644
--- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
@@ -118,7 +118,7 @@ static const uint64_t kNetBSD_ShadowOffset64 = 1ULL << 46;
static const uint64_t kNetBSDKasan_ShadowOffset64 = 0xdfff900000000000;
static const uint64_t kPS_ShadowOffset64 = 1ULL << 40;
static const uint64_t kWindowsShadowOffset32 = 3ULL << 28;
-static const uint64_t kEmscriptenShadowOffset = 0;
+static const uint64_t kWebAssemblyShadowOffset = 0;
// The shadow memory space is dynamically allocated.
static const uint64_t kWindowsShadowOffset64 = kDynamicShadowSentinel;
@@ -499,9 +499,9 @@ static ShadowMapping getShadowMapping(const Triple &TargetTriple, int LongSize,
bool IsRISCV64 = TargetTriple.getArch() == Triple::riscv64;
bool IsWindows = TargetTriple.isOSWindows();
bool IsFuchsia = TargetTriple.isOSFuchsia();
- bool IsEmscripten = TargetTriple.isOSEmscripten();
bool IsAMDGPU = TargetTriple.isAMDGPU();
bool IsHaiku = TargetTriple.isOSHaiku();
+ bool IsWasm = TargetTriple.isWasm();
ShadowMapping Mapping;
@@ -525,8 +525,8 @@ static ShadowMapping getShadowMapping(const Triple &TargetTriple, int LongSize,
Mapping.Offset = kDynamicShadowSentinel;
else if (IsWindows)
Mapping.Offset = kWindowsShadowOffset32;
- else if (IsEmscripten)
- Mapping.Offset = kEmscriptenShadowOffset;
+ else if (IsWasm)
+ Mapping.Offset = kWebAssemblyShadowOffset;
else
Mapping.Offset = kDefaultShadowOffset32;
} else { // LongSize == 64
|
This looks good to me, @sbc100 and @sunfishcode might also be interested (presumably the sanitizer runtime will end up alongside wasi-libc in the wasi SDK?) |
@dschuff Yes, it will be placed alongside the other clang_rt libraries in WASI SDK and Swift SDK for Wasm/WASI |
I'm working on porting ASan to Wasm/WASI targets, and this is the first part of the change sets. I'll post runtime changes separately.
This change makes
-fsanitize=address
available for WASI target by replicating what we do for Emscripten because they share the same memory model.