Skip to content

Fix infinite recursion in find intrinsics#9215

Open
abadams wants to merge 4 commits into
mainfrom
abadams/fix_lossless_cast_infinite_recursion
Open

Fix infinite recursion in find intrinsics#9215
abadams wants to merge 4 commits into
mainfrom
abadams/fix_lossless_cast_infinite_recursion

Conversation

@abadams

@abadams abadams commented Jul 16, 2026

Copy link
Copy Markdown
Member

The recent change to find intrinsics threw up a segfault on another PR in the lossless cast fuzzer due to infinite recursion. This is what lead me to uncover the fill issue in #9214 . This PR builds on that and actually fixes the infinite recursion in what I think is the intended way.

abadams and others added 3 commits July 16, 2026 12:59
…lement

Buffer::for_each_element previously accepted a callable taking fewer int
arguments than the buffer's dimensionality, silently iterating only over a
reduced-dimensionality slice. A nullary callable (e.g. buffer.fill(rng))
therefore wrote only the origin element. This is an easy mistake to make and
produces no error.

Require the callable to take either a const int *, or exactly as many int
arguments as the buffer has dimensions. This uncovered three real bugs where
buffers were only ever having their first element randomized:

  - test/fuzz/lossless_cast.cpp: buf.fill(fuzz)
  - test/correctness/interleave_rgb.cpp: fill([]() { return rand(); })
  - test/correctness/multiple_scatter.cpp: fill(std::mt19937{0})

All three are fixed to use for_each_value, which passes an element reference
and does not depend on coordinates. The reduced-slice usage in
test/correctness/for_each_element.cpp is rewritten to slice off a channel
first so the callable's arg count matches the iteration domain.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
A zero-argument callable passed to fill (for example a random number
generator) has no coordinate to consume, so there is no distinction
between filling via for_each_element and for_each_value. Dispatch such
callables to for_each_value, which evaluates the callable once per
element. This makes the natural buffer.fill(rng) idiom fill the whole
buffer instead of tripping the for_each_element arg-count assertion.

With this, the previously-broken buffer initializations in
interleave_rgb, multiple_scatter, and lossless_cast (which only ever
randomized the origin element) are fixed by reverting them to their
original fill(rng) form.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
strip_widening_cast is meant to return a narrower-or-equal-width
equivalent of its argument. When the argument was a narrowing cast (for
example int16(int32_expr)), its innermost type was wider than the
argument's type, and the consider(inner_t) branch returned an expression
wider than the input. Feeding that back into the widening_mul narrowing
rewrite re-widened the multiply, so the pass ping-ponged a widening_mul
between its int32 and int64 forms forever, overflowing the stack.

Only consider the inner type when it is no wider than the argument, i.e.
when there is a genuine widening cast to strip.

Found by the lossless_cast fuzzer once buffer.fill was fixed to actually
randomize its input buffers. Adds a deterministic regression test.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The for_each_element arg-count assertion rejects a callable taking fewer
int arguments than the buffer's dimensionality. These calls iterated a
2D domain over a 3D buffer and handled all channels inside the callable,
so slice off the channel dimension first to get a matching 2D domain.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@codecov

codecov Bot commented Jul 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@c43b345). Learn more about missing BASE report.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #9215   +/-   ##
=======================================
  Coverage        ?   69.73%           
=======================================
  Files           ?      255           
  Lines           ?    78575           
  Branches        ?    18790           
=======================================
  Hits            ?    54796           
  Misses          ?    18177           
  Partials        ?     5602           

☔ 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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