mirror of
https://github.com/capstone-engine/llvm-capstone.git
synced 2024-11-24 06:10:12 +00:00
[SanitizerBinaryMetadata] Support ignore list
For large projects it will be required to opt out entire subdirectories. In the absence of fine-grained control over the flags passed via the build system, introduce -fexperimental-sanitize-metadata-ignorelist=. The format is identical to other sanitizer ignore lists, and its effect will be to simply not instrument either functions or entire modules based on the rules in the ignore list file. Reviewed By: dvyukov Differential Revision: https://reviews.llvm.org/D143664
This commit is contained in:
parent
c340956e4e
commit
421215b919
@ -417,6 +417,11 @@ public:
|
||||
/// coverage pass should actually not be instrumented.
|
||||
std::vector<std::string> SanitizeCoverageIgnorelistFiles;
|
||||
|
||||
/// Path to ignorelist file specifying which objects
|
||||
/// (files, functions) listed for instrumentation by sanitizer
|
||||
/// binary metadata pass should not be instrumented.
|
||||
std::vector<std::string> SanitizeMetadataIgnorelistFiles;
|
||||
|
||||
/// Name of the stack usage file (i.e., .su file) if user passes
|
||||
/// -fstack-usage. If empty, it can be implied that -fstack-usage is not
|
||||
/// passed on the command line.
|
||||
|
@ -215,6 +215,8 @@ def err_drv_malformed_sanitizer_coverage_allowlist : Error<
|
||||
"malformed sanitizer coverage allowlist: '%0'">;
|
||||
def err_drv_malformed_sanitizer_coverage_ignorelist : Error<
|
||||
"malformed sanitizer coverage ignorelist: '%0'">;
|
||||
def err_drv_malformed_sanitizer_metadata_ignorelist : Error<
|
||||
"malformed sanitizer metadata ignorelist: '%0'">;
|
||||
def err_drv_unsupported_static_ubsan_darwin : Error<
|
||||
"static UndefinedBehaviorSanitizer runtime is not supported on darwin">;
|
||||
def err_drv_duplicate_config : Error<
|
||||
|
@ -1756,6 +1756,10 @@ def fexperimental_sanitize_metadata_EQ : CommaJoined<["-"], "fexperimental-sanit
|
||||
def fno_experimental_sanitize_metadata_EQ : CommaJoined<["-"], "fno-experimental-sanitize-metadata=">,
|
||||
Group<f_Group>, Flags<[CoreOption]>,
|
||||
HelpText<"Disable emitting metadata for binary analysis sanitizers">;
|
||||
def fexperimental_sanitize_metadata_ignorelist_EQ : Joined<["-"], "fexperimental-sanitize-metadata-ignorelist=">,
|
||||
Group<f_Group>, Flags<[CoreOption]>,
|
||||
HelpText<"Disable sanitizer metadata for modules and functions that match the provided special case list">,
|
||||
MarshallingInfoStringVector<CodeGenOpts<"SanitizeMetadataIgnorelistFiles">>;
|
||||
def fsanitize_memory_track_origins_EQ : Joined<["-"], "fsanitize-memory-track-origins=">,
|
||||
Group<f_clang_Group>,
|
||||
HelpText<"Enable origins tracking in MemorySanitizer">,
|
||||
|
@ -30,6 +30,7 @@ class SanitizerArgs {
|
||||
std::vector<std::string> SystemIgnorelistFiles;
|
||||
std::vector<std::string> CoverageAllowlistFiles;
|
||||
std::vector<std::string> CoverageIgnorelistFiles;
|
||||
std::vector<std::string> BinaryMetadataIgnorelistFiles;
|
||||
int CoverageFeatures = 0;
|
||||
int BinaryMetadataFeatures = 0;
|
||||
int MsanTrackOrigins = 0;
|
||||
|
@ -665,7 +665,8 @@ static void addSanitizers(const Triple &TargetTriple,
|
||||
|
||||
if (CodeGenOpts.hasSanitizeBinaryMetadata()) {
|
||||
MPM.addPass(SanitizerBinaryMetadataPass(
|
||||
getSanitizerBinaryMetadataOptions(CodeGenOpts)));
|
||||
getSanitizerBinaryMetadataOptions(CodeGenOpts),
|
||||
CodeGenOpts.SanitizeMetadataIgnorelistFiles));
|
||||
}
|
||||
|
||||
auto MSanPass = [&](SanitizerMask Mask, bool CompileKernel) {
|
||||
|
@ -864,6 +864,16 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
|
||||
}
|
||||
}
|
||||
|
||||
// Parse -fsanitize-metadata-ignorelist option if enabled.
|
||||
if (BinaryMetadataFeatures) {
|
||||
parseSpecialCaseListArg(
|
||||
D, Args, BinaryMetadataIgnorelistFiles,
|
||||
options::OPT_fexperimental_sanitize_metadata_ignorelist_EQ,
|
||||
OptSpecifier(), // Cannot clear ignore list, only append.
|
||||
clang::diag::err_drv_malformed_sanitizer_metadata_ignorelist,
|
||||
DiagnoseErrors);
|
||||
}
|
||||
|
||||
SharedRuntime =
|
||||
Args.hasFlag(options::OPT_shared_libsan, options::OPT_static_libsan,
|
||||
TC.getTriple().isAndroid() || TC.getTriple().isOSFuchsia() ||
|
||||
@ -1141,6 +1151,9 @@ void SanitizerArgs::addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,
|
||||
CmdArgs.push_back(
|
||||
Args.MakeArgString("-fexperimental-sanitize-metadata=" + F.second));
|
||||
}
|
||||
addSpecialCaseListOpt(Args, CmdArgs,
|
||||
"-fexperimental-sanitize-metadata-ignorelist=",
|
||||
BinaryMetadataIgnorelistFiles);
|
||||
|
||||
if (TC.getTriple().isOSWindows() && needsUbsanRt()) {
|
||||
// Instruct the code generator to embed linker directives in the object file
|
||||
|
55
clang/test/CodeGen/sanitize-metadata-ignorelist.c
Normal file
55
clang/test/CodeGen/sanitize-metadata-ignorelist.c
Normal file
@ -0,0 +1,55 @@
|
||||
// RUN: %clang -O -fexperimental-sanitize-metadata=all -target x86_64-gnu-linux -x c -S -emit-llvm %s -o - | FileCheck %s --check-prefixes=ALLOW
|
||||
// RUN: echo "fun:foo" > %t.fun
|
||||
// RUN: %clang -O -fexperimental-sanitize-metadata=all -fexperimental-sanitize-metadata-ignorelist=%t.fun -target x86_64-gnu-linux -x c -S -emit-llvm %s -o - | FileCheck %s --check-prefixes=FUN
|
||||
// RUN: echo "src:%s" > %t.src
|
||||
// RUN: %clang -O -fexperimental-sanitize-metadata=all -fexperimental-sanitize-metadata-ignorelist=%t.src -target x86_64-gnu-linux -x c -S -emit-llvm %s -o - | FileCheck %s --check-prefixes=SRC
|
||||
|
||||
int y;
|
||||
|
||||
// ALLOW-LABEL: define {{[^@]+}}@foo
|
||||
// ALLOW-SAME: () local_unnamed_addr #[[ATTR0:[0-9]+]] !pcsections !5 {
|
||||
// ALLOW-NEXT: entry:
|
||||
// ALLOW-NEXT: [[TMP0:%.*]] = atomicrmw add ptr @y, i32 1 monotonic, align 4, !pcsections !7
|
||||
// ALLOW-NEXT: ret void
|
||||
//
|
||||
// FUN-LABEL: define {{[^@]+}}@foo
|
||||
// FUN-SAME: () local_unnamed_addr #[[ATTR0:[0-9]+]] {
|
||||
// FUN-NEXT: entry:
|
||||
// FUN-NEXT: [[TMP0:%.*]] = atomicrmw add ptr @y, i32 1 monotonic, align 4
|
||||
// FUN-NEXT: ret void
|
||||
//
|
||||
// SRC-LABEL: define {{[^@]+}}@foo
|
||||
// SRC-SAME: () local_unnamed_addr #[[ATTR0:[0-9]+]] {
|
||||
// SRC-NEXT: entry:
|
||||
// SRC-NEXT: [[TMP0:%.*]] = atomicrmw add ptr @y, i32 1 monotonic, align 4
|
||||
// SRC-NEXT: ret void
|
||||
//
|
||||
void foo() {
|
||||
__atomic_fetch_add(&y, 1, __ATOMIC_RELAXED);
|
||||
}
|
||||
|
||||
// ALLOW-LABEL: define {{[^@]+}}@bar
|
||||
// ALLOW-SAME: () local_unnamed_addr #[[ATTR0]] !pcsections !5 {
|
||||
// ALLOW-NEXT: entry:
|
||||
// ALLOW-NEXT: [[TMP0:%.*]] = atomicrmw add ptr @y, i32 2 monotonic, align 4, !pcsections !7
|
||||
// ALLOW-NEXT: ret void
|
||||
//
|
||||
// FUN-LABEL: define {{[^@]+}}@bar
|
||||
// FUN-SAME: () local_unnamed_addr #[[ATTR0]] !pcsections !5 {
|
||||
// FUN-NEXT: entry:
|
||||
// FUN-NEXT: [[TMP0:%.*]] = atomicrmw add ptr @y, i32 2 monotonic, align 4, !pcsections !7
|
||||
// FUN-NEXT: ret void
|
||||
//
|
||||
// SRC-LABEL: define {{[^@]+}}@bar
|
||||
// SRC-SAME: () local_unnamed_addr #[[ATTR0]] {
|
||||
// SRC-NEXT: entry:
|
||||
// SRC-NEXT: [[TMP0:%.*]] = atomicrmw add ptr @y, i32 2 monotonic, align 4
|
||||
// SRC-NEXT: ret void
|
||||
//
|
||||
void bar() {
|
||||
__atomic_fetch_add(&y, 2, __ATOMIC_RELAXED);
|
||||
}
|
||||
|
||||
// ALLOW: __sanitizer_metadata_covered.module_ctor
|
||||
// FUN: __sanitizer_metadata_covered.module_ctor
|
||||
// SRC-NOT: __sanitizer_metadata_covered.module_ctor
|
14
clang/test/Driver/fsanitize-metadata-ignorelist.c
Normal file
14
clang/test/Driver/fsanitize-metadata-ignorelist.c
Normal file
@ -0,0 +1,14 @@
|
||||
// Verify Driver passes on -fsanitize-metadata-ignorelist.
|
||||
|
||||
// RUN: echo "fun:foo" > %t.1
|
||||
// RUN: echo "fun:bar" > %t.2
|
||||
|
||||
// RUN: %clang -target x86_64-linux-gnu -fexperimental-sanitize-metadata=all -fexperimental-sanitize-metadata-ignorelist=%t.1 -fexperimental-sanitize-metadata-ignorelist=%t.2 %s -### 2>&1 | FileCheck %s
|
||||
// RUN: %clang -target aarch64-linux-gnu -fexperimental-sanitize-metadata=atomics -fexperimental-sanitize-metadata-ignorelist=%t.1 -fexperimental-sanitize-metadata-ignorelist=%t.2 %s -### 2>&1 | FileCheck %s
|
||||
// CHECK: "-fexperimental-sanitize-metadata-ignorelist={{.*}}.1" "-fexperimental-sanitize-metadata-ignorelist={{.*}}.2"
|
||||
|
||||
// Verify -fsanitize-metadata-ignorelist flag not passed if there is no -fsanitize-metadata flag.
|
||||
// RUN: %clang -target x86_64-linux-gnu -fexperimental-sanitize-metadata-ignorelist=%t.1 -fexperimental-sanitize-metadata-ignorelist=%t.2 %s -### 2>&1 | FileCheck %s --check-prefix=NOSANMD
|
||||
// RUN: %clang -target aarch64-linux-gnu -fexperimental-sanitize-metadata-ignorelist=%t.1 -fexperimental-sanitize-metadata-ignorelist=%t.2 %s -### 2>&1 | FileCheck %s --check-prefix=NOSANMD
|
||||
// NOSANMD: warning: argument unused during compilation: '-fexperimental-sanitize-metadata-ignorelist
|
||||
// NOSANMD-NOT: "-fexperimental-sanitize-metadata-ignorelist
|
@ -12,6 +12,7 @@
|
||||
#ifndef LLVM_TRANSFORMS_INSTRUMENTATION_SANITIZERBINARYMETADATA_H
|
||||
#define LLVM_TRANSFORMS_INSTRUMENTATION_SANITIZERBINARYMETADATA_H
|
||||
|
||||
#include "llvm/ADT/ArrayRef.h"
|
||||
#include "llvm/IR/Function.h"
|
||||
#include "llvm/IR/Module.h"
|
||||
#include "llvm/IR/PassManager.h"
|
||||
@ -50,12 +51,14 @@ class SanitizerBinaryMetadataPass
|
||||
: public PassInfoMixin<SanitizerBinaryMetadataPass> {
|
||||
public:
|
||||
explicit SanitizerBinaryMetadataPass(
|
||||
SanitizerBinaryMetadataOptions Opts = {});
|
||||
SanitizerBinaryMetadataOptions Opts = {},
|
||||
ArrayRef<std::string> IgnorelistFiles = {});
|
||||
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
|
||||
static bool isRequired() { return true; }
|
||||
|
||||
private:
|
||||
const SanitizerBinaryMetadataOptions Options;
|
||||
const ArrayRef<std::string> IgnorelistFiles;
|
||||
};
|
||||
|
||||
} // namespace llvm
|
||||
|
@ -38,13 +38,16 @@
|
||||
#include "llvm/Support/Allocator.h"
|
||||
#include "llvm/Support/CommandLine.h"
|
||||
#include "llvm/Support/Debug.h"
|
||||
#include "llvm/Support/SpecialCaseList.h"
|
||||
#include "llvm/Support/StringSaver.h"
|
||||
#include "llvm/Support/VirtualFileSystem.h"
|
||||
#include "llvm/TargetParser/Triple.h"
|
||||
#include "llvm/Transforms/Instrumentation.h"
|
||||
#include "llvm/Transforms/Utils/ModuleUtils.h"
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
|
||||
using namespace llvm;
|
||||
|
||||
@ -121,9 +124,11 @@ transformOptionsFromCl(SanitizerBinaryMetadataOptions &&Opts) {
|
||||
|
||||
class SanitizerBinaryMetadata {
|
||||
public:
|
||||
SanitizerBinaryMetadata(Module &M, SanitizerBinaryMetadataOptions Opts)
|
||||
SanitizerBinaryMetadata(Module &M, SanitizerBinaryMetadataOptions Opts,
|
||||
std::unique_ptr<SpecialCaseList> Ignorelist)
|
||||
: Mod(M), Options(transformOptionsFromCl(std::move(Opts))),
|
||||
TargetTriple(M.getTargetTriple()), IRB(M.getContext()) {
|
||||
Ignorelist(std::move(Ignorelist)), TargetTriple(M.getTargetTriple()),
|
||||
IRB(M.getContext()) {
|
||||
// FIXME: Make it work with other formats.
|
||||
assert(TargetTriple.isOSBinFormatELF() && "ELF only");
|
||||
}
|
||||
@ -168,6 +173,7 @@ private:
|
||||
|
||||
Module &Mod;
|
||||
const SanitizerBinaryMetadataOptions Options;
|
||||
std::unique_ptr<SpecialCaseList> Ignorelist;
|
||||
const Triple TargetTriple;
|
||||
IRBuilder<> IRB;
|
||||
BumpPtrAllocator Alloc;
|
||||
@ -243,6 +249,8 @@ void SanitizerBinaryMetadata::runOn(Function &F, MetadataInfoSet &MIS) {
|
||||
return;
|
||||
if (F.hasFnAttribute(Attribute::DisableSanitizerInstrumentation))
|
||||
return;
|
||||
if (Ignorelist && Ignorelist->inSection("metadata", "fun", F.getName()))
|
||||
return;
|
||||
// Don't touch available_externally functions, their actual body is elsewhere.
|
||||
if (F.getLinkage() == GlobalValue::AvailableExternallyLinkage)
|
||||
return;
|
||||
@ -455,12 +463,20 @@ Twine SanitizerBinaryMetadata::getSectionEnd(StringRef SectionSuffix) {
|
||||
} // namespace
|
||||
|
||||
SanitizerBinaryMetadataPass::SanitizerBinaryMetadataPass(
|
||||
SanitizerBinaryMetadataOptions Opts)
|
||||
: Options(std::move(Opts)) {}
|
||||
SanitizerBinaryMetadataOptions Opts, ArrayRef<std::string> IgnorelistFiles)
|
||||
: Options(std::move(Opts)), IgnorelistFiles(std::move(IgnorelistFiles)) {}
|
||||
|
||||
PreservedAnalyses
|
||||
SanitizerBinaryMetadataPass::run(Module &M, AnalysisManager<Module> &AM) {
|
||||
SanitizerBinaryMetadata Pass(M, Options);
|
||||
std::unique_ptr<SpecialCaseList> Ignorelist;
|
||||
if (!IgnorelistFiles.empty()) {
|
||||
Ignorelist = SpecialCaseList::createOrDie(IgnorelistFiles,
|
||||
*vfs::getRealFileSystem());
|
||||
if (Ignorelist->inSection("metadata", "src", M.getSourceFileName()))
|
||||
return PreservedAnalyses::all();
|
||||
}
|
||||
|
||||
SanitizerBinaryMetadata Pass(M, Options, std::move(Ignorelist));
|
||||
if (Pass.run())
|
||||
return PreservedAnalyses::none();
|
||||
return PreservedAnalyses::all();
|
||||
|
Loading…
Reference in New Issue
Block a user