Skip to content

Use floor modulo for % in generated C - #852

Open
sueszli wants to merge 2 commits into
exo-lang:mainfrom
sueszli:fix/floor-mod-codegen
Open

Use floor modulo for % in generated C#852
sueszli wants to merge 2 commits into
exo-lang:mainfrom
sueszli:fix/floor-mod-codegen

Conversation

@sueszli

@sueszli sueszli commented Aug 30, 2026

Copy link
Copy Markdown
Contributor
@proc
def foo(dst: f32[5], i: index):
    dst[i % 5] = 1.0

Actual:

dst[i % 5] = 1.0f;

Called with i = -1 this writes dst[-1]. ASan reports
stack-buffer-underflow, WRITE of size 4.

Expected:

dst[exo_floor_mod(i, 5)] = 1.0f;

Bounds checking treats % as floor modulo, so it proves that i % 5 lies in
[0, 5). C truncates toward zero instead. Division already routes through
exo_floor_div for the same reason. Modulo did not.

@codecov

codecov Bot commented Aug 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.56%. Comparing base (defe172) to head (7ec9909).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #852   +/-   ##
=======================================
  Coverage   88.56%   88.56%           
=======================================
  Files         100      100           
  Lines       21917    21926    +9     
=======================================
+ Hits        19410    19419    +9     
  Misses       2507     2507           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sueszli
sueszli force-pushed the fix/floor-mod-codegen branch from fb88c5e to b046038 Compare September 1, 2026 21:59
Bounds checking treats % as floor modulo and proves that i % 5 lies in
[0, 5). C truncates toward zero, so a negative numerator produced a negative
index and an accepted proc wrote out of bounds.

Route % through a floor modulo helper when the numerator is not provably
non-negative, matching how / already routes through exo_floor_div.
@sueszli
sueszli force-pushed the fix/floor-mod-codegen branch from b046038 to 0e511a4 Compare September 2, 2026 21:43
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