I face one issue in my customized malloc().
My software
First of all, let me explain my software:
My software is huge and is composed of multiple static libraries. As one of static libraries has the customized malloc(), whose main function is a sort of proprietary memory tracer on the top of calling mall() from glibc. Currently the customized malloc() is available only some applications but not all applications in my system. My aim is to link the customized malloc() to every application running on my system. In my previous post, I asked why there was 'undefined sysmbol' shown during the run time even though I used the flag of LTLD_LAZY. It turned out the my code was not correct. So I fixed my code. And the issue is resolved. But still I have an issue. Let me explain my code first.
cat common.c
#include <stdio.h> #include <stdlib.h> void *gp_malloc(size_t size) { void *ptr; ptr = malloc(size); fprintf(stderr, "%s: size(%d) = %p\n", __func__, (int)size, ptr); return ptr; } void gp_free(void *ptr) { free(ptr); fprintf(stderr, "%s free=%p\n", __func__, ptr); }cat my_malloc.c
#define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <dlfcn.h> #include <malloc.h> static void *handle = NULL; static void *(*gp_malloc)(size_t size) = NULL; static void (*gp_free)(void *ptr) = NULL; void *my_malloc(size_t size) { char *error; void *ptr; /* dynamically load the shared lib that contains gp_malloc() */ if (gp_malloc == NULL) { handle = dlopen("./libcommon.so", RTLD_LAZY| RTLD_GLOBAL); if (!handle) { fprintf(stderr, "%s\n", dlerror()); return NULL; } /* get a pointer to the gp_malloc() function we just loaded */ gp_malloc = dlsym(handle, "gp_malloc"); if ((error = dlerror()) != NULL) { fprintf(stderr, "%s\n", error); return NULL; } } /* Now we can call gp_malloc() just like any other function */ fprintf(stderr, "%s size=%ld\n", __func__, size); ptr = gp_malloc(size); return ptr; } void my_free(void *ptr) { char *error; //void *ptr; /* dynamically load the shared lib that contains gp_malloc() */ if (gp_free == NULL) { gp_free = dlsym(handle, "gp_malloc"); if ((error = dlerror()) != NULL) { fprintf(stderr, "%s\n", error); return; } } /* Now we can call gp_malloc() just like any other function */ fprintf(stderr, "%s: start %p\n", __func__, ptr); gp_free(ptr); } void bbb(void) { extern void aaa(void); aaa(); }cat hello_exits.c
#include <stdio.h> #include <stdlib.h> extern void *gp_malloc(int); extern void gp_free(void *); int main(void) { char *ptr; ptr = gp_malloc(10); gp_free(ptr); exit(0); }cat hello_new.c
#include <stdio.h> #include <stdlib.h> #include "my_malloc.h" int main(void) { char *ptr; ptr = malloc(10); free(ptr); }cat my_static.c
#include <stdio.h> void aaa(void) { printf("I am in aaa\n"); }cat my_malloc.h
#ifdef COMPILE_TIME_MY_MALLOC #define malloc(size) my_malloc(size) #define free(ptr) my_free(ptr) void *my_malloc(size_t size); void my_free(void *ptr); #endifcat Makefile_new
LOCAL_PATH=${shell pwd} all: gcc -c -Wall -fPIC -o my_malloc.o -c my_malloc.c gcc -O2 -Wall -shared -fPIC -o libmy_malloc.so my_malloc.o # This shared library is built with Makefile_exist # gcc -O2 -Wall -shared -fPIC -o libcommon.so common.c gcc -L${LOCAL_PATH} -g -Wall -D COMPILE_TIME_MY_MALLOC -o hello_new hello_new.c -lmy_malloc -ldlcat Makefile_exist
LOCAL_PATH=${shell pwd} all: gcc -c -Wall -o my_static.o -c my_static.c ar -rc libmy_static.a my_static.o gcc -O2 -Wall -shared -fPIC -o libcommon.so common.c gcc -L${LOCAL_PATH} -g -Wall -o hello_exist hello_exist.c -lcommon -lmy_static -ldl
In order to build the image, run Makefile_exist, Makefile_new sequentially so that hello_new is created. (Makefile_exist/Makefile_new is a simplified version of my makefiles. The former is one that I use. The later is one that I am going to apply to other applications)
When I run hello_new, I got gp_malloc twice. I understand the 1st one. What causes the 2nd gp_malloc()?
./hello_new my_malloc size=10 gp_malloc: size(10) = 0x7fffe0d5b900 my_free: start 0x7fffe0d5b900 gp_malloc: size(-522864384) = (nil) //what causes this to be called.
Any suggestion will be highly appreciated.