<div dir="ltr"><div><div>Does the C and C++ standards not state that loading a value from an unaligned addres (e.g. float from an address not aligned to 4) is undefined behaviour?<br><div>I'm not saying SO is the definitive reference for this, but it
would seem like it says so, with a reference to the spec:
<br><a href="https://stackoverflow.com/questions/28893303/is-a-misaligned-load-due-to-a-cast-undefined-behavior">https://stackoverflow.com/questions/28893303/is-a-misaligned-load-due-to-a-cast-undefined-behavior</a><br></div><br></div>Whilst it may well be that this "works" on some archiectures (particularly x86), I do believe it's perfectly valid to produce code that assumes aligned data.<br><br></div><div>Or am I missing something on the subject?<br></div><br><div>--<br></div>Mats<br></div><div class="gmail_extra"><br><div class="gmail_quote">On 10 August 2017 at 14:50, Dan Walmsley via cfe-dev <span dir="ltr"><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">
<div id="m_-3693839330198491667divtagdefaultwrapper" style="font-size:12pt;color:#000000;font-family:Calibri,Helvetica,sans-serif" dir="ltr">
<p>If have filed a bug here:</p>
<p><br>
</p>
<p><a class="m_-3693839330198491667OWAAutoLink" id="m_-3693839330198491667LPlnk479257" href="https://bugs.llvm.org/show_bug.cgi?id=34143" target="_blank">https://bugs.llvm.org/show_<wbr>bug.cgi?id=34143</a><br>
</p>
</div>
<hr style="display:inline-block;width:98%">
<div id="m_-3693839330198491667divRplyFwdMsg" dir="ltr"><font style="font-size:11pt" face="Calibri, sans-serif" color="#000000"><b>From:</b> cfe-dev <<a href="mailto:cfe-dev-bounces@lists.llvm.org" target="_blank">cfe-dev-bounces@lists.llvm.<wbr>org</a>> on behalf of Dan Walmsley via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>><br>
<b>Sent:</b> 10 August 2017 13:41:39<br>
<b>To:</b> <a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>; Hans Wennborg<br>
<b>Subject:</b> [cfe-dev] Cortex-M4F Assembly Code Generation Incorrect for float casting, causing HardFaults</font>
<div> </div>
</div><div><div class="h5">
<div>
<div id="m_-3693839330198491667divtagdefaultwrapper" style="font-size:12pt;color:#000000;font-family:Calibri,Helvetica,sans-serif" dir="ltr">
<div id="m_-3693839330198491667divtagdefaultwrapper" style="color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,"EmojiFont","Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:12pt" dir="ltr">
<p>Basically the following code can repro the issue:</p>
<p><br>
</p>
<p>Compile flags are:</p>
<p><span>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
</span></p>
<p><br>
</p>
<p>code to reproduce is:</p>
<p><br>
</p>
<p>```<br>
</p>
<p>template <typename T> inline T Read (uint8_t* data)<br>
{<br>
auto valuePtr = reinterpret_cast<T*> (data);</p>
<p> auto value = *valuePtr;</p>
<p> return value;<br>
}</p>
<p>static float test = 180.0f;</p>
<p>int main (void)<br>
{<br>
uint8_t* data = new uint8_t[8];</p>
<p> memcpy (data, &test, sizeof (float));</p>
<p> auto value = Read<float> (data);</p>
<p><br>
</p>
<p> memcpy (data + 1, &test, sizeof (float));</p>
<p> value = Read<float> (data + 1); // hard fault here!</p>
<p> return value;<br>
}</p>
<p>```</p>
<p><br>
</p>
<p>My diagnosis is that when it dereferences the float pointer it uses the VLDR instruction, <a class="m_-3693839330198491667OWAAutoLink" id="m_-3693839330198491667LPlnk126221" href="https://developer.arm.com/docs/100069/0607/advanced-simd-instructions-32-bit/vldr" target="_blank">https://<wbr>developer.arm.com/docs/100069/<wbr>0607/advanced-simd-<wbr>instructions-32-bit/vldr</a></p>
<p><br>
</p>
<p>This instruction can only be used on word aligned boundaries.</p>
<p><br>
</p>
<p>So It works for first call to my Read method, but the second call which is not aligned, it crashes.</p>
<p><br>
</p>
<p>Not sure who is the right person to fix this.</p>
<p></p>
<p><br>
</p>
<p>I will reply with a bug, once I manage to login to the bug database.</p>
</div>
<div style="color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,"EmojiFont","Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:12pt" dir="ltr">
Thanks</div>
<div style="color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,"EmojiFont","Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:12pt" dir="ltr">
<br>
</div>
<div style="color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,"EmojiFont","Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:12pt" dir="ltr">
Dan</div>
<p style="color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,"EmojiFont","Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:12pt" dir="ltr">
<br>
</p>
<div style="color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,"EmojiFont","Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:12pt" dir="ltr">
</div>
</div>
</div>
</div></div></div>
<br>______________________________<wbr>_________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br></div>