Skip to content

Static analysis rounds 5 and 6, yasc slices, in/inout/out, enforced initialization, unreachable - #29

Merged
matgla merged 2 commits into
mobfrom
feat/round5-checks
Nov 25, 2026
Merged

matgla merged 2 commits into
mobfrom
feat/round5-checks

Conversation

@matgla

@matgla matgla commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner

Everything outstanding on top of #28, squashed into one commit on mob: static analysis rounds 5 and 6, yasc slices, in/inout/out, enforced initialization, checked unreachable, nullptr only. The x86_64 target is not included: it stays on feat/x86_64-target.

Checks and the safe default

  • Rounds 5 and 6: strict lifetimes, unsafe, tagged unions, register fields ro/wo/w1c, -fyasc-facts, borrows from several owners, taint across units, error recovery for macro/asm/setup/codegen errors.
  • The safe default has every check. Fortify also comes from the declaration: access and null_terminated_string_arg are checked at calls.
  • Diagnostics on a terminal show the source line with a caret, [-Wname] and bold names.

yasc

  • Parameters are in (read-only) unless they say inout or out. out T * borrows(s) * view p says what an out parameter stores borrows from. A redeclaration that says something else about its pointers is an error.
  • Initialization is enforced: scalars by flow, through out parameters and inlined callees; aggregates need an initializer or = undefined.
  • unreachable is a checked trap. -fsanitize=unreachable covers __builtin_unreachable(). while(1) and __builtin_trap end a path, so they no longer give missing-return false positives.
  • nullptr is the only null pointer (-Wzero-as-null-pointer-constant). char s[:0], T p[n] and const void buf[n] parameters say their buffer. yasc_view/yasc_maybe work on regular-C struct members.
  • Slices []T / [:0]T: ptr + len in r0:r1, s[i] checked at every -O, x[a:b:0].

Bounds checks: T p[n] parameters are checked with cmp/branch/udf, and checks that ssa:vrp / ssa:bounds_iv prove go away, so a checked loop compiles like the unchecked one.

Runtime: libgcc's __clz/__ctz/__popcount/__parity si2/di2 helpers (weak) are in libtcc1. Complex division (__divsc3/__divdc3) is in the soft FP runtime. The YasOS libc no longer defines them.

Fixes: sl_forward miscompile, PT_LOAD p_filesz, the -O0 frame floor, static !T storage class, __func__ NULL under newlib (__GNUC_MINOR__), a false initialization error through an inlined pointer out parameter, two ASan leaks (a yasc bound declared later, parameter names after a recovered error), ssa:cmp_eq_prop keeps its equality facts on never-written parameters (golden ssa:cmp_eq_prop/simple updated: the loads cprop forwards are gone, the second compare still folds).

Tested locally

  • With this repo's CI configure, the build-and-test job's steps: unity check; make test CI=1 passes through aeabi, asm, warn/optflag/opt-dsl checks, all unit suites, frontend, linker, debug, runtime. test-selfhost fails locally only because my host's sys/mman.h has no MAP_SLOW. Float ABI matrix softfp/hard 334 passed each; golden IR 25/25.
  • IR suite with this repo's CI configure (--enable-cross --enable-O2 --debug --enable-asan): 23261 passed. The 20 failures are all local environment: mibench sources missing (8), gcc pr78622 needs %hhd from the toolchain's prebuilt newlib (USE_NEWLIB_BUILD=0), and the valgrind tests cannot run an ASan binary.
  • IR suite with yasos's build_rootfs.sh cross configure: 23353 passed. Its 34 failures only happen with that configuration: objdump cannot decode the YasOS-only relocation 0x8b that test_compiler_fortify reads, 912_complex_div_runtime hard-faults when built with the YasOS PIC cross for bare metal (it passes with the CI configure), plus mibench and pr78622 as above.
  • yasos: build_rootfs, kernel, libc's unit tests on QEMU 155/155 at -O0/-O1/-O2/-Os, and tcc self-host on QEMU (stage 2 77/77 and stage 3 77/77 identical to the host cross, fixpoint 78/78 identical: PASS).

🤖 Generated with Claude Code

matgla added a commit to matgla/yasos.zig that referenced this pull request Nov 23, 2026
…chet floor

libs/tinycc -> matgla/tinycc#29 (80165e52b6, on mob = #28): -b VLAs/alloca/
longjmp, lifetime with several owners, taint across units, %N$ formats,
_FORTIFY_SOURCE builtins, more error recovery; plus #28's CI fixes (ASan leak
fixes on error paths, -fmodule-header qualifiers, -fmax-errors in the flow
checks).
libs/libc -> matgla/yasos_libc#5 (0905390): sys/_fortify.h, the __*_chk
functions, alloc_size, stpcpy/stpncpy; the no-OS build undefines
_FORTIFY_SOURCE (zig's clang defines it; the kernel did not build).
safe_mode_ratchet: libs/libc 371 -> 380 warnings, 1775 -> 1894 errors -- the
new fortify.c and tests/fortify_tests.c are regular C, and round 5's checks.

an524 QEMU: smoke gate 15322 passed (the 6 failures are mibench sources
missing in the worktree), tcc self-host fixpoint PASS.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@matgla matgla changed the title Static analysis round 5: -b VLAs/longjmp, lifetime owners, taint across units, _FORTIFY_SOURCE, more error recovery Static analysis rounds 5 and 6: round 5 checks, yasc strict lifetimes, unsafe, tagged unions, register fields, -fyasc-facts Nov 24, 2026
matgla added a commit to matgla/yasos.zig that referenced this pull request Nov 24, 2026
…d unions, register fields); ratchet floor

tinycc feat/round5-checks 21312f7744 (matgla/tinycc#29): yasc strict lifetimes with
unsafe { } / unsafe(expr), restrict as a checked exclusive borrow, tagged unions,
register fields ro / wo / w1c, -fyasc-facts optimizations, and the fixes a review of
them found (an ASan stack-use-after-return after asm errors, a nested tagged-union
crash, four -fyasc-facts miscompiles, false strict errors, leaks).

scripts/safe_mode_ratchet.json: the new floor. Our C is regular C, and in the yasc
default a pointer from regular C is dereferenced or becomes owned only inside unsafe,
so the counts grow (libs/libc 1894 -> 2388 errors, apps/yasvi 2124 -> 3182, ...).

Tested on an524 QEMU: build_rootfs, kernel, tcc self-host fixpoint PASS (77 + 78
identical); the pimoroni_pico_plus2_and_vga image builds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
matgla added a commit that referenced this pull request Nov 25, 2026
…nitialization, unreachable

Static analysis (rounds 5 and 6, PR #29):
- strict lifetimes, unsafe regions, tagged unions checked at compile time,
  register fields ro/wo/w1c (-Wregister-access), -fyasc-facts optimisations;
  borrows from several owners, borrowing structs, frame addresses through
  borrowing calls; -Wtainted-format across units; recovery from macro, asm,
  setup and codegen errors; __has_builtin for the __builtin___*_chk.
- yasc: parameter arrays are views ([owned]/[maybe]/... in the brackets),
  `T * readonly p`, @min and @max, cheat sheet.
- -Warray-bounds=2 through pointers, -Wuse-after-free, -b registers main's argv.
- The safe default has every check; fortify from the declaration: access and
  null_terminated_string_arg checked at calls; -Wcross-tu does not depend on
  the output path; -Wdiscarded-qualifiers names the qualifier and both types.
- Diagnostics on a terminal: the source line with a caret, [-Wname], bold names.

yasc:
- Parameters are in (read-only) unless they say inout or out; out = written
  before read.  `out T * borrows(s) * view p`: what an out parameter stores
  borrows from s.  A redeclaration that says something else about its
  pointers is an error.
- Initialization is enforced: scalars by flow (through out parameters and
  inlined callees), aggregates need an initializer or `= undefined`.
- `unreachable` is a checked trap; -fsanitize=unreachable for
  __builtin_unreachable(); while(1), do-while(1) and __builtin_trap end a path
  (no missing-return false positives).
- One null pointer, nullptr (-Wzero-as-null-pointer-constant); C23 bool, true,
  false, nullptr under -ccompat + #pragma yasc too.
- Parameters that say their buffer: char s[:0], T p[n], const void buf[n].
- yasc_view / yasc_maybe attribute on a regular-C struct member.
- `static !T f(void)` keeps its storage class.

Bounds checks (-fsanitize=bounds):
- a parameter declared `T p[n]` / `T p[N]` is checked against n (not
  [static N]), through inline expansion too; the check is cmp/branch/udf.
- proven checks go: ssa:vrp (induction variables up to and down from the
  bound, w - 1, phi joins, one value through copies and equal pure defs,
  strnlen's zero and i + 1 <= len, strlen's zero) and ssa:bounds_iv before
  the loop passes, so a checked loop compiles as the unchecked one;
  stack_deref_fold folds a frame base with a constant scaled index; ssa:fold
  folds a constant barrel-shifted operand; ssa:cprop forwards a never-written
  parameter's copy across blocks.
- __dirname_chk / __basename_chk with a known path are the plain calls.

yasc slices:
- `[]T` and `[:0]T` (ptr + len in r0:r1), s[i] checked at every -O, x[a:b],
  x[a:], x[:b], p[0:n], x[a:b:0], .ptr / .len, conversions from arrays and
  string literals, slices of slices; a slice parameter is a view source;
  [:0] slices keep their zero (s[s.len] readable, written only with a zero).

Runtime:
- libgcc's __clz/__ctz/__popcount/__parity si2/di2 helpers in libtcc1 (weak);
  complex division __divsc3/__divdc3 in the soft FP runtime (lib/fp/soft/divc3.c)
  -- the YasOS libc no longer defines them.

Fixes:
- sl_forward forwarded a slot's stored value into `T <- Addr[StackLoc] [LOAD]`
  (a frame object's address widened to 64 bits passed a[0] at -O1+).
- PT_LOAD p_filesz ends at the last section with file contents.
- The -O0 frame floor keeps a frontend object whole when the IR names only
  its end or middle.
- A function a regular-C macro defines in yasc code was checked as yasc from -O1.
- __GNUC_MINOR__ and __GNUC_PATCHLEVEL__ predefined beside __GNUC__ for YasOS.
- yasc enforced initialization: `&v` passed to an inlined callee's
  `out T * maybe * view p` (out on the pointer p points at) counts as setting v.
- Leaks under ASan: a yasc parameter bound declared later (`T p[n]`), and the
  parameter names of a list an error recovered out of.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@matgla
matgla force-pushed the feat/round5-checks branch from 21312f7 to 7d29fa0 Compare November 25, 2026 17:14
@matgla matgla changed the title Static analysis rounds 5 and 6: round 5 checks, yasc strict lifetimes, unsafe, tagged unions, register fields, -fyasc-facts Static analysis rounds 5 and 6, yasc slices, in/inout/out, enforced initialization, unreachable Nov 25, 2026
matgla added a commit that referenced this pull request Nov 25, 2026
…nitialization, unreachable

Static analysis (rounds 5 and 6, PR #29):
- strict lifetimes, unsafe regions, tagged unions checked at compile time,
  register fields ro/wo/w1c (-Wregister-access), -fyasc-facts optimisations;
  borrows from several owners, borrowing structs, frame addresses through
  borrowing calls; -Wtainted-format across units; recovery from macro, asm,
  setup and codegen errors; __has_builtin for the __builtin___*_chk.
- yasc: parameter arrays are views ([owned]/[maybe]/... in the brackets),
  `T * readonly p`, @min and @max, cheat sheet.
- -Warray-bounds=2 through pointers, -Wuse-after-free, -b registers main's argv.
- The safe default has every check; fortify from the declaration: access and
  null_terminated_string_arg checked at calls; -Wcross-tu does not depend on
  the output path; -Wdiscarded-qualifiers names the qualifier and both types.
- Diagnostics on a terminal: the source line with a caret, [-Wname], bold names.

yasc:
- Parameters are in (read-only) unless they say inout or out; out = written
  before read.  `out T * borrows(s) * view p`: what an out parameter stores
  borrows from s.  A redeclaration that says something else about its
  pointers is an error.
- Initialization is enforced: scalars by flow (through out parameters and
  inlined callees), aggregates need an initializer or `= undefined`.
- `unreachable` is a checked trap; -fsanitize=unreachable for
  __builtin_unreachable(); while(1), do-while(1) and __builtin_trap end a path
  (no missing-return false positives).
- One null pointer, nullptr (-Wzero-as-null-pointer-constant); C23 bool, true,
  false, nullptr under -ccompat + #pragma yasc too.
- Parameters that say their buffer: char s[:0], T p[n], const void buf[n].
- yasc_view / yasc_maybe attribute on a regular-C struct member.
- `static !T f(void)` keeps its storage class.

Bounds checks (-fsanitize=bounds):
- a parameter declared `T p[n]` / `T p[N]` is checked against n (not
  [static N]), through inline expansion too; the check is cmp/branch/udf.
- proven checks go: ssa:vrp (induction variables up to and down from the
  bound, w - 1, phi joins, one value through copies and equal pure defs,
  strnlen's zero and i + 1 <= len, strlen's zero) and ssa:bounds_iv before
  the loop passes, so a checked loop compiles as the unchecked one;
  stack_deref_fold folds a frame base with a constant scaled index; ssa:fold
  folds a constant barrel-shifted operand; ssa:cprop forwards a never-written
  parameter's copy across blocks.
- __dirname_chk / __basename_chk with a known path are the plain calls.

yasc slices:
- `[]T` and `[:0]T` (ptr + len in r0:r1), s[i] checked at every -O, x[a:b],
  x[a:], x[:b], p[0:n], x[a:b:0], .ptr / .len, conversions from arrays and
  string literals, slices of slices; a slice parameter is a view source;
  [:0] slices keep their zero (s[s.len] readable, written only with a zero).

Runtime:
- libgcc's __clz/__ctz/__popcount/__parity si2/di2 helpers in libtcc1 (weak);
  complex division __divsc3/__divdc3 in the soft FP runtime (lib/fp/soft/divc3.c)
  -- the YasOS libc no longer defines them.

Fixes:
- sl_forward forwarded a slot's stored value into `T <- Addr[StackLoc] [LOAD]`
  (a frame object's address widened to 64 bits passed a[0] at -O1+).
- PT_LOAD p_filesz ends at the last section with file contents.
- The -O0 frame floor keeps a frontend object whole when the IR names only
  its end or middle.
- A function a regular-C macro defines in yasc code was checked as yasc from -O1.
- __GNUC_MINOR__ and __GNUC_PATCHLEVEL__ predefined beside __GNUC__ for YasOS.
- yasc enforced initialization: `&v` passed to an inlined callee's
  `out T * maybe * view p` (out on the pointer p points at) counts as setting v.
- ssa:cmp_eq_prop: a never-written PARAM counts like an SSA TEMP, so the
  equality facts survive cprop forwarding parameters into compares.
- Leaks under ASan: a yasc parameter bound declared later (`T p[n]`), and the
  parameter names of a list an error recovered out of.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@matgla
matgla force-pushed the feat/round5-checks branch from 7d29fa0 to b4ae35f Compare November 25, 2026 18:15
…nitialization, unreachable

Static analysis (rounds 5 and 6, PR #29):
- strict lifetimes, unsafe regions, tagged unions checked at compile time,
  register fields ro/wo/w1c (-Wregister-access), -fyasc-facts optimisations;
  borrows from several owners, borrowing structs, frame addresses through
  borrowing calls; -Wtainted-format across units; recovery from macro, asm,
  setup and codegen errors; __has_builtin for the __builtin___*_chk.
- yasc: parameter arrays are views ([owned]/[maybe]/... in the brackets),
  `T * readonly p`, @min and @max, cheat sheet.
- -Warray-bounds=2 through pointers, -Wuse-after-free, -b registers main's argv.
- The safe default has every check; fortify from the declaration: access and
  null_terminated_string_arg checked at calls; -Wcross-tu does not depend on
  the output path; -Wdiscarded-qualifiers names the qualifier and both types.
- Diagnostics on a terminal: the source line with a caret, [-Wname], bold names.

yasc:
- Parameters are in (read-only) unless they say inout or out; out = written
  before read.  `out T * borrows(s) * view p`: what an out parameter stores
  borrows from s.  A redeclaration that says something else about its
  pointers is an error.
- Initialization is enforced: scalars by flow (through out parameters and
  inlined callees), aggregates need an initializer or `= undefined`.
- `unreachable` is a checked trap; -fsanitize=unreachable for
  __builtin_unreachable(); while(1), do-while(1) and __builtin_trap end a path
  (no missing-return false positives).
- One null pointer, nullptr (-Wzero-as-null-pointer-constant); C23 bool, true,
  false, nullptr under -ccompat + #pragma yasc too.
- Parameters that say their buffer: char s[:0], T p[n], const void buf[n].
- yasc_view / yasc_maybe attribute on a regular-C struct member.
- `static !T f(void)` keeps its storage class.

Bounds checks (-fsanitize=bounds):
- a parameter declared `T p[n]` / `T p[N]` is checked against n (not
  [static N]), through inline expansion too; the check is cmp/branch/udf.
- proven checks go: ssa:vrp (induction variables up to and down from the
  bound, w - 1, phi joins, one value through copies and equal pure defs,
  strnlen's zero and i + 1 <= len, strlen's zero) and ssa:bounds_iv before
  the loop passes, so a checked loop compiles as the unchecked one;
  stack_deref_fold folds a frame base with a constant scaled index; ssa:fold
  folds a constant barrel-shifted operand; ssa:cprop forwards a never-written
  parameter's copy across blocks.
- __dirname_chk / __basename_chk with a known path are the plain calls.

yasc slices:
- `[]T` and `[:0]T` (ptr + len in r0:r1), s[i] checked at every -O, x[a:b],
  x[a:], x[:b], p[0:n], x[a:b:0], .ptr / .len, conversions from arrays and
  string literals, slices of slices; a slice parameter is a view source;
  [:0] slices keep their zero (s[s.len] readable, written only with a zero).

Runtime:
- libgcc's __clz/__ctz/__popcount/__parity si2/di2 helpers in libtcc1 (weak);
  complex division __divsc3/__divdc3 in the soft FP runtime (lib/fp/soft/divc3.c)
  -- the YasOS libc no longer defines them.

Fixes:
- sl_forward forwarded a slot's stored value into `T <- Addr[StackLoc] [LOAD]`
  (a frame object's address widened to 64 bits passed a[0] at -O1+).
- PT_LOAD p_filesz ends at the last section with file contents.
- The -O0 frame floor keeps a frontend object whole when the IR names only
  its end or middle.
- A function a regular-C macro defines in yasc code was checked as yasc from -O1.
- __GNUC_MINOR__ and __GNUC_PATCHLEVEL__ predefined beside __GNUC__ for YasOS.
- yasc enforced initialization: `&v` passed to an inlined callee's
  `out T * maybe * view p` (out on the pointer p points at) counts as setting v.
- ssa:cmp_eq_prop: a never-written PARAM counts like an SSA TEMP, so the
  equality facts survive cprop forwarding parameters into compares.
- Leaks under ASan: a yasc parameter bound declared later (`T p[n]`), and the
  parameter names of a list an error recovered out of.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@matgla
matgla force-pushed the feat/round5-checks branch from b4ae35f to 62c1cc3 Compare November 25, 2026 18:30
A CI cross (--enable-cross, no YasOS sysroot) has no <stddef.h> or
<string.h> on its include path: test_compiler_fortify now takes tcc's
own include/ (-nostdinc -isystem include), test_diag_color declares
strcpy instead of including <string.h>.  They passed only on crosses
configured by build_rootfs.sh, whose sysinclude is the rootfs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@matgla
matgla merged commit d7c438c into mob Nov 25, 2026
3 checks passed
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