|
| 1 | +# LTO (Link Time Optimization) Status |
| 2 | + |
| 3 | +## Current Status: **NOT WORKING** ❌ |
| 4 | + |
| 5 | +LTO support has been implemented in the build system but exposes serious bugs that prevent the test suite from passing. |
| 6 | + |
| 7 | +## Build System Changes |
| 8 | + |
| 9 | +### CMakeLists.txt |
| 10 | +- Added `LUA_ENABLE_LTO` option (default: OFF) |
| 11 | +- When enabled, sets `CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE` |
| 12 | +- Adds `-fno-strict-aliasing` to handle type punning |
| 13 | +- Adds `-ffat-lto-objects` to reduce LTO aggressiveness |
| 14 | + |
| 15 | +### Usage |
| 16 | +```bash |
| 17 | +cmake -B build -DCMAKE_BUILD_TYPE=Release -DLUA_ENABLE_LTO=ON |
| 18 | +cmake --build build |
| 19 | +``` |
| 20 | + |
| 21 | +## Issues Discovered |
| 22 | + |
| 23 | +### 1. Corrupted Type Values |
| 24 | +**Symptom**: GC objects show invalid type values (e.g., `0xab` = 171) |
| 25 | +**Location**: `GCCore::getgclist()` receives objects with corrupted type fields |
| 26 | +**Failure**: Test suite crashes immediately with assertion failures |
| 27 | + |
| 28 | +### 2. Checkliveness Failures |
| 29 | +**Symptom**: Assertions fail in `checkliveness()` after GC operations |
| 30 | +**Cause**: Memory corruption or incorrect GC state |
| 31 | + |
| 32 | +### 3. Root Cause Analysis |
| 33 | +LTO is exposing **undefined behavior** in the codebase: |
| 34 | + |
| 35 | +- **Strict Aliasing Violations**: Lua uses extensive type punning (same memory read as different types) |
| 36 | +- **Uninitialized Memory**: Some code paths may read memory before initialization |
| 37 | +- **Memory Lifetime Issues**: Objects accessed before construction or after destruction |
| 38 | +- **GC Invariant Violations**: LTO's aggressive inlining/reordering breaks GC assumptions |
| 39 | + |
| 40 | +## Why LTO Breaks This Code |
| 41 | + |
| 42 | +### LTO Optimization Characteristics |
| 43 | +1. **Whole Program Analysis**: Sees all code at once, makes global assumptions |
| 44 | +2. **Aggressive Inlining**: Merges functions that normally wouldn't execute together |
| 45 | +3. **Memory Reordering**: Can change memory layout and access patterns |
| 46 | +4. **Strict Aliasing**: Assumes C++ aliasing rules (Lua violates these) |
| 47 | +5. **UB Exploitation**: Uses undefined behavior for optimizations |
| 48 | + |
| 49 | +### Lua's C Heritage Issues |
| 50 | +The codebase was converted from C to C++, but retains C patterns that violate C++ rules: |
| 51 | +- Type punning through unions (technically UB in C++) |
| 52 | +- Pointer casts that LTO treats as strict aliasing violations |
| 53 | +- Memory layout assumptions that LTO can break |
| 54 | + |
| 55 | +## Code Changes Made |
| 56 | + |
| 57 | +### GC Core (src/memory/gc/gc_core.cpp) |
| 58 | +Added handling for types that can appear in gray list: |
| 59 | +- `LUA_VUPVAL`: Uses base GCObject `next` field for gray list linkage |
| 60 | +- `LUA_VSHRSTR`/`LUA_VLNGSTR`: Added defensive fallback (strings shouldn't be gray) |
| 61 | +- Default case: Returns base `next` pointer instead of asserting (prevents crash) |
| 62 | + |
| 63 | +### GC Weak (src/memory/gc/gc_weak.cpp) |
| 64 | +Removed duplicate `getgclist()` implementation, now forwards to `GCCore::getgclist()` |
| 65 | + |
| 66 | +## Attempted Fixes (All Failed) |
| 67 | + |
| 68 | +1. ✗ Added `-fno-strict-aliasing` - Still crashes |
| 69 | +2. ✗ Changed to `-ffat-lto-objects` - Still crashes |
| 70 | +3. ✗ Added missing type handlers in `getgclist()` - Revealed deeper corruption |
| 71 | +4. ✗ Defensive programming in GC code - Corruption too fundamental |
| 72 | + |
| 73 | +## Path Forward |
| 74 | + |
| 75 | +### Short Term: Disable LTO (Current State) |
| 76 | +- Keep `LUA_ENABLE_LTO` option but default to OFF |
| 77 | +- Document that LTO is experimental and broken |
| 78 | +- Warn users in documentation |
| 79 | + |
| 80 | +### Long Term: Fix Underlying Issues |
| 81 | +To make LTO work, need to eliminate ALL undefined behavior: |
| 82 | + |
| 83 | +1. **Audit Type Punning**: Replace C-style type punning with proper C++ patterns |
| 84 | + - Use `std::bit_cast` (C++20) |
| 85 | + - Use proper variant types |
| 86 | + - Avoid pointer cast hackery |
| 87 | + |
| 88 | +2. **Fix Memory Initialization**: Ensure all objects fully initialized before use |
| 89 | + - Constructor improvements |
| 90 | + - Explicit zero-initialization |
| 91 | + - Valgrind/MSAN audits |
| 92 | + |
| 93 | +3. **GC Invariant Enforcement**: Make GC state transitions explicit and verifiable |
| 94 | + - Add more assertions |
| 95 | + - State machine verification |
| 96 | + - Sanitizer testing |
| 97 | + |
| 98 | +4. **Strict Aliasing Compliance**: Restructure code to follow C++ aliasing rules |
| 99 | + - Eliminate type punning |
| 100 | + - Use proper casts |
| 101 | + - Mark aliasing with attributes |
| 102 | + |
| 103 | +### Estimated Effort |
| 104 | +**High**: 40-80 hours of careful analysis and refactoring |
| 105 | +**Risk**: High - touching GC code is dangerous |
| 106 | +**Benefit**: Modest - LTO typically gives 5-15% performance improvement |
| 107 | + |
| 108 | +## Testing |
| 109 | +Without LTO: ✅ All tests pass (`final OK !!!`) |
| 110 | +With LTO: ❌ Immediate crash in test suite |
| 111 | + |
| 112 | +## Compiler Tested |
| 113 | +- GCC 13.3.0 |
| 114 | +- Linux 4.4.0 |
| 115 | + |
| 116 | +## Recommendation |
| 117 | +**DO NOT enable LTO** until underlying undefined behavior is fixed. |
| 118 | + |
| 119 | +## References |
| 120 | +- GCC LTO docs: https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#index-flto |
| 121 | +- Strict Aliasing: https://en.cppreference.com/w/c/language/object#Strict_aliasing |
| 122 | +- UB in C++: https://en.cppreference.com/w/cpp/language/ub |
| 123 | + |
| 124 | +--- |
| 125 | +**Last Updated**: 2025-11-21 |
| 126 | +**Status**: LTO support attempted but currently broken due to undefined behavior |
0 commit comments