Skip to content

fix(types,sa,codegen): signed idiv, multi-decl init, pointer arithmetic - #79

Merged
rieske merged 1 commit into
masterfrom
fix/fuzz-signed-div-pointer-arith
Jul 27, 2026
Merged

fix(types,sa,codegen): signed idiv, multi-decl init, pointer arithmetic#79
rieske merged 1 commit into
masterfrom
fix/fuzz-signed-div-pointer-arith

Conversation

@rieske

@rieske rieske commented Jul 27, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes three correctness bugs found by mutation fuzzing of the master language surface:

  1. Signed division/moduloidiv was preceded by xor rdx,rdx instead of cqo, so negative dividends could SIGFPE or yield wrong quotients/remainders.
  2. Multi-declarator initializers – all initializers were analyzed before any name was inserted, so forms like int a = 1, b = a / int a = 1, *p = &a falsely reported undefined symbols.
  3. Pointer arithmeticp+i, p-i, p-q, and pointer ++/-- did not scale by pointee size (byte math instead of element math).

Design

  • Type::isPrimitive() excludes indirection so int* is not treated as a scalar.
  • Shared type::classifyPointerArithmetic (TypeQuery) for SA result typing and CG IR selection.
  • New IR: PointerOffset / PointerDiff; shared backend scaledBaseIndex with array indexing.
  • Integer Add/Sub stay unscaled; pointer ++/-- use a documented byte step on Inc/Dec.
  • Declarations: visit each declarator, insert, then analyze initializer (C visibility).

Test plan

  • Functional: signed div/mod (locals + global), multi-declarator cross-init, pointer scale/diff/++, char ptrdiff
  • Unit: Type.pointerToSignedInteger not primitive; TypeQuery.classifyPointerArithmeticForms
  • Array indexing still green (shared scaledBaseIndex)
  • CI full make test / ctest

@rieske
rieske force-pushed the fix/fuzz-signed-div-pointer-arith branch from 3146754 to f986bf2 Compare July 27, 2026 13:44
@mergify

mergify Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

…ithmetic

- Sign-extend with cqo for signed idiv/mod; isPrimitive excludes pointers
- Multi-decl: insert symbol before initializer (analyzeInitializedDeclarator)
- Pointer +/- scales by pointee size via shared classifier + PointerOffset/Diff IR
- StackMachine addressing extracted; materializeBaseAddress shared
- Cover int+ptr, ptr-int, ptr--, invalid ptr+ptr; non-unit Inc/Dec; AT&T cqo
@rieske
rieske force-pushed the fix/fuzz-signed-div-pointer-arith branch from f986bf2 to 2ab261e Compare July 27, 2026 14:05
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 93.503% (+0.2%) from 93.287% — fix/fuzz-signed-div-pointer-arith into master

@rieske
rieske merged commit 3527311 into master Jul 27, 2026
4 checks passed
@rieske
rieske deleted the fix/fuzz-signed-div-pointer-arith branch July 27, 2026 14:15
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.

2 participants