Skip to content

wasm2c: Initial implementation of memory64 - #2086

Merged
sbc100 merged 1 commit into
mainfrom
wasm2c_memory64
Jan 13, 2023
Merged

sbc100 merged 1 commit into
mainfrom
wasm2c_memory64

Conversation

@sbc100

@sbc100 sbc100 commented Dec 1, 2022

Copy link
Copy Markdown
Member

No description provided.

@sbc100
sbc100 requested a review from keithw December 1, 2022 17:21
Base automatically changed from wasm2c_spec_tests to main December 1, 2022 17:39

@keithw keithw left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess it's understandable, but I wish some of the memory64 tests tested growing a memory to $\ge 2^{33}$ bytes and then reading and writing to those high addresses. I realize it may be difficult to run that test on most architectures.

(Edit: or am I missing that somewhere in the tests?)

Comment thread wasm2c/wasm-rt-impl.c
uint64_t byte_length = initial_pages * PAGE_SIZE;
#if WASM_RT_MEMCHECK_SIGNAL_HANDLER
/* Reserve 8GiB. */
void* addr = os_mmap(0x200000000ul);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we'll have to forbid using WASM_RT_MEMCHECK_SIGNAL_HANDLER (or find some other strategy?) if a memory address+offset is allowed to be more than $2^{33}$ -- otherwise can't a memory read go beyond the allocated virtual address space?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hmm, yes indeed. I guess maybe we are going to need a WASM_RT_MEMORY64 macro that is defined in memory64 mode.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

How do you feel about landing this simple change, as-is, and then continuing to iterate on full wasm64 in followup PRs? I guess we should probably remove the check mark from the README until later too?

@keithw keithw Dec 1, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't know how much effort you feel this is worth, but maybe our "dream" solution here would involve:

  1. Letting wasm2c emit explicit "sw_checked" versions (i.e. mandatory bounds check) of allocate_memory/grow_memory/free_memory and load and store, on a per-memory basis, depending on decisions made at wasm2c time. @angelamontemayor has already done this part for use in Fixpoint: dfc437b , which you'd be very welcome to take upstream if it proves more broadly useful.

  2. By default, wasm2c generates "sw_checked" allocate calls and load/store instructions for 64-bit indexed memories, and keeps the allocates and load/store instructions for 32-bit indexed memories the same (either sw-checked or hw-checked depending on whether we have the signal handler).

  3. Optionally, we could imagine wasm2c gets more clever by allocating some guard pages on memory64 memories and then issuing an optimized load/store instruction that only checks the operand in cases where the immediate offset is known to be small enough, as the memory64 overview suggests?

@keithw keithw Dec 1, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't have a problem landing the simple change, sure, although maybe the 100% safe thing would be to bomb out if memory64 is enabled and WASM_RT_SIGNAL_HANDLER is also set. (And then the memory64 test .txt files would include --cflags="-DWASM_RT_SIGNAL_HANDLER=0" in the args or something.)

Anyway I'm not trying to stand in the way of doing this incrementally, and I'm happy to help port over @angelamontemayor's per-memory safety commit if I can be helpful.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

OK, I decided to revisit this.

For this initial version we don't support WASM_RT_MEMCHECK_SIGNAL_HANDLER at all when memory64 is enabled.

I've used __builtin_add_overflow to check for 64-bit overflow when calculating base + offset in RANGE_CHECK. This means we don't currently support non-clang/non-gcc compiler with wasm2c + memory64.

This keeps this change simple, and we can them improve on things incrementally if we wish.

@keithw keithw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lftm % discussion

@sbc100 sbc100 changed the title wasm2c: Implement memory64 wasm2c: Initial implementation of memory64 Jan 13, 2023
@sbc100

sbc100 commented Jan 13, 2023

Copy link
Copy Markdown
Member Author

This PR is actually much simpler now, and passes all the memory64 tests.

Comment thread wasm2c/wasm-rt-impl.c Resolved
@keithw

keithw commented Jan 13, 2023

Copy link
Copy Markdown
Member

Still lgtm...

Note, there are still some issues here that needs to be resolved,
mostly about memory sandboxing (bounds checking).

Since this is still experimental I've also added a `--experimental` flag
to wasm2c that is required in addition to passing `--enable-memory64`.
@sbc100
sbc100 merged commit 400fa0f into main Jan 13, 2023
@sbc100
sbc100 deleted the wasm2c_memory64 branch January 13, 2023 06:38
dimitry- pushed a commit to AndroidWasm/wabt that referenced this pull request Jan 13, 2023
Note, there are still some issues here that needs to be resolved,
mostly about memory sandboxing (bounds checking).

Since this is still experimental I've also added a `--experimental` flag
to wasm2c that is required in addition to passing `--enable-memory64`.
sbc100 added a commit that referenced this pull request Jan 18, 2023
I mistakenly broke the wasm2c test runner in #2086.  I guess we need
tests for our test runner..
@sbc100 sbc100 mentioned this pull request Jan 18, 2023
sbc100 added a commit that referenced this pull request Jan 18, 2023
I mistakenly broke the wasm2c test runner in #2086.  I guess we need
tests for our test runner..
sbc100 added a commit that referenced this pull request Jan 18, 2023
I mistakenly broke the wasm2c test runner in #2086.  I guess we need
tests for our test runner..
sbc100 pushed a commit that referenced this pull request Oct 22, 2026
`run-spec-wasm2c.py` skips a test on Windows when memory64 is enabled.
That skip was written
when memory64 was opt-in (`--enable-memory64`, #2086). Since #2837
memory64 is on by default
(`--disable-memory64`), so the condition holds for almost every test and
260 of the 262 wasm2c
tests are skipped on `windows-latest`: the C that wasm2c writes is not
compiled by cl.exe.

This removes the skip. Split out of #2901 at @sbc100's request.
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.

2 participants