Skip to content

[lld][WebAssembly] Return 0 for synthetic function offsets #96134

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 10 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lld/test/wasm/Inputs/signature-mismatch-debug-info-a.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.functype foo (i32) -> ()
.functype test0 () -> ()

.section .text.foo,"",@
.weak foo
.type foo,@function
foo:
.functype foo (i32) -> ()
end_function

.section .text.test0,"",@
.globl test0
.type test0,@function
test0:
.functype test0 () -> ()
i32.const 3
call foo
end_function

.section .debug_info,"",@
.int32 foo
.int32 test0
23 changes: 23 additions & 0 deletions lld/test/wasm/Inputs/signature-mismatch-debug-info-b.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.functype foo (i32, i32) -> ()
.functype test1 () -> ()

.section .text.foo,"",@
.weak foo
.type foo,@function
foo:
.functype foo (i32, i32) -> ()
end_function

.section .text.test1,"",@
.globl test1
.type test1,@function
test1:
.functype test1 () -> ()
i32.const 4
i32.const 5
call foo
end_function

.section .debug_info,"",@
.int32 foo
.int32 test1
17 changes: 17 additions & 0 deletions lld/test/wasm/Inputs/signature-mismatch-debug-info-main.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.functype test0 () -> ()
.functype test1 () -> ()
.functype main (i32, i32) -> (i32)

.section .text.main,"",@
.globl main
.type main,@function
main:
.functype main (i32, i32) -> (i32)
call test0
call test1
i32.const 0
end_function

.section .debug_info,"",@
.int32 test0
.int32 test1
8 changes: 8 additions & 0 deletions lld/test/wasm/signature-mismatch-debug-info.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This is a regression test that ensures a function signature mismatch in
# functions with debug info does not cause does not cause a segmentation fault
# when writing .debug_info section.

; RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %p/Inputs/signature-mismatch-debug-info-a.s -o %t.a.o
; RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %p/Inputs/signature-mismatch-debug-info-b.s -o %t.b.o
; RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %p/Inputs/signature-mismatch-debug-info-main.s -o %t.main.o
; RUN: wasm-ld -o %t.wasm %t.a.o %t.b.o %t.main.o --export=main --no-entry
11 changes: 9 additions & 2 deletions lld/wasm/InputChunks.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,14 @@ class InputFunction : public InputChunk {
}
void setExportName(std::string exportName) { this->exportName = exportName; }
uint32_t getFunctionInputOffset() const { return getInputSectionOffset(); }
uint32_t getFunctionCodeOffset() const { return function->CodeOffset; }
uint32_t getFunctionCodeOffset() const {
// For generated synthetic functions, such as unreachable stubs generated
// for signature mismatches, 'function' reference does not exist. This
// function is used to get function offsets for .debug_info section, and for
// those generated stubs function offsets are not meaningful anyway. So just
// return 0 in those cases.
return function ? function->CodeOffset : 0;
}
uint32_t getFunctionIndex() const { return *functionIndex; }
bool hasFunctionIndex() const { return functionIndex.has_value(); }
void setFunctionIndex(uint32_t index);
Expand All @@ -301,7 +308,7 @@ class InputFunction : public InputChunk {
return compressedSize;
}

const WasmFunction *function;
const WasmFunction *function = nullptr;

protected:
std::optional<std::string> exportName;
Expand Down