Skip to content

Commit c0f4f7c

Browse files
authored
Clamp sizes of element segments in wasm-smith (#2632)
The `max_elements` configuration previously only applied within one element segment but it's now interpreted as a maximum for the entire set of element segments generated for the module.
1 parent 579ae00 commit c0f4f7c

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

crates/wasm-smith/src/core.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2990,6 +2990,8 @@ impl Module {
29902990
return Ok(());
29912991
}
29922992

2993+
let mut total_elements = 0_usize;
2994+
29932995
arbitrary_loop(
29942996
u,
29952997
self.config.min_element_segments,
@@ -3000,7 +3002,7 @@ impl Module {
30003002
let (kind, max_size_hint) = u.choose(&choices)?(u)?;
30013003
let max = max_size_hint
30023004
.map(|i| usize::try_from(i).unwrap())
3003-
.unwrap_or_else(|| self.config.max_elements);
3005+
.unwrap_or(self.config.max_elements);
30043006

30053007
// Infer, from the kind of segment, the type of the element
30063008
// segment. Passive/declared segments can be declared with any
@@ -3043,6 +3045,11 @@ impl Module {
30433045
}
30443046
}
30453047

3048+
// Clamp the max elements for this segment based on the
3049+
// configuration's maximum number of elements for the entire
3050+
// module minus what we've generated so far.
3051+
let max = (total_elements.saturating_sub(self.config.max_elements)).min(max);
3052+
30463053
// And finally actually generate the arbitrary elements of this
30473054
// element segment. Function indices are used if they're either
30483055
// forced or allowed, and otherwise expressions are used
@@ -3058,13 +3065,15 @@ impl Module {
30583065
Ok(true)
30593066
})?;
30603067
}
3068+
total_elements += init.len();
30613069
Elements::Functions(init)
30623070
} else {
30633071
let mut init = vec![];
30643072
arbitrary_loop(u, self.config.min_elements, max, |u| {
30653073
init.push(self.arbitrary_const_expr(ValType::Ref(ty), u, true)?);
30663074
Ok(true)
30673075
})?;
3076+
total_elements += init.len();
30683077
Elements::Expressions(init)
30693078
};
30703079

0 commit comments

Comments
 (0)