bcoin-org / bcrypto

JS crypto library
Other
99 stars 41 forks source link

"A dynamic link library (DLL) initialization routine failed" when run in Windows under Electron or PKG #49

Open mslipper opened 4 years ago

mslipper commented 4 years ago

Hi there,

When I run software under Electron or PKG that runs bcrypto with native bindings on Windows, I get a "DLL Initialization Routine Failed" error. The error looks like this:

Error: A dynamic link library (DLL) initialization routine failed.
\\?\C:\Users\Matthew Slipper\IdeaProjects\hsd\node_modules\bcrypto\build\Release\bcrypto.node
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:1186:18)
    at Module.load (internal/modules/cjs/loader.js:984:32)
    at Function.Module._load (internal/modules/cjs/loader.js:877:14)
    at Module.require (internal/modules/cjs/loader.js:1024:19)
    at Module.require (pkg/prelude/bootstrap.js:1225:31)
    at require (internal/modules/cjs/helpers.js:72:18)
    at bindings (C:\snapshot\hsd\node_modules\loady\lib\loady.js:98:17)
    at Object.<anonymous> (C:\snapshot\hsd\node_modules\bcrypto\lib\native\binding.js:14:33)
    at Module._compile (pkg/prelude/bootstrap.js:1320:22)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1156:10)

I've already verified that the Node ABI version in use is correct. After attaching WinDBG to the PKG-generated process, I see the following error:

ModLoad: 00007ffe`4a130000 00007ffe`4a23e000   \\?\C:\Users\Matthew Slipper\IdeaProjects\hsd\node_modules\bdb\build\Release\leveldown.node
ModLoad: 00007ffe`409d0000 00007ffe`40b1a000   \\?\C:\Users\Matthew Slipper\IdeaProjects\hsd\node_modules\bcrypto\build\Release\bcrypto.node
ModLoad: 00007ffe`8c4b0000 00007ffe`8c4c7000   C:\WINDOWS\SYSTEM32\VCRUNTIME140.dll
ModLoad: 00007ff7`12a80000 00007ff7`1499f000   C:\Program Files\nodejs\node.exe
(436c.1a2c): Access violation - code c0000005 (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.

(Note: The exception is not handled, since WinDBG goes into a loop after stepping over the exception.)

The error is the same when I debug Bob in the same way. Error code c0000005 isn't a missing DLL error, it implies a runtime bug within bcrypto itself that is triggering a memory access violation. Any chance you could take a look and see what is going on? Being able to bundle HSD via PKG would dramatically simplify Bob development, and allow people to make standalone HSD binaries in the future.

The issue is not present when HSD is run from my system Nodejs interpreter. The error disappears when I set process.env.NODE_BACKEND to js.

chjj commented 4 years ago

I can't seem to reproduce this with pkg. What bcrypto version are you using? Is there any chance of creating an isolated test case which reproduces the issue?

sdtsui commented 4 years ago

@chjj since this is for Bob, I think I can answer with reasonable confidence.

Here's our package-lock.json for Bob 4.0:

    "hsd": {
      "version": "https://github.com/kyokan/hsd/tarball/296f36c996d9e1561fd0718d9887402181d57c85",
      "integrity": "sha512-wrLgzlMnL/1vI5lwzR+FEfF9PUPXhuxPkxosFiekuDFHmkB2tvJQeETcS0w+mxLYBS8CFnQFfDyklczO74RCnw==",
      "requires": {
        "bcfg": "~0.1.6",
        "bcrypto": "bcrypto@git+https://github.com/bcoin-org/bcrypto.git#5c3e2b8b3f79d13e1b5c26be197708d0b9e5bcf9",
        "bdb": "~1.2.1",
        "bdns": "~0.1.5",
        "bevent": "~0.1.5",
        "bfile": "~0.2.2",
        "bfilter": "~1.0.5",
        "bheep": "~0.1.5",
        "binet": "~0.3.5",
        "blgr": "~0.1.7",
        "blru": "~0.1.6",
        "blst": "~0.1.5",
        "bmutex": "~0.1.6",
        "bns": "~0.11.0",
        "bsert": "~0.0.10",
        "bsock": "~0.1.9",
        "bsocks": "~0.2.5",
        "btcp": "~0.1.5",
        "buffer-map": "~0.0.7",
        "bufio": "~1.0.6",
        "bupnp": "~0.2.6",
        "bval": "~0.1.6",
        "bweb": "~0.1.10",
        "goosig": "git+https://github.com/kyokan/goosig.git#29cd9dce78abb296400104ab8fea62ee515cefc4",
        "hs-client": "~0.0.8",
        "n64": "~0.2.10",
        "urkel": "~0.6.3"
      },

This commit, from May 26th, is somewhere between 5.1 (may 19th) and 5.2 (jun 13th).

pinheadmz commented 4 years ago

@chjj @mslipper I am able to get Bob to build and run in dev mode on Windows by reverting this commit: https://github.com/bcoin-org/bcrypto/commit/f8f5cb73cf44e21ae8ef3df36c832ef313cb7fcb or in other words, using cmake instead of node-gyp as you're already planning on doing I think.

I'm not sure but reading up on this issue I think the problem is because electron doesn't contain nodejs, it runs a fork, and so there are these extra tools like electron-builder and electron-rebuild that have to compile the native addons for the correct javascript engine.

What I did just now is clone master branch of Bob, restore the hsd dependency to its master branch from handshake-org, remove the process.env.NODE_BACKEND = 'js' line and install. Then I removed node_modules/bcrypto and replaced it with the master branch of bcrypto, reverting that commit and rebuilding with cmake.

Bob seems to running fine on Windows now in dev mode, and synced the HNS blockchain in about 45 minutes.

chjj commented 4 years ago

@pinheadmz very interesting that it works with cmake. node-gyp adds a ton of unnecessary flags to each build (most of which are intended for building node.js itself, not addons). I would guess something here is borking the build on windows.

Maybe this justifies switching to cmake for everything, though it is an extra dependency for users depending on how you look at it.

pinheadmz commented 4 years ago

Oh to be clear - hsd and bcrypto build fine on windows as-is master branches and latest releases. But there is something wrong with building for Electron on windows.