[PATCH] D45768: [mips] Implement GetWriteFlag() for mips

Miloš Stojanović via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 18 06:10:49 PDT 2018


mstojanovic created this revision.
mstojanovic added reviewers: sdardis, zoran.jovanovic, petarj.
mstojanovic added a project: Sanitizers.
Herald added subscribers: arichardson, kubamracek.

The read/write flag is set by manually decoding the instruction that caused the exception. It is implemented this way because the the cause register which contains the needed flag was removed from the signal context structure which the user handler receives from the kernel.


https://reviews.llvm.org/D45768

Files:
  lib/sanitizer_common/sanitizer_linux.cc
  test/sanitizer_common/TestCases/Posix/illegal_read_test.cc
  test/sanitizer_common/TestCases/Posix/illegal_write_test.cc


Index: test/sanitizer_common/TestCases/Posix/illegal_write_test.cc
===================================================================
--- /dev/null
+++ test/sanitizer_common/TestCases/Posix/illegal_write_test.cc
@@ -0,0 +1,12 @@
+// Test that there was an illegal WRITE memory access.
+// RUN: %clangxx -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s
+
+// REQUIRES: stable-runtime
+
+volatile int *null = 0;
+
+int main(int argc, char **argv) {
+  *null = 0;
+}
+
+// CHECK: The signal is caused by a WRITE memory access.
Index: test/sanitizer_common/TestCases/Posix/illegal_read_test.cc
===================================================================
--- /dev/null
+++ test/sanitizer_common/TestCases/Posix/illegal_read_test.cc
@@ -0,0 +1,13 @@
+// Test that there was an illegal READ memory access.
+// RUN: %clangxx -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s
+
+// REQUIRES: stable-runtime
+
+volatile int *null = 0;
+volatile int a;
+
+int main(int argc, char **argv) {
+  a = *null;
+}
+
+// CHECK: The signal is caused by a READ memory access.
Index: lib/sanitizer_common/sanitizer_linux.cc
===================================================================
--- lib/sanitizer_common/sanitizer_linux.cc
+++ lib/sanitizer_common/sanitizer_linux.cc
@@ -1736,6 +1736,33 @@
   uptr err = ucontext->uc_mcontext.gregs[REG_ERR];
 #endif // SANITIZER_FREEBSD
   return err & PF_WRITE ? WRITE : READ;
+#elif defined(__mips64)
+  long *exception_source;
+  int32_t faulty_instruction;
+  int16_t op_code;
+
+  exception_source = (long *)ucontext->uc_mcontext.pc;
+  faulty_instruction = (int32_t)(*exception_source);
+
+  op_code = (faulty_instruction >> 26) & 0x3f;
+
+  switch (op_code) {
+    case 0x28:  // sb
+    case 0x29:  // sh
+    case 0x2b:  // sw
+    case 0x3f:  // sd
+      return SignalContext::WRITE;
+
+    case 0x20:  // lb
+    case 0x24:  // lbu
+    case 0x21:  // lh
+    case 0x25:  // lhu
+    case 0x23:  // lw
+    case 0x27:  // lwu
+    case 0x37:  // ld
+      return SignalContext::READ;
+  }
+  return SignalContext::UNKNOWN;
 #elif defined(__arm__)
   static const uptr FSR_WRITE = 1U << 11;
   uptr fsr = ucontext->uc_mcontext.error_code;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D45768.142922.patch
Type: text/x-patch
Size: 2181 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180418/8fa00f4b/attachment.bin>


More information about the llvm-commits mailing list