cryptocoinjs / keccak

Keccak sponge function family
MIT License
87 stars 24 forks source link

[bug] keccak does not work in linux alpine image #15

Closed wanseob closed 4 years ago

wanseob commented 4 years ago

Tested the tutorial on node:12-alpine docker image shell but failed. Tutorial works well on Ubuntu.

It failed when I try to call update() function with "Segmentation fault (core dumped)" error.

wanseob commented 4 years ago

Related issues

https://github.com/ethereum/js-ethereum-cryptography#3 https://github.com/ethereumjs/ethereumjs-util/issues/266 https://github.com/ethereumjs/ethereumjs-vm#817

alcuadrado commented 4 years ago

@wanseob can you help me reprodce this? Do you have a link to some place where I can do it?

PhilippLgh commented 4 years ago

here is a very basic test:

docker run --rm -it node:alpine /bin/sh

/ # cat /etc/os-release
NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.11.6
PRETTY_NAME="Alpine Linux v3.11"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://bugs.alpinelinux.org/"

/ # mkdir test
/ # cd test
/ # npm init -y
/ # npm install --save keccak

> keccak@3.0.0 install /node_modules/keccak
> node-gyp-build || exit 0

npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN @1.0.0 No description
npm WARN @1.0.0 No repository field.

+ keccak@3.0.0
added 3 packages from 52 contributors and audited 3 packages in 1.643s
found 0 vulnerabilities

/ # node
Welcome to Node.js v14.5.0.
Type ".help" for more information.
> const createKeccakHash = require('keccak')
> createKeccakHash('keccak256').digest().toString('hex')
'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'
> createKeccakHash('keccak256').update('Hello')
Segmentation fault
alcuadrado commented 4 years ago

Hey @fanatid,

I dug deeper into this, and I now understand what's going on. The Linux version of this package is being built using glibc, but Alpine Linux (a super popular distro in Docker-land) uses another libc. These are not compatible with each other, so the module ends up crashing.

I asked about this in the prebuildify repo and they told me about two things that can be done here:

  1. Use a prebuildify param that tags the prebuilds with the libc used at build time. This is then understood by node-gyp-build, which will use the js fallback if the system libc doesn't have a prebuilt.

  2. prebuildify-cross can be used to also crosscompile a version for Alpine Linux, so that a native version can be used in that distro.

I think (1) is pretty easy to implement, but maybe not so much to test, and that (2) is really nice to have, but requires more work, and can be added in the future.

If you agree with this, I can send a PR implementing (1).

Also, this same problem is probably present in secp256k1.

fanatid commented 4 years ago

Thanks for digging! If first sounds easy we can choose this way right now and try a better solution later. Probably I'll able check this issue on weekend.

fanatid commented 4 years ago

Published as 3.0.1.

alcuadrado commented 4 years ago

Awesome! Thanks for doing both releases!