oven-sh / bun

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

bun:ffi module error: Segmentation fault. #6933

Closed CsVeryLoveXieWenLi closed 6 months ago

CsVeryLoveXieWenLi commented 1 year ago

What version of Bun is running?

1.0.9+98f20170a

What platform is your computer?

Linux version 5.10.0-25-amd64 (debian-kernel@lists.debian.org) (gcc-10 (Debian 10.2.1-6) 10.2.1 20210110, GNU ld (GNU Binutils for Debian) 2.35.2) #1 SMP Debian 5.10.191-1 (2023-08-16)

1. 重现BUG || Reproduce BUG

设置好bun的注释、在函数setTimeout里调用共享库的函数、设置合理的延迟时间,就能重现BUG。 By setting the comments of the bun, calling the shared library function in the setTimeout function, and setting a reasonable delay time, the BUG can be reproduced.

共享库的函数需要访问内存,100毫秒以上的时间就能触发BUG,不行就多加点时间。 The functions of the shared library need to access memory, and it can trigger a BUG in more than 100 milliseconds. If not, add more time.

在这里我调用了函数imdecode,这是opencv库中一个从内存数据中解码图片的函数。 Here I called the function imdecode, which is a function in the opencv library that decodes images from memory data.

// @bun
import { dlopen, ptr, FFIType } from "bun:ffi";

const lib = dlopen(`${import.meta.dir}/libocr.so`, {
  'slider_universalb': {
    'args': [FFIType.pointer, FFIType.int, FFIType.pointer, FFIType.int],
    'returns': FFIType.int
  }
});

setTimeout(async () => {
  const src = await Bun.file("./src.jpg").arrayBuffer();
  const cut = await Bun.file("./cut.png").arrayBuffer();

  console.log(lib.symbols.slider_universalb(ptr(src), src.byteLength, ptr(cut), cut.byteLength));
}, 100);
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>

using namespace cv;
using namespace std;

int slider_universalb(char *src_bytes, int src_length, char *cut_bytes, int cut_length)
{
    Mat image_src = imdecode(Mat(1, src_length, CV_8UC1, src_bytes), 1);
    Mat image_cut = imdecode(Mat(1, cut_length, CV_8UC1, cut_bytes), 1);

    Mat image_src_, image_cut_;

    Canny(image_src, image_src_, 255, 255);
    Canny(image_cut, image_cut_, 255, 255);

    cvtColor(image_src_, image_src_, cv::COLOR_GRAY2RGB);
    cvtColor(image_cut_, image_cut_, cv::COLOR_GRAY2RGB);

    Mat result;
    Point max;
    matchTemplate(image_src_, image_cut_, result, TM_CCOEFF_NORMED);
    minMaxLoc(result, nullptr, nullptr, nullptr, &max);

    return max.x;
}

2. 预期输出 || expect output

过低的延迟导致无法重现BUG,成功执行共享库的函数,输出正确的数字。 Excessive low latency makes it impossible to reproduce the BUG, successfully execute the function of the shared library, and output the correct number.

root@sysdebian:~/Projects/Test# bun main.js
149
root@sysdebian:~/Projects/Test# bun main.js
149
root@sysdebian:~/Projects/Test# bun main.js
149
root@sysdebian:~/Projects/Test# bun main.js
149
root@sysdebian:~/Projects/Test# bun main.js
149
root@sysdebian:~/Projects/Test# 

3. 错误输出 || error output

重现BUG。 Reproduce the BUG.

root@sysdebian:~/Projects/Test# bun main.js
Segmentation fault
root@sysdebian:~/Projects/Test# bun main.js
Segmentation fault
root@sysdebian:~/Projects/Test# bun main.js
Segmentation fault
root@sysdebian:~/Projects/Test# bun main.js
Segmentation fault
root@sysdebian:~/Projects/Test# bun main.js
Segmentation fault
root@sysdebian:~/Projects/Test# 

4. 附加说明 || Additional information

如果你有别的问题,不相信我说的,请下载我的源代码调试或者你编写代码自行重现。我使用了版本为4.8.0的opencv库。 If you have other questions and don't believe what I say, please download my source code for debugging or write code to reproduce it yourself. I used the opencv library with version 4.8.0.

如果这个不是bun本身的BUG,我感到很抱歉。 I'm sorry if this is not a bug in the bun itself.

我的源代码 || my source code

CsVeryLoveXieWenLi commented 1 year ago

抱歉,我不会说英语,英语都是谷歌机器翻译的。 Sorry, I don't speak English, the English is all machine translated by Google.

CsVeryLoveXieWenLi commented 1 year ago

旧的问题。 old question.

6914

CsVeryLoveXieWenLi commented 1 year ago

我的代码,使用@bun,我的服务会出现错误。但是,不使用@bun,一切正常。 My code, using @bun, gives an error on my service. However, without using @bun, everything works fine.

经过我的再次测试,我觉得就是这样的BUG,就算不是也是相似吧。 After my re-test, I think it is just such a BUG, even if it is not, it is similar.

image

image

CsVeryLoveXieWenLi commented 1 year ago

这BUG是内存访问越界吗,我查询了相关的资料,我觉得是这样。 Is this BUG an out-of-bounds memory access? I checked the relevant information and I think so.

CsVeryLoveXieWenLi commented 1 year ago

在旧问题中,我提到了,可能是因为ptr函数转换的指针无效才导致问题。 In the old question, I mentioned that the problem might be caused by an invalid pointer converted by the ptr function.

可以测试一下,我要睡了。 You can test it, I'm going to sleep.

CsVeryLoveXieWenLi commented 1 year ago

请问什么时候修复呢? When will it be repaired?

我使用了NodeAddon,发现也是这样,就算我使用dlopen单独导入,也是这样,看来并不只是ffi的问题。 I used NodeAddon and found that it was the same. Even if I used dlopen to import it separately, it was still the same. It seems that it is not just a problem with ffi.

CsVeryLoveXieWenLi commented 6 months ago

经过我的测试,这个BUG已经在目前最新版本被修复了。 After my test, this BUG has been fixed in the latest version.

使用Bun版本:1.1.12+43f0913c3。 Using Bun version: 1.1.12+43f0913c3.

使用Opencv版本:4.9.0。 Using Opencv version: 4.9.0.

CsVeryLoveXieWenLi commented 6 months ago

备注:仅仅在Linux下测试,该BUG也来源于Linux,在我的多台机器或服务器下测试均没问题。 Note: This is only tested on Linux. This BUG also comes from Linux. There is no problem when testing on multiple machines or servers.