1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
//===-- cl_options_sanitizers.cpp -------------------------------*- C++ -*-===//
//
// LDC – the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//
//
// Creates and handles the -fsanitize=... and -fsanitize-coverage=...
// commandline options.
//
//===----------------------------------------------------------------------===//
#include "driver/cl_options_sanitizers.h"
#include "dmd/errors.h"
#include "dmd/declaration.h"
#include "dmd/dsymbol.h"
#include "dmd/mangle.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/SpecialCaseList.h"
#if LDC_LLVM_VER >= 1300
#include "llvm/Support/VirtualFileSystem.h"
#endif
namespace {
using namespace opts;
cl::list<std::string> fSanitize(
"fsanitize", cl::CommaSeparated,
cl::desc("Turn on runtime checks for various forms of undefined or "
"suspicious behavior."),
cl::value_desc("checks"));
cl::list<std::string> fSanitizeBlacklist(
"fsanitize-blacklist", cl::CommaSeparated,
cl::desc("Add <file> to the blacklist files for the sanitizers."),
cl::value_desc("file"));
std::unique_ptr<llvm::SpecialCaseList> sanitizerBlacklist;
cl::list<std::string> fSanitizeCoverage(
"fsanitize-coverage", cl::CommaSeparated,
cl::desc("Specify the type of coverage instrumentation for -fsanitize"),
cl::value_desc("type"));
llvm::SanitizerCoverageOptions sanitizerCoverageOptions;
SanitizerBits parseFSanitizeCmdlineParameter() {
SanitizerBits retval = 0;
for (const auto &name : fSanitize) {
SanitizerCheck check = parseSanitizerName(name, [&name] {
error(Loc(), "Unrecognized -fsanitize value '%s'.", name.c_str());
});
retval |= SanitizerBits(check);
}
return retval;
}
void parseFSanitizeCoverageParameter(llvm::StringRef name,
llvm::SanitizerCoverageOptions &opts) {
if (name == "func") {
opts.CoverageType = std::max(opts.CoverageType,
llvm::SanitizerCoverageOptions::SCK_Function);
} else if (name == "bb") {
opts.CoverageType =
std::max(opts.CoverageType, llvm::SanitizerCoverageOptions::SCK_BB);
} else if (name == "edge") {
opts.CoverageType =
std::max(opts.CoverageType, llvm::SanitizerCoverageOptions::SCK_Edge);
} else if (name == "indirect-calls") {
opts.IndirectCalls = true;
} else if (name == "trace-bb") {
opts.TraceBB = true;
}
else if (name == "trace-cmp") {
opts.TraceCmp = true;
}
else if (name == "trace-div") {
opts.TraceDiv = true;
}
else if (name == "trace-gep") {
opts.TraceGep = true;
}
else if (name == "8bit-counters") {
opts.Use8bitCounters = true;
}
else if (name == "trace-pc") {
opts.TracePC = true;
}
else if (name == "trace-pc-guard") {
opts.TracePCGuard = true;
}
else if (name == "inline-8bit-counters") {
opts.Inline8bitCounters = true;
}
else if (name == "no-prune") {
opts.NoPrune = true;
}
else if (name == "pc-table") {
opts.PCTable = true;
}
else {
error(Loc(), "Unrecognized -fsanitize-coverage option '%s'.", name.str().c_str());
}
}
void parseFSanitizeCoverageCmdlineParameter(llvm::SanitizerCoverageOptions &opts) {
for (const auto &name : fSanitizeCoverage) {
// Enable CoverageSanitizer when one or more -fsanitize-coverage parameters are passed
enabledSanitizers |= CoverageSanitizer;
parseFSanitizeCoverageParameter(name, opts);
}
}
} // anonymous namespace
namespace opts {
SanitizerBits enabledSanitizers = 0;
// Parse sanitizer name passed on commandline and return the corresponding
// sanitizer bits.
SanitizerCheck parseSanitizerName(llvm::StringRef name,
std::function<void()> actionUponError) {
SanitizerCheck parsedValue = llvm::StringSwitch<SanitizerCheck>(name)
.Case("address", AddressSanitizer)
.Case("fuzzer", FuzzSanitizer)
.Case("leak", LeakSanitizer)
.Case("memory", MemorySanitizer)
.Case("thread", ThreadSanitizer)
.Default(NoneSanitizer);
if (parsedValue == NoneSanitizer) {
actionUponError();
}
return parsedValue;
}
void initializeSanitizerOptionsFromCmdline()
{
enabledSanitizers |= parseFSanitizeCmdlineParameter();
auto &sancovOpts = sanitizerCoverageOptions;
// The Fuzz sanitizer implies -fsanitize-coverage=inline-8bit-counters,indirect-calls,trace-cmp,pc-table
if (isSanitizerEnabled(FuzzSanitizer)) {
enabledSanitizers |= CoverageSanitizer;
sancovOpts.Inline8bitCounters = true;
sancovOpts.PCTable = true;
sancovOpts.IndirectCalls = true;
sancovOpts.TraceCmp = true;
}
parseFSanitizeCoverageCmdlineParameter(sancovOpts);
// trace-pc/trace-pc-guard/inline-8bit-counters without specifying the
// insertion type implies edge
if ((sancovOpts.CoverageType == llvm::SanitizerCoverageOptions::SCK_None) &&
(sancovOpts.TracePC || sancovOpts.TracePCGuard ||
sancovOpts.Inline8bitCounters)) {
sancovOpts.CoverageType = llvm::SanitizerCoverageOptions::SCK_Edge;
}
if (isAnySanitizerEnabled() && !fSanitizeBlacklist.empty()) {
std::string loadError;
sanitizerBlacklist =
llvm::SpecialCaseList::create(fSanitizeBlacklist,
#if LDC_LLVM_VER >= 1000
*llvm::vfs::getRealFileSystem(),
#endif
loadError);
if (!sanitizerBlacklist)
error(Loc(), "-fsanitize-blacklist error: %s", loadError.c_str());
}
}
llvm::SanitizerCoverageOptions getSanitizerCoverageOptions() {
return sanitizerCoverageOptions;
}
// Output to `hash_os` all optimization settings that influence object code
// output and that are not observable in the IR before running LLVM passes. This
// is used to calculate the hash use for caching that uniquely identifies the
// object file output.
void outputSanitizerSettings(llvm::raw_ostream &hash_os) {
hash_os << SanitizerBits(enabledSanitizers);
hash_os.write(reinterpret_cast<char *>(&sanitizerCoverageOptions),
sizeof(sanitizerCoverageOptions));
}
bool functionIsInSanitizerBlacklist(FuncDeclaration *funcDecl) {
if (!sanitizerBlacklist)
return false;
auto funcName = mangleExact(funcDecl);
auto fileName = funcDecl->loc.filename;
// TODO: LLVM supports sections (e.g. "[address]") in the blacklist file to
// only blacklist a function for a particular sanitizer. We could make use of
// that too.
return sanitizerBlacklist->inSection(/*Section=*/"", "fun", funcName) ||
sanitizerBlacklist->inSection(/*Section=*/"", "src", fileName);
}
} // namespace opts
|