Bug ID: 34143
           Summary: Hardfault when dereferencing unaligned float
           Product: clang
           Version: 4.0
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: LLVM Codegen

Code to reproduce:

template <typename T> inline T Read (uint8_t* data)
    auto valuePtr = reinterpret_cast<T*> (data);
    auto value = *valuePtr;
    return value;
static float test = 180.0f;
int main (void)
    uint8_t* data = new uint8_t[8];
    memcpy (data, &test, sizeof (float));
    auto value = Read<float> (data);

    memcpy (data + 1, &test, sizeof (float));
    value = Read<float> (data + 1); // hard fault here!
    return value;

first call to Read will cause an aligned dereferencing of the pointer.
second call will cause an in-aligned deferencing of pointer to float.

second call causes a hard fault.

Wall -c -fshort-enums -ggdb3 -std=c++14 -fno-rtti -fno-exceptions
-mfpu=fpv4-sp-d16 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mfloat-abi=hard
-ffunction-sections -fdata-sections -O1 -target arm-none-eabi -mcpu=cortex-m4

When code is compiled with -O0, it works.

with -01 it uses VLDR instruction which can not be used unaligned.

You are receiving this mail because:
You are on the CC list for the bug.
llvm-bugs mailing list

Reply via email to