Unable do debug a .so library that makes my shell segfault [closed]

Unable do debug a .so library that makes my shell segfault [closed]



I am trying to make malloc, free and realloc functions in C (using mmap).



I am using following commands line to include them in my shell (I am using sh) :


export DYLD_LIBRARY_PATH=.
export DYLD_FORCE_FLAT_NAMESPACE=1
export DYLD_INSERT_LIBRARIES="./malloc.so:./free.so:./realloc.so"



Here is some of my malloc code :


#include "../incs/malloc.h"

void *malloc(size_t size)

write(2, "nMALLOC", 7);
t_block *res;

write(2, "0", 1);

res = NULL;

if (!(glob))

write(2, "1", 1);
// First call of malloc, need to init glob variable
glob = init_glob();

write(2, "2", 1);
res = get_block(size);
write(2, "3", 1);

if (!res)
return (NULL);

write(2, "4", 1);

return (res->memory);



I have a debug write too at the start of my init_glob() function.



When I execute the previous command lines in my shell, and run a random command (ls, for example), here is what I get :


MALLOC01
MALLOC01
MALLOC01
MALLOC01
MALLOC01
MALLOC01
MALLOC01
MALLOC01
MALLOC01
MALLOC01
MALLOC01
MALLOC01
MALLOC01
MALLOC01
MALLOC01Segmentation fault: 11



I don't really understand why it is not working, and how do debug this.



It should just write "MALLOC01" once, ten go to my init_glob function.
Why is this looping like this ?
How can i see WHERE it is crashing in ls command ?



Thanks in advance.



===== EDIT =====



Here is my init_glob() function :


#include "../incs/malloc.h"

/*
** This function returns a t_glob.
** It shall init the global variable of type t_glob, on the first time
** malloc is called in a process.
*/
t_glob *init_glob(void)

write(2, "a", 1);
t_glob *res;

res = NULL;

write(2, "b", 1);
res = (t_glob *)allocate_memory(sizeof(t_glob));

write(2, "c", 1);

res->tiny = NULL;
res->small = NULL;
res->large = NULL;
write(2, "d", 1);

return (res);



And my allocate_memory() function (but it seems that the program is not even going there) :


void *allocate_memory(size_t size)
MAP_PRIVATE, -1, 0);

return (res);



My t_glob struct is prototyped like this :


typedef struct s_glob

t_page *tiny;
t_page *small;
t_page *large;
// size_t sizeof_block; // Avoid repeat of sizeof() call
// size_t sizeof_page;
// size_t getpagesize_result;
t_glob;



This question appears to be off-topic. The users who voted to close gave this specific reason:






Show us your init_glob function.

– kiran Biradar
Sep 15 '18 at 11:36


init_glob






It looks like malloc() is called recursively from init_glob(). Are you sure you're using write() in init_glob() and not printf() or other stdio function? Or is write() itself some kind of wrapper instead of the write(2) system call?

– mosvy
Sep 15 '18 at 17:29







Yes, I am sure that I am using write, as printf have some strange behaviors.

– Elynad
Sep 16 '18 at 9:57




1 Answer
1



I don't really understand why it is not working, and how do debug this.



The usual way to debug this is to let the program dump core (ulimit -c unlimited), and then look with the debugger where the infinite recursion happens.


core


ulimit -c unlimited



If I were to guess, I would guess that when the dynamic loader tries to resolve the call from malloc to init_glob, this dynamic symbol resolution itself needs dynamic memory and calls malloc.


malloc


init_glob


malloc



You would get a better answer (less guessing) if you provide MCVE, including build instructions.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

PHP code is not being executed, instead code shows on the page

Administrative divisions of China