title: STM32 call to memcpy causes hardfault (the call to memcpy itself, not the execution of memcpy)
body: <p><strong>Situation:</strong> I am working with a crypto library called embedded disco, I have a demo working on my PC but when porting it over to the MCU I get a hard fault when executing a library procedure. In the faulting code, the library is trying to simply copy the content of one <code>strobe_s</code> struct into another <code>strobe_s</code>. This is done twice: once for <code>s1</code> and once for <code>s2</code>. For <code>s1</code>, the library simply assigns the dest. struct to the source struct. For <code>s2</code> however, such an assign gave a hard fault. As the Cortex-M ISA requires aligned memory accesses, I reckoned that replacing the assignment with a memcpy should fix the problem. Nevertheless, simply stepping into memcpy using the debugger results in a hard fault! I.e. I have a breakpoint at the line with the memcpy and when stepping inside the fault handler is called! I have used memcpy to fix misaligned memory accesses in other parts of the code just fine...</p>
  <p><strong>MCU:</strong> STM32L552ZET6QU</p>
  <p><strong>Faulting code:</strong></p>
  <p>The code below is my modification of the original library code where the assignment to <code>*s2</code> was replaced by a memcpy. The original code from the <a href="https://github.com/mimoo/disco-c/blob/bbf84d64d7de6956009872caa77bfe7691cd471b/lib/disco_asymmetric.c#L116" rel="nofollow noreferrer">library's github</a> was:</p>
  <pre><code>  // s1 = our current strobe state
    *s1 = ss-&gt;strobe;
    if (!half_duplex) {
      // s2 = s1
      *s2 = ss-&gt;strobe;
    }
  </code></pre>
  <p>My modified version:</p>
  <pre><code>  // s1 = our current strobe state
    *s1 = ss-&gt;strobe;
    if (!half_duplex) {
      // s2 = s1
      // WARNING: The below code will give a HARD FAULT ON THE STM32L552ZE!
      // *s2 = ss-&gt;strobe;
      // Fix I tried: Use memcpy instead!
      memcpy((void*) s2, (void*)(&amp;(ss -&gt; strobe)), sizeof(strobe_s));
    }
  </code></pre>
  <p><strong>Parameter values of memcpy:</strong></p>
  <p>Right before the execution of memcpy, the debugger shows me the following values for the variables in question:</p>
  <pre><code>Expr.   Type                Value
  ----------------------------------------------------
  s1      strobe_s *          0x800c374   
  s2      strobe_s *          0x800a497 &lt;_fflush_r+66&gt;    
  ss      symmetricState *    0x2002f988  
  &amp;s1     strobe_s **         0x2002f690  
  &amp;s2     strobe_s **         0x2002f68c  
  &amp;ss     symmetricState **   0x2002f694
  </code></pre>
  <p><strong>Typedefs:</strong></p>
  <pre><code>typedef struct symmetricState_ {
    strobe_s strobe;
    bool isKeyed;
  } symmetricState;
  
  /** Keccak's domain: 25 words of size b/25, or b/8 bytes. */
  typedef union {
    kword_t w[25];
    uint8_t b[25 * sizeof(kword_t) / sizeof(uint8_t)];
  } kdomain_s;
  
  /** The main strobe state object. */
  typedef struct strobe_s_ {
    kdomain_s state;
    uint8_t position;
    uint8_t pos_begin;
    uint8_t flags;
    uint8_t initiator;
    uint8_t initialized;  // strobe is initialized if this value is set to 111.
                          // This is because we cannot assume that a boolean would
                          // be set to false initially (C stuff). A uint8_t is a
                          // short value but here we do not care about security
                          // much, rather catching bugs early in a development
                          // environement.
  } strobe_s;
  </code></pre>
  <p><strong>Questions:</strong></p>
  <ol>
  <li>How is it possible that just the <em>call</em> to memcpy without actually executing a single instruction within memcpy gives a hard fault?</li>
  <li>How can I fix this?</li>
  </ol>