Skip to content

Comments

RTS: Introduce WASM_PAGES_SIZE and WASM_HEAP_SIZE constants#2632

Merged
mergify[bot] merged 3 commits intomasterfrom
osa1/rts_constants
Jun 29, 2021
Merged

RTS: Introduce WASM_PAGES_SIZE and WASM_HEAP_SIZE constants#2632
mergify[bot] merged 3 commits intomasterfrom
osa1/rts_constants

Conversation

@osa1
Copy link
Contributor

@osa1 osa1 commented Jun 29, 2021

No description provided.

@dfinity-ci
Copy link

This PR does not affect the produced WebAssembly code.

Copy link
Contributor

@ulan ulan left a comment

Choose a reason for hiding this comment

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

lgtm, thanks!

Feel free to ignore the suggestions below.

// Array payload should not be larger than half of the memory
if len > 1 << (32 - 2 - 1) {
// 2 for word size, 1 to divide by two
if Words(len) > WASM_HEAP_SIZE / 2 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit (feel free to ignore): how about WASM_HEAP_SIZE_IN_WORDS to make it clear for the reader?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I used the type Words, which is a newtype and requires explicit unwrapping or conversion to Bytes. Do you still think _IN_WORDS suffix would be helpful? I can add it, just wanted to make sure you know that Words is a newtype and won't be implicitly converted to anything else and when used in arithmetic with Bytes or scalars will cause compile errors.

pub const WASM_PAGE_SIZE: Bytes<u32> = Bytes(64 * 1024);

/// Wasm heap size (4GiB) in words. Note that `to_bytes` on this value will overflow as 4GiB in
/// bytes is `u32::MAX + 1`.
Copy link
Contributor

Choose a reason for hiding this comment

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

The overflow is a bit of a footgun. Alternatively we could introduce MAX_HEAP_SIZE and make it smaller than WASM_HEAP_SIZE by a couple of pages, so that we don't have to worry about accidentally overflowing u32.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if we want to reduce heap size to avoid a bug in the RTS. In Rust overflow in arithmetic is checked in debug mode, and we run the tests in both debug mode and release mode, so hopefully any bugs caused by this will be caught before getting merged.


#[no_mangle]
pub unsafe extern "C" fn alloc_array(len: u32) -> SkewedPtr {
// Array payload should not be larger than half of the memory
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wish we documented why this is the case. I believe this is because the copying GC will want to copy it and overflow the heap if this is 2GiB, but I don't know if there are other reasons.

@osa1 osa1 added the automerge-squash When ready, merge (using squash) label Jun 29, 2021
@osa1 osa1 requested a review from crusso June 29, 2021 12:43
@mergify mergify bot merged commit bf1b5e3 into master Jun 29, 2021
@mergify mergify bot deleted the osa1/rts_constants branch June 29, 2021 14:46
@mergify mergify bot removed the automerge-squash When ready, merge (using squash) label Jun 29, 2021
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.

4 participants