-
Notifications
You must be signed in to change notification settings - Fork 0
/
mhshim.c
234 lines (234 loc) · 7.7 KB
/
mhshim.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*Code could've been shorter but didn't shorten to make it less rigid.*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include <unistd.h>
#include <signal.h>
#include <stdint.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include "mhshim.h"
#define N_OFF_ADD sizeof(void*)+sizeof(size_t)+sizeof(struct __ml_internal_store__ptrs)
static uint64_t memory_allocated=0;
static uint64_t voffsets=0; /*sizeof(vp)+sizeof(i)+sizeof(np)+...*/
static short init=0;
static struct __ml_internal_store__ptrs **mlh=NULL;
static void general_handler(int sig){
printf("Memory still not deallocated: %llu\nMemory list head pointer: %p\nVirtual offset of tail: %llu\nExit?(Y/N)\n",memory_allocated,*mlh,voffsets);
if(getchar()=='Y'){
exit(EXIT_SUCCESS);
}
}
static void general_handlerv(void){
printf("Memory still not deallocated: %llu\nMemory list head pointer: %p\nVirtual offset of tail: %llu\nExit?(Y/N)\n",memory_allocated,*mlh,voffsets);
if(getchar()=='Y'){
exit(EXIT_SUCCESS);
}
}
void exit_handlerc(){
#if defined(ATEXIT)
atexit(general_handlerv);
#else
signal(SIGINT,general_handler);
signal(SIGSTOP,general_handler);
signal(SIGHUP,general_handler);
signal(SIGQUIT,general_handler);
#endif
}
void *malloc(size_t size){
if(init==0){
exit_handlerc();
init=1;
}
void* (*rmalloc)(size_t)=dlsym(RTLD_NEXT,"malloc");
void *ret=rmalloc(size);
insert_m(mlh,ret,size);
return ret;
}
void *calloc(size_t nmemb, size_t size){
if(init==0){
exit_handlerc();
init=1;
}
void* (*rcalloc)(size_t,size_t)=dlsym(RTLD_NEXT,"calloc");
void *ret=rcalloc(nmemb,size);
insert_m(mlh,ret,nmemb*size);
return ret;
}
void free(void *ptr){
if(init==0){
exit_handlerc();
init=1;
}
delete_vp(mlh,ptr);
}
void *realloc(void *ptr, size_t size){
if(init==0){
exit_handlerc();
init=1;
}
void* (*rrealloc)(void*,size_t)=dlsym(RTLD_NEXT,"realloc");
struct __ml_internal_store__ptrs *n;
if((n=find_m(*mlh,ptr))==NULL){
if(PERR_F){
printf("Pointer(%p) to use realloc on not found in memory list.\n",ptr);
}
if(LOG_AC){
char emsg[MAX_ERR_W_LF+1];
sprintf((char*)emsg,"REALLOCATION OF %p FAILED. NOT FOUND IN MEMORY LIST.\n",ptr);
if(!write_log(LOGF_N,(char*)emsg)){
if(PERR_F){
printf("Can't write to log file(%s).",LOGF_N);
}
if(EXIT_F)exit(EXIT_FAILURE);
}
}
if(EXIT_F)exit(EXIT_FAILURE);
return NULL;
}
n->ptr=rrealloc(n->ptr,size);
memory_allocated+=size-n->size;
n->size=size;
char cmsg[MAX_ERR_W_LF+1];
sprintf((char*)cmsg,"REALLOCATE %p. NEW SIZE: %lu.\n",ptr,size);
if(!write_log(LOGF_N,(char*)cmsg)){
if(PERR_F)printf("Can't write to log file(%s).",LOGF_N);
if(EXIT_F)exit(EXIT_FAILURE);
}
return n->ptr;
}
static struct __ml_internal_store__ptrs *find_m(struct __ml_internal_store__ptrs *l, void *ptr){
struct __ml_internal_store__ptrs *selected=l;
while(l->ptr!=ptr||l->next==NULL)selected=selected->next;
if(l->ptr==ptr)return l;
return NULL;
}
static void insert_m(struct __ml_internal_store__ptrs **head, void *vp, size_t i){
void* (*get_m)(size_t)=dlsym(RTLD_NEXT,"malloc");
if(*head==NULL){
*head=(struct __ml_internal_store__ptrs*)get_m(sizeof(struct __ml_internal_store__ptrs));
if(*head==NULL){
if(PERR_F){
printf("Internal allocation not permitted. Virtual offset with respect to entire list and its fields: 0\n"); /*Hardcoded virtual offset because memory for head of list is being allocated.*/
return;
}
if(EXIT_F)exit(EXIT_FAILURE);
}
(**head).ptr=vp;
(**head).size=i;
(**head).next=NULL;
voffsets+=(uint64_t)N_OFF_ADD; /*Not N_OFF_ADD-sizeof(vp) because the void pointer will have taken space after creation of next node.*/
memory_allocated+=(uint64_t)i;
if(LOG_AC){
char nbs[MAX_ERR_W_LF+1];
sprintf((char*)nbs,"ALLOCATE %luB. VIRTUAL OFFSET: 0\n",i); /*Hardcoded because dealing with head*/
if(!write_log((char*)LOGF_N,(char*)nbs)){
if(PERR_F)printf("Can't write to log file(%s).",LOGF_N);
if(EXIT_F)exit(EXIT_FAILURE);
}
}
return;
}
struct __ml_internal_store__ptrs *selected=*head;
while(selected->next!=NULL)selected=selected->next;
selected->next=(struct __ml_internal_store__ptrs*)get_m(sizeof(struct __ml_internal_store__ptrs));
if(selected->next==NULL){
if(PERR_F){
printf("Internal allocation not permitted. Virtual offset with respect to entire list and its fields: %llu\n",voffsets);
return;
}
if(EXIT_F){
exit(EXIT_FAILURE);
}
}
selected->next->ptr=vp;
selected->next->size=i;
selected->next->next=NULL;
voffsets+=(uint64_t)N_OFF_ADD;
memory_allocated+=(uint64_t)i;
if(LOG_AC){
char nbs[MAX_ERR_W_LF+1];
sprintf((char*)nbs,"ALLOCATE %luB. VIRTUAL OFFSET: %llu\n",i,voffsets-N_OFF_ADD);
if(!write_log((char*)LOGF_N,(char*)nbs)){
if(PERR_F){
printf("Can't write to log file(%s).",LOGF_N);
}
}
}
}
static void delete_vp(struct __ml_internal_store__ptrs **l, void *v){
void (*rfree)(void*)=dlsym(RTLD_NEXT,"free");
if(*l==NULL)return;
if((*l)->ptr==v){
struct __ml_internal_store__ptrs *save=*l;
size_t size=save->size;
*l=(*l)->next;
rfree(save->ptr);
rfree((void*)save);
voffsets-=(uint64_t)N_OFF_ADD;
memory_allocated-=(uint64_t)size;
char wm[MAX_ERR_W_LF+1];
sprintf((char*)wm,"DEALLOCATE %luB. VIRTUAL OFFSET: %llu\n",size,voffsets);
if(!write_log((char*)LOGF_N,(char*)wm)){
if(PERR_F){
printf("Can't write to log file(%s).\n",LOGF_N);
}
if(EXIT_F){
exit(EXIT_FAILURE);
}
}
return;
}
struct __ml_internal_store__ptrs *selected=*l;
while(selected->next->ptr!=v||selected->next!=NULL)selected=selected->next;
if(selected->next==NULL){
if(PERR_F){
printf("%p not found in memory list(%p)\n",v,l);
}
if(LOG_AC){
char erm[MAX_ERR_W_LF+1];
sprintf((char*)erm,"LIST ACCESS ERROR. %p NOT FOUND.\n",v);
if(!write_log((char*)LOGF_N,(char*)erm)){
if(PERR_F){
printf("Can't write to log file(%s).\n",LOGF_N);
}
}
}
if(EXIT_F){
exit(EXIT_FAILURE);
}
return;
}
struct __ml_internal_store__ptrs *s=selected->next;
selected->next=s->next;
size_t size=s->size;
rfree(s->ptr);
rfree(s);
voffsets-=(uint64_t)N_OFF_ADD;
memory_allocated-=(uint64_t)size;
char wm[MAX_ERR_W_LF+1];
sprintf((char*)wm,"DEALLOCATE %luB. VIRTUAL OFFSET: %llu\n",size,voffsets);
if(!write_log((char*)LOGF_N,(char*)wm)){
if(PERR_F){
printf("Can't write to log file(%s).\n",LOGF_N);
}
if(EXIT_F){
exit(EXIT_FAILURE);
}
}
}
static int write_log(char *lname,char *message){
int f=open(lname,O_APPEND | O_CREAT);
if(f==-1){
return -1;
}
if(write(f,message,(size_t)MAX_ERR_W_LF)<0){
close(f);
return -1;
}
close(f);
return 1;
}