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

Added address sanitization feature for translating ML models to LLVM modules #4676

Merged
merged 18 commits into from
Feb 11, 2021
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
11 changes: 11 additions & 0 deletions iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,15 @@ class LLVMAOTTargetBackend final : public TargetBackend {
"dialect to the native llvm::Module";
}

switch (options_.sanitizerKind) {
case SanitizerKind::kNone:
break;
case SanitizerKind::kAddress: {
for (auto &function : llvmModule->getFunctionList())
function.addFnAttr(llvm::Attribute::SanitizeAddress);
} break;
}

// Try to grab a linker tool based on the options (and target environment).
auto linkerTool = LinkerTool::getForTarget(targetTriple, options_);
if (!linkerTool) {
Expand Down Expand Up @@ -340,6 +349,8 @@ class LLVMAOTTargetBackend final : public TargetBackend {
builder, debugDatabaseFilenameRef);
iree_DyLibExecutableDef_debug_database_embedded_add(builder,
debugDatabaseRef);
iree_DyLibExecutableDef_sanitized_kind_add(
builder, static_cast<unsigned char>(options_.sanitizerKind));
iree_DyLibExecutableDef_end_as_root(builder);

// Add the binary data to the target executable.
Expand Down
26 changes: 26 additions & 0 deletions iree/compiler/Dialect/HAL/Target/LLVM/LLVMIRPasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "llvm/Support/Host.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Instrumentation/AddressSanitizer.h"

namespace mlir {
namespace iree_compiler {
Expand Down Expand Up @@ -87,6 +88,31 @@ LogicalResult runLLVMIRPasses(const LLVMTargetOptions &options,
passBuilder.registerLoopAnalyses(loopAnalysisManager);
passBuilder.crossRegisterProxies(loopAnalysisManager, functionAnalysisManager,
cGSCCAnalysisManager, moduleAnalysisManager);

switch (options.sanitizerKind) {
case SanitizerKind::kNone:
break;
case SanitizerKind::kAddress: {
passBuilder.registerOptimizerLastEPCallback(
[](llvm::ModulePassManager &modulePassManager,
llvm::PassBuilder::OptimizationLevel Level) {
bool compileKernel = false;
bool recover = false;
bool useAfterScope = true;
bool moduleUseAfterScope = false;
bool useOdrIndicator = false;
modulePassManager.addPass(
llvm::RequireAnalysisPass<llvm::ASanGlobalsMetadataAnalysis,
llvm::Module>());
modulePassManager.addPass(llvm::ModuleAddressSanitizerPass(
compileKernel, recover, moduleUseAfterScope, useOdrIndicator));
modulePassManager.addPass(
createModuleToFunctionPassAdaptor(llvm::AddressSanitizerPass(
compileKernel, recover, useAfterScope)));
});
} break;
}

if (options.optLevel != llvm::PassBuilder::OptimizationLevel::O0) {
llvm::ModulePassManager modulePassManager;
modulePassManager =
Expand Down
7 changes: 7 additions & 0 deletions iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ LLVMTargetOptions getLLVMTargetOptionsFromFlags() {
llvmTargetOptions.targetCPUFeatures = clTargetCPUFeatures;
}

static llvm::cl::opt<SanitizerKind> clSanitizerKind(
"iree-llvm-sanitize", llvm::cl::desc("Apply LLVM sanitize feature"),
llvm::cl::init(SanitizerKind::kNone),
llvm::cl::values(clEnumValN(SanitizerKind::kAddress, "address",
"Address sanitizer support")));
llvmTargetOptions.sanitizerKind = clSanitizerKind;

hanhanW marked this conversation as resolved.
Show resolved Hide resolved
static llvm::cl::opt<std::string> clTargetABI(
"iree-llvm-target-abi",
llvm::cl::desc("LLVM target machine ABI; specify for -mabi"),
Expand Down
10 changes: 10 additions & 0 deletions iree/compiler/Dialect/HAL/Target/LLVM/LLVMTargetOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ namespace iree_compiler {
namespace IREE {
namespace HAL {

// Defines kinds of Sanitizer
// The order in enum class should be same as one in flat buffer schema
enum class SanitizerKind {
kNone = 0,
kAddress,
};

struct LLVMTargetOptions {
// Target machine configuration.
std::string targetTriple;
Expand All @@ -39,6 +46,9 @@ struct LLVMTargetOptions {
// and benchmarking
bool debugSymbols = true;

// Sanitizer Kind for CPU Kernels
SanitizerKind sanitizerKind = SanitizerKind::kNone;

// Link any required runtime libraries into the produced binaries statically.
// This increases resulting binary size but enables the binaries to be used on
// any machine without requiring matching system libraries to be installed.
Expand Down
15 changes: 15 additions & 0 deletions iree/hal/local/loaders/legacy_library_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "iree/base/dynamic_library.h"
#include "iree/base/internal/file_io.h"
#include "iree/base/internal/file_path.h"
#include "iree/base/target_platform.h"
#include "iree/base/tracing.h"
#include "iree/hal/local/local_executable.h"

Expand Down Expand Up @@ -81,6 +82,20 @@ static iree_status_t iree_hal_dylib_executable_flatbuffer_verify(
"executable library_embedded is missing/empty");
}

switch (iree_DyLibExecutableDef_sanitized_kind_get(executable_def)) {
case iree_SanitizerKind_None:
break;
case iree_SanitizerKind_Address: {
if (!IREE_SANITIZER_ADDRESS) {
return iree_make_status(
IREE_STATUS_UNAVAILABLE,
hanhanW marked this conversation as resolved.
Show resolved Hide resolved
"Dynamic library executable is compiled with ASAN support, but "
"this host application failed to enable ASAN to load this "
"executable");
}
} break;
}
hanhanW marked this conversation as resolved.
Show resolved Hide resolved

return iree_ok_status();
}

Expand Down
11 changes: 10 additions & 1 deletion iree/schemas/dylib_executable_def.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,22 @@

namespace iree;

// Define kinds of sanitizers (the order in enum should be same as enum in LLVM
// TargetOptions)
enum SanitizerKind : ubyte {
None = 0,
Address
}

// 'Dynamic Library (dylib) Executable'.

file_identifier "DLIB";
file_extension "dlib";

// Dynamic library (.so/.dll/.dylib) executable module.
table DyLibExecutableDef {
// A map of entry points to string names with the same order as in the executable op.
// A map of entry points to string names with the same order as in the
// executable op.
entry_points:[string];
// An embedded (as opposed to external) dynamic library file.
// TODO(scotttodd): List of embedded files?
Expand All @@ -30,6 +38,7 @@ table DyLibExecutableDef {

debug_database_filename:string;
debug_database_embedded:[ubyte];
sanitized_kind:SanitizerKind = None;

// TODO(scotttodd): Relative file path from this flatbuffer file
}
Expand Down