From 2597182a4663fbf2797bc9782fcfc934ed159ba5 Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Wed, 29 May 2019 18:31:50 +0000 Subject: [PATCH] [WebAssembly] Support VPtr sanitizer for Emscripten Summary: After https://github.com/emscripten-core/emscripten/pull/8651, Emscripten supports the full UBSan runtime. This includes the VPtr sanitizer. This diff allows clang to generate code that uses the VPtr sanitizer for Emscripten. Patch by Guanzhong Chen Reviewers: tlively, aheejin Reviewed By: aheejin Subscribers: dschuff, sbc100, jgravelle-google, sunfish, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D62559 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@362004 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Driver/ToolChains/WebAssembly.cpp | 8 ++++++++ lib/Driver/ToolChains/WebAssembly.h | 1 + test/CodeGenCXX/wasm-sanitize-vptr.cpp | 20 ++++++++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 test/CodeGenCXX/wasm-sanitize-vptr.cpp diff --git a/lib/Driver/ToolChains/WebAssembly.cpp b/lib/Driver/ToolChains/WebAssembly.cpp index af6e856b9f..7fffbbe6bf 100644 --- a/lib/Driver/ToolChains/WebAssembly.cpp +++ b/lib/Driver/ToolChains/WebAssembly.cpp @@ -208,6 +208,14 @@ void WebAssembly::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, } } +SanitizerMask WebAssembly::getSupportedSanitizers() const { + SanitizerMask Res = ToolChain::getSupportedSanitizers(); + if (getTriple().isOSEmscripten()) { + Res |= SanitizerKind::Vptr; + } + return Res; +} + Tool *WebAssembly::buildLinker() const { return new tools::wasm::Linker(*this); } diff --git a/lib/Driver/ToolChains/WebAssembly.h b/lib/Driver/ToolChains/WebAssembly.h index 8e4e545c98..67d5fce845 100644 --- a/lib/Driver/ToolChains/WebAssembly.h +++ b/lib/Driver/ToolChains/WebAssembly.h @@ -66,6 +66,7 @@ private: llvm::opt::ArgStringList &CC1Args) const override; void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const override; + SanitizerMask getSupportedSanitizers() const override; const char *getDefaultLinker() const override { return "wasm-ld"; } diff --git a/test/CodeGenCXX/wasm-sanitize-vptr.cpp b/test/CodeGenCXX/wasm-sanitize-vptr.cpp new file mode 100644 index 0000000000..2a9055aea1 --- /dev/null +++ b/test/CodeGenCXX/wasm-sanitize-vptr.cpp @@ -0,0 +1,20 @@ +// RUN: %clang_cc1 -std=c++11 -fsanitize=vptr -emit-llvm %s -o - -triple wasm32-unknown-emscripten | FileCheck %s + +struct S { + virtual ~S() {} + int a; +}; + +struct T : S { + int b; +}; + +// CHECK-LABEL: @_Z15bad_static_castv +void bad_static_cast() { + S s; + // CHECK: br i1 %[[NONNULL:.*]], label %[[CONT:.*]], label %[[MISS:.*]], !prof + // CHECK: [[MISS]]: + // CHECK: call void @__ubsan_handle_dynamic_type_cache_miss_abort + // CHECK: [[CONT]]: + T &r = static_cast(s); +} -- 2.40.0