A sophisticated C compiler implementation featuring lexical analysis, parsing, and custom memory management with comprehensive debugging and sanitization capabilities.
- Lexical Analyzer: Built with Flex for efficient token recognition
- Parser: Implemented using Yacc/Bison for syntax analysis
- Symbol Table: Advanced tracking of identifiers, types, scopes, and line numbers
- Constant Table: Efficient management of string and numeric constants
- Abstract Syntax Tree (AST): Visual representation of code structure
- Custom Memory Management: Pool-based allocation with reference counting
- Memory Pool: Pre-allocated memory space for efficient allocation
- Reference Counting: Tracks object lifetime and prevents memory leaks
- Block Headers: Metadata tracking for each allocated block
- Free List: Efficient reuse of freed memory blocks
- Dynamic memory pool initialization
- Automatic memory cleanup
- Memory usage statistics
- Peak memory tracking
- Reference count management
- Memory leak prevention
┌────────────┬──────────┬──────────┬───────────┬──────────┐
│ SYMBOL │ TYPE │ CLASS │ SCOPE │ LINE │
├────────────┼──────────┼──────────┼───────────┼──────────┤
│ ... │ ... │ ... │ ... │ ... │
└────────────┴──────────┴──────────┴───────────┴──────────┘
┌────────────────────────┬────────────────────────┐
│ CONSTANT │ TYPE │
├────────────────────────┼────────────────────────┤
│ ... │ ... │
└────────────────────────┴────────────────────────┘
╔════════════════════════════════════╗
║ Memory Pool Statistics ║
╠════════════════════════════════════╣
║ Total allocations: xxx ║
║ Total frees: xxx ║
║ Peak memory used: xxx ║
║ Memory pool size: xxx ║
╚════════════════════════════════════╝
make clean # Clean previous build artifacts
make # Build the compiler- Address Sanitizer (ASan)
make debug
ASAN_OPTIONS=detect_leaks=1:print_stacktrace=1 ./compiler- Memory Leak Check
make memcheck- Thread Sanitizer
make thread- Undefined Behavior Detection
make undefined-
Memory Error Detection
- Buffer overflows (stack, heap, global)
- Use-after-free
- Use-after-return
- Use-after-scope
- Double-free
- Memory leaks
- Initialization issues
-
How It Works
- Instruments memory accesses at compile time
- Maintains shadow memory to track memory state
- Checks every memory operation for validity
- Provides detailed error reports with stack traces
- Buffer Overflow
char *array = malloc(10);
strcpy(array, "This is too long"); // ASan will detect this- Use After Free
int *ptr = malloc(sizeof(int));
free(ptr);
*ptr = 42; // ASan will detect this- Memory Leak
int *leak = malloc(sizeof(int));
// No free - ASan will report leakASan provides detailed error reports including:
- Error type and location
- Stack trace of the error
- Allocation/deallocation history
- Memory state visualization
- Shadow memory mapping
- Pre-allocated large memory block
- Reduces system call overhead
- Enables efficient memory reuse
struct BlockHeader {
size_t size; // Size of the block
int ref_count; // Reference counter
struct BlockHeader *next_free; // Free list pointer
};-
Allocation
- Search free list for suitable block
- Split blocks if necessary
- Update metadata and statistics
-
Deallocation
- Decrease reference count
- Merge adjacent free blocks
- Add to free list when ref_count = 0
-
Reference Counting
- Track object usage
- Automatic cleanup when no references remain
- Prevents memory leaks
- Always initialize the memory pool before use
- Check allocation return values
- Properly maintain reference counts
- Clean up memory pool on program exit
- Use ASan for development builds
- Enable stack traces for detailed error reports
- Monitor memory statistics
- Regular memory leak checks
- Flex: Lexical analyzer generator
- Bison: Parser generator
- GCC: C compiler with sanitizer support
- Make: Build automation
This project is open source and available under the MIT License.
- Fork the repository
- Create a feature branch
- Submit a pull request
- Ensure tests pass
- Update documentation