Skip to content

Bus fault when importing ONNX model on Android #16373

Closed

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.

Activity

  1. alalek commented on Jan 17, 2020

    @alalek
    Member

    Right, ARMv7 doesn't tolerate unaligned memory access (unchecked char* => int64_t*).

  2. alalek commented on Jan 17, 2020

    @alalek
    Member

    @Palmitoxico Thank you for the report and investigation!
    Could you please try this patch: #16377 ?

  3. augustofg commented on Jan 17, 2020

    @augustofg
    ContributorAuthor

    @alalek The #16377 patch doesn't solve the issue, still getting SIGBUS when deferencing the src pointer. Are the unaligned_int64_t type supposed to force the compiler to access the data in a byte-per-byte basis?

  4. alalek commented on Jan 17, 2020

    @alalek
    Member

    Thank you! This doesn't work because templates can't handle attributes properly.
    Please validate updated patch.

  5. augustofg commented on Jan 17, 2020

    @augustofg
    ContributorAuthor

    @alalek #16377 works now, thanks!

  6. amichayfeld commented on Apr 1, 2020

    @amichayfeld

    Hi. This issue occurs also with version 4.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcategory: dnnplatform: androidplatform: arm

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions