Skip to content

Conversation

jserv
Copy link
Collaborator

@jserv jserv commented Aug 17, 2025

This pull request adds memory safety improvements:

  1. Add sanitizer support to Makefile - Added build targets for AddressSanitizer and UndefinedBehaviorSanitizer
  2. Fix arena allocator alignment for 64-bit hosts - Fixed memory alignment bug that caused crashes on 64-bit systems
  3. Replace arena_alloc with arena_calloc - Changed structure allocations to zero-initialize memory, preventing undefined behavior
  4. Fix parser crash on pointer dereference assignment - Fixed crash when parsing statements like *ptr = value
  5. Add sanitizer validation to CI pipeline - Integrated sanitizer testing into GitHub Actions workflow

Summary by Bito

This pull request enhances memory safety by integrating sanitizer support, fixing memory alignment issues for 64-bit systems, and replacing arena_alloc with arena_calloc to ensure zero-initialized memory. It also addresses a parser crash related to pointer dereferencing, improving overall stability.

jserv added 5 commits August 17, 2025 13:49
Add infrastructure for building and testing shecc with AddressSanitizer
and UndefinedBehaviorSanitizer to detect memory safety issues.
- Add 'sanitizer' target that builds stage 0 with sanitizers enabled
- Add 'check-sanitizer' target for running tests with sanitizer build
- Use -fsanitize=address -fsanitize=undefined flags
- Add -fno-omit-frame-pointer for better stack traces
The arena allocator was using a hardcoded PTR_SIZE (4 bytes) for memory
alignment, which caused misalignment issues on 64-bit host systems where
pointers are 8 bytes. This led to UndefinedBehaviorSanitizer errors.
Changed all structure allocations to use arena_calloc instead of arena_alloc
to ensure zero-initialization and eliminate undefined behavior from accessing
uninitialized memory. This fixes sanitizer errors related to:
- block_t, var_t, basic_block_t structures in parsing
- insn_t structures in SSA passes
- ph2_ir_t structures in register allocation
- constant_t, alias_t, macro_t structures in symbol management
Fixed a parser crash that occurred when encountering pointer dereference
assignment statements like '*ap = 0;'. The issue was that the parser
was trying to find a function named '*ap' before properly handling
the pointer dereference syntax.

The fix adds a has_asterisk flag to detect when the statement begins
with an asterisk operator and skips the function call check in this case,
allowing the existing pointer dereference handling code to process
the statement correctly.
Integrate AddressSanitizer and UndefinedBehaviorSanitizer testing into
the GitHub Actions workflow to catch memory safety and undefined behavior
issues early in the development cycle.
@jserv jserv merged commit a235e67 into master Aug 17, 2025
12 checks passed
@jserv jserv deleted the sanitizers branch August 17, 2025 06:13
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.

1 participant