Skip to content

Comments

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

Open
NandiniDhanrale wants to merge 1 commit intogoogle-deepmind:mainfrom
NandiniDhanrale:fix/vfs-move-3098
Open

Fix VFS use-after-move crash by using stable handles for mjVFS#3105
NandiniDhanrale wants to merge 1 commit intogoogle-deepmind:mainfrom
NandiniDhanrale:fix/vfs-move-3098

Conversation

@NandiniDhanrale
Copy link

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.
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

1 participant