Skip to content

use page_size=1 for instrumented components - #15

Merged
chenyan2002 merged 19 commits into
mainfrom
one-byte-page
Oct 4, 2026
Merged

chenyan2002 merged 19 commits into
mainfrom
one-byte-page

Conversation

@chenyan2002

@chenyan2002 chenyan2002 commented Apr 17, 2026 •

Copy link
Copy Markdown
Owner

Page size 1 memory is not supported with -fPIC and in the native dlmalloc. There are two different code paths depending on the target:

  • For wasip2, the default allocator is the wasi-libc's malloc (dlmalloc). Rust's allocator calls malloc/free. In wasi-libc, sbrk is the only code that touches memory.grow and memory.size. We use our custom crate libc-alloc to replace sbrk with a version that grows by bytes. Everything else stays stock.
    • We tried to use talc as the Rust #[global_allocator], with a malloc/free shim on top of it. But it's not enough. cabi_realloc comes from wasip2 and allocates canonical ABI buffers through __rust_alloc. wasi-libc frees some of those buffers with free(), but two allocators disagree about the block layout, so free trapped with an out-of-bounds access.
    • We need one allocator shared by Rust, C and cabi_realloc. Rust's system allocator on wasi already calls malloc. So the easiest fix is to keep wasi-libc's malloc and fixing only sbrk.
  • For wasm-unknown-unknown, there is no wasi-libc. The default allocator is std's own dlmalloc. The built-in memory source hardcodes 64K pages.
    • We create a custom source OneBytePage in assets/one_byte_page_alloc.rs. Its alloc(size) call memorry.grow(size). page_size() still reports 64K, which dlmalloc only uses for rounding.
    • We could also use talc as the global allocator, but it either requires a fixed-size arena, or the custom memory source has to grow memory by enough to cover talc's private per-heap overhead.

@chenyan2002 chenyan2002 mentioned this pull request Oct 4, 2026
@chenyan2002
chenyan2002 merged commit e2762fb into main Oct 4, 2026
2 checks passed
@chenyan2002
chenyan2002 deleted the one-byte-page branch October 4, 2026 18:32
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.

1 participant