Skip to content

Instantly share code, notes, and snippets.

@pepyakin
Created February 23, 2018 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pepyakin/79de4ac26d20adcd9db64bf85ef34cba to your computer and use it in GitHub Desktop.
Save pepyakin/79de4ac26d20adcd9db64bf85ef34cba to your computer and use it in GitHub Desktop.

When one try to run wasm-reduce on macOS machine, they will see something like this:

wasm-reduce -v -f \
	--timeout=15 \
	--command="./predicate.sh" \
	--test=test.wasm \
	--working=working.wasm \
	~/Downloads/wasm-collection-master/misc-valid/cv-wasm.wasm

|wasm-reduce
|input: /Users/pepyakin/Downloads/wasm-collection-master/misc-valid/cv-wasm.wasm
|test: test.wasm
|working: working.wasm
|expected result:
[ProgramResult] code: 32512 stdout:
[/ProgramResult]

|checking that command has different behavior on invalid binary
|! running command on an invalid module should give different results
[ProgramResult] code: 32512 stdout:
[/ProgramResult]

Fatal: |! stopping, as it is very unlikely reduction can succeed (use -f to ignore this check)

It took quite some to figure out why my predicate script always returns status code 32512! : ) Here is my story:

It turns out that exit code 32512 is returned when command not found. As I found out later, it is not my predicate script is not found but the some timeout command which is used by wasm-reduce to cap the execution time of the predicate script.

Unfortunately, macOS comes without installed timeout. Fortunately, it can easily be installed with brew: brew install coreutils.

Since macOS comes with some of coreutils programs installed, all these programs installed with names prepended by g (e.g timeout is gtimeout). Luckily, these are just aliases and programs with original names are installed in /usr/local/opt/coreutils/libexec/gnubin. So it is sufficient to just export new PATH that will use coreutils

export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"

Next, after I try to launch reducing again, I stopped by this issue:

|checking that command has different behavior on invalid binary
|checking that command has expected behavior on canonicalized (read-written) binary
|! failed to read and write the binary
[ProgramResult] code: 32512 stdout:
[/ProgramResult]

So it seems that wasm-reduce trying to launch wasm-opt but for some reason can't.

https://github.com/WebAssembly/binaryen/blob/d9692277357ba6fd67a7e25ce16934209d049033/src/tools/wasm-reduce.cpp#L685-L695

That's weird since wasm-opt is on my PATH and wasm-reduce is launched from the same directory where wasm-opt is placed. Looking at the code,

https://github.com/WebAssembly/binaryen/blob/6cf7343bffa3d485161e221b23dfacd9243dec68/src/support/path.h#L41-L55

I was able to see that wasm-reduce is only trying to search wasm-opt at BINARYEN_ROOT. So adding another export might help:

export BINARYEN_ROOT=~/dev/etc/binaryen

After that was done, I was able to launch reducing! Hope this will help for future reducers (incl. me).

Here is my predicate.sh for the reference:

#!/bin/bash
target/release/wasm-stack-height test.wasm result.wasm 2>&1 || exit 1
wasm2wat result.wasm 2>&1 || exit 2
@willcohen
Copy link

For future reference with those trying to run wasm-reduce and running into similar errors, BINARYEN_ROOT is no longer the correct solution to pass the correct directory. wasm-reduce now provides a flag -- -b /path/to/binaryen/bin -- as an argument to provide the location.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment