Skip to content
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

Move MemoryImageSource::map_at to mmap module #9687

Merged
merged 2 commits into from
Dec 7, 2024

Conversation

sunshowers
Copy link
Contributor

@sunshowers sunshowers commented Nov 26, 2024

This is part of the work to centralize memory management into the mmap
module. This commit introduces a few structures which aid in that process, and
starts converting one of the functions (MemoryImageSource::map_at) into this
module.

The structures introduced are:

  • MemoryBase: RuntimeLinearMemory::base_ptr is now
    RuntimeLinearMemory::base, which returns a MemoryBase. This is either a
    raw pointer or an MmapOffset as described below.

  • MmapOffset: A combination of a reference to an mmap and an offset into it.
    Logically represents a pointer into a mapped section of memory.

In future work, we'll move more image-mapping code over to Mmap instances.

Comment on lines 540 to 546
// Note that this code would be incorrect if clear-on-drop
// were enabled. That's because in the struct definition,
// `memory_image` above is listed after `alloc`. Rust drops
// fields in the order they're defined, so `memory_image`
// would be dropped after `alloc`. If clear-on-drop were
// enabled, `memory_image` would end up remapping a bunch of
// memory after it was unmapped by the `alloc` drop.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was a subtle dependency that took me a bit to understand.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(And this is only dangerous when alloc represents an owned memory allocation. It's fine if alloc is borrowed. This is a subtle point.)

Copy link
Member

Choose a reason for hiding this comment

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

Do you think it'd perhaps be better to remove this no_clear_on_drop and instead perform it in Drop for LocalMemory? (where that could .take() and explicitly do the image stuff before dropping the mmap)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh that's a good idea, similar to MemoryPool. I think it's correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh actually there's another subtle issue here, I think if slot.instantiate below fails, we should clear on drop (that's part of the point of clear-on-drop, right?) So I think it would be best to make this change in a followup since it's a behavior change, not a refactor.

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've expanded this comment -- it would definitely be good to move this to the destructor, but it would be a behavior change so it should be a followup. This PR itself should not contain any behavior changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, and adding a Drop impl identified the problem with unwrap_static_image, haha, since you can't move out of a type when there's a Drop impl.

Copy link
Member

Choose a reason for hiding this comment

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

Ah ok that all makes sense to me yeah, thanks for checking!

@github-actions github-actions bot added the wasmtime:api Related to the API of the `wasmtime` crate itself label Nov 26, 2024
Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

👍

Comment on lines 540 to 546
// Note that this code would be incorrect if clear-on-drop
// were enabled. That's because in the struct definition,
// `memory_image` above is listed after `alloc`. Rust drops
// fields in the order they're defined, so `memory_image`
// would be dropped after `alloc`. If clear-on-drop were
// enabled, `memory_image` would end up remapping a bunch of
// memory after it was unmapped by the `alloc` drop.
Copy link
Member

Choose a reason for hiding this comment

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

Do you think it'd perhaps be better to remove this no_clear_on_drop and instead perform it in Drop for LocalMemory? (where that could .take() and explicitly do the image stuff before dropping the mmap)

@sunshowers sunshowers marked this pull request as ready for review December 6, 2024 22:42
@sunshowers sunshowers requested a review from a team as a code owner December 6, 2024 22:42
@sunshowers sunshowers requested review from fitzgen and removed request for a team December 6, 2024 22:42
@sunshowers sunshowers changed the title [draft] show how Arc<Mmap> would work Move MemoryImageSource::map_at to mmap module Dec 6, 2024
@alexcrichton alexcrichton added this pull request to the merge queue Dec 6, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Dec 6, 2024
@sunshowers
Copy link
Contributor Author

Fixing the issues

@sunshowers
Copy link
Contributor Author

Hmm, actually I want to rename the os-specific Mmap as_ptr instances.

Return a single `SendSyncPtr` -- the platform-independent context converts to
the various raw pointer types as desired. This simplifies upcoming work where I
wanted to return a `SendSyncPtr`.
This is part of the work to centralize memory management into the `mmap`
module. This commit introduces a few structures which aid in that process, and
starts converting one of the functions (`MemoryImageSource::map_at`) into this
module.

The structures introduced are:

* `MemoryBase`: `RuntimeLinearMemory::base_ptr` is now
  `RuntimeLinearMemory::base`, which returns a `MemoryBase`. This is either a
  raw pointer or an `MmapOffset` as described below.

* `MmapOffset`: A combination of a reference to an mmap and an offset into it.
  Logically represents a pointer into a mapped section of memory.

In future work, we'll move more image-mapping code over to `Mmap` instances.
Comment on lines +137 to +143
if VirtualProtect(
ret.as_send_sync_ptr().as_ptr().cast(),
ret.len(),
PAGE_WRITECOPY,
&mut old,
) == 0
{
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed some of this code's pointer typology (though not the arithmetic) -- would be worth a look.

@alexcrichton alexcrichton added this pull request to the merge queue Dec 6, 2024
Merged via the queue into bytecodealliance:main with commit d5ee2a0 Dec 7, 2024
42 checks passed
alexcrichton added a commit to alexcrichton/wasmtime that referenced this pull request Dec 7, 2024
Fuzzing found a small issue with bytecodealliance#9687 and this commit relaxes a few
off-by-one checks to allow addressing one-byte-beyond-the-end of a
linear memory.
github-merge-queue bot pushed a commit that referenced this pull request Dec 10, 2024
* Fix some off-by-one comparisons in assertions

Fuzzing found a small issue with #9687 and this commit relaxes a few
off-by-one checks to allow addressing one-byte-beyond-the-end of a
linear memory.

* Review comments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wasmtime:api Related to the API of the `wasmtime` crate itself
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants