Index: lib/Transforms/Instrumentation/MemorySanitizer.cpp =================================================================== --- lib/Transforms/Instrumentation/MemorySanitizer.cpp +++ lib/Transforms/Instrumentation/MemorySanitizer.cpp @@ -234,6 +234,24 @@ cl::desc("Place MSan constructors in comdat sections"), cl::Hidden, cl::init(false)); +// These options allow to specify custom memory map parameters +// See MemoryMapParams for details. +static cl::opt ClAndMask("msan-and-mask", + cl::desc("Define custom MSan AndMask"), + cl::Hidden, cl::init(0)); + +static cl::opt ClXorMask("msan-xor-mask", + cl::desc("Define custom MSan XorMask"), + cl::Hidden, cl::init(0)); + +static cl::opt ClShadowBase("msan-shadow-base", + cl::desc("Define custom MSan ShadowBase"), + cl::Hidden, cl::init(0)); + +static cl::opt ClOriginBase("msan-origin-base", + cl::desc("Define custom MSan OriginBase"), + cl::Hidden, cl::init(0)); + static const char *const kMsanModuleCtorName = "msan.module_ctor"; static const char *const kMsanInitName = "__msan_init"; @@ -328,6 +346,15 @@ 0x100000000000, // OriginBase }; +// Custom platform derived from options +// At least one value must be defined for usage +static MemoryMapParams Custom_MemoryMapParams = { + 0, // AndMask + 0, // XorMask + 0, // ShadowBase + 0, // OriginBase +}; + static const PlatformMemoryMapParams Linux_X86_MemoryMapParams = { &Linux_I386_MemoryMapParams, &Linux_X86_64_MemoryMapParams, @@ -576,55 +603,70 @@ bool MemorySanitizer::doInitialization(Module &M) { auto &DL = M.getDataLayout(); - Triple TargetTriple(M.getTargetTriple()); - switch (TargetTriple.getOS()) { - case Triple::FreeBSD: - switch (TargetTriple.getArch()) { - case Triple::x86_64: - MapParams = FreeBSD_X86_MemoryMapParams.bits64; - break; - case Triple::x86: - MapParams = FreeBSD_X86_MemoryMapParams.bits32; - break; - default: - report_fatal_error("unsupported architecture"); - } - break; - case Triple::NetBSD: - switch (TargetTriple.getArch()) { - case Triple::x86_64: - MapParams = NetBSD_X86_MemoryMapParams.bits64; - break; - default: - report_fatal_error("unsupported architecture"); - } - break; - case Triple::Linux: - switch (TargetTriple.getArch()) { - case Triple::x86_64: - MapParams = Linux_X86_MemoryMapParams.bits64; - break; - case Triple::x86: - MapParams = Linux_X86_MemoryMapParams.bits32; - break; - case Triple::mips64: - case Triple::mips64el: - MapParams = Linux_MIPS_MemoryMapParams.bits64; - break; - case Triple::ppc64: - case Triple::ppc64le: - MapParams = Linux_PowerPC_MemoryMapParams.bits64; - break; - case Triple::aarch64: - case Triple::aarch64_be: - MapParams = Linux_ARM_MemoryMapParams.bits64; - break; - default: - report_fatal_error("unsupported architecture"); - } - break; - default: - report_fatal_error("unsupported operating system"); + bool ShadowPassed = ClShadowBase.getNumOccurrences() > 0; + bool OriginPassed = ClOriginBase.getNumOccurrences() > 0; + // Check the overrides first + if (ShadowPassed || OriginPassed) { + if (ClAndMask.getNumOccurrences() > 0) + Custom_MemoryMapParams.AndMask = ClAndMask; + if (ClXorMask.getNumOccurrences() > 0) + Custom_MemoryMapParams.XorMask = ClXorMask; + if (ShadowPassed) + Custom_MemoryMapParams.ShadowBase = ClShadowBase; + if (OriginPassed) + Custom_MemoryMapParams.OriginBase = ClOriginBase; + MapParams = &Custom_MemoryMapParams; + } else { + Triple TargetTriple(M.getTargetTriple()); + switch (TargetTriple.getOS()) { + case Triple::FreeBSD: + switch (TargetTriple.getArch()) { + case Triple::x86_64: + MapParams = FreeBSD_X86_MemoryMapParams.bits64; + break; + case Triple::x86: + MapParams = FreeBSD_X86_MemoryMapParams.bits32; + break; + default: + report_fatal_error("unsupported architecture"); + } + break; + case Triple::NetBSD: + switch (TargetTriple.getArch()) { + case Triple::x86_64: + MapParams = NetBSD_X86_MemoryMapParams.bits64; + break; + default: + report_fatal_error("unsupported architecture"); + } + break; + case Triple::Linux: + switch (TargetTriple.getArch()) { + case Triple::x86_64: + MapParams = Linux_X86_MemoryMapParams.bits64; + break; + case Triple::x86: + MapParams = Linux_X86_MemoryMapParams.bits32; + break; + case Triple::mips64: + case Triple::mips64el: + MapParams = Linux_MIPS_MemoryMapParams.bits64; + break; + case Triple::ppc64: + case Triple::ppc64le: + MapParams = Linux_PowerPC_MemoryMapParams.bits64; + break; + case Triple::aarch64: + case Triple::aarch64_be: + MapParams = Linux_ARM_MemoryMapParams.bits64; + break; + default: + report_fatal_error("unsupported architecture"); + } + break; + default: + report_fatal_error("unsupported operating system"); + } } C = &(M.getContext()); Index: test/Instrumentation/MemorySanitizer/manual-shadow.ll =================================================================== --- test/Instrumentation/MemorySanitizer/manual-shadow.ll +++ test/Instrumentation/MemorySanitizer/manual-shadow.ll @@ -0,0 +1,34 @@ +; Test that the msan layout customization options work as expected +; +; RUN: opt < %s -msan -msan -msan-shadow-base 0xdeadbeef -S | FileCheck --check-prefix=CHECK-BASE %s +; RUN: opt < %s -msan -msan -msan-shadow-base 0xdeadbeef -msan -msan-and-mask 0xffff0000 -S | FileCheck --check-prefix=CHECK-AND %s +; RUN: opt < %s -msan -msan -msan-shadow-base 0xdeadbeef -msan -msan-xor-mask 0x0000beef -S | FileCheck --check-prefix=CHECK-XOR %s +; RUN: opt < %s -msan -msan -msan-shadow-base 0xdeadbeef -msan -msan-xor-mask 0x0000beef -msan-and-mask 0xffff0000 -S | FileCheck --check-prefix=CHECK-XOR-AND %s + + +target triple = "x86_64-unknown-linux-gnu" + +define i32 @read_value(i32* %a) sanitize_memory { +entry: + %tmp1 = load i32, i32* %a, align 4 + ret i32 %tmp1 +} +; CHECK-BASE-LABEL: @read_value +; CHECK-BASE-NOT: ret +; CHECK-BASE: load{{.*}}3735928559 +; CHECK-BASE: ret + +; CHECK-AND-LABEL: @read_value +; CHECK-AND-NOT: ret +; CHECK-AND: load{{.*}}and{{.*}}-4294901761{{.*}}3735928559 +; CHECK-AND: ret + +; CHECK-XOR-LABEL: @read_value +; CHECK-XOR-NOT: ret +; CHECK-XOR: load{{.*}}xor{{.*}}48879{{.*}}3735928559 +; CHECK-XOR: ret + +; CHECK-XOR-AND-LABEL: @read_value +; CHECK-XOR-AND-NOT: ret +; CHECK-XOR-AND: load{{.*}}xor{{.*}}and{{.*}}-4294901761{{.*}}48879{{.*}}3735928559 +; CHECK-XOR-AND: ret