annotate dbmalloc.c @ 1667:986126448688

Update remaining advise to edit options.h You should edit localoptions.h instead.
author Alexander Dahl <ada@thorsis.com>
date Tue, 10 Mar 2020 15:38:38 +0100
parents c42e8ff42bd1
children 1051e4eea25a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1361
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 #include "dbmalloc.h"
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 #include "dbutil.h"
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3
1569
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
4
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
5 void * m_calloc(size_t nmemb, size_t size) {
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
6 if (SIZE_T_MAX / nmemb < size) {
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
7 dropbear_exit("m_calloc failed");
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
8 }
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
9 return m_malloc(nmemb*size);
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
10 }
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
11
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
12 void * m_strdup(const char * str) {
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
13 char* ret;
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
14 unsigned int len;
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
15 len = strlen(str);
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
16
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
17 ret = m_malloc(len+1);
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
18 if (ret == NULL) {
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
19 dropbear_exit("m_strdup failed");
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
20 }
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
21 memcpy(ret, str, len+1);
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
22 return ret;
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
23 }
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
24
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
25 #if !DROPBEAR_TRACKING_MALLOC
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
26
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
27 /* Simple wrappers around malloc etc */
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
28 void * m_malloc(size_t size) {
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
29
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
30 void* ret;
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
31
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
32 if (size == 0) {
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
33 dropbear_exit("m_malloc failed");
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
34 }
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
35 ret = calloc(1, size);
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
36 if (ret == NULL) {
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
37 dropbear_exit("m_malloc failed");
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
38 }
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
39 return ret;
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
40
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
41 }
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
42
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
43 void * m_realloc(void* ptr, size_t size) {
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
44
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
45 void *ret;
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
46
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
47 if (size == 0) {
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
48 dropbear_exit("m_realloc failed");
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
49 }
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
50 ret = realloc(ptr, size);
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
51 if (ret == NULL) {
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
52 dropbear_exit("m_realloc failed");
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
53 }
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
54 return ret;
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
55 }
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
56
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
57
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
58 #else
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
59
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
60 /* For fuzzing */
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
61
1361
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 struct dbmalloc_header {
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 unsigned int epoch;
1378
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
64 struct dbmalloc_header *prev;
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
65 struct dbmalloc_header *next;
1361
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 };
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67
1378
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
68 static void put_alloc(struct dbmalloc_header *header);
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
69 static void remove_alloc(struct dbmalloc_header *header);
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
70
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
71 /* end of the linked list */
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
72 static struct dbmalloc_header* staple;
1361
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 unsigned int current_epoch = 0;
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 void m_malloc_set_epoch(unsigned int epoch) {
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 current_epoch = epoch;
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 }
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79
1378
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
80 void m_malloc_free_epoch(unsigned int epoch, int dofree) {
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
81 struct dbmalloc_header* header;
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
82 struct dbmalloc_header* nextheader = NULL;
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
83 struct dbmalloc_header* oldstaple = staple;
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
84 staple = NULL;
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
85 /* free allocations from this epoch, create a new staple-anchored list from
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
86 the remainder */
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
87 for (header = oldstaple; header; header = nextheader)
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
88 {
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
89 nextheader = header->next;
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
90 if (header->epoch == epoch) {
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
91 if (dofree) {
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
92 free(header);
1361
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 }
1378
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
94 } else {
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
95 header->prev = NULL;
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
96 header->next = NULL;
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
97 put_alloc(header);
1361
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 }
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 }
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 }
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102 static void put_alloc(struct dbmalloc_header *header) {
1378
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
103 assert(header->next == NULL);
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
104 assert(header->prev == NULL);
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
105 if (staple) {
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
106 staple->prev = header;
1361
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107 }
1378
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
108 header->next = staple;
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
109 staple = header;
1361
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110 }
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 static void remove_alloc(struct dbmalloc_header *header) {
1378
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
113 if (header->prev) {
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
114 header->prev->next = header->next;
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
115 }
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
116 if (header->next) {
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
117 header->next->prev = header->prev;
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
118 }
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
119 if (staple == header) {
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
120 staple = header->next;
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
121 }
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
122 header->prev = NULL;
7209a6e30932 linked list dbmalloc now
Matt Johnston <matt@ucc.asn.au>
parents: 1365
diff changeset
123 header->next = NULL;
1361
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 }
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
125
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126 static struct dbmalloc_header* get_header(void* ptr) {
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127 char* bptr = ptr;
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128 return (struct dbmalloc_header*)&bptr[-sizeof(struct dbmalloc_header)];
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
129 }
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
131 void * m_malloc(size_t size) {
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132 char* mem = NULL;
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
133 struct dbmalloc_header* header = NULL;
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
134
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
135 if (size == 0 || size > 1e9) {
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
136 dropbear_exit("m_malloc failed");
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
137 }
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
138
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
139 size = size + sizeof(struct dbmalloc_header);
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
140
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
141 mem = calloc(1, size);
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
142 if (mem == NULL) {
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143 dropbear_exit("m_malloc failed");
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144 }
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145 header = (struct dbmalloc_header*)mem;
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146 put_alloc(header);
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147 header->epoch = current_epoch;
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
148 return &mem[sizeof(struct dbmalloc_header)];
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149 }
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
150
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151 void * m_realloc(void* ptr, size_t size) {
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
152 char* mem = NULL;
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
153 struct dbmalloc_header* header = NULL;
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
154 if (size == 0 || size > 1e9) {
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
155 dropbear_exit("m_realloc failed");
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
156 }
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
157
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
158 header = get_header(ptr);
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
159 remove_alloc(header);
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
160
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
161 size = size + sizeof(struct dbmalloc_header);
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
162 mem = realloc(header, size);
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
163 if (mem == NULL) {
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
164 dropbear_exit("m_realloc failed");
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
165 }
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
166
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
167 header = (struct dbmalloc_header*)mem;
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
168 put_alloc(header);
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
169 return &mem[sizeof(struct dbmalloc_header)];
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
170 }
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
171
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
172 void m_free_direct(void* ptr) {
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
173 struct dbmalloc_header* header = NULL;
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
174 if (!ptr) {
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
175 return;
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
176 }
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
177 header = get_header(ptr);
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
178 remove_alloc(header);
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
179 free(header);
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
180 }
f9f930e1a516 add dbmalloc epoch cleanup
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
181
1569
c42e8ff42bd1 Only use malloc wrapper if fuzzing
Matt Johnston <matt@ucc.asn.au>
parents: 1378
diff changeset
182 #endif /* DROPBEAR_TRACKING_MALLOC */