tweag / asterius

DEPRECATED in favor of ghc wasm backend, see https://www.tweag.io/blog/2022-11-22-wasm-backend-merged-in-ghc
1.98k stars 55 forks source link

ahc-dist fails during the wasm verification phase #824

Open vu3rdd opened 3 years ago

vu3rdd commented 3 years ago

Describe the bug

I am trying to build the magic-wormhole project to web assembly using Asterius. During the ahc-dist phase, it quits at the wasm-validator phase with errors similar to this:

[wasm-validator error in function cryptonitezm0zi26zmH0gw9uA2NzzCmRLvv1i5sC_CryptoziHash_.Lc1pAI] unexpected false: non-final block elements returning a value must be drop()ed (binaryen's autodrop option might help you),

To Reproduce

mkdir test
cd test
# add a cabal.project file with contents:
cat << EOF >> cabal.project
> packages: */*.cabal
EOF
# clone the project and switch to a specific branch.
git clone -b 44.build-with-new-protolude https://github.com/LeastAuthority/haskell-magic-wormhole.git

# get a dependency which is not in the asterius docker image.
cabal unpack protolude-0.3.0

# edit protolude-0.3.0/src/Protolude/Base.hs
# comment out lines 67, 68, 69, 70
#     67  -- #if MIN_VERSION_base(4,9,0)
#    68  --   , gcdInt'
#    69  --   , gcdWord'
#   70  -- #endif

# install some more missing C and build dependencies.
apt update
apt install libsodium-dev pkg-config

# now we are ready to build
cd haskell-magic-wormhole
mkdir tmp
ahc-cabal new-install --keep-going --installdir ./tmp/ --overwrite-policy=always

# after a while, this should result in a file called ./tmp/hocus-pocus

# now, run ahc-dist on it.
ahc-dist --browser   --input-exe ./tmp/hocus-pocus

Expected behavior

Was expecting to see a wasm module generated.

Environment

Additional context

None

thecount2a commented 3 years ago

I'm getting a similar bunch of errors while trying to build a source file that includes a large library that is pre-built in the asterius docker image. I am building a source file that is available in the hledger source code tree and it utilizes the hledger-lib library which is prebuilt.

I can reproduce this issue, from the asterius docker shell using these two lines:

git clone https://github.com/simonmichael/hledger.git && cd hledger && git checkout 1.18-branch && cd bin ahc-link --input-hs hledger-check.hs --run

Here is the beginning of the output, although it continues on to give many more of the same error about "unexpected false":

[INFO] Compiling hledger-check.hs to WebAssembly [1 of 1] Compiling Main ( hledger-check.hs, hledger-check.o ) Linking /tmp/hledger-check391-0 ... [INFO] Converting linked IR to binaryen IR [INFO] Running binaryen optimization [INFO] Validating binaryen IR [wasm-validator error in function regexzmtdfazm1zi3zi1zi0zm16m11FfqhKd3wPZZTDHF5VzzTextziRegexziTDFAziNewDFAziEngine.Lc5m7A] unexpected false: non-final block elements returning a value must be drop()ed (binaryen's autodrop option might help you), on ................. [many lines of output]

Any thoughts on anything I could try to further diagnose this? This error definitely comes from the binaryen project (I found it in their source code) but I wouldn't know whether this is a false positive error or whether there is truly an issue with asterius.

thomasbach-dev commented 3 years ago

EDIT: Sorry for the noise, the error vanished because I passed --no-validation to ahc-dist (I've overseen that flag) and Cloudflare didn't accept the bundle. So the below doesn't hold and you can count me in on this issue. :/

I haven't looked into the provided repositories. I stumbled over this issue as well and it took me a while to find the problem – I forgot to set ghc-options: -optl--export-function=handleFetch in the cabal file. You might want to check that.

(I found this here: https://blog.cloudflare.com/cloudflare-worker-with-webassembly-and-haskell/ not sure if it's documented somewhere else.)