Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

πŸ› Avoid stack overflow during shrinking of tuples #3428

Merged
merged 10 commits into from
Dec 1, 2022
7 changes: 7 additions & 0 deletions .yarn/versions/2ae83f1c.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
releases:
fast-check: patch

declined:
- "@fast-check/ava"
- "@fast-check/jest"
- "@fast-check/worker"
20 changes: 13 additions & 7 deletions packages/fast-check/src/arbitrary/_internals/TupleArbitrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { cloneIfNeeded, cloneMethod, WithCloneMethod } from '../../check/symbols
import { Arbitrary } from '../../check/arbitrary/definition/Arbitrary';
import { Value } from '../../check/arbitrary/definition/Value';
import { safeMap, safePush, safeSlice } from '../../utils/globals';
import { makeLazy } from '../../stream/LazyIterableIterator';

const safeArrayIsArray = Array.isArray;
const safeObjectDefineProperty = Object.defineProperty;
Expand Down Expand Up @@ -55,13 +56,18 @@ export function tupleShrink<Ts extends unknown[]>(
let s = Stream.nil<TupleExtendedValue<Ts>>();
const safeContext: TupleContext = safeArrayIsArray(context) ? context : [];
for (let idx = 0; idx !== arbs.length; ++idx) {
const shrinksForIndex: Stream<TupleExtendedValue<Ts>> = arbs[idx]
.shrink(value[idx], safeContext[idx])
.map((v) => {
const nextValues: Value<unknown>[] = safeMap(value, (v, idx) => new Value(cloneIfNeeded(v), safeContext[idx]));
return [...safeSlice(nextValues, 0, idx), v, ...safeSlice(nextValues, idx + 1)];
})
.map((values) => tupleWrapper(values) as TupleExtendedValue<Ts>);
const shrinksForIndex: IterableIterator<TupleExtendedValue<Ts>> = makeLazy(() =>
arbs[idx]
.shrink(value[idx], safeContext[idx])
.map((v) => {
const nextValues: Value<unknown>[] = safeMap(
value,
(v, idx) => new Value(cloneIfNeeded(v), safeContext[idx])
);
return [...safeSlice(nextValues, 0, idx), v, ...safeSlice(nextValues, idx + 1)];
})
.map((values) => tupleWrapper(values) as TupleExtendedValue<Ts>)
);
s = s.join(shrinksForIndex);
}
return s;
Expand Down
17 changes: 17 additions & 0 deletions packages/fast-check/test/e2e/NoStackOverflowOnShrink.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,23 @@ describe(`NoStackOverflowOnShrink (seed: ${seed})`, () => {
expect(() => iterateOverShrunkValues(arb, value!)).not.toThrow();
});

it('should not run into stack overflow while calling shrink on very large tuple', () => {
// We expect the depth used by this test to be greater than
// the maximal depth we computed before reaching a stack overflow
expect(maxDepthForArrays).toBeGreaterThan(callStackSizeWithMargin);

const mrng = new fc.Random(prand.xorshift128plus(seed));
const arb = fc.tuple<boolean[]>(...[...Array(maxDepthForArrays)].fill(fc.boolean()));
let value: fc.Value<boolean[]> | null = null;
while (value === null) {
const tempShrinkable = arb.generate(mrng, undefined);
if (tempShrinkable.value.length >= callStackSize) {
value = tempShrinkable;
}
}
dubzzz marked this conversation as resolved.
Show resolved Hide resolved
expect(() => iterateOverShrunkValues(arb, value!)).not.toThrow();
});

it('should not run into stack overflow while calling shrink on very large shuffled sub-arrays', () => {
// We expect the depth used by this test to be greater than
// the maximal depth we computed before reaching a stack overflow
Expand Down