Skip to content

Conversation

jserv
Copy link
Contributor

@jserv jserv commented Oct 1, 2025

This adds explicit NULL checks for all malloc/calloc/mpool_alloc calls that previously relied solely on assert(), which compiles out with -DNDEBUG. It prevents NULL pointer dereferences in production builds.


Summary by cubic

Adds NULL checks and graceful fallbacks for critical allocations across the emulator, JIT, cache, IO, and syscalls. Prevents crashes when assert() is disabled and degrades behavior safely on OOM.

  • Bug Fixes
    • Block/IR/branch-table allocations now checked; translation can fail cleanly.
    • Fused operation generation falls back to non-fused ops if malloc fails.
    • JIT init and queue entry allocations validated; cleanup on failure; skip tier-2 enqueue on OOM.
    • Cache put restores the replaced entry if new allocation fails.
    • IO, syscall, and riscv init guard malloc/calloc; return NULL or -1 instead of crashing.

cubic-dev-ai[bot]

This comment was marked as outdated.

Copy link
Contributor Author

@jserv jserv left a comment

Choose a reason for hiding this comment

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

Benchmarks

Benchmark suite Current: adab7d7 Previous: 9220ffb Ratio
Dhrystone 1302 Average DMIPS over 10 runs 1267 Average DMIPS over 10 runs 0.97
Coremark 952.843 Average iterations/sec over 10 runs 958.558 Average iterations/sec over 10 runs 1.01

This comment was automatically generated by workflow using github-action-benchmark.

@jserv jserv force-pushed the memory-safety branch 2 times, most recently from 554be67 to 9220ffb Compare October 1, 2025 20:30
@sysprog21 sysprog21 deleted a comment from cubic-dev-ai bot Oct 1, 2025
@sysprog21 sysprog21 deleted a comment from cubic-dev-ai bot Oct 1, 2025
cubic-dev-ai[bot]

This comment was marked as outdated.

cubic-dev-ai[bot]

This comment was marked as outdated.

@visitorckw
Copy link
Collaborator

Perhaps we can consider introducing a fault injection framework down the road to improve test coverage for error handling cases such as this one.

This adds explicit NULL checks for all malloc/calloc/mpool_alloc calls
that previously relied solely on assert(), which compiles out with
'-DNDEBUG'. It prevents NULL pointer dereferences in production builds.
- Add bounds checking in memory_write/memory_fill to prevent guest code
  from accessing arbitrary host memory
- Fix memcpy/memset handlers to validate addresses and raise traps on
  violations (STORE_MISALIGNED/LOAD_MISALIGNED)
- Add overflow-safe arithmetic for all size calculations
- Implement proper error handling with goto-based cleanup chains
- Fix memory leaks in error paths (mpool_extend, syscall_open)
@jserv jserv changed the title Fix critical memory allocation error handling Fix critical memory safety issues Oct 2, 2025
@jserv jserv added this to the release-2025.2 milestone Oct 2, 2025
@jserv jserv merged commit 65a1ec6 into master Oct 2, 2025
46 of 51 checks passed
@jserv jserv deleted the memory-safety branch October 2, 2025 06:45
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