Skip to content
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

arm64: account for imported functions when encoding relocation islands #2346

Merged
merged 1 commit into from
Nov 23, 2024

Conversation

evacchi
Copy link
Contributor

@evacchi evacchi commented Nov 23, 2024

Fixes #2340

Thanks @davidmdm for reporting the issue. Please do confirm if this solves your problem!

Here's a summary for the lazy. On arm64, when we compute the offsets in a trampoline, the following function is given a mapping between all function refs (basically Indexes in the original Wasm file) and offsets (refToBinaryOffset).

func encodeCallTrampolineIsland(refToBinaryOffset []int, islandOffset int, executable []byte) {
	for i := 0; i < len(refToBinaryOffset); i++ {
		trampolineOffset := islandOffset + trampolineCallSize*i

Wasm function indexes account for all functions, including imported functions. However, we do not need to allocate space for such functions in the "trampoline island". In fact, we do not account for them:

	trampolineInterval, callTrampolineIslandSize, err := machine.CallTrampolineIslandInfo(localFns)

however:

  1. because we do not account for them,
  2. because relocations are being written after the code of each function has been laid out into the mmap'ed segment,

if we write out of the expected bounds, we end up overwriting the beginning of the function that follows that island. This causes all kind of wacky behavior, because when you invoke that function, it starts with some jumps to "random" places.

The fix is relatively simple, and it can be rewritten in a few ways. Essentially, we need to account for the imported functions, and ignore those indexes when we write the island.

…oded

Signed-off-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
@evacchi evacchi requested a review from mathetake as a code owner November 23, 2024 00:01
Copy link
Member

@mathetake mathetake left a comment

Choose a reason for hiding this comment

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

💯

@mathetake mathetake merged commit 610c202 into tetratelabs:main Nov 23, 2024
50 checks passed
@davidmdm
Copy link

I confirm that this has solved my issue !

@evacchi evacchi deleted the no-main-is-an-island branch November 26, 2024 20:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Programs that succeed to execute with v1.6.0 sometimes hang indefinitely with v1.8.1 during instantiation.
3 participants