oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
72.91k stars 2.65k forks source link

(Bun v1.1.18) napi: napi_create_reference #12525

Open fzn0x opened 1 month ago

fzn0x commented 1 month ago

How can we reproduce the crash?

Step 1: Set Up the Project

bun init

Step 2: Create the C Code

#include <stdio.h>

int add(int a, int b) {
    return a + b;
}

Step 3: Compile the C Code

gcc -shared -o libexample.so -fPIC example.c

Step 4: Create the TypeScript Code

// index.ts
import * as ffi from 'ffi-napi';
import * as ref from 'ref-napi';

// Define the types of the C function parameters and return type
const int = ref.types.int;

// Define the interface for the C library
const exampleLib = ffi.Library('./libexample', {
  'add': [int, [int, int]] // int add(int a, int b);
});

// Use the C function
const result = exampleLib.add(5, 3);
console.log(`Result of add(5, 3): ${result}`);

Step 5: Run the TypeScript Code

bun run index.ts

Relevant log output

$ bun run index.ts 
napi: napi_create_reference
  Error::Error
============================================================
Bun Canary v1.1.8-canary.1 (889ec13b) Windows x64 (baseline)
Args: "C:\Program Files\Git\usr\local\node_modules\bun\bin\bun.exe", "run", "index.ts"
Features: jsc tsconfig(2) 
Builtins: "bun:main" "node:assert" "node:fs" "node:os" "node:path" "node:string_decoder" "node:tty" "node:util" "node:util/types" 
Elapsed: 174ms | User: 0ms | Sys: 0ms
RSS: 119.42MB | Peak: 119.42MB | Commit: 0.16GB | Faults: 29393

panic(main thread): napi: napi_create_reference
  Error::Error
oh no: Bun has crashed. This indicates a bug in Bun, not your code.

To send a redacted crash report to Bun's team,
please file a GitHub issue using the link below:

 https://bun.report/1.1.8/er1889ec13AiggggE86+tS++z2Ogq47J4l47JCe\node.napi.nodew9lCCe\node.napi.nodew+qDCe\node.napi.nodemw6BCe\node.napi.node65/CCe\node.napi.node+wgCCe\node.napi.nodei04BA0eNrLSyzItFLISyzIjE8uSk0sSY0vSk1LLUrNS07lUlBwLSrKL7KyAlMAT0oPXg

Stack Trace (bun.report)

Bun v1.1.8 (889ec13) on windows x86_64_baseline [RunCommand]

panic: napi: napi_create_reference Error::Error

github-actions[bot] commented 1 month ago

@fzn0x, the latest version of Bun is v1.1.18, but this crash was reported on Bun v1.1.8.

Are you able to reproduce this crash on the latest version of Bun?

bun upgrade
fzn0x commented 1 month ago

@fzn0x, the latest version of Bun is v1.1.18, but this crash was reported on Bun v1.1.8.

Are you able to reproduce this crash on the latest version of Bun?

bun upgrade

Yes

fzn0x commented 1 month ago

Alternative is using bun.ffi:

import { dlopen, FFIType } from "bun:ffi";
import { join } from "path";

// Define the path to the shared library
const libraryPath = join(process.cwd(), `libexample.so`);

console.log(`Loading library from: ${libraryPath}`);

// Define the interface for the C library
const lib = dlopen(libraryPath, {
  add: {
    args: [FFIType.i32, FFIType.i32],
    returns: FFIType.i32,
  },
});

// Use the C function
const result = lib.symbols.add(5, 3);
console.log(`Result of add(5, 3): ${result}`);

I hope we have cross-runtime solution to run ffi

fzn0x commented 1 month ago

I don't know why it so slow, compared with JS written functions

Loading library from: D:\node-gyp\libexample.so
cpu: 12th Gen Intel(R) Core(TM) i7-12700F
runtime: bun 1.1.19 (x64-win32)

benchmark      time (avg)             (min … max)       p75       p99      p999
------------------------------------------------- -----------------------------
• group
------------------------------------------------- -----------------------------
C - add      13.8 ns/iter      (12.3 ns … 430 ns)  12.99 ns  35.64 ns    181 ns
JS - add      257 ps/iter     (244 ps … 25.05 ns)    244 ps    342 ps    439 ps