openpeeps / denim

Node đź’– Nim = Denim! Build powerful NodeJS / BunJS addons with Nim language via Node API (NAPI)
https://openpeeps.github.io/denim/
MIT License
40 stars 1 forks source link

Denim addon segfaults with Node, but works with Bun #15

Closed jfilby closed 1 month ago

jfilby commented 1 month ago

I'm installing and testing a Denim add-on on a new server (Debian 12). I kept getting segfaults when running my Next.js web app with an add-on. So I used a minimal test program and js script, the one that prints a message to the console (only), and it's still segfaulting.

I'm running Nim v.2.0.4 and Denim v0.1.9 (latest).

This happens with both Node v20.14.0 (lts) and v22.2.0 (current). However it works with Bun v1.1.12 (latest)!

Any idea why this could be?

With Node v18 and v19 it prints the message, but then aborts (SIGABRT).

In nim.cfg:

--path:"."
--passC:"-I/usr/include/node -I/usr/local/include/node"
--gc:orc
--define:release

test.nim:

proc getWelcomeMessage(): string =

  return "Hi!"

when defined napibuild:
  # Running `denim build myprogram.nim` compiles with `-d:napibuild`
  import denim # NAPI bindings

  init proc(module: Module) =

    # Test
    proc getWelcomeMessageNim(): string {.export_napi} =

      return %* getWelcomeMessage()

elif isMainModule:
  echo(getWelcomeMessage())

test.js:

const nim = require('../bin/test.node')

// Welcome message
console.log(nim.getWelcomeMessageNim()) // Hello, World!
jfilby commented 1 month ago

I should add that I'm running GCC v12.2.0.

When running valgrind node js/test.js the final part is:

==61918== Process terminating with default action of signal 11 (SIGSEGV)
==61918==    at 0x6B78E2C: __pthread_kill_implementation (pthread_kill.c:44)
==61918==    by 0x6B29FB1: raise (raise.c:26)
==61918==    by 0xF15EE1: node::TrapWebAssemblyOrContinue(int, siginfo_t*, void*) (in /home/debian/.nvm/versions/node/v22.2.0/bin/node)
==61918==    by 0x6B2A04F: ??? (in /usr/lib/x86_64-linux-gnu/libc.so.6)
==61918==    by 0x67BBAC4: bcmp (vg_replace_strmem.c:1203)
==61918==    by 0x11E6EEC: std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, node::UnionBytes, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, node::UnionBytes> > >::map(std::initializer_list<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, node::UnionBytes> >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > const&, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, node::UnionBytes> > const&) (in /home/debian/.nvm/versions/node/v22.2.0/bin/node)
==61918==    by 0x2AB10776: ??? (in /usr/lib/x86_64-linux-gnu/libnode.so.108)
==61918==    by 0x5F769CD: call_init (dl-init.c:74)
==61918==    by 0x5F769CD: call_init (dl-init.c:26)
==61918==    by 0x5F76AB3: _dl_init (dl-init.c:121)
==61918==    by 0x6C3D023: _dl_catch_exception (dl-error-skeleton.c:182)
==61918==    by 0x5F7D21D: dl_open_worker (dl-open.c:808)
==61918==    by 0x6C3CFC9: _dl_catch_exception (dl-error-skeleton.c:208)
jfilby commented 1 month ago

I found the problem. I had installed node-gyp using sudo apt install node-gyp.

Installing with npm install -g node-gyp fixed the issue.