Registry Mirror
Skip to content

Conversation

@clingfei

@clingfei clingfei commented Oct 10, 2026 •

Copy link
Copy Markdown

In MMU mode, shmem_init() allocates POSIX shared memory via shm_open() to back LKL physical memory. Two issues currently exist:

  1. The shared memory object is never unlinked, so it persists after the host process exits or crashes, potentially exhausting /dev/shm space during repeated testing or fuzzing.

  2. The backing file descriptor is not closed when LKL shuts down. Reinitialization within the same host process overwrites shared_mem_fd without closing the previous descriptor, leaking file descriptors.

Fix these by:

  • Calling shm_unlink() immediately after shm_open(), keeping the descriptor available for subsequent mappings.
  • Adding an optional shmem_cleanup host operation, called from free_mem() in MMU mode, to close shared_mem_fd and reset it to -1 on shutdown.
  • Executing shm_unlink() and ftruncate() outside assert() so both calls are preserved when NDEBUG is defined.

Closes: #629

@github-actions

github-actions Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Test Results

111 files  ±0  111 suites  ±0   11m 27s ⏱️ -1s
216 tests ±0  205 ✅ ±0  11 💤 ±0  0 ❌ ±0 
828 runs  ±0  770 ✅ ±0  58 💤 ±0  0 ❌ ±0 

Results for commit 6755eb9. ± Comparison against base commit 6a9ae43.

♻️ This comment has been updated with latest results.

@tavip tavip left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry for taking so long to review, and thanks for the fix! A couple of things before merging:

  1. Commit message mismatch: Please update the commit message and PR description to match the diff — it currently mentions setting FD_CLOEXEC (already guaranteed by POSIX shm_open()) and closing an existing shared_mem_fd in shmem_init(), neither of which is in the patch.

  2. Avoid putting shm_unlink() inside assert() (tools/lkl/lib/posix-host.c): -DNDEBUG compiles it out completely. Please capture the return value first (e.g. ret = shm_unlink(...); assert(ret == 0);, and ideally the same for ftruncate()).

@clingfei
clingfei force-pushed the fix branch 2 times, most recently from 800d8b0 to 3d59b01 Compare November 30, 2026 13:05
@clingfei

Copy link
Copy Markdown
Author

Sorry for taking so long to review, and thanks for the fix! A couple of things before merging:

  1. Commit message mismatch: Please update the commit message and PR description to match the diff — it currently mentions setting FD_CLOEXEC (already guaranteed by POSIX shm_open()) and closing an existing shared_mem_fd in shmem_init(), neither of which is in the patch.
  2. Avoid putting shm_unlink() inside assert() (tools/lkl/lib/posix-host.c): -DNDEBUG compiles it out completely. Please capture the return value first (e.g. ret = shm_unlink(...); assert(ret == 0);, and ideally the same for ftruncate()).

Fixed. Thanks!

Btw, the Windows CI build is failing because flex is missing from the MSYS2 environment. I'll send a separate PR to explicitly install flex and bison.

In MMU mode, the POSIX shared memory object persists after process exit
because it is never unlinked. Its backing file descriptor is also left
open when LKL shuts down.

Unlink the object immediately after shm_open(), keeping the descriptor
available for subsequent mappings. Add an optional shmem_cleanup host
operation, called from free_mem() in MMU mode, to close shared_mem_fd
and reset it to -1 on shutdown.

Execute shm_unlink() and ftruncate() outside assert() so both calls are
preserved when NDEBUG is defined.

Closes: lkl#629

Signed-off-by: Cheng Lingfei <chenglingfei@foxmail.com>
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.

MMU lkl_phys_mem shm leak

2 participants