Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bun:ffi module error: Segmentation fault. #6933

Closed
CsVeryLoveXieWenLi opened this issue Nov 6, 2023 · 8 comments
Closed

bun:ffi module error: Segmentation fault. #6933

CsVeryLoveXieWenLi opened this issue Nov 6, 2023 · 8 comments
Labels
bug Something isn't working bun:ffi Something related with FFI in Bun crash An issue that could cause a crash

Comments

@CsVeryLoveXieWenLi
Copy link

CsVeryLoveXieWenLi commented Nov 6, 2023

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 CsVeryLoveXieWenLi added the bug Something isn't working label Nov 6, 2023
@CsVeryLoveXieWenLi
Copy link
Author

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

@Electroid Electroid added crash An issue that could cause a crash bun:ffi Something related with FFI in Bun labels Nov 6, 2023
@CsVeryLoveXieWenLi
Copy link
Author

旧的问题。
old question.

#6914

@CsVeryLoveXieWenLi
Copy link
Author

CsVeryLoveXieWenLi commented Nov 6, 2023

我的代码,使用@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
Copy link
Author

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

@CsVeryLoveXieWenLi
Copy link
Author

在旧问题中,我提到了,可能是因为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
Copy link
Author

请问什么时候修复呢?
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
Copy link
Author

经过我的测试,这个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
Copy link
Author

备注:仅仅在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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working bun:ffi Something related with FFI in Bun crash An issue that could cause a crash
Projects
None yet
Development

No branches or pull requests

2 participants