<p>Jérémy Compostella has uploaded this change for <strong>review</strong>.</p><p><a href="https://review.coreboot.org/20535">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">libpayload: Support unaligned pointer for memcpy, memmove and memcmp<br><br>The memcpy(), memmove() and memcmp() functions use word by word<br>operations regardless of the pointer alignment.  Depending on the<br>platform, this could lead to a crash.<br><br>This patch makes the memcpy(), memmove() or memcmp() operate byte per<br>byte if they are supplied with unaligned pointers.<br><br>Change-Id: I0b668739b7b58d47266f10f2dff2dc9cbf38577e<br>Signed-off-by: Jeremy Compostella <jeremy.compostella@intel.com><br>---<br>M payloads/libpayload/libc/memory.c<br>1 file changed, 20 insertions(+), 2 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://review.coreboot.org:29418/coreboot refs/changes/35/20535/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">diff --git a/payloads/libpayload/libc/memory.c b/payloads/libpayload/libc/memory.c<br>index 1adfb32..78418ba 100644<br>--- a/payloads/libpayload/libc/memory.c<br>+++ b/payloads/libpayload/libc/memory.c<br>@@ -66,13 +66,19 @@<br>        size_t i;<br>     void *ret = dst;<br> <br>+  if (!IS_ALIGNED((uintptr_t)dst, sizeof(unsigned long)) ||<br>+        !IS_ALIGNED((uintptr_t)src, sizeof(unsigned long)))<br>+              goto copy_bytes;<br>+<br>   for(i = 0; i < n / sizeof(unsigned long); i++)<br>             ((unsigned long *)dst)[i] = ((unsigned long *)src)[i];<br> <br>     src += i * sizeof(unsigned long);<br>     dst += i * sizeof(unsigned long);<br>+    n -= i * sizeof(unsigned long);<br> <br>-   for(i = 0; i < n % sizeof(unsigned long); i++)<br>+copy_bytes:<br>+      for(i = 0; i < n ; i++)<br>            ((u8 *)dst)[i] = ((u8 *)src)[i];<br> <br>   return ret;<br>@@ -88,6 +94,13 @@<br> <br>    if (src > dst)<br>             return memcpy(dst, src, n);<br>+<br>+       if (!IS_ALIGNED((uintptr_t)dst, sizeof(unsigned long)) ||<br>+        !IS_ALIGNED((uintptr_t)src, sizeof(unsigned long))) {<br>+            for (i = n - 1; i >= 0; i--)<br>+                      ((u8 *)dst)[i] = ((u8 *)src)[i];<br>+             return dst;<br>+  }<br> <br>  offs = n - (n % sizeof(unsigned long));<br> <br>@@ -116,12 +129,17 @@<br> <br> static int default_memcmp(const void *s1, const void *s2, size_t n)<br> {<br>-       size_t i;<br>+    size_t i = 0;<br>+<br>+     if (!IS_ALIGNED((uintptr_t)s1, sizeof(unsigned long)) ||<br>+         !IS_ALIGNED((uintptr_t)s2, sizeof(unsigned long)))<br>+               goto compare_bytes;<br> <br>        for (i = 0; i < n / sizeof(unsigned long); i++)<br>            if (((unsigned long *)s1)[i] != ((unsigned long *)s2)[i])<br>                     break;  /* fall through to find differing byte */<br> <br>+compare_bytes:<br>         for (i *= sizeof(unsigned long); i < n; i++)<br>               if (((u8 *)s1)[i] != ((u8 *)s2)[i])<br>                   return ((u8 *)s1)[i] - ((u8 *)s2)[i];<br></pre><p>To view, visit <a href="https://review.coreboot.org/20535">change 20535</a>. To unsubscribe, visit <a href="https://review.coreboot.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://review.coreboot.org/20535"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: coreboot </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>
<div style="display:none"> Gerrit-Change-Id: I0b668739b7b58d47266f10f2dff2dc9cbf38577e </div>
<div style="display:none"> Gerrit-Change-Number: 20535 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Jérémy Compostella <jeremy.compostella@gmail.com> </div>