2

I'm porting a C++ library on Android (Android Studio 2.3.1/NDK 25); the library works flawlessly on UWP (VS2017 VC 1.41 - ARM & Win32) a custom ARM7 board (GCC 4.8).

When debugging on Android Studio, I get a "SIGBUS (signal SIGBUS: illegal alignment)" during an assignment to a struct member. Here is the struct (I need it to be 64-bit aligned):

typedef unsigned int        _t_u32;         // 32-bit unsigned
typedef unsigned long long  _t_u64;         // 64-bit unsigned

typedef struct __attribute__((aligned(8)))
{
    _t_u32      crc32;
    _t_u64      counter;

} t_security;

Now, here is the code snippet:

void prepareBuffer(_t_u8 cmd, _t_u8 *buffer, _t_u32 buffferLen)
{
    t_security *secPtr = ((t_security *)(buffer + sizeof(_t_u8)));

    secPtr->crc32 = 0;
    secPtr->counter= 0; << when this is being executed, on Android Studio-only, I get *"SIGBUS (signal SIGBUS: illegal alignment)"*
    ...
    ...
}

From Android Studio debugger watches:

sizeof(t_security) = {unsigned int} 16
&secPtr = {t_security * | 0xdc98eb41} 0xdc98eb41
&secPtr->crc32 = {_t_u32 * | 0xdc98eb41} 0xdc98eb41
&secPtr->counter = {_t_u64 * | 0xdc98eb49} 0xdc98eb49

From Visual Studio debugger watches (ARM platform):

sizeof(t_security)  16  unsigned int
secPtr  0x00afe2e5 {crc32=3435973836 ...}   t_security *
&secPtr->crc32  0x00afe2e5 {3435973836} unsigned int *
&secPtr->counter    0x00afe2ed {14757395258967641292}   unsigned __int64 *

I suppose it has to due to packing/member alignment... but as you can notice, the packaging seems consistent on the two platforms... just on Android Studio-only, I get "SIGBUS (signal SIGBUS: illegal alignment)".

Can someone please help me understand what's going on? May be a compiler switch I'm missing? Here's the ndk's gradle config:

android.ndk {
    moduleName = "NativeLib"

    // add compilation flags
    cppFlags.add("-DANDROID")
    cppFlags.add("-frtti")
    cppFlags.add("-std=c++14")
    cppFlags.add("-fexceptions")

    // include headers
    cppFlags.add("-I${file("native-src")}".toString())

    ldLibs.addAll("android", "dl", "log", "z", "atomic")

    stl = "c++_static"  // LLVM compiler
}
android.buildTypes {
    all {
        // To solve struct packing issues, setting abiFilters to package only 32-bit architectures:
        ndk.with {
            abiFilters.add("armeabi")
            abiFilters.add("armeabi-v7a")
            abiFilters.add("mips")
            abiFilters.add("x86")
        }
    }
    debug {
        ndk.with {
            cppFlags.add("-DDEBUG")
            CFlags.add("-DDEBUG")
        }
    }
}

Many thanks!

5
  • Looks like a copy/paste mistake. Isn't the last counter address 0x00efe26d?
    – Alex Cohn
    Commented Apr 22, 2017 at 14:37
  • Apologies for that, I corrected with fresh data from another run. Thanks
    – gtrevi
    Commented Apr 22, 2017 at 16:00
  • I guess the other ARM platform handles unaligned access. ARM7 architecture allows this, but at rather high price. I strongly recommend to rewrite your code so that all data access is aligned.
    – Alex Cohn
    Commented Apr 22, 2017 at 17:22
  • Thanks Alex, I appreciate your suggestion. Although in the "tiny world" (ARM7), I can't afford memory allocation & communication with align-bloated memory chunks. I designed the ARM7 hw platform & a custom rtos, and can't see why ndk has such limitation (as VS & gcc don't)... there has to be some compiler switch or so... I hope :)
    – gtrevi
    Commented Apr 22, 2017 at 17:55
  • Here the CPU setup is described: infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0360f/…
    – Alex Cohn
    Commented Apr 23, 2017 at 6:00

1 Answer 1

2

I had exactly the same issue.

In your code snippet, secPtr is not aligned because it points to buffer offseted by 1(sizeof(_t_u8)) byte. (assuming that buffer is aligned address)

All 4-byte ailgned memory addresses should end in '0', '4', '8' or 'C'. Since secPtr ends in '5', it is not aligned.

ARM processors support some of unaligned memory access. That is why secPtr->crc32 = 0; is legal, but secPtr->counter= 0; is not.

Try removing 1-byte offset in secPtr somehow and see if the problem goes away.

Also check out this page for detailed information: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka15414.html

5
  • Hi gasbank, that is exactly the point, so marking yours as the answer. Didn't had yet the chance to update the community (apologies for that), but I managed to "restructure" my data & code so that to carry over the full aligned pointer, rather than re-basing it for casting. Thanks!
    – gtrevi
    Commented May 16, 2017 at 18:36
  • PS: just as an info, custom struct packing size on the default Android Studio compiler can be achieved with the cppFlags.add("-fpack-struct=[no of bytes]") , within the android.ndk section of the build gradle.
    – gtrevi
    Commented May 16, 2017 at 18:47
  • Good to hear you solved your's gtrevi. Funny enough that actually it's your question that helped me a lot to get ideas what's going wrong with mine. Thanks for sharing your issue with quite a detailed description. :)
    – gasbank
    Commented May 17, 2017 at 9:37
  • Hi, thanks for your answer @gasbank, you helped me fixing the same issue on my project. I was allocating some extra bytes at the top of my buffers and using some neon instructions were causing a SIGBUS. I solved it by aligning the extra bytes however for me it was 8 bytes and not 4, not sure why.
    – Damien
    Commented Feb 7, 2019 at 11:53
  • 1
    Dead link. The wayback machine didn't help either.
    – vesperto
    Commented Mar 12, 2021 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.