<html>
<head>
<base href="https://bugs.llvm.org/">
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW - Hardfault when dereferencing unaligned float"
href="https://bugs.llvm.org/show_bug.cgi?id=34143">34143</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Hardfault when dereferencing unaligned float
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>4.0
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>Windows NT
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>enhancement
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>LLVM Codegen
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>dan@walms.co.uk
</td>
</tr>
<tr>
<th>CC</th>
<td>llvm-bugs@lists.llvm.org
</td>
</tr></table>
<p>
<div>
<pre>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
-mthumb
When code is compiled with -O0, it works.
with -01 it uses VLDR instruction which can not be used unaligned.</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>