1

I recoded malloc free and realloc in c using mmap and munmap. I compiled them as a shared library .so file.

Here is a simple test:

#include <stdlib.h>

int main() {
    int i;
    char *addr;
    i = 0;
    while (i < 1024)
    {
        addr = (char*)malloc(1024);
        addr[0] = 42;
        i++;
    }
    return (0);
}

Here is a run.sh that must replace the stdlib by my shared library:

#/bin/sh
export DYLD_LIBRARY_PATH=.
export DYLD_INSERT_LIBRARIES="libft_malloc.so"
export DYLD_FORCE_FLAT_NAMESPACE=1
$@

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:

-> gcc test1.c libft_malloc.so
-> ./a.out
-> no error

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:

-> gcc test1.c
-> ./run.sh ./a.out
-> ./run.sh: line 5: 73502 Segmentation fault: 11  $@

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.

Here is my repository: https://github.com/Shirakawa42/malloc.git

I tried debugged by placing write() everywhere but the segfault don't seems to be in the malloc, so i'm lost.

edit: It also segfault if we run a test without any malloc, but just by loading my library:

#include <stdlib.h>

int main() {
    int i;
    i = 0;
    while (i < 1024)
    {
        i++;
    }
    return (0);
}

-> gcc test1.c
-> ./run.sh ./a.out
-> ./run.sh: line 5: 74764 Segmentation fault: 11  $@

edit 2: Compiling with flag fsanitize=address repair the segfault, but it's absolutely not optimal

edit 3: Setting the 2 first export manually in shell tell me:

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'

and after setting the third export all my actions make me segfault, like ls and vim, cd made me abort

  • 2
    "I tried debugged by placing write() everywhere" Tried running it in gdb? – tkausl May 15 '18 at 14:44
  • i have not tried gdb i am actually trying but it's my first time – Shirakawa42 May 15 '18 at 15:11
  • looks i can't use gdb has ./run.sh ./test is not recognized as an executable – Shirakawa42 May 15 '18 at 15:24
  • 1
    just run ./test directly - set your environment variables in your shell so you don't need to use the run.sh wrapper. – Doug May 15 '18 at 15:57
  • What is the environment? MacOS, Xcode? Have a look at this : https://stackoverflow.com/questions/26024100/dyld-library-not-loaded-rpath-libswiftcore-dylib – vkx May 17 '18 at 14:44

2 Answers2

1
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'

This error append when there are a segfault in your malloc or free, repairing free made it work.

0

You can debug it in gdb. First build your code with debug options:

gcc -g -O0 

Above options should be used for both your lib and test program. Then you can try running you program in gdb:

gdb a.out
(gdb) r <arguments to a.out>
(gdb) bt     <-- when it crashes

Linux loads you program in the memory and calls the entry point main. Startup code that compiler adds for the platform may have calls to malloc. Hence, the crash without malloc in your test code.

Mohammad Azim
  • 2,604
  • 20
  • 21