Skip to content

Fix VFS use-after-move crash by using stable handles for mjVFS#3105

Merged
copybara-service[bot] merged 1 commit intogoogle-deepmind:mainfrom
NandiniDhanrale:fix/vfs-move-3098
Mar 11, 2026
Merged

Fix VFS use-after-move crash by using stable handles for mjVFS#3105
copybara-service[bot] merged 1 commit intogoogle-deepmind:mainfrom
NandiniDhanrale:fix/vfs-move-3098

Conversation

@NandiniDhanrale
Copy link
Copy Markdown
Contributor

Summary

Fixes #3098.

mjResource::vfs and default mounts could hold pointers tied to the public mjVFS address. If wrappers moved mjVFS after mj_defaultVFS, close/read paths could hit stale pointers and crash.

This change stores a stable internal mjVFS handle inside mujoco::user::VFS, points resources/default mount to that stable handle, and tracks the current public owner pointer. Upcast now rebinds the owner pointer so lifecycle operations target the current mjVFS address after moves.

Code changes

  • src/user/user_vfs.h
    • Add stable_vfs_, owner_, and Bind(mjVFS*).
  • src/user/user_vfs.cc
    • Initialize stable handle (stable_vfs_.impl_ = this).
    • Use stable handle for default_mount_.vfs and
      es->vfs in CreateResource.
    • Use owner_ in self-destruct path.
    • Rebind owner in Upcast.
  • est/user/user_vfs_test.cc
  • Add UserVfsTest.MoveVfsAfterOpenResource regression test.

Validation

  • Built user_vfs_test on Windows (VS 2022 toolchain).
  • Ran new regression test: UserVfsTest.MoveVfsAfterOpenResource (PASS).
  • Ran full user_vfs_test suite excluding AddFile fixture-path artifact (14/14 PASS).
  • Ran issue reproducer (WrapperVfs C sample from VFS relies on mjVFS not being moved, resulting in segmentation faults #3098) against patched mujoco.dll (prints Exiting ok, no segfault).

Root cause: resources and default mount stored pointers tied to the public mjVFS object address. If mjVFS was moved after initialization, resource close/read paths could dereference stale pointers and crash.\n\nThis change stores a stable internal mjVFS handle inside VFS and points resources/default mount to that stable handle. It also tracks the current public owner pointer and rebinds it in Upcast so lifecycle operations (including self-destruct cleanup) target the current mjVFS address after moves.\n\nAdds regression test UserVfsTest.MoveVfsAfterOpenResource, which opens a resource, moves mjVFS, then reads/closes/deletes successfully.
// This supports scenarios where the public mjVFS struct is moved.
void Bind(mjVFS* vfs);

mjVFS stable_vfs_;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please add a comment about what makes this "stable".

}
}

void VFS::Bind(mjVFS* vfs) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Let's remove the owner_ member and the Bind functionality entirely.

We only need this pointer in order to perform the self-destruction. However, we only call SetToSelfDestruct from one place in the code. We can change that call site to capture the mjVFS it should destroy rather than having to keep the owner_ up-to-date on each bind.

@haroonq
Copy link
Copy Markdown
Collaborator

haroonq commented Mar 9, 2026

Thanks for this fix! I just have a couple small suggestions, but otherwise it looks good to me.

@copybara-service copybara-service bot merged commit 64ee2d0 into google-deepmind:main Mar 11, 2026
5 of 20 checks passed
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.

VFS relies on mjVFS not being moved, resulting in segmentation faults

2 participants