Skip to content

Instantly share code, notes, and snippets.

@crabtw
Last active June 6, 2023 06:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crabtw/5593628 to your computer and use it in GitHub Desktop.
Save crabtw/5593628 to your computer and use it in GitHub Desktop.
cross compile address sanitizer
Index: lib/Target/Mips/MipsISelLowering.cpp
===================================================================
--- lib/Target/Mips/MipsISelLowering.cpp (revision 182727)
+++ lib/Target/Mips/MipsISelLowering.cpp (working copy)
@@ -1073,7 +1073,14 @@
BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
.addReg(Ptr).addReg(MaskLSB2);
BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
- BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
+ if (Subtarget->isLittle()) {
+ BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
+ } else {
+ unsigned Off = RegInfo.createVirtualRegister(RC);
+ BuildMI(BB, DL, TII->get(Mips::XORi), Off)
+ .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
+ BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
+ }
BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
.addReg(Mips::ZERO).addImm(MaskImm);
BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
@@ -1316,7 +1323,14 @@
BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
.addReg(Ptr).addReg(MaskLSB2);
BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
- BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
+ if (Subtarget->isLittle()) {
+ BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
+ } else {
+ unsigned Off = RegInfo.createVirtualRegister(RC);
+ BuildMI(BB, DL, TII->get(Mips::XORi), Off)
+ .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
+ BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
+ }
BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
.addReg(Mips::ZERO).addImm(MaskImm);
BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Index: lib/Transforms/Instrumentation/AddressSanitizer.cpp
===================================================================
--- lib/Transforms/Instrumentation/AddressSanitizer.cpp (revision 182727)
+++ lib/Transforms/Instrumentation/AddressSanitizer.cpp (working copy)
@@ -39,6 +39,7 @@
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/Debug.h"
+#include "llvm/Support/Endian.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/system_error.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
@@ -55,6 +56,7 @@
static const uint64_t kDefaultShadowOffset64 = 1ULL << 44;
static const uint64_t kDefaultShort64bitShadowOffset = 0x7FFF8000; // < 2G.
static const uint64_t kPPC64_ShadowOffset64 = 1ULL << 41;
+static const uint64_t kMIPS32_ShadowOffset32 = 0x0aaa8000;
static const size_t kMaxStackMallocSize = 1 << 16; // 64K
static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
@@ -207,6 +209,7 @@
bool IsMacOSX = TargetTriple.getOS() == llvm::Triple::MacOSX;
bool IsPPC64 = TargetTriple.getArch() == llvm::Triple::ppc64;
bool IsX86_64 = TargetTriple.getArch() == llvm::Triple::x86_64;
+ bool IsMIPS32 = TargetTriple.getArch() == llvm::Triple::mips;
ShadowMapping Mapping;
@@ -216,7 +219,8 @@
Mapping.OrShadowOffset = !IsPPC64 && !ClShort64BitOffset;
Mapping.Offset = (IsAndroid || ZeroBaseShadow) ? 0 :
- (LongSize == 32 ? kDefaultShadowOffset32 :
+ (LongSize == 32 ?
+ (IsMIPS32 ? kMIPS32_ShadowOffset32 : kDefaultShadowOffset32) :
IsPPC64 ? kPPC64_ShadowOffset64 : kDefaultShadowOffset64);
if (!ZeroBaseShadow && ClShort64BitOffset && IsX86_64 && !IsMacOSX) {
assert(LongSize == 64);
@@ -1269,6 +1273,11 @@
RedzoneSize(),
1ULL << Mapping.Scale,
kAsanStackPartialRedzoneMagic);
+ if (ASan.TD->isLittleEndian()) {
+ Poison = support::endian::byte_swap<uint32_t, support::little>(Poison);
+ } else {
+ Poison = support::endian::byte_swap<uint32_t, support::big>(Poison);
+ }
}
Value *PartialPoison = ConstantInt::get(RZTy, Poison);
IRB.CreateStore(PartialPoison, IRB.CreateIntToPtr(Ptr, RZPtrTy));
#PLATFORM=clang_linux
#TARGET=asan-arm
#CC=$HOME/bin/arm-linux-gnueabihf-clang
PLATFORM=clang_linux
TARGET=asan-mips
CC=$HOME/bin/mips-linux-gnu-clang
LLVM_SRC=$HOME/src/llvm-svn
LLVM_BUILD=$LLVM_SRC/b
RT_SRC=$LLVM_SRC/projects/compiler-rt
RT_BUILD=$LLVM_BUILD/tools/clang/runtime/compiler-rt
CLANG_LIB=$LLVM_BUILD/Release+Asserts/lib/clang/3.4/lib/linux
rm -rf $RT_BUILD/$PLATFORM/$TARGET
make -C $RT_SRC \
VERBOSE=1 \
ProjSrcRoot=$RT_SRC \
ProjObjRoot=$RT_BUILD \
CC=$CC \
$PLATFORM
yes|cp $RT_BUILD/$PLATFORM/$TARGET/libcompiler_rt.a \
$CLANG_LIB/libclang_rt.$TARGET.a
Index: lib/sanitizer_common/sanitizer_stacktrace.h
===================================================================
--- lib/sanitizer_common/sanitizer_stacktrace.h (revision 182727)
+++ lib/sanitizer_common/sanitizer_stacktrace.h (working copy)
@@ -21,7 +21,8 @@
#if SANITIZER_LINUX && (defined(__arm__) || \
defined(__powerpc__) || defined(__powerpc64__) || \
- defined(__sparc__))
+ defined(__sparc__) || \
+ defined(__mips__))
#define SANITIZER_CAN_FAST_UNWIND 0
#else
#define SANITIZER_CAN_FAST_UNWIND 1
Index: lib/sanitizer_common/sanitizer_stoptheworld_linux_libcdep.cc
===================================================================
--- lib/sanitizer_common/sanitizer_stoptheworld_linux_libcdep.cc (revision 182727)
+++ lib/sanitizer_common/sanitizer_stoptheworld_linux_libcdep.cc (working copy)
@@ -373,6 +373,10 @@
typedef pt_regs regs_struct;
#define REG_SP gpr[PT_R1]
+#elif defined(__mips__)
+typedef struct user regs_struct;
+#define REG_SP regs[EF_REG29]
+
#else
#error "Unsupported architecture"
#endif // SANITIZER_ANDROID && defined(__arm__)
Index: lib/asan/asan_mapping.h
===================================================================
--- lib/asan/asan_mapping.h (revision 182727)
+++ lib/asan/asan_mapping.h (working copy)
@@ -49,6 +49,13 @@
// || `[0x24000000, 0x27ffffff]` || ShadowGap ||
// || `[0x20000000, 0x23ffffff]` || LowShadow ||
// || `[0x00000000, 0x1fffffff]` || LowMem ||
+//
+// Default Linux/MIPS mapping:
+// || `[0x2aaa8000, 0xffffffff]` || HighMem ||
+// || `[0x0fffd000, 0x2aaa7fff]` || HighShadow ||
+// || `[0x0bffd000, 0x0fffcfff]` || ShadowGap ||
+// || `[0x0aaa8000, 0x0bffcfff]` || LowShadow ||
+// || `[0x00000000, 0x0aaa7fff]` || LowMem ||
#if ASAN_FLEXIBLE_MAPPING_AND_OFFSET == 1
extern SANITIZER_INTERFACE_ATTRIBUTE uptr __asan_mapping_scale;
@@ -62,7 +69,11 @@
# else
# define SHADOW_SCALE (3)
# if SANITIZER_WORDSIZE == 32
-# define SHADOW_OFFSET (1 << 29)
+# if defined(__mips__)
+# define SHADOW_OFFSET 0x0aaa8000
+# else
+# define SHADOW_OFFSET (1 << 29)
+# endif
# else
# if defined(__powerpc64__)
# define SHADOW_OFFSET (1ULL << 41)
Index: lib/asan/asan_linux.cc
===================================================================
--- lib/asan/asan_linux.cc (revision 182727)
+++ lib/asan/asan_linux.cc (working copy)
@@ -89,6 +89,11 @@
stk_ptr = (uptr *) *sp;
*bp = stk_ptr[15];
# endif
+# elif defined(__mips__)
+ ucontext_t *ucontext = (ucontext_t*)context;
+ *pc = ucontext->uc_mcontext.gregs[31];
+ *bp = ucontext->uc_mcontext.gregs[30];
+ *sp = ucontext->uc_mcontext.gregs[29];
#else
# error "Unsupported arch"
#endif
Index: lib/asan/asan_thread.cc
===================================================================
--- lib/asan/asan_thread.cc (revision 182727)
+++ lib/asan/asan_thread.cc (working copy)
@@ -39,7 +39,7 @@
thread = 0;
}
-static char thread_registry_placeholder[sizeof(ThreadRegistry)];
+static char thread_registry_placeholder[sizeof(ThreadRegistry)] __attribute__((aligned(16)));
static ThreadRegistry *asan_thread_registry;
static ThreadContextBase *GetAsanThreadContext(u32 tid) {
Index: make/platform/clang_linux.mk
===================================================================
--- make/platform/clang_linux.mk (revision 182727)
+++ make/platform/clang_linux.mk (working copy)
@@ -80,8 +80,19 @@
endif
endif
+
+ifeq ($(call contains,arm,$(CompilerTargetArch)),true)
+Configs += asan-arm
+Arch.asan-arm := arm
endif
+ifeq ($(call contains,mips,$(CompilerTargetArch)),true)
+Configs += asan-mips
+Arch.asan-mips := mips
+endif
+
+endif
+
###
CFLAGS := -Wall -Werror -O3 -fomit-frame-pointer
@@ -113,6 +124,10 @@
LDFLAGS.asan-arm-android := $(LDFLAGS) $(ANDROID_COMMON_FLAGS) -ldl \
-Wl,-soname=libclang_rt.asan-arm-android.so
+CFLAGS.asan-arm := $(CFLAGS) -fPIC -fno-builtin -fno-rtti -mllvm -arm-enable-ehabi
+
+CFLAGS.asan-mips := $(CFLAGS) -fPIC -fno-builtin -fno-rtti
+
# Use our stub SDK as the sysroot to support more portable building. For now we
# just do this for the non-ASAN modules, because the stub SDK doesn't have
# enough support to build ASAN.
@@ -142,6 +157,12 @@
FUNCTIONS.ubsan_cxx-i386 := $(UbsanCXXFunctions)
FUNCTIONS.ubsan_cxx-x86_64 := $(UbsanCXXFunctions)
+FUNCTIONS.asan-arm := $(AsanFunctions) $(InterceptionFunctions) \
+ $(SanitizerCommonFunctions)
+
+FUNCTIONS.asan-mips := $(AsanFunctions) $(InterceptionFunctions) \
+ $(SanitizerCommonFunctions)
+
# Always use optimized variants.
OPTIMIZED := 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment