[PATCH] D46479: [WebAssembly] Only perform sanity checking of relocation targets in debug builds

Sam Clegg via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri May 4 15:21:41 PDT 2018


sbc100 updated this revision to Diff 145308.
sbc100 added a comment.

feedback


Repository:
  rLLD LLVM Linker

https://reviews.llvm.org/D46479

Files:
  wasm/InputChunks.cpp
  wasm/InputChunks.h


Index: wasm/InputChunks.h
===================================================================
--- wasm/InputChunks.h
+++ wasm/InputChunks.h
@@ -78,6 +78,7 @@
   virtual ~InputChunk() = default;
   virtual ArrayRef<uint8_t> data() const = 0;
   virtual uint32_t getInputSectionOffset() const = 0;
+  void verifyRelocTargets() const;
 
   std::vector<WasmRelocation> Relocations;
   Kind SectionKind;
Index: wasm/InputChunks.cpp
===================================================================
--- wasm/InputChunks.cpp
+++ wasm/InputChunks.cpp
@@ -51,6 +51,42 @@
   for (const WasmRelocation &R : Section.Relocations)
     if (R.Offset >= Start && R.Offset < Start + Size)
       Relocations.push_back(R);
+
+#ifndef NDEBUG
+  verifyRelocTargets();
+#endif
+}
+
+void InputChunk::verifyRelocTargets() const {
+  for (const WasmRelocation &Rel : Relocations) {
+    uint32_t ExistingValue;
+    uint32_t Offset = Rel.Offset - getInputSectionOffset();
+    const uint8_t *Loc = data().data() + Offset;
+    switch (Rel.Type) {
+    case R_WEBASSEMBLY_TYPE_INDEX_LEB:
+    case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
+    case R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
+    case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
+      ExistingValue = decodeULEB128(Loc);
+      break;
+    case R_WEBASSEMBLY_TABLE_INDEX_SLEB:
+    case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
+      ExistingValue = static_cast<uint32_t>(decodeSLEB128(Loc));
+      break;
+    case R_WEBASSEMBLY_TABLE_INDEX_I32:
+    case R_WEBASSEMBLY_MEMORY_ADDR_I32:
+      ExistingValue = static_cast<uint32_t>(read32le(Loc));
+      break;
+    default:
+      llvm_unreachable("unknown relocation type");
+    }
+
+    uint32_t ExpectedValue = File->calcExpectedValue(Rel);
+    if (ExpectedValue != ExistingValue)
+      error("unexpected existing value for " + ReloctTypeToString(Rel.Type) +
+            ": existing=" + Twine(ExistingValue) +
+            " expected=" + Twine(ExpectedValue));
+  }
 }
 
 // Copy this input chunk to an mmap'ed output file and apply relocations.
@@ -69,7 +105,6 @@
   for (const WasmRelocation &Rel : Relocations) {
     uint8_t *Loc = Buf + Rel.Offset + Off;
     uint32_t Value = File->calcNewValue(Rel);
-    uint32_t ExistingValue;
     DEBUG(dbgs() << "apply reloc: type=" << ReloctTypeToString(Rel.Type)
                  << " addend=" << Rel.Addend << " index=" << Rel.Index
                  << " value=" << Value << " offset=" << Rel.Offset << "\n");
@@ -79,28 +114,19 @@
     case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
     case R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
     case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
-      ExistingValue = decodeULEB128(Loc);
       encodeULEB128(Value, Loc, 5);
       break;
     case R_WEBASSEMBLY_TABLE_INDEX_SLEB:
     case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
-      ExistingValue = static_cast<uint32_t>(decodeSLEB128(Loc));
       encodeSLEB128(static_cast<int32_t>(Value), Loc, 5);
       break;
     case R_WEBASSEMBLY_TABLE_INDEX_I32:
     case R_WEBASSEMBLY_MEMORY_ADDR_I32:
-      ExistingValue = static_cast<uint32_t>(read32le(Loc));
       write32le(Loc, Value);
       break;
     default:
       llvm_unreachable("unknown relocation type");
     }
-
-    uint32_t ExpectedValue = File->calcExpectedValue(Rel);
-    if (ExpectedValue != ExistingValue)
-      error("unexpected existing value for " + ReloctTypeToString(Rel.Type) +
-            ": existing=" + Twine(ExistingValue) +
-            " expected=" + Twine(ExpectedValue));
   }
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D46479.145308.patch
Type: text/x-patch
Size: 3448 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180504/cee831a7/attachment.bin>


More information about the llvm-commits mailing list