#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
#include <execinfo.h>
#include <signal.h>
#define STORESIZE sizeof(size_t)
void show_stack()
{
int i;
void *buffer[1024];
int n = backtrace(buffer, 1024);
char **symbols = backtrace_symbols(buffer, n);
for (i = 0; i < n; i++) {
printf("%s\n", symbols[i]);
}
}
void signal_handler(int sig) {
if(SIGSEGV==sig)
{
show_stack();
exit(-1);
}
else{
printf("signal with %d\n",sig);
}
}
void my_free(void* addr){
printf("free addr:%p size:%d append_size:%d\n",addr,*(size_t*)((size_t)addr-STORESIZE),STORESIZE);
memset(addr,0xFF,*(size_t*)((size_t)addr-STORESIZE));
free((void*)((size_t)addr-STORESIZE));
}
void* my_malloc(size_t len){
void* addr=malloc(len+STORESIZE);
printf("malloc addr:%p size:%d app_size:%d\n",(void*)((size_t)addr+STORESIZE),len,STORESIZE);
*(size_t*)addr=len;
return (void*)((size_t)addr+STORESIZE);
}
void main()
{
signal(SIGSEGV, signal_handler);
do();
}
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
#include <execinfo.h>
#include <signal.h>
#define STORESIZE sizeof(size_t)
void my_free(void* addr){
printf("free addr:%p size:%d append_size:%d\n",addr,*(size_t*)((size_t)addr-STORESIZE),2*STORESIZE);
memset(addr,0xFF,*(size_t*)((size_t)addr-STORESIZE));
if(*(size_t*)((size_t)addr-STORESIZE)!=((size_t)addr+*(size_t*)((size_t)addr-STORESIZE)))
{
printf("heap over_flow!\n");
show_stack();
exit(-1);
}
free((void*)((size_t)addr-STORESIZE));
}
void* my_malloc(size_t len){
void* addr=malloc(len+2*STORESIZE);
printf("malloc addr:%p size:%d app_size:%d\n",(void*)((size_t)addr+STORESIZE),len,2*STORESIZE);
*(size_t*)addr=len;
*(size_t*)((size_t)addr+len+STORESIZE)=len;
return (void*)((size_t)addr+STORESIZE);
}
溢出的检测是在malloc的buffer前后分别添加一个buffer的size 类似[len][buffer][len],在free的时候检测头尾的len是否相等。如不相等则溢出。