Skip to content

Commit

Permalink
Avoid running wasm-opt indefinitely
Browse files Browse the repository at this point in the history
POSIX says [1]:

> If a file is removed from or added to the directory after the most recent
> call to opendir() or rewinddir(), whether a subsequent call to readdir()
> returns an entry for that file is unspecified.

Writing `wasm-opt` output, renaming in a same directory being `readdir()`-ed is
unspecified, and can cause the `readdir()` to loop indefinitely on certain
filesystems.

Fix it by collecting `readdir()` result first before making changes to the
directory.

[1]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/readdir.html
  • Loading branch information
quark-zju committed Nov 11, 2022
1 parent 10f30be commit 37650af
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/wasm_opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ pub fn run(
let wasm_opt_path = wasm_opt.binary(&Tool::WasmOpt.to_string())?;
PBAR.info("Optimizing wasm binaries with `wasm-opt`...");

let mut input_paths = Vec::new();
for file in out_dir.read_dir()? {
let file = file?;
let path = file.path();
if path.extension().and_then(|s| s.to_str()) != Some("wasm") {
continue;
}

let tmp = path.with_extension("wasm-opt.wasm");
input_paths.push(path);
}
for path in input_paths {
let tmp = path.with_extension("wasm-opt");
let mut cmd = Command::new(&wasm_opt_path);
cmd.arg(&path).arg("-o").arg(&tmp).args(args);
child::run(cmd, "wasm-opt")?;
Expand Down

0 comments on commit 37650af

Please sign in to comment.