Skip to content

Conversation

@alexcrichton
Copy link
Member

@alexcrichton alexcrichton commented Jul 16, 2025

This is some more progress on #11179 aimed at improving the safety of management of tables internally within Wasmtime:

  • Instance::table_index is removed as it can be replaced with data stored directly in the VMTableImport now.
  • Instance::get_table now returns &mut Table
  • Instance::get_defined_table_with_lazy_init now returns &mut Table
  • Instance::with_defined_table_index_and_instance now directly returns DefinedTableIndex plus Pin<&mut Instance>, codifying the ability to "laterally move" between instances.
  • Instance::table_init_segment was refactored to "take" the tables during initialization and replace them afterwards, resolving the split borrow issue and removing an unsafe block in the function.

Closes #11179

This is some more progress on bytecodealliance#11179 aimed at improving the safety of
management of tables internally within Wasmtime:

* `Instance::table_index` is removed as it can be replaced with data
  stored directly in the `VMTableImport` now.
* `Instance::get_table` now returns `&mut Table`
* `Instance::get_defined_table_with_lazy_init` now returns `&mut Table`
* `Instance::with_defined_table_index_and_instance` now directly returns
  `DefinedTableIndex` plus `Pin<&mut Instance>`, codifying the ability
  to "laterally move" between instances.
* `Instance::table_init_segment` was refactored to "take" the tables
  during initialization and replace them afterwards, resolving the split
  borrow issue and removing an `unsafe` block in the function.

cc bytecodealliance#11179
@alexcrichton alexcrichton requested a review from a team as a code owner July 16, 2025 16:07
@alexcrichton alexcrichton requested review from pchickey and removed request for a team July 16, 2025 16:07
@alexcrichton alexcrichton added this pull request to the merge queue Jul 16, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jul 16, 2025
@github-actions github-actions bot added the wasmtime:api Related to the API of the `wasmtime` crate itself label Jul 16, 2025
This commit fixes an issue in the previous commit with respect to Miri
and Stacked Borrows. This does so by improving the safety of the
`Table::copy`-related functions to all work mostly on safe code rather
than unsafe references. Some minor amount of unsafety is still present
but it is now clearly documented and easier to verify.
@alexcrichton alexcrichton requested a review from a team as a code owner July 16, 2025 18:43
@alexcrichton alexcrichton requested review from fitzgen and removed request for a team July 16, 2025 18:43
@alexcrichton
Copy link
Member Author

The Miri violation of stacked borrows is I believe legitimate, so I've added a new commit which improves the safety situation around Table::copy. @pchickey mind reviewing that new commit as well?

@github-actions github-actions bot added the cranelift Issues related to the Cranelift code generator label Jul 16, 2025
@alexcrichton
Copy link
Member Author

I've also flagged this as closing #11179 as this resolves all remaining bits I know of there, and I otherwise split out #11262 from that issue.

@pchickey pchickey added this pull request to the merge queue Jul 17, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jul 17, 2025
@alexcrichton alexcrichton added this pull request to the merge queue Jul 17, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jul 17, 2025
@alexcrichton alexcrichton added this pull request to the merge queue Jul 17, 2025
Merged via the queue into bytecodealliance:main with commit f021346 Jul 17, 2025
42 checks passed
@alexcrichton alexcrichton deleted the more-safer-tables branch July 17, 2025 17:40
fitzgen added a commit to fitzgen/wasmtime that referenced this pull request Jul 24, 2025
The `match` makes the cases a lot more clear IMO, and additionally the
conditions asserted in each branch become trivially obvious (they are exactly
what was matched upon for their associated branch) so I removed them.

I also made some minor copy-edit tweaks to some comments while I was here.

This would have just been a nitpick review comment on
bytecodealliance#11255 but I was traveling and
didn't get a chance to leave review comments in a reasonable amount of time, so
instead I am just fixing them up myself in this follow up.
github-merge-queue bot pushed a commit that referenced this pull request Jul 24, 2025
The `match` makes the cases a lot more clear IMO, and additionally the
conditions asserted in each branch become trivially obvious (they are exactly
what was matched upon for their associated branch) so I removed them.

I also made some minor copy-edit tweaks to some comments while I was here.

This would have just been a nitpick review comment on
#11255 but I was traveling and
didn't get a chance to leave review comments in a reasonable amount of time, so
instead I am just fixing them up myself in this follow up.
bongjunj pushed a commit to prosyslab/wasmtime that referenced this pull request Oct 20, 2025
* More table safety improvements

This is some more progress on bytecodealliance#11179 aimed at improving the safety of
management of tables internally within Wasmtime:

* `Instance::table_index` is removed as it can be replaced with data
  stored directly in the `VMTableImport` now.
* `Instance::get_table` now returns `&mut Table`
* `Instance::get_defined_table_with_lazy_init` now returns `&mut Table`
* `Instance::with_defined_table_index_and_instance` now directly returns
  `DefinedTableIndex` plus `Pin<&mut Instance>`, codifying the ability
  to "laterally move" between instances.
* `Instance::table_init_segment` was refactored to "take" the tables
  during initialization and replace them afterwards, resolving the split
  borrow issue and removing an `unsafe` block in the function.

cc bytecodealliance#11179

* Improve safety of `Table::copy`

This commit fixes an issue in the previous commit with respect to Miri
and Stacked Borrows. This does so by improving the safety of the
`Table::copy`-related functions to all work mostly on safe code rather
than unsafe references. Some minor amount of unsafety is still present
but it is now clearly documented and easier to verify.

* Fix tests
bongjunj pushed a commit to prosyslab/wasmtime that referenced this pull request Oct 20, 2025
The `match` makes the cases a lot more clear IMO, and additionally the
conditions asserted in each branch become trivially obvious (they are exactly
what was matched upon for their associated branch) so I removed them.

I also made some minor copy-edit tweaks to some comments while I was here.

This would have just been a nitpick review comment on
bytecodealliance#11255 but I was traveling and
didn't get a chance to leave review comments in a reasonable amount of time, so
instead I am just fixing them up myself in this follow up.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cranelift Issues related to the Cranelift code generator wasmtime:api Related to the API of the `wasmtime` crate itself

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Management of tables in Wasmtime is excessively unsafe

2 participants