Skip to content
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

Add @ldc.attributes.noSanitize UDA to selectively disable sanitizer instrumentation of functions #3889

Merged
merged 1 commit into from
Jan 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion dmd/id.d
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,8 @@ immutable Msgtable[] msgtable =
{ "udaDynamicCompileConst", "_dynamicCompileConst" },
{ "udaDynamicCompileEmit", "_dynamicCompileEmit" },
{ "udaHidden", "_hidden" },

{ "udaNoSanitize", "noSanitize" },

// IN_LLVM: DCompute specific types and functionss
{ "dcompute" },
{ "dcPointer", "Pointer" },
Expand Down
1 change: 1 addition & 0 deletions dmd/id.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ struct Id
static Identifier *udaDynamicCompileConst;
static Identifier *udaDynamicCompileEmit;
static Identifier *udaHidden;
static Identifier *udaNoSanitize;
static Identifier *io;
#endif
};
40 changes: 21 additions & 19 deletions driver/cl_options_sanitizers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,12 @@ cl::list<std::string> fSanitizeCoverage(

llvm::SanitizerCoverageOptions sanitizerCoverageOptions;

// Parse sanitizer name passed on commandline and return the corresponding
// sanitizer bits.
SanitizerCheck parseSanitizerName(llvm::StringRef name) {
SanitizerCheck parsedValue =
llvm::StringSwitch<SanitizerCheck>(name)
.Case("address", AddressSanitizer)
.Case("fuzzer", FuzzSanitizer)
.Case("memory", MemorySanitizer)
.Case("thread", ThreadSanitizer)
.Default(NoneSanitizer);

if (parsedValue == NoneSanitizer) {
error(Loc(), "Unrecognized -fsanitize value '%s'.", name.str().c_str());
}

return parsedValue;
}

SanitizerBits parseFSanitizeCmdlineParameter() {
SanitizerBits retval = 0;
for (const auto &name : fSanitize) {
SanitizerCheck check = parseSanitizerName(name);
SanitizerCheck check = parseSanitizerName(name, [&name] {
error(Loc(), "Unrecognized -fsanitize value '%s'.", name.c_str());
});
retval |= SanitizerBits(check);
}
return retval;
Expand Down Expand Up @@ -139,6 +123,24 @@ 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("memory", MemorySanitizer)
.Case("thread", ThreadSanitizer)
.Default(NoneSanitizer);

if (parsedValue == NoneSanitizer) {
actionUponError();
}

return parsedValue;
}

void initializeSanitizerOptionsFromCmdline()
{
enabledSanitizers |= parseFSanitizeCmdlineParameter();
Expand Down
6 changes: 5 additions & 1 deletion driver/cl_options_sanitizers.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
class FuncDeclaration;
namespace llvm {
class raw_ostream;
class StringRef;
}

namespace opts {
Expand All @@ -36,10 +37,13 @@ enum SanitizerCheck : SanitizerBits {
extern SanitizerBits enabledSanitizers;

inline bool isAnySanitizerEnabled() { return enabledSanitizers; }
inline bool isSanitizerEnabled(SanitizerCheck san) {
inline bool isSanitizerEnabled(SanitizerBits san) {
return enabledSanitizers & san;
}

SanitizerCheck parseSanitizerName(llvm::StringRef name,
std::function<void()> actionUponError);

void initializeSanitizerOptionsFromCmdline();

llvm::SanitizerCoverageOptions getSanitizerCoverageOptions();
Expand Down
9 changes: 6 additions & 3 deletions gen/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1202,16 +1202,19 @@ void DtoDefineFunction(FuncDeclaration *fd, bool linkageAvailableExternally) {
}
if (opts::isAnySanitizerEnabled() &&
!opts::functionIsInSanitizerBlacklist(fd)) {
// Get the @noSanitize mask
auto noSanitizeMask = getMaskFromNoSanitizeUDA(*fd);

// Set the required sanitizer attribute.
if (opts::isSanitizerEnabled(opts::AddressSanitizer)) {
if (opts::isSanitizerEnabled(opts::AddressSanitizer & noSanitizeMask)) {
func->addFnAttr(LLAttribute::SanitizeAddress);
}

if (opts::isSanitizerEnabled(opts::MemorySanitizer)) {
if (opts::isSanitizerEnabled(opts::MemorySanitizer & noSanitizeMask)) {
func->addFnAttr(LLAttribute::SanitizeMemory);
}

if (opts::isSanitizerEnabled(opts::ThreadSanitizer)) {
if (opts::isSanitizerEnabled(opts::ThreadSanitizer & noSanitizeMask)) {
func->addFnAttr(LLAttribute::SanitizeThread);
}
}
Expand Down
49 changes: 49 additions & 0 deletions gen/uda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "dmd/id.h"
#include "dmd/identifier.h"
#include "dmd/module.h"
#include "driver/cl_options_sanitizers.h"
#include "gen/irstate.h"
#include "gen/llvm.h"
#include "gen/llvmhelpers.h"
Expand Down Expand Up @@ -123,6 +124,32 @@ StructLiteralExp *getMagicAttribute(Dsymbol *sym, const Identifier *id,
return nullptr;
}

/// Calls `action` for each magic attribute with identifier `id` from
/// the ldc magic module with identifier `from` (attributes or dcompute)
/// applied to `sym`.
void callForEachMagicAttribute(Dsymbol &sym, const Identifier *id,
const Identifier *from,
std::function<void(StructLiteralExp *)> action) {
if (!sym.userAttribDecl)
return;

// Loop over all UDAs and call `action` if a match was found.
Expressions *attrs = sym.userAttribDecl->getAttributes();
expandTuples(attrs);
for (auto &attr : *attrs) {
if (attr->op != TOKstructliteral)
continue;
auto sle = static_cast<StructLiteralExp *>(attr);
if (!isFromMagicModule(sle, from))
continue;
if (id != sle->sd->ident)
continue;

// Match found!
action(sle);
}
}

sinteger_t getIntElem(StructLiteralExp *sle, size_t idx) {
auto arg = (*sle->elements)[idx];
return arg->toInteger();
Expand Down Expand Up @@ -530,3 +557,25 @@ bool hasKernelAttr(Dsymbol *sym) {

return true;
}

/// Creates a mask (for &) of @ldc.attributes.noSanitize UDA applied to the
/// function.
/// If a bit is set in the mask, then the sanitizer is enabled.
/// If a bit is not set in the mask, then the sanitizer is explicitly disabled
/// by @noSanitize.
unsigned getMaskFromNoSanitizeUDA(FuncDeclaration &fd) {
opts::SanitizerBits inverse_mask = opts::NoneSanitizer;

callForEachMagicAttribute(fd, Id::udaNoSanitize, Id::attributes,
[&inverse_mask](StructLiteralExp *sle) {
checkStructElems(sle, {Type::tstring});
auto name = getFirstElemString(sle);
inverse_mask |= opts::parseSanitizerName(name, [&] {
sle->warning(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want this to be an error, or a warning?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose this to be a warning, for easier backward/forward compatibility.

"Unrecognized sanitizer name '%s' for `@ldc.attributes.noSanitize`.",
name.str().c_str());
});
});

return ~inverse_mask;
}
2 changes: 2 additions & 0 deletions gen/uda.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ enum class DComputeCompileFor : int
hostAndDevice = 2
};
extern "C" DComputeCompileFor hasComputeAttr(Dsymbol *sym);

unsigned getMaskFromNoSanitizeUDA(FuncDeclaration &fd);
2 changes: 1 addition & 1 deletion runtime/druntime
Submodule druntime updated 1 files
+10 −0 src/ldc/attributes.d
43 changes: 43 additions & 0 deletions tests/sanitizers/attr_nosanitize.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Test ldc.attributes.noSanitize UDA

// RUN: %ldc -c -output-ll -fsanitize=address,thread -of=%t.normal.ll %s -d-version=NORMAL && FileCheck %s --check-prefix=NORMAL < %t.normal.ll
// RUN: %ldc -c -output-ll -fsanitize=address,thread -of=%t.nosanitize.ll %s -d-version=NOSANITIZE && FileCheck %s --check-prefix=NOSANITIZE < %t.nosanitize.ll

// RUN: %ldc -wi -c -fsanitize=address -d-version=WARNING %s 2>&1 | FileCheck %s --check-prefix=WARNING

import ldc.attributes;

extern (C): // For easier name mangling

version (NORMAL)
{
// NORMAL: sanitize_address
// NORMAL: sanitize_thread
void foo()
{
}
}
else version (NOSANITIZE)
{
// NOSANITIZE-NOT: sanitize_address
// NOSANITIZE: sanitize_thread
// NOSANITIZE-NOT: sanitize_address
@noSanitize("address")
void foo_noaddress()
{
}

@noSanitize("thread")
@noSanitize("address")
void foo_nothread_noaddress()
{
}
}
else version (WARNING)
{
// WARNING: attr_nosanitize.d([[@LINE+1]]){{.*}} Unrecognized sanitizer name 'invalid_name'
@noSanitize("invalid_name")
void foo_error()
{
}
}