From 82bcfcf1019cfea5b9a7f9cbe7c31436339f0cf4 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Tue, 4 Feb 2025 10:34:09 -0800 Subject: [PATCH] [Object][WebAssembly] Fix data segment offsets higher than 2^31 Fixes: #58555 --- llvm/include/llvm/BinaryFormat/Wasm.h | 4 ++-- llvm/lib/Object/WasmObjectFile.cpp | 4 ++-- llvm/test/Object/Wasm/data-offsets.yaml | 22 +++++++++++++++++++ llvm/test/ObjectYAML/wasm/global_section.yaml | 4 ++-- .../wasm/invalidate_data_offset.yaml | 18 +++++++++++++++ 5 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 llvm/test/Object/Wasm/data-offsets.yaml create mode 100644 llvm/test/ObjectYAML/wasm/invalidate_data_offset.yaml diff --git a/llvm/include/llvm/BinaryFormat/Wasm.h b/llvm/include/llvm/BinaryFormat/Wasm.h index ede2d692a5949..30271be232ca7 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 0f6fd5612f9d8..7815c26702231 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 0000000000000..b2e1d00675233 --- /dev/null +++ b/llvm/test/Object/Wasm/data-offsets.yaml @@ -0,0 +1,22 @@ +# RUN: yaml2obj %s | llvm-objdump -s - + +## Tests data offsets above 2**31 for I32_CONST and offset over 2**32 work for I64_CONST + +--- !WASM +FileHeader: + Version: 0x00000001 +Sections: + - Type: DATA + Segments: + - SectionOffset: 0 + InitFlags: 0 + Offset: + Opcode: I32_CONST + Value: 2147483649 + Content: '6401020304' + - SectionOffset: 0 + InitFlags: 0 + Offset: + Opcode: I64_CONST + Value: 4294967297 + Content: '6401020304' diff --git a/llvm/test/ObjectYAML/wasm/global_section.yaml b/llvm/test/ObjectYAML/wasm/global_section.yaml index e330b072a726e..20f4d5440990f 100644 --- a/llvm/test/ObjectYAML/wasm/global_section.yaml +++ b/llvm/test/ObjectYAML/wasm/global_section.yaml @@ -10,7 +10,7 @@ Sections: Mutable: false InitExpr: Opcode: I64_CONST - Value: -5 + Value: 5 ... # CHECK: --- !WASM # CHECK: FileHeader: @@ -23,5 +23,5 @@ Sections: # CHECK: Mutable: false # CHECK: InitExpr: # CHECK: Opcode: I64_CONST -# CHECK: Value: -5 +# CHECK: Value: 5 # CHECK: ... diff --git a/llvm/test/ObjectYAML/wasm/invalidate_data_offset.yaml b/llvm/test/ObjectYAML/wasm/invalidate_data_offset.yaml new file mode 100644 index 0000000000000..ac12026379905 --- /dev/null +++ b/llvm/test/ObjectYAML/wasm/invalidate_data_offset.yaml @@ -0,0 +1,18 @@ +# RUN: not yaml2obj %s 2>&1 | FileCheck %s + +## Tests data offsets above 2**32 are not valid for for I32_CONST + +--- !WASM +FileHeader: + Version: 0x00000001 +Sections: + - Type: DATA + Segments: + - SectionOffset: 0 + InitFlags: 0 + Offset: + Opcode: I32_CONST + Value: 4294967297 + Content: '6401020304' + +# CHECK: error: out of range number