Skip to content

[Object][WebAssembly] Fix data segment offsets higher than 2^31 #125739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 4, 2025

Conversation

sbc100
Copy link
Collaborator

@sbc100 sbc100 commented Feb 4, 2025

Fixes: #58555

@llvmbot
Copy link
Member

llvmbot commented Feb 4, 2025

@llvm/pr-subscribers-backend-webassembly

@llvm/pr-subscribers-llvm-binary-utilities

Author: Sam Clegg (sbc100)

Changes

Fixes: #58555


Full diff: https://github.com/llvm/llvm-project/pull/125739.diff

3 Files Affected:

  • (modified) llvm/include/llvm/BinaryFormat/Wasm.h (+2-2)
  • (modified) llvm/lib/Object/WasmObjectFile.cpp (+2-2)
  • (added) llvm/test/Object/Wasm/data-offsets.yaml (+16)
diff --git a/llvm/include/llvm/BinaryFormat/Wasm.h b/llvm/include/llvm/BinaryFormat/Wasm.h
index ede2d692a594916..30271be232ca773 100644
--- a/llvm/include/llvm/BinaryFormat/Wasm.h
+++ b/llvm/include/llvm/BinaryFormat/Wasm.h
@@ -333,8 +333,8 @@ struct WasmTable {
 struct WasmInitExprMVP {
   uint8_t Opcode;
   union {
-    int32_t Int32;
-    int64_t Int64;
+    uint32_t Int32;
+    uint64_t Int64;
     uint32_t Float32;
     uint64_t Float64;
     uint32_t Global;
diff --git a/llvm/lib/Object/WasmObjectFile.cpp b/llvm/lib/Object/WasmObjectFile.cpp
index 0f6fd5612f9d82a..7815c2670223198 100644
--- a/llvm/lib/Object/WasmObjectFile.cpp
+++ b/llvm/lib/Object/WasmObjectFile.cpp
@@ -201,10 +201,10 @@ static Error readInitExpr(wasm::WasmInitExpr &Expr,
   Expr.Inst.Opcode = readOpcode(Ctx);
   switch (Expr.Inst.Opcode) {
   case wasm::WASM_OPCODE_I32_CONST:
-    Expr.Inst.Value.Int32 = readVarint32(Ctx);
+    Expr.Inst.Value.Int32 = readVaruint32(Ctx);
     break;
   case wasm::WASM_OPCODE_I64_CONST:
-    Expr.Inst.Value.Int64 = readVarint64(Ctx);
+    Expr.Inst.Value.Int64 = readVaruint64(Ctx);
     break;
   case wasm::WASM_OPCODE_F32_CONST:
     Expr.Inst.Value.Float32 = readFloat32(Ctx);
diff --git a/llvm/test/Object/Wasm/data-offsets.yaml b/llvm/test/Object/Wasm/data-offsets.yaml
new file mode 100644
index 000000000000000..26b2be2b78f5323
--- /dev/null
+++ b/llvm/test/Object/Wasm/data-offsets.yaml
@@ -0,0 +1,16 @@
+# RUN: yaml2obj %s | llvm-objdump -s -
+
+## Tests data offsets above 2**31
+
+--- !WASM
+FileHeader:
+  Version:         0x00000001
+Sections:
+  - Type:            DATA
+    Segments:
+      - SectionOffset:   0
+        InitFlags:       0
+        Offset:
+          Opcode:          I32_CONST
+          Value:           2147483649
+        Content:         '6401020304'

@dschuff
Copy link
Member

dschuff commented Feb 4, 2025

For wasm64, could there be offsets higher than 2^32? Or are those just speced to be varint64?

@sbc100
Copy link
Collaborator Author

sbc100 commented Feb 4, 2025

For wasm64, could there be offsets higher than 2^32? Or are those just speced to be varint64?

Yup, that should work. Do you want me to add a testing for that?

@sbc100 sbc100 force-pushed the fix_large_data_offsets branch from 070dfbc to 3addae9 Compare February 4, 2025 19:51
@dschuff
Copy link
Member

dschuff commented Feb 4, 2025

Yeah, that seems like we should have that if we don't already.

Copy link
Member

@dschuff dschuff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

otherwise LGTM

@sbc100 sbc100 force-pushed the fix_large_data_offsets branch from 5ad5c02 to 82bcfcf Compare February 4, 2025 22:05
@sbc100 sbc100 merged commit c798a5c into llvm:main Feb 4, 2025
4 of 5 checks passed
@sbc100 sbc100 deleted the fix_large_data_offsets branch February 4, 2025 22:06
sbc100 added a commit that referenced this pull request Feb 5, 2025
…31 (#125739)" (#125786)

This reverts commit c798a5c.

This broke bunch of test the emscripten side. Reverting while we
investigate.
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
…31 (llvm#125739)" (llvm#125786)

This reverts commit c798a5c.

This broke bunch of test the emscripten side. Reverting while we
investigate.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[wasm] LLVM ERROR: LEB is outside Varint32 range
3 participants