title: __memcpy_sse2_unaligned - what does this mean in detail?
body: <p>While working on my compiler I got this error:</p>
  
  <pre><code>Program received signal SIGSEGV, Segmentation fault.
  __memcpy_sse2_unaligned () at ../sysdeps/x86_64/multiarch/memcpy-sse2-unaligned.S:33
  </code></pre>
  
  <p>How do I get details of what went wrong here? I know from the backtrace it's a <code>memcpy</code> line that causes it, but how do I see how the memory is aligned? And how do I know how it <em>should</em> be aligned?</p>
  
  <p>The project is a compiler with an LLVM back-end using the Zend/PHP runtime with the OCaml garbage collector, so there's is a lot of things that can go wrong.</p>
  
  <p>I suspect this line being part of the problem:</p>
  
  <pre><code>zend_string *str = (zend_string *)caml_alloc(ZEND_MM_ALIGNED_SIZE(_STR_HEADER_SIZE + len + 1), 0);
  </code></pre>
  
  <p>where <code>caml_alloc</code> were <code>pemalloc</code> in the Zend source-code.</p>
  
  <p>The segfault happens when doing 10'000 string concatenations. This is the output from valgrind:</p>
  
  <pre><code>==7501== Invalid read of size 8
  ==7501==    at 0x4C2F790: memcpy@@GLIBC_2.14 (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==7501==    by 0x4D7E58: subsetphp_concat_function (bindings.c:160)
  ==7501==    by 0x4D7F52: foo (llvm_test.s:21)
  ==7501==    by 0x4D7FA9: main (llvm_test.s:60)
  ==7501==  Address 0x61db938 is 2,660,600 bytes inside a block of size 3,936,288 free'd
  ==7501==    at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==7501==    by 0x4C2627: do_compaction (in /home/olle/kod/subsetphp/test)
  ==7501==    by 0x4C2735: caml_compact_heap (in /home/olle/kod/subsetphp/test)
  ==7501==    by 0x4D08DF: caml_major_collection_slice (in /home/olle/kod/subsetphp/test)
  ==7501==    by 0x4D2DCF: caml_minor_collection (in /home/olle/kod/subsetphp/test)
  ==7501==    by 0x4D2FBC: caml_check_urgent_gc (in /home/olle/kod/subsetphp/test)
  ==7501==    by 0x4D7C45: subsetphp_string_alloc (bindings.c:90)
  ==7501==    by 0x4D7CEE: subsetphp_string_init (bindings.c:122)
  ==7501==    by 0x4D7DEA: subsetphp_concat_function (bindings.c:149)
  ==7501==    by 0x4D7F52: foo (llvm_test.s:21)
  ==7501==    by 0x4D7FA9: main (llvm_test.s:60)
  </code></pre>
  
  <p>Any tips appreciated.</p>
  
  <p><strong>Edit</strong>: </p>
  
  <pre><code>extern value subsetphp_concat_function(value v1, value v2) 
  {
  
    CAMLparam2(v1, v2);
  
    zend_string *str1 = Zend_string_val(v1);
    zend_string *str2 = Zend_string_val(v2);
    size_t str1_len = str1-&gt;len;
    size_t str2_len = str2-&gt;len;
    size_t result_len = str1_len + str2_len;
  
    value result = subsetphp_string_init("", result_len, 1);
    zend_string *zend_result = Zend_string_val(result);
  
    if (str1_len &gt; SIZE_MAX - str2_len) {
      zend_error_noreturn(E_ERROR, "String size overflow");
    }
  
    memcpy(zend_result-&gt;val, str1-&gt;val, str1_len);  // This is line 160
    memcpy(zend_result-&gt;val + str1_len, str2-&gt;val, str2_len);
    zend_result-&gt;len = result_len;
    zend_result-&gt;val[result_len] = '\0';
  
    CAMLreturn(result);
  }
  </code></pre>
  
  <p><strong>Edit 2</strong>:</p>
  
  <p>Since valgrind gives me this line</p>
  
  <pre><code>Address 0x61db938 is 2,660,600 bytes inside a block of size 3,936,288 free'd
  </code></pre>
  
  <p>I guess I'm trying to copy something that has already been freed, meaning that I don't tell the OCaml GC correctly when something is no longer referenced.</p>