Skip to content

Commit c6458ff

Browse files
authored
Limit depth more strictly in CSE fuzz test (#8512)
1 parent c2f87bc commit c6458ff

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

test/fuzz/cse.cpp

+10-5
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,24 @@
88
using namespace Halide;
99
using namespace Halide::ConciseCasts;
1010
using namespace Halide::Internal;
11+
using std::pair;
1112
using std::vector;
1213

1314
// Note that this deliberately uses int16 values everywhere --
1415
// *not* int32 -- because we want to test CSE, not the simplifier's
1516
// overflow behavior, and using int32 can end up with results
1617
// containing signed_integer_overflow(), which is not helpful here.
17-
Expr random_expr(FuzzedDataProvider &fdp, int depth, vector<Expr> &exprs) {
18+
Expr random_expr(FuzzedDataProvider &fdp, int depth, vector<pair<Expr, int>> &exprs) {
1819
if (depth <= 0) {
1920
return i16(fdp.ConsumeIntegralInRange<int>(-5, 4));
2021
}
2122
if (!exprs.empty() && fdp.ConsumeBool()) {
22-
// Reuse an existing expression
23-
return pick_value_in_vector(fdp, exprs);
23+
// Reuse an existing expression that was generated under conditions at
24+
// least as strict as our current depth limit.
25+
auto p = pick_value_in_vector(fdp, exprs);
26+
if (p.second <= depth) {
27+
return p.first;
28+
}
2429
}
2530
std::function<Expr()> build_next_expr[] = {
2631
[&]() {
@@ -67,13 +72,13 @@ Expr random_expr(FuzzedDataProvider &fdp, int depth, vector<Expr> &exprs) {
6772
},
6873
};
6974
Expr next = fdp.PickValueInArray(build_next_expr)();
70-
exprs.push_back(next);
75+
exprs.emplace_back(next, depth);
7176
return next;
7277
}
7378

7479
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
7580
FuzzedDataProvider fdp(data, size);
76-
vector<Expr> exprs;
81+
vector<pair<Expr, int>> exprs;
7782
Expr orig = random_expr(fdp, 5, exprs);
7883

7984
Expr csed = common_subexpression_elimination(orig);

0 commit comments

Comments
 (0)