[PATCH] [UBSan][MIPS]

Sagar Thakur Sagar.Thakur at imgtec.com
Fri Apr 24 03:14:19 PDT 2015


Hi rsmith, kcc, samsonov, dsanders,

On MIPS, shift operation on signed types give unpredictable results. Therefore we use unsigned value while shifting and after shifting is done we convert it back to signed value.

REPOSITORY
  rL LLVM

http://reviews.llvm.org/D9247

Files:
  lib/ubsan/ubsan_value.cc

Index: lib/ubsan/ubsan_value.cc
===================================================================
--- lib/ubsan/ubsan_value.cc
+++ lib/ubsan/ubsan_value.cc
@@ -12,6 +12,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include <endian.h>
 #include "ubsan_platform.h"
 #if CAN_SANITIZE_UB
 #include "ubsan_value.h"
@@ -27,7 +28,24 @@
     // to SIntMax.
     const unsigned ExtraBits =
       sizeof(SIntMax) * 8 - getType().getIntegerBitWidth();
+#if defined(__mips__)
+    UIntMax Value = UIntMax(Val) << ExtraBits >> ExtraBits;;
+    uptr ExtraBytes= ExtraBits / 8;
+#if BYTE_ORDER == LITTLE_ENDIAN
+    u8 * ValuePtr = (u8 *) &Value + (sizeof(UIntMax) - ExtraBytes);
+#else
+    u8 * ValuePtr = (u8 *) &Value;
+#endif
+    uptr Mask = 1UL << (getType().getIntegerBitWidth() - 1);
+    if(Val & Mask) {
+      for (uptr i = 0 ; i < ExtraBytes ; i++) {
+        *(ValuePtr + i) |= 0xff;
+      }
+    }
+    return SIntMax(Value);
+#else
     return SIntMax(Val) << ExtraBits >> ExtraBits;
+#endif
   }
   if (getType().getIntegerBitWidth() == 64)
     return *reinterpret_cast<s64*>(Val);

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D9247.24370.patch
Type: text/x-patch
Size: 1138 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150424/bce2b0ba/attachment.bin>


More information about the llvm-commits mailing list