[PATCH] D45280: WebAssembly: Never write more than 32-bits for WebAssembly::OPERAND_OFFSET32

Sam Clegg via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 4 12:33:03 PDT 2018


sbc100 created this revision.
Herald added subscribers: llvm-commits, sunfish, aheejin, jgravelle-google, dschuff, jfb.

A bug was found where an offset of -1 would generate an encoding
of max int64 which is invalid in the binary format.


Repository:
  rL LLVM

https://reviews.llvm.org/D45280

Files:
  lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCCodeEmitter.cpp
  test/CodeGen/WebAssembly/offset.ll


Index: test/CodeGen/WebAssembly/offset.ll
===================================================================
--- test/CodeGen/WebAssembly/offset.ll
+++ test/CodeGen/WebAssembly/offset.ll
@@ -277,6 +277,17 @@
   ret i32 %t
 }
 
+; When loading from a fixed address, offsets are coerced into u32 range
+
+; CHECK-LABEL: load_i32_from_negative_address
+; CHECK: i32.const $push0=, 0{{$}}
+; CHECK: i32.load  $push1=, 4294967295($pop0){{$}}
+define i32 @load_i32_from_negative_address() {
+  %s = inttoptr i32 -1 to i32*
+  %t = load i32, i32* %s
+  ret i32 %t
+}
+
 ; CHECK-LABEL: load_i32_from_global_address
 ; CHECK: i32.const $push0=, 0{{$}}
 ; CHECK: i32.load  $push1=, gv($pop0){{$}}
Index: lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCCodeEmitter.cpp
===================================================================
--- lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCCodeEmitter.cpp
+++ lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCCodeEmitter.cpp
@@ -23,9 +23,11 @@
 #include "llvm/MC/MCRegisterInfo.h"
 #include "llvm/MC/MCSubtargetInfo.h"
 #include "llvm/MC/MCSymbol.h"
+#include "llvm/Support/Debug.h"
 #include "llvm/Support/EndianStream.h"
 #include "llvm/Support/LEB128.h"
 #include "llvm/Support/raw_ostream.h"
+
 using namespace llvm;
 
 #define DEBUG_TYPE "mccodeemitter"
@@ -60,6 +62,7 @@
     const MCSubtargetInfo &STI) const {
   uint64_t Start = OS.tell();
 
+  DEBUG(dbgs() << "encodeInstruction: " << MI << "\n");
   uint64_t Binary = getBinaryCodeForInstr(MI, Fixups, STI);
   if (Binary <= UINT8_MAX) {
     OS << uint8_t(Binary);
@@ -86,8 +89,11 @@
         assert(Desc.TSFlags == 0 &&
                "WebAssembly non-variable_ops don't use TSFlags");
         const MCOperandInfo &Info = Desc.OpInfo[i];
+        DEBUG(dbgs() << "Encoding immediate: type=" << int(Info.OperandType) << "\n");
         if (Info.OperandType == WebAssembly::OPERAND_I32IMM) {
           encodeSLEB128(int32_t(MO.getImm()), OS);
+        } else if (Info.OperandType == WebAssembly::OPERAND_OFFSET32) {
+          encodeULEB128(uint32_t(MO.getImm()), OS);
         } else if (Info.OperandType == WebAssembly::OPERAND_I64IMM) {
           encodeSLEB128(int64_t(MO.getImm()), OS);
         } else if (Info.OperandType == WebAssembly::OPERAND_GLOBAL) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D45280.141022.patch
Type: text/x-patch
Size: 2268 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180404/c0c2ab4d/attachment.bin>


More information about the llvm-commits mailing list