From 10f86e310192091811e20070013d90b71d9a1eb5 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 8 Nov 2023 11:51:39 +0100 Subject: [PATCH] No wasm nm (#263) # Motivation We use `wasm-nm` (the Wasm version of [`nm`](https://web.mit.edu/gnu/doc/html/binutils_3.html)) to geta list of exported functions. Unfortunately `wasm-nm` is no longer maintained and fails with the latest Wasm files that have new instructions. `ic-wasm info` provides the same information, but not in a machine readable form. Where we have switched to ic-wasm we use sed to extract the interesting information, at which point the output is practically in the standard `nm` output format. I propose to ask upstream to provide a machine readable output, preferably in the `nm` format, and in the meantime make a polyfill. ## Alternatives considered Copy and paste the sed incantation into the one place we use `wasm-nm` in this repository. I'd rather get a clean solution pushed upstream. --------- Co-authored-by: Max Murphy --- .github/workflows/checks.yml | 12 ++++++-- bin/dfx-canister-nm | 2 +- bin/dfx-software-wasm-nm-install | 47 ----------------------------- bin/versions.bash | 1 + bin/wasm-nm | 52 ++++++++++++++++++++++++++++++++ 5 files changed, 64 insertions(+), 50 deletions(-) delete mode 100755 bin/dfx-software-wasm-nm-install create mode 100755 bin/wasm-nm diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 8d0c2ef9..3f9fb11a 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -60,8 +60,12 @@ jobs: - uses: actions/checkout@v3 - name: Install apt-dependencies run: sudo apt-get update && sudo apt-get install moreutils -yy && command -v more + - name: Install cargo binstall + uses: ./.github/actions/install_binstall - name: Install tools - run: ./bin/dfx-software-wasm-nm-install + run: | + . bin/versions.bash + cargo binstall --force --no-confirm "ic-wasm@${IC_WASM_VERSION}" - name: "Test the sns aggregator version command" run: ./bin/dfx-software-sns-aggregator-version.test env: @@ -117,8 +121,12 @@ jobs: run: ./bin/dfx-software-nns-dapp-version.test env: GH_TOKEN: ${{ github.token }} + - name: Install cargo binstall + uses: ./.github/actions/install_binstall - name: Install tools - run: ./bin/dfx-software-wasm-nm-install + run: | + . bin/versions.bash + cargo binstall --force --no-confirm "ic-wasm@${IC_WASM_VERSION}" - name: "Test the nns-dapp url command" run: ./bin/dfx-software-nns-dapp-ci-wasm-url.test env: diff --git a/bin/dfx-canister-nm b/bin/dfx-canister-nm index 6adcb90b..4dc8f804 100755 --- a/bin/dfx-canister-nm +++ b/bin/dfx-canister-nm @@ -18,7 +18,7 @@ source "$SOURCE_DIR/clap.bash" source "$(clap.build)" # The only meaningful export from canister wasms is function names. -canister_nm=(wasm-nm -je) +canister_nm=(wasm-nm -j -e) canister_wasm="$1" case "$(file "$canister_wasm")" in diff --git a/bin/dfx-software-wasm-nm-install b/bin/dfx-software-wasm-nm-install deleted file mode 100755 index 9aacd7c4..00000000 --- a/bin/dfx-software-wasm-nm-install +++ /dev/null @@ -1,47 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail -SOURCE_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" -PATH="$SOURCE_DIR:$PATH" -. "$SOURCE_DIR/versions.bash" - -print_help() { - cat <<-EOF - - Installs wasm-nm. - - wasm-nm prints the symbols in a wasm binary, similar to 'nm' for native executables. - - Canister wasms are typically compressed, so need to be decompressed before use, or - use dfx-canister-nm. - EOF -} - -# Source the clap.bash file --------------------------------------------------- -source "$SOURCE_DIR/clap.bash" -# Define options -clap.define short=v long=version desc="The version of wasm-nm to install" variable=VERSION default="0.2.0" -clap.define short=b long=bin desc="Local directory for executables" variable=USER_BIN default="$HOME/.local/bin" -# Source the output file ---------------------------------------------------------- -source "$(clap.build)" - -export DFX_NETWORK - -wasm_nm_version() { - wasm-nm --version | awk '{print $2}' -} -wasm_nm_check() { - [[ "$(wasm_nm_version 2>/dev/null)" == "$VERSION" ]] -} -wasm_nm_install() { - cargo install wasm-nm@"$VERSION" -} - -if wasm_nm_check; then - echo "Version $VERSION is already installed." -else - echo "Installing wasm-nm $VERSION..." - wasm_nm_install - echo " Checking installation..." - wasm_nm_check - echo " Installation successful." -fi diff --git a/bin/versions.bash b/bin/versions.bash index 1fcaca86..3caa7de8 100644 --- a/bin/versions.bash +++ b/bin/versions.bash @@ -9,3 +9,4 @@ DIDC_VERSION=2023-07-25 QUILL_VERSION=0.4.0 IDL2JSON_VERSION=0.8.5 BINSTALL_VERSION=1.3.0 +IC_WASM_VERSION=0.6.0 diff --git a/bin/wasm-nm b/bin/wasm-nm new file mode 100755 index 00000000..4db8a480 --- /dev/null +++ b/bin/wasm-nm @@ -0,0 +1,52 @@ +#!/usr/bin/env bash +set -euo pipefail +SOURCE_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" +PATH="$SOURCE_DIR:$PATH" + +print_help() { + cat <<-EOF + + Polyfill for the now outdated wasm-nm. + + Supports only: + wasm-nm -e SOMEFILE.wasm + wasm-nm -e -j SOMEFILE.wasm + + Note: ic-wasm now provides much the same information, but not in a very machine-readable format. + EOF +} + +# Source the clap.bash file --------------------------------------------------- +source "$SOURCE_DIR/clap.bash" +# Define options +clap.define short=e long=export desc="Display only export symbols." variable=ONLY_EXPORT nargs=0 +clap.define short=j long=export desc="Just display the symbol names (no type)." variable=NO_TYPE nargs=0 +# Source the output file ---------------------------------------------------------- +source "$(clap.build)" + +(($# == 1)) || { + echo "ERROR: Please provide the Wasm filename as a positional argument, like this:" + echo " wasm-nm -e my-file.wasm" + exit 1 +} >&2 + +WASM_FILENAME="${1:-}" + +export_symbols() { + ic-wasm "${WASM_FILENAME:-}" info | sed -nE '/^Exported methods:/,/^]/p' | sed '1d;$d' | sed -E 's/.*"(.*)",/\1/;s/ *\(.*//g' | { + if [[ "${NO_TYPE:-}" == "true" ]]; then + cat + else + sed 's/^/e /g' + fi + } +} + +if [[ "${ONLY_EXPORT}" == "true" ]]; then + export_symbols +else + { + echo "ERROR: Action not supported, sorry." + exit 1 + } >&2 +fi