forked from jedbarlow/links
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.c
591 lines (538 loc) · 12.3 KB
/
error.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/* error.c
* (c) 2002 Mikulas Patocka
* This file is a part of the Links program, released under GPL.
*/
#include "links.h"
#if DEBUGLEVEL >= 2
#define RED_ZONE 'R'
#endif
#if DEBUGLEVEL >= 3
#define FREE_FILL 0xfe
#define REALLOC_FILL 0xfd
#define ALLOC_FILL 0xfc
#endif
#if DEBUGLEVEL < 0
#define FREE_FILL 0xfe
#endif
#if DEBUGLEVEL < 0
#define NO_IE
#endif
#ifdef RED_ZONE
#define RED_ZONE_INC 1
#else
#define RED_ZONE_INC 0
#endif
volatile char dummy_val;
volatile char * volatile dummy_ptr = &dummy_val;
volatile char * volatile x_ptr;
void *do_not_optimize_here(void *p)
{
/* break ANSI aliasing */
x_ptr = p;
*dummy_ptr = 0;
return p;
}
#ifdef LEAK_DEBUG
my_uintptr_t mem_amount = 0;
my_uintptr_t mem_blocks = 0;
#define ALLOC_MAGIC 0xa110c
#define ALLOC_FREE_MAGIC 0xf4ee
#define ALLOC_REALLOC_MAGIC 0x4ea110c
#ifndef LEAK_DEBUG_LIST
struct alloc_header {
int magic;
size_t size;
};
#else
struct alloc_header {
struct alloc_header *next;
struct alloc_header *prev;
size_t size;
unsigned char *file;
unsigned char *comment;
int line;
int magic;
};
static struct list_head memory_list = { &memory_list, &memory_list };
#endif
#define L_D_S ((sizeof(struct alloc_header) + 15) & ~15)
const unsigned alloc_overhead = L_D_S + RED_ZONE_INC;
#endif
static inline void force_dump(void)
{
#if defined(DOS)
fprintf(stderr, "\n"ANSI_SET_BOLD"Exiting"ANSI_CLEAR_BOLD"\n");
fflush(stdout);
fflush(stderr);
exit(RET_INTERNAL);
#else
int rs;
fprintf(stderr, "\n"ANSI_SET_BOLD"Forcing core dump"ANSI_CLEAR_BOLD"\n");
fflush(stdout);
fflush(stderr);
EINTRLOOP(rs, raise(SIGSEGV));
#endif
}
void check_memory_leaks(void)
{
#if defined(NO_IE)
return;
#else
#ifdef LEAK_DEBUG
if (mem_amount || mem_blocks) {
fatal_tty_exit();
fprintf(stderr, "\n"ANSI_SET_BOLD"Memory leak by %lu bytes (%lu blocks)"ANSI_CLEAR_BOLD"\n", (unsigned long)mem_amount, (unsigned long)mem_blocks);
#ifdef LEAK_DEBUG_LIST
fprintf(stderr, "\nList of blocks: ");
{
int r = 0;
struct alloc_header *ah;
foreach (ah, memory_list) {
fprintf(stderr, "%s%p:%lu @ %s:%d", r ? ", ": "", (unsigned char *)ah + L_D_S, (unsigned long)ah->size, ah->file, ah->line), r = 1;
if (ah->comment) fprintf(stderr, ":\"%s\"", ah->comment);
}
fprintf(stderr, "\n");
}
#endif
force_dump();
}
#endif
#endif
}
static void er(int b, char *m, va_list l)
{
#ifdef HAVE_VPRINTF
vfprintf(stderr, cast_const_char m, l);
#else
fprintf(stderr, "%s", m);
#endif
if (b) fprintf(stderr, ANSI_BELL);
fprintf(stderr, "\n");
fflush(stderr);
sleep(1);
}
void error(char *m, ...)
{
va_list l;
va_start(l, m);
fprintf(stderr, "\n");
er(1, m, l);
va_end(l);
}
void fatal_exit(char *m, ...)
{
va_list l;
fatal_tty_exit();
va_start(l, m);
fprintf(stderr, "\n");
er(1, m, l);
va_end(l);
fflush(stdout);
fflush(stderr);
exit(RET_FATAL);
}
int errline;
unsigned char *errfile;
static unsigned char errbuf[4096];
void int_error(char *m, ...)
{
#ifdef NO_IE
return;
#else
va_list l;
fatal_tty_exit();
va_start(l, m);
sprintf(cast_char errbuf, "\n"ANSI_SET_BOLD"INTERNAL ERROR"ANSI_CLEAR_BOLD" at %s:%d: ", errfile, errline);
strcat(cast_char errbuf, cast_const_char m);
er(1, cast_char errbuf, l);
force_dump();
va_end(l);
#endif
}
void debug_msg(char *m, ...)
{
va_list l;
va_start(l, m);
sprintf(cast_char errbuf, "\nDEBUG MESSAGE at %s:%d: ", errfile, errline);
strcat(cast_char errbuf, cast_const_char m);
er(0, cast_char errbuf, l);
va_end(l);
}
#ifdef LEAK_DEBUG
void *debug_mem_alloc(unsigned char *file, int line, size_t size, int mayfail)
{
void *p;
#ifdef LEAK_DEBUG
struct alloc_header *ah;
#endif
dos_poll_break();
debug_test_free(file, line);
if (!size) return DUMMY;
if (size > MAXINT) {
if (mayfail) return NULL;
overalloc();
}
size += L_D_S;
retry:
#ifdef LEAK_DEBUG
mem_amount += size - L_D_S;
mem_blocks++;
#endif
if (!(p = malloc(size + RED_ZONE_INC))) {
#ifdef LEAK_DEBUG
mem_amount -= size - L_D_S;
mem_blocks--;
#endif
if (out_of_memory_fl(0, !mayfail ? cast_uchar "malloc" : NULL, size + RED_ZONE_INC, file, line)) goto retry;
return NULL;
}
#ifdef RED_ZONE
*((unsigned char *)p + size + RED_ZONE_INC - 1) = RED_ZONE;
#endif
#ifdef LEAK_DEBUG
ah = p;
p = (unsigned char *)p + L_D_S;
ah->size = size - L_D_S;
ah->magic = ALLOC_MAGIC;
#ifdef LEAK_DEBUG_LIST
ah->file = file;
ah->line = line;
ah->comment = NULL;
add_to_list(memory_list, ah);
#endif
#endif
#ifdef ALLOC_FILL
memset(p, ALLOC_FILL, size - L_D_S);
#endif
return p;
}
void *debug_mem_calloc(unsigned char *file, int line, size_t size, int mayfail)
{
void *p;
#ifdef LEAK_DEBUG
struct alloc_header *ah;
#endif
dos_poll_break();
debug_test_free(file, line);
if (!size) return DUMMY;
if (size > MAXINT) {
if (mayfail) return NULL;
overalloc();
}
size += L_D_S;
retry:
#ifdef LEAK_DEBUG
mem_amount += size - L_D_S;
mem_blocks++;
#endif
if (!(p = x_calloc(size + RED_ZONE_INC))) {
#ifdef LEAK_DEBUG
mem_amount -= size - L_D_S;
mem_blocks--;
#endif
if (out_of_memory_fl(0, !mayfail ? cast_uchar "calloc" : NULL, size + RED_ZONE_INC, file, line)) goto retry;
return NULL;
}
#ifdef RED_ZONE
*((unsigned char *)p + size + RED_ZONE_INC - 1) = RED_ZONE;
#endif
#ifdef LEAK_DEBUG
ah = p;
p = (unsigned char *)p + L_D_S;
ah->size = size - L_D_S;
ah->magic = ALLOC_MAGIC;
#ifdef LEAK_DEBUG_LIST
ah->file = file;
ah->line = line;
ah->comment = NULL;
add_to_list(memory_list, ah);
#endif
#endif
return p;
}
void debug_mem_free(unsigned char *file, int line, void *p)
{
#ifdef LEAK_DEBUG
struct alloc_header *ah;
#endif
dos_poll_break();
if (p == DUMMY) return;
if (!p) {
errfile = file, errline = line, int_error("mem_free(NULL)");
return;
}
#ifdef LEAK_DEBUG
p = (unsigned char *)p - L_D_S;
ah = p;
if (ah->magic != ALLOC_MAGIC) {
errfile = file, errline = line, int_error("mem_free: magic doesn't match: %08x", ah->magic);
return;
}
#ifdef FREE_FILL
memset((unsigned char *)p + L_D_S, FREE_FILL, ah->size);
#endif
ah->magic = ALLOC_FREE_MAGIC;
#ifdef LEAK_DEBUG_LIST
del_from_list(ah);
if (ah->comment) free(ah->comment);
#endif
mem_amount -= ah->size;
mem_blocks--;
#endif
#ifdef RED_ZONE
if (*((unsigned char *)p + L_D_S + ah->size + RED_ZONE_INC - 1) != RED_ZONE) {
errfile = file, errline = line, int_error("mem_free: red zone damaged: %02x (block allocated at %s:%d%s%s)", *((unsigned char *)p + L_D_S + ah->size + RED_ZONE_INC - 1),
#ifdef LEAK_DEBUG_LIST
ah->file, ah->line, ah->comment ? ":" : "", ah->comment ? ah->comment : (unsigned char *)"");
#else
"-", 0, "-");
#endif
return;
}
#endif
free(p);
}
void *debug_mem_realloc(unsigned char *file, int line, void *p, size_t size, int mayfail)
{
#ifdef LEAK_DEBUG
struct alloc_header *ah;
#endif
void *np;
if (p == DUMMY) return debug_mem_alloc(file, line, size, mayfail);
dos_poll_break();
debug_test_free(file, line);
if (!p) {
errfile = file, errline = line, int_error("mem_realloc(NULL, %lu)", (unsigned long)size);
return NULL;
}
if (!size) {
debug_mem_free(file, line, p);
return DUMMY;
}
if (size > MAXINT) {
if (mayfail) return NULL;
overalloc();
}
#ifdef LEAK_DEBUG
p = (unsigned char *)p - L_D_S;
ah = p;
if (ah->magic != ALLOC_MAGIC) {
errfile = file, errline = line, int_error("mem_realloc: magic doesn't match: %08x", ah->magic);
return NULL;
}
ah->magic = ALLOC_REALLOC_MAGIC;
#ifdef REALLOC_FILL
if (!mayfail && size < (size_t)ah->size) memset((unsigned char *)p + L_D_S + size, REALLOC_FILL, ah->size - size);
#endif
#endif
#ifdef RED_ZONE
if (*((unsigned char *)p + L_D_S + ah->size + RED_ZONE_INC - 1) != RED_ZONE) {
errfile = file, errline = line, int_error("mem_realloc: red zone damaged: %02x (block allocated at %s:%d%s%s)", *((unsigned char *)p + L_D_S + ah->size + RED_ZONE_INC - 1),
#ifdef LEAK_DEBUG_LIST
ah->file, ah->line, ah->comment ? ":" : "", ah->comment ? ah->comment : (unsigned char *)"");
#else
"-", 0, "-");
#endif
return (unsigned char *)p + L_D_S;
}
#endif
retry:
if (!(np = realloc(p, size + L_D_S + RED_ZONE_INC))) {
if (out_of_memory_fl(0, !mayfail ? cast_uchar "realloc" : NULL, size + L_D_S + RED_ZONE_INC, file, line)) goto retry;
ah->magic = ALLOC_MAGIC;
return NULL;
}
p = np;
#ifdef RED_ZONE
*((unsigned char *)p + size + L_D_S + RED_ZONE_INC - 1) = RED_ZONE;
#endif
#ifdef LEAK_DEBUG
ah = p;
mem_amount += size - ah->size;
ah->size = size;
ah->magic = ALLOC_MAGIC;
#ifdef LEAK_DEBUG_LIST
ah->prev->next = ah;
ah->next->prev = ah;
#endif
#endif
return (unsigned char *)p + L_D_S;
}
void set_mem_comment(void *p, unsigned char *c, int l)
{
#ifdef LEAK_DEBUG_LIST
struct alloc_header *ah = (struct alloc_header *)((unsigned char *)p - L_D_S);
if (ah->comment) free(ah->comment);
if ((ah->comment = malloc(l + 1))) memcpy(ah->comment, c, l), ah->comment[l] = 0;
#endif
}
#ifdef JS
unsigned char *get_mem_comment(void *p)
{
#ifdef LEAK_DEBUG_LIST
/* perm je prase: return ((struct alloc_header*)((unsigned char*)((void*)((unsigned char*)p-sizeof(int))) - L_D_S))->comment;*/
struct alloc_header *ah = (struct alloc_header *)((unsigned char *)p - L_D_S);
if (!ah->comment) return cast_uchar "";
else return ah->comment;
#else
return cast_uchar "";
#endif
}
#endif
#else
void *mem_alloc_(size_t size, int mayfail)
{
void *p;
dos_poll_break();
debug_test_free(NULL, 0);
if (!size) return DUMMY;
if (size > MAXINT) {
if (mayfail) return NULL;
overalloc();
}
retry:
if (!(p = malloc(size))) {
if (out_of_memory_fl(0, !mayfail ? cast_uchar "malloc" : NULL, size, NULL, 0)) goto retry;
return NULL;
}
return p;
}
void *mem_calloc_(size_t size, int mayfail)
{
void *p;
dos_poll_break();
debug_test_free(NULL, 0);
if (!size) return DUMMY;
if (size > MAXINT) {
if (mayfail) return NULL;
overalloc();
}
retry:
if (!(p = x_calloc(size))) {
if (out_of_memory_fl(0, !mayfail ? cast_uchar "calloc" : NULL, size, NULL, 0)) goto retry;
return NULL;
}
return p;
}
void mem_free(void *p)
{
dos_poll_break();
if (p == DUMMY) return;
if (!p) {
internal("mem_free(NULL)");
return;
}
free(p);
}
void *mem_realloc_(void *p, size_t size, int mayfail)
{
void *np;
if (p == DUMMY) return mem_alloc_(size, mayfail);
dos_poll_break();
debug_test_free(NULL, 0);
if (!p) {
internal("mem_realloc(NULL, %lu)", (unsigned long)size);
return NULL;
}
if (!size) {
mem_free(p);
return DUMMY;
}
if (size > MAXINT) {
if (mayfail) return NULL;
overalloc();
}
retry:
if (!(np = realloc(p, size))) {
if (out_of_memory_fl(0, !mayfail ? cast_uchar "realloc" : NULL, size, NULL, 0)) goto retry;
return NULL;
}
return np;
}
#endif
#if !(defined(LEAK_DEBUG) && defined(LEAK_DEBUG_LIST))
unsigned char *memacpy(const unsigned char *src, size_t len)
{
unsigned char *m;
if (!(len + 1)) overalloc();
m = (unsigned char *)mem_alloc(len + 1);
memcpy(m, src, len);
m[len] = 0;
return m;
}
unsigned char *stracpy(const unsigned char *src)
{
return src ? memacpy(src, src != DUMMY ? strlen(cast_const_char src) : 0) : NULL;
}
#else
unsigned char *debug_memacpy(unsigned char *f, int l, unsigned char *src, size_t len)
{
unsigned char *m;
m = (unsigned char *)debug_mem_alloc(f, l, len + 1, 0);
memcpy(m, src, len);
m[len] = 0;
return m;
}
unsigned char *debug_stracpy(unsigned char *f, int l, unsigned char *src)
{
return src ? (unsigned char *)debug_memacpy(f, l, src, src != DUMMY ? strlen(cast_const_char src) : 0L) : NULL;
}
#endif
#ifdef OOPS
struct prot {
struct prot *next;
struct prot *prev;
sigjmp_buf buf;
};
static struct list_head prot = {&prot, &prot };
static void fault(void *dummy)
{
struct prot *p;
/*fprintf(stderr, "FAULT: %d !\n", (int)(unsigned long)dummy);*/
if (list_empty(prot)) {
fatal_tty_exit();
exit(0);
}
p = prot.next;
del_from_list(p);
longjmp(p->buf, 1);
}
sigjmp_buf *new_stack_frame(void)
{
static int handled = 0;
struct prot *new;
if (!handled) {
#ifdef SIGILL
install_signal_handler(SIGILL, fault, (void *)SIGILL, 1);
#endif
#ifdef SIGABRT
install_signal_handler(SIGABRT, fault, (void *)SIGABRT, 1);
#endif
#ifdef SIGFPE
install_signal_handler(SIGFPE, fault, (void *)SIGFPE, 1);
#endif
#ifdef SIGSEGV
install_signal_handler(SIGSEGV, fault, (void *)SIGSEGV, 1);
#endif
#ifdef SIGBUS
install_signal_handler(SIGBUS, fault, (void *)SIGBUS, 1);
#endif
handled = 1;
}
if (!(new = mem_alloc(sizeof(struct prot)))) return NULL;
add_to_list(prot, new);
return &new->buf;
}
void xpr(void)
{
if (!list_empty(prot)) {
struct prot *next = prot.next;
del_from_list(next);
mem_free(next);
}
}
void nopr(void)
{
free_list(prot);
}
#endif