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

Cap the maximum basic block padding in Cranelift #6736

Merged
merged 4 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cranelift/codegen/src/machinst/vcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1059,13 +1059,13 @@ impl<I: VCodeInst> VCode<I> {
// Padding can get quite large during fuzzing though so place a
// total cap on it where when a per-function threshold is exceeded
// the padding is turned back down to zero. This avoids a small-ish
// test case generating a 2GB memory footprint in Cranelift for
// test case generating a GB+ memory footprint in Cranelift for
// example.
if !bb_padding.is_empty() {
buffer.put_data(&bb_padding);
buffer.align_to(I::LabelUse::ALIGN);
total_bb_padding += bb_padding.len();
if total_bb_padding > (1 << 20) {
if total_bb_padding > (64 << 20) {
bb_padding = Vec::new();
}
}
Expand Down
35 changes: 26 additions & 9 deletions crates/fuzzing/src/generators/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,32 @@ impl Config {
}
}

// Allow at most 1<<16 == 64k of padding between basic blocks. If this
// gets too large then functions actually grow to 4G+ sizes which is not
// really that interesting to test as it's pretty unrealistic at this
// time.
unsafe {
cfg.cranelift_flag_set(
"bb_padding_log2_minus_one",
&(self.wasmtime.bb_padding_log2 & 0xf).to_string(),
);
// Try to configure cranelift to insert padding between basic blocks to
// stress code layout mechanisms within Cranelift. This padding can get
// unwieldy quickly, however, generating a 4G+ function which isn't
// particularly interesting at this time.
//
// To keep this setting under control there are a few limits in place:
//
// * Cranelift will generate at most 64M of padding per-function,
// regardless of how many basic blocks there are.
// * Here it's limited to enable this setting only when there's at most
// 10 functions to ensure that the overhead for all functions is <1G
// ish or otherwise dosn't run away.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*doesn't

// * The `bb_padding_log2_minus_one` setting isn't allowed to be
// larger than 26 as that allows for `1<<25` maximum padding size
// which is 32M.
//
// With all that combined this is intended to still be enabled,
// although perhaps not all the time, and stress enough interesting test
// cases in cranelift.
if self.module_config.config.max_funcs < 10 {
unsafe {
cfg.cranelift_flag_set(
"bb_padding_log2_minus_one",
&(self.wasmtime.bb_padding_log2 % 26).to_string(),
);
}
}

// Vary the memory configuration, but only if threads are not enabled.
Expand Down