Skip to content

Bus fault when importing ONNX model on Android #16373

@augustofg

Description

@augustofg
System information (version)
  • OpenCV => 1a74de1 (4.2)
  • Operating System / Platform => Android 4.4.4 amv7l
  • Compiler => Android NDK r18b (clang, Android API level = 16)
  • Optmization => -DCMAKE_BUILD_TYPE=Release (-Oz)
Detailed description

I'm getting a SIGBUS when loading an ONNX model on Android (armv7l), this problem only occurs when building with optimizations enabled, -O0 doesn't trigger the issue. I've nailed down the problem to these specific lines:

{
char* val = const_cast<char*>(tensor_proto.raw_data().c_str());
int64_t* src = reinterpret_cast<int64_t*>(val);
convertInt64ToInt32(src, dst, blob.total());
}

The problem is that the pointer returned by std::string::c_str() doesn't have any guarantee that it is 8 bytes aligned, and sure enough when inspecting with GDB the val pointer isn't aligned, thus src.

I've workaround this by allocating a intermediate buffer and copying the data pointed by val:

{
    char* val = const_cast<char*>(tensor_proto.raw_data().c_str());
    int size = tensor_proto.raw_data().size();
    int64_t* src = new int64_t[size];
    memcpy(src, val, size);
    convertInt64ToInt32(src, dst, blob.total());
    delete[] src;
}

I don't known if this is an good solution, please let me known.

Steps to reproduce

Build for Android armv7l, NDK r18b with optimizations. Call cv::dnn::readNetFromONNX() with a valid ONNX file.

Thanks,
Augusto.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions