Skip to content

Fix least valid pointer value #2240

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
Nov 18, 2020
Merged

Conversation

kateinoigakukun
Copy link
Member

@kateinoigakukun kateinoigakukun commented Nov 18, 2020

WebAssembly doesn't reserve low addresses But without "extra inhabitants" of
the pointer representation, runtime performance and memory footprint are
worse. So assume that compiler driver uses wasm-ld and --global-base=1024 to
reserve low 1KB.

Fix #2223

MaxDesiatov
MaxDesiatov previously approved these changes Nov 18, 2020
Copy link

@MaxDesiatov MaxDesiatov left a comment

Choose a reason for hiding this comment

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

👏

@MaxDesiatov
Copy link

So how come this issue wasn't reproducible in browsers, but only in Wasmer and Wasmtime?

WebAssembly doesn't reserve low addresses But without "extra inhabitants" of
the pointer representation, runtime performance and memory footprint are
worse. So assume that compiler driver uses wasm-ld and --global-base=1024 to
reserve low 1KB.
@kateinoigakukun kateinoigakukun changed the title Set LeastValidPointerValue as 1 Fix least valid pointer value Nov 18, 2020
@kateinoigakukun
Copy link
Member Author

I confirmed that JavaScriptKit allocates some memories before main func. After JavaScriptKit initial allocation, heap objects are allocated at more than 4KB address, so this issue doesn't happen.

@kateinoigakukun kateinoigakukun merged commit 4baea30 into swiftwasm Nov 18, 2020
@kateinoigakukun kateinoigakukun deleted the katei/fix-enum-case-tag branch April 21, 2021 05:03
@kerimcharfi
Copy link

WebAssembly doesn't reserve low addresses But without "extra inhabitants" of the pointer representation, runtime performance and memory footprint are worse. So assume that compiler driver uses wasm-ld and --global-base=1024 to reserve low 1KB.

Fix #2223

Can you explain: "extra inhabitants" of the pointer representation? What is this low 1kb used for?

@kateinoigakukun
Copy link
Member Author

kateinoigakukun commented May 15, 2023

Can you explain: "extra inhabitants" of the pointer representation? What is this low 1kb used for?

To distinguish enum cases without adding extra but width. Think that MemoryLayout<Optional<UnsafeRawPointer>>.size is a 32bits or 64bits value for example.

@kerimcharfi
Copy link

Thank you very much for your answer. However, I don't understand it without more context, as I have little experience in this domain.

Im trying to do this for my SwiftWasm REPL experiment:
image
source: https://wiki.hyperledger.org/display/iroha/WebAssembly+FFI+-+module+linking

I though I could use --global-base flag to achieve this but found it blocked.
I assumed this layout for SwiftWasm:
image
source: https://wiki.hyperledger.org/display/iroha/WebAssembly+FFI+-+module+linking
Is this how SwiftWasm is laid out?
Where / When does read/write happen to this lower 1KB?
Can this lower 1KB be relocated?

@kateinoigakukun
Copy link
Member Author

I'm sorry, my first explanation was wrong. I updated my comment.

If you are thinking about dynamic linking, it's much more challenging than you think. The most difficult part is making a relative pointer across data and code to be position independent. See this thread for more details.

But let me answer your questions respectively just in case:

Is this how SwiftWasm is laid out?

The memory layout explained by the figure is correct only when passing --stack-first to wasm-ld. You can learn how the wasm-ld layout the memory space at link-time here: https://github.com/llvm/llvm-project/blob/1365ff74cb7d9f15feafdd4fbe2996d2f9e42a5e/lld/wasm/Writer.cpp#L330

Where / When does read/write happen to this lower 1KB?

No read/write should happen against the lower 1KB. This convention let the compiler assume that Optional<UnsafeRawPointer>.none can be represented as 0 (because a valid pointer should not be 0~1024, so we can distinguish 0 from a pointer.)

Can this lower 1KB be relocated?

What do you mean "relocated"? "Relocation" is a term that represents a process during the linking, right? I'm not sure what you are asking, sorry.

@kerimcharfi
Copy link

kerimcharfi commented May 16, 2023

Thank you for your helpful answer and the link to the Wasm Support forum thread.
Are there any other uses despite nullptr destinction in the range 0~1024?
Could this theoretically be replaced with any range 0~X?
Why was 1024 chosen?

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.

JSONDecoder fails, same code works on x86_64
3 participants