@@ -2,68 +2,70 @@ |
| 2 | 2 | |
| 3 | 3 | ## TL;DR |
| 4 | 4 | |
| 5 | | -- **Linux**: Use `gfortran` (works great) |
| 6 | | -- **macOS ARM64 (M1/M2/M3)**: Use **LLVM Flang (`flang-new`)** - gfortran has serious bugs |
| 7 | | -- **macOS x86_64**: Use `gfortran` (should work fine, untested) |
| 5 | +- **Linux x86_64**: Use `gfortran` (works great) |
| 6 | +- **Linux aarch64**: Use `gfortran` (auto-enables C stat helpers for struct layout differences) |
| 7 | +- **macOS ARM64 (M1/M2/M3/M4)**: Use **LLVM Flang (`flang-new`)** — gfortran has serious bugs |
| 8 | +- **macOS x86_64**: Use `gfortran` with `-frecursive` |
| 8 | 9 | |
| 10 | +The Makefile auto-detects your platform and selects the right compiler. Just run `make`. |
| 9 | 11 | |
| 10 | | -Lots of potential bugs uncovered in gfortran ARM64 |
| 12 | +## macOS ARM64: Why flang-new? |
| 11 | 13 | |
| 12 | | -1. **Stack corruption** - Large stack arrays (600KB+) corrupt memory |
| 13 | | -2. **Deferred-length allocatable bug** - `character(len=:), allocatable` loses length descriptor |
| 14 | | -3. **Intent(out) crashes** - Subroutine return epilogue segfaults |
| 15 | | -4. **Allocatable string assignment corruption** - Assigning to allocatable strings in types corrupts heap |
| 16 | | -5. **Automatic finalization crashes** - Crashes during automatic cleanup |
| 17 | | -6. **Substring slice crashes** - `buffer(:length)` operations segfault |
| 18 | | -7. **Empty string assignment corruption** - `buffer = ''` corrupts heap |
| 19 | | -8. **flush() in loops corruption** - Frequent stderr flush in tight loops corrupts heap |
| 14 | +gfortran on Apple Silicon has at least 8 confirmed bugs that make it unusable: |
| 20 | 15 | |
| 21 | | -So we switched to flang-new |
| 16 | +1. **Stack corruption** — Large stack arrays (600KB+) corrupt memory |
| 17 | +2. **Deferred-length allocatable bug** — `character(len=:), allocatable` loses length descriptor |
| 18 | +3. **Intent(out) crashes** — Subroutine return epilogue segfaults |
| 19 | +4. **Allocatable string assignment corruption** — Assigning to allocatable strings in types corrupts heap |
| 20 | +5. **Automatic finalization crashes** — Crashes during automatic cleanup |
| 21 | +6. **Substring slice crashes** — `buffer(:length)` operations segfault |
| 22 | +7. **Empty string assignment corruption** — `buffer = ''` corrupts heap |
| 23 | +8. **flush() in loops corruption** — Frequent stderr flush in tight loops corrupts heap |
| 22 | 24 | |
| 25 | +Install flang-new: |
| 23 | 26 | ```bash |
| 24 | | -brew install llvm |
| 27 | +brew install flang |
| 25 | 28 | ``` |
| 26 | 29 | |
| 27 | | -This isntalls the full LLVM toolchain including `flang-new`. |
| 30 | +## flang-new String Buffer Issue (Resolved) |
| 28 | 31 | |
| 29 | | -The Makefile auto-detects and uses Flang if available: |
| 32 | +flang-new has a known issue where Fortran string operations (substring slicing, direct assignment) on buffers larger than 128 bytes can cause heap corruption. |
| 30 | 33 | |
| 31 | | -```bash |
| 32 | | -make clean |
| 33 | | -make |
| 34 | | -``` |
| 34 | +**This limitation has been fully worked around** via the C string library (`src/c_interop/fortsh_strings.c`), which routes all critical string operations through C code instead of flang-new's Fortran runtime. The C string library is auto-enabled for all flang-new builds (`USE_C_STRINGS`). |
| 35 | 35 | |
| 36 | | -You'll see: |
| 37 | | -``` |
| 38 | | -Using LLVM Flang (flang-new) - recommended for macOS ARM64 |
| 39 | | -``` |
| 36 | +Additionally, a `safe_assign_alloc_str` routine performs char-by-char copies for allocatable strings >16 bytes, and the expansion pipeline uses C-backed growing buffers (`buffer_grow`, `buffer_append_chars`) for all variable and parameter expansion. |
| 40 | 37 | |
| 41 | | -## Note: Flang limitations |
| 38 | +The workaround is transparent — no command length limits, no feature restrictions. macOS ARM64 passes the full test suite (3,600+ POSIX tests, 850+ builtin tests, 200+ stress tests) identically to Linux. |
| 42 | 39 | |
| 43 | | -flang is far more stable it seems, but it has **one pretty glaring limitation**: string buffers larger than 128 bytes cause heap corruption when performing certain operations (substring slicing, direct assignment from allocatable strings) on them. |
| 40 | +## flang-new Fortran I/O Caveat |
| 44 | 41 | |
| 45 | | -**Impact for fortsh:** |
| 46 | | -- Command lines are limited to **127 characters** on apple silicon. |
| 47 | | -- All other features work normally (history, tab completion, syntax highlighting, etc.) |
| 48 | | -- This is a fundamental limitation we cannot work around without risking heap corruption |
| 42 | +flang-new's `write(output_unit, ...)` and `write(error_unit, ...)` cache file descriptors at process startup and don't follow `dup2` redirections. This means builtin output written via Fortran I/O bypasses shell redirections like `> /dev/null`. |
| 49 | 43 | |
| 50 | | -**Why? because that seems odd:** |
| 51 | | -- Allocating strings >128 bytes works fine, obviously |
| 52 | | -- BUT operating on them (substring ops, assignments) triggers heap corruption |
| 53 | | -- We attempted a "shadow buffer" pattern (1024-byte storage, 128-byte working buffer) |
| 54 | | -- Even this approach still limits effective command length to 128 bytes |
| 44 | +**Fix**: Key builtins use `write_stdout`/`write_stderr` from `io_helpers.f90`, which call C `write()` directly to fd 1/2. This respects all `dup2` redirections. Files affected: `builtins.f90`, `aliases.f90`, `shell_options.f90`, `better_errors.f90`, `grammar_parser.f90`, `fd_redirection.f90`, `variables.f90`, `ast_executor.f90`. |
| 55 | 45 | |
| 56 | | -## Alternative: x86_64 gfortran via Rosetta |
| 46 | +When adding new builtin output that users might redirect, use `write_stdout`/`write_stderr` instead of `write(output_unit/error_unit, ...)`. |
| 57 | 47 | |
| 58 | | -If you prefer not to use Flang, you can use x86_64 gfortran through Rosetta, and tell us how it goes: |
| 48 | +## Linux aarch64: struct stat Layout |
| 59 | 49 | |
| 60 | | -### other |
| 50 | +glibc on aarch64 uses a different `struct stat` layout than x86_64: |
| 51 | +- `st_mode` and `st_nlink` are **swapped** (mode at offset 16 on aarch64, offset 24 on x86_64) |
| 52 | +- `st_nlink` is 4 bytes (`unsigned int`) on aarch64, 8 bytes (`unsigned long`) on x86_64 |
| 53 | +- `st_blksize` is 4 bytes on aarch64, 8 bytes on x86_64 |
| 54 | +- Total struct size: 128 bytes (aarch64) vs 144 bytes (x86_64) |
| 61 | 55 | |
| 62 | | -Force a specific compiler: |
| 56 | +**Fix**: C stat helper functions in `fd_wrapper.c` (`fortsh_stat_mode`, `fortsh_stat_size`, etc.) use system headers for the correct layout. Enabled via `-DUSE_C_STAT` (auto-set by Makefile when `uname -m` is `aarch64`). x86_64 uses the Fortran `stat_t` struct directly. |
| 63 | 57 | |
| 58 | +## Compiler Selection |
| 59 | + |
| 60 | +Force a specific compiler: |
| 64 | 61 | ```bash |
| 65 | 62 | make FC=gfortran clean all # Force gfortran |
| 66 | 63 | make FC=flang-new clean all # Force LLVM Flang |
| 67 | 64 | ``` |
| 68 | | -- **gfortran**: Frequent crashes, unusable |
| 69 | | -- **flang-new**: Works great, but 127-character command limit |
| 65 | + |
| 66 | +Build flags: |
| 67 | +```bash |
| 68 | +make NO_C_STRINGS=1 # Disable C string library (will crash on flang-new) |
| 69 | +make NO_MEMPOOL=1 # Disable memory pooling |
| 70 | +make MEMPOOL_DEBUG=1 # Enable memory pool debug output |
| 71 | +``` |