Repository navigation
Description
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:
opencv/modules/dnn/src/onnx/onnx_importer.cpp
Lines 149 to 153 in d85e67d
| { | |
| 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
alalek commentedon Jan 17, 2020
Right, ARMv7 doesn't tolerate unaligned memory access (unchecked
char*=>int64_t*).alalek commentedon Jan 17, 2020
@Palmitoxico Thank you for the report and investigation!
Could you please try this patch: #16377 ?
augustofg commentedon Jan 17, 2020
@alalek The #16377 patch doesn't solve the issue, still getting SIGBUS when deferencing the src pointer. Are the
unaligned_int64_ttype supposed to force the compiler to access the data in a byte-per-byte basis?alalek commentedon Jan 17, 2020
Thank you! This doesn't work because templates can't handle attributes properly.
Please validate updated patch.
augustofg commentedon Jan 17, 2020
@alalek #16377 works now, thanks!
amichayfeld commentedon Apr 1, 2020
Hi. This issue occurs also with version 4.1.0