title: Can't find my segmentation fault in my malloc shared library
body: <p>I recoded malloc free and realloc in c using mmap and munmap. I compiled them as a shared library .so file.</p>
  
  <p>Here is a simple test:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  
  int main() {
      int i;
      char *addr;
      i = 0;
      while (i &lt; 1024)
      {
          addr = (char*)malloc(1024);
          addr[0] = 42;
          i++;
      }
      return (0);
  }
  </code></pre>
  
  <p>Here is a run.sh that must replace the stdlib by my shared library:</p>
  
  <pre><code>#/bin/sh
  export DYLD_LIBRARY_PATH=.
  export DYLD_INSERT_LIBRARIES="libft_malloc.so"
  export DYLD_FORCE_FLAT_NAMESPACE=1
  $@
  </code></pre>
  
  <p>The problem is that when i am compiling directly the test file with my shared library and replacing the header in it, it is working well:</p>
  
  <pre><code>-&gt; gcc test1.c libft_malloc.so
  -&gt; ./a.out
  -&gt; no error
  </code></pre>
  
  <p>But when i am running it with the run.sh that should just replace the official malloc library by my libft_malloc.so file, i am getting a segfault:</p>
  
  <pre><code>-&gt; gcc test1.c
  -&gt; ./run.sh ./a.out
  -&gt; ./run.sh: line 5: 73502 Segmentation fault: 11  $@
  </code></pre>
  
  <p>I know the error is in my code and not in the run.sh or in the test.c because they are the officials files i must use to test my library in my school and those files are working well on other malloc repositories, but i can't find what can be the problem.</p>
  
  <p>Here is my repository: <a href="https://github.com/Shirakawa42/malloc.git" rel="nofollow noreferrer">https://github.com/Shirakawa42/malloc.git</a></p>
  
  <p>I tried debugged by placing write() everywhere but the segfault don't seems to be in the malloc, so i'm lost.</p>
  
  <p>edit:
  It also segfault if we run a test without any malloc, but just by loading my library:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  
  int main() {
      int i;
      i = 0;
      while (i &lt; 1024)
      {
          i++;
      }
      return (0);
  }
  
  -&gt; gcc test1.c
  -&gt; ./run.sh ./a.out
  -&gt; ./run.sh: line 5: 74764 Segmentation fault: 11  $@
  </code></pre>
  
  <p>edit 2:
  Compiling with flag fsanitize=address repair the segfault, but it's absolutely not optimal</p>
  
  <p>edit 3:
  Setting the 2 first export manually in shell tell me:</p>
  
  <pre><code>dyld: warning: could not load inserted library 'libft_malloc.so' into library validated process because no suitable image found.  Did find:
      libft_malloc.so: code signing blocked mmap() of 'libft_malloc.so'
  </code></pre>
  
  <p>and after setting the third export all my actions make me segfault, like ls and vim, cd made me abort</p>