Skip to content

Conversation

@ryanbreen
Copy link
Owner

Summary

  • Add load_elf_from_ext2() helper function that loads ELF binaries from the ext2 filesystem with proper permission checks
  • Modify sys_execv_with_frame() to try ext2 filesystem first, with test disk fallback for backward compatibility

Changes

New helper function load_elf_from_ext2():

  • Resolves path to inode using ext2 driver
  • Validates file is not a directory (returns ENOTDIR)
  • Checks execute permission bit S_IXUSR (returns EACCES if not set)
  • Returns file content as Vec

Modified exec syscall behavior:

  • For paths containing / (e.g., /bin/ls): load directly from ext2 filesystem
  • For bare names (e.g., ls): try /bin/<name> on ext2 first, fall back to test disk

Test plan

  • Build completes with zero warnings
  • All 210 boot stages pass (backward compatibility verified)
  • Manual test with binaries installed to ext2 /bin directory (requires ext2 tools)

Next steps (Phase 2b)

  • Update xtask to install compiled binaries to ext2 image during build
  • Add automated test that verifies loading from ext2

🤖 Generated with Claude Code

Add load_elf_from_ext2() helper function that loads ELF binaries from
the ext2 filesystem with proper permission checks:
- Resolves path to inode using ext2 driver
- Validates file is not a directory (ENOTDIR)
- Checks execute permission bit S_IXUSR (EACCES)
- Returns file content as Vec<u8>

Modify sys_execv_with_frame() to try ext2 filesystem first:
- For paths containing '/': load directly from ext2
- For bare names: try /bin/<name> on ext2, fall back to test disk

This enables exec("/bin/ls", argv) to load programs from the ext2
filesystem, preparing for full Unix-like program execution. The
test disk fallback ensures backward compatibility during transition.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@cursor
Copy link

cursor bot commented Jan 13, 2026

You have run out of free Bugbot PR reviews for this billing cycle. This will reset on February 12.

To receive reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.

ryanbreen and others added 4 commits January 13, 2026 18:03
Fixed critical bugs in ext2 block allocation that caused filesystem
corruption when writing files:

1. allocate_block: Was calculating global_block as (bg_index * blocks_per_group + local_block),
   but ext2 bitmap bit 0 represents block s_first_data_block (usually 1 for 1KB blocks),
   not block 0. Fixed to add s_first_data_block to the calculation.

2. free_block: Same off-by-one error in reverse - was calculating local_block as
   (block_num % blocks_per_group) instead of ((block_num - s_first_data_block) % blocks_per_group).

3. truncate_file: Was clearing inode block pointers without freeing the blocks in the
   bitmap, causing block leaks. When O_TRUNC reopened a file, the old block stayed
   marked "in use" so allocate_block would return a different (already-in-use) block,
   corrupting unrelated files.

The combination of these bugs caused /bin directory corruption: when fs_write_test
truncated and rewrote hello.txt, it would allocate /bin's data block (296) instead
of hello.txt's original block (290), overwriting the directory entries.

Also adds exec_from_ext2_test which verifies:
- exec("/bin/hello_world") loads and runs binary from ext2
- exec("/nonexistent") returns ENOENT
- exec("/hello.txt") returns EACCES (not executable)
- exec("/test") returns ENOTDIR/EACCES for directories

All 216 boot stages now pass.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Add kernel unit tests and userspace regression tests to validate the
ext2 block allocation fixes from commit d190da5.

Kernel unit tests (kernel/src/fs/ext2/block_group.rs):
- 11 tests with MockBlockDevice for allocate_block/free_block arithmetic
- test_allocate_block_1kb_first_data_block_offset: bit 0 → block 1
- test_allocate_block_bit_295_returns_block_296: exact bug scenario
- test_free_block_1kb_offset: block 296 clears bit 295
- test_truncate_frees_blocks_to_bitmap: multi-block free + realloc

Userspace regression test (userspace/tests/fs_block_alloc_test.rs):
- Test 1: O_TRUNC properly frees blocks (not just clears size)
- Test 2: Multi-file corruption regression (reproduces original bug)
- Test 3: Block reuse after truncate

These tests provide regression safety for the s_first_data_block offset
bugs in allocate_block() and free_block(), and the block leak bug in
truncate_file().

Co-Authored-By: Claude Opus 4.5 <[email protected]>
This file was referenced in Cargo.toml but not committed, causing CI failure.
simple_exit.rs is a minimal test binary that exits with code 42, used for
testing exec from ext2 filesystem.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
The load_elf_from_ext2() function was logging 10+ messages per exec
syscall, adding significant overhead from serial I/O. This caused
timing issues in CI's slower environment, breaking TCP tests.

Since most binaries fall back to the test disk, every exec was:
1. Logging "loading '/bin/program_name'"
2. Scanning ext2 filesystem
3. Logging the failure
4. Falling back to test disk

Removed all logging from the exec path. Debug issues with GDB, not logs.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@ryanbreen ryanbreen merged commit 6c4f8f0 into main Jan 14, 2026
1 check 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.

2 participants