Stream: git-wasmtime

Topic: wasmtime / PR #14714 Add Memory::discard to zero and deco...


view this post on Zulip Wasmtime GitHub notifications bot (Oct 14 2026 at 04:49):

josefguenther opened PR #14714 from josefguenther:feat/memory-discard to bytecodealliance:main:

Closes #9572.

This adds Memory::discard(store, start, count), the host API fitzgen sketched in #9572. It zeroes Wasm pages of a non-shared memory and gives them back to the OS where Wasmtime owns the mapping. Like grow and size, it counts in Wasm pages, so a range is always page-aligned. The memory's size and base pointer don't change, and the pages read as zeros if they are touched again. Linear memory can't shrink, so this lets an embedder whose guest allocator frees large spans return that memory instead of holding the guest's high-water mark until the instance is dropped.

The separate memory.discard proposal takes bytes and rounds them to whole pages; taking pages means nothing is rounded and is consistent with grow/size, so that's the approach I took.

How it works:

The tests cover each of these cases, including the Linux image case, and a C++ test covers the new binding. I ran the Rust tests on macOS and on x86_64 and aarch64 Linux, and the C API tests on macOS.

This is my first contribution to wasmtime and it's a larger one, so let me know if you have changes or concerns.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 14 2026 at 04:49):

josefguenther requested fitzgen for a review on PR #14714.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 14 2026 at 04:49):

josefguenther requested wasmtime-core-reviewers for a review on PR #14714.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 14 2026 at 07:49):

github-actions[bot] added the label wasmtime:c-api on PR #14714.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 14 2026 at 07:49):

github-actions[bot] added the label wasmtime:api on PR #14714.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 15 2026 at 19:53):

:memo: fitzgen submitted PR review:

Thanks! Overall looks good to me, but I have a few comments/questions below.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 15 2026 at 19:53):

:speech_balloon: fitzgen created PR review comment:

I realize this is a pre-existing comment, but it should end with "or restore the original mapping" as per decommit_behavior just below.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 15 2026 at 19:53):

:speech_balloon: fitzgen created PR review comment:

We should document that this method will trap when given out-of-bounds regions to discard, and that callers should have already done their own bounds checks.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 15 2026 at 19:53):

:speech_balloon: fitzgen created PR review comment:

Isn't it the opposite? When we restore the original mapping we get the CoW image, not zeros.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 15 2026 at 19:53):

:speech_balloon: fitzgen created PR review comment:

As just mentioned, we should match the proposed instruction and use bytes-rounded-to-pages rather than pages.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 15 2026 at 19:53):

:speech_balloon: fitzgen created PR review comment:

Doc comment mentions len and offset but the parameters are count and start.

Also, I think we should match the proposed memory.discard instruction's parameters, which last time I looked was bytes that get rounded to page multiples of the memory's page size. I personally think the instruction should take pages instead of bytes, but that is a spec conversation to have, and not matching it in the wasmtime API would be even worse. We can always change this API if the instruction changes its operands.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 15 2026 at 19:53):

:speech_balloon: fitzgen created PR review comment:

Why do we only need to reapply the pkey for the overlap and not for the anonymous ranges? Especially since the DecommitBehavior::Zero branch reapplies it to the whole range.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 15 2026 at 19:53):

:speech_balloon: fitzgen created PR review comment:

This makes it sound like a guarantee, which it is not. If the platform does not support virtual memory, then we will just write the zeroes.

And this may not be what the embedder wants, so it may make sense to also expose a method for testing whether wasmtime::Memory::discard will write zeroes or use virtual memory so they can decide what to do in that case. This doesn't need to be part of this PR though, fine as a follow up PR or a new issue so we don't forget about it.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 15 2026 at 19:53):

:speech_balloon: fitzgen created PR review comment:

It looks like we are not quite complete on the different kinds of overlaps:

view this post on Zulip Wasmtime GitHub notifications bot (Oct 15 2026 at 19:53):

:speech_balloon: fitzgen created PR review comment:

Also, we should test each of these both when dirty and when not dirty.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 15 2026 at 19:53):

:speech_balloon: fitzgen created PR review comment:

Unless the memory creator's memory overwrites the default RuntimeLinearMemory::discard implementation, right? That seems like an important disctinction and that we should link to that method's docs from here.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 15 2026 at 19:53):

:speech_balloon: fitzgen created PR review comment:

The last one is not an empty range?

view this post on Zulip Wasmtime GitHub notifications bot (Nov 06 2026 at 01:55):

josefguenther updated PR #14714.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 06 2026 at 02:10):

josefguenther edited PR #14714:

Closes #9572.

This adds Memory::discard(store, offset, len), the host API fitzgen sketched in #9572. It zeroes the pages of a non-shared memory that a byte range touches, and gives them back to the OS where Wasmtime owns the mapping. Like the proposed memory.discard instruction, it takes a byte offset and length and rounds them out to whole pages: the start is rounded down and the end up, so every page the range touches is discarded. The memory's size and base pointer don't change, and the pages read as zeros if they are touched again. Linear memory can't shrink, so this lets an embedder whose guest allocator frees large spans return that memory instead of holding the guest's high-water mark until the instance is dropped.

How it works:

The tests cover each of these cases, including every way a range can overlap an image, with the pages written first and untouched, and a C++ test covers the new binding.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 06 2026 at 02:10):

josefguenther commented on PR #14714:

Thanks for the review! I rebased onto main, since the memory layer changed under this PR (discard is now a provided method on LinearMemory), and addressed your comments in separate commits on top. It now takes a byte range rounded out to whole pages, like the proposed instruction, so I've updated the description to match.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 06 2026 at 02:10):

:memo: josefguenther submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 06 2026 at 02:10):

:speech_balloon: josefguenther created PR review comment:

Done. MmapMemory::discard and the pooling slot's discard now say that an out-of-bounds range panics and that callers check bounds first, as Memory::discard does.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 06 2026 at 02:10):

:memo: josefguenther submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 06 2026 at 02:10):

:speech_balloon: josefguenther created PR review comment:

The comment was about the reset after a discard, but I agree it read the wrong way round. Restoring the image mapping brings the image back, which is why discard maps anonymous memory over those pages. After that the reset has no image mapping left to restore. I've reworded it.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 06 2026 at 02:10):

:memo: josefguenther submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 06 2026 at 02:10):

:speech_balloon: josefguenther created PR review comment:

Done.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 06 2026 at 02:10):

:memo: josefguenther submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 06 2026 at 02:10):

:speech_balloon: josefguenther created PR review comment:

The anonymous ranges are decommitted with madvise(MADV_DONTNEED), which keeps the existing mapping and its protection key. Only the overlap gets a fresh mmap, so only it needs the key back. In the Zero branch the whole range is a fresh mapping. I added a comment saying so.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 06 2026 at 02:10):

:memo: josefguenther submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 06 2026 at 02:10):

:speech_balloon: josefguenther created PR review comment:

Fixed: the parameters are now offset and len, in bytes like the instruction (see below).

view this post on Zulip Wasmtime GitHub notifications bot (Nov 06 2026 at 02:11):

:memo: josefguenther submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 06 2026 at 02:11):

:speech_balloon: josefguenther created PR review comment:

Done, it takes offset and len in bytes now. The proposal says the address and size are "aligned down and up respectively", which is ambiguous: rounding the size up on its own can miss the end of a range that crosses a page boundary, like 0xF000 + 0x2000. I read it as rounding the start down and the end (offset + len) up, so the whole requested range is covered. Out of bounds is checked on the range as given and once rounded, and a zero length does nothing. The docs say so, and the tests cover the edges. The C and C++ APIs take size_t bytes to match.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 06 2026 at 02:11):

:memo: josefguenther submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 06 2026 at 02:11):

:speech_balloon: josefguenther created PR review comment:

Agreed. The docs now say that how the pages are zeroed depends on the memory, and that without virtual memory it writes zeros. I'll open an issue for a way to ask which one a memory does, so we don't forget it.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 06 2026 at 02:11):

:memo: josefguenther submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 06 2026 at 02:11):

:speech_balloon: josefguenther created PR review comment:

Right. After rebasing onto main, where the internal trait is gone, discard is a provided method on the public LinearMemory that writes zeros. A custom memory can override it to release its own pages, and the docs on Memory::discard now link it.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 06 2026 at 02:11):

:memo: josefguenther submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 06 2026 at 02:11):

:speech_balloon: josefguenther created PR review comment:

Added both. The image test now discards a range before the image, across its start, inside it, exactly over it, across its end, containing it, and after it. It runs with on-demand memories and with the pooling allocator, with a fresh store per case so the slot is reset and reused in between.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 06 2026 at 02:11):

:memo: josefguenther submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 06 2026 at 02:11):

:speech_balloon: josefguenther created PR review comment:

Each of those cases now runs dirty, with every page written first, and clean, with the pages untouched since instantiation.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 06 2026 at 02:11):

:memo: josefguenther submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 06 2026 at 02:11):

:speech_balloon: josefguenther created PR review comment:

Right, it wasn't. The empty cases now all have a zero length, including one at an unaligned offset and one at the end.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 06 2026 at 19:09):

:speech_balloon: fitzgen created PR review comment:

const DEFAULT_WASM_PAGE: usize = 0x10000;

view this post on Zulip Wasmtime GitHub notifications bot (Nov 06 2026 at 19:09):

:speech_balloon: fitzgen created PR review comment:

    /// offset and length are multiples of this memory's page size.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 06 2026 at 19:09):

:memo: fitzgen submitted PR review:

Thanks! LGTM, just a last couple nitpicks before this lands.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 09 2026 at 14:30):

:repeat: alexcrichton submitted PR review:

Reading over this a bit I'm personally pretty concerned about the partial failure aspect I document below. I think that this'll want to probably make discard an unsafe function, and clearly document that it's kind of UB if an error is returned.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 09 2026 at 14:30):

:speech_balloon: alexcrichton created PR review comment:

This to me seems pretty dangerous where the naming "discard" of this function seems to indicate that it would map the range specified here to zero, but in reality this is not actually doing that on Linux and is restoring the mapping which the page ranges originally had. Could an alternative name be considered for this backend-style function which doesn't mention "discard" and more accurately reflects what it's doing?

Or, alternatively, why make a new function here as opposed to using the preexisting decommit_pages?

view this post on Zulip Wasmtime GitHub notifications bot (Nov 09 2026 at 14:30):

:speech_balloon: alexcrichton created PR review comment:

This documentation does not match the implementation because if an error is returned then the memory is basically left in an undefined state. For example it could be unmapped and then fail to re-map. On Unix there are many code paths I see implemented here which just perform a sequence of operations using ? at the end, and failure in the middle doesn't do any cleanup.

IIRC this is one of the main holdups on the native wasm memory.discard, what to do on partial failure, and I don't think that's addressed in this PR. I don't know if that means that this implementation is suitable for use, but I think that the documentation should at least be clear here in indicating that on failure access to the memory is basically UB as it could fault, it could return zeros, it could return the original mapping, etc.

The faulting behavior probably means that this function needs to be unsafe as well.


Last updated: Nov 19 2026 at 20:05 UTC