-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_stack.cpp
More file actions
256 lines (178 loc) · 7.13 KB
/
Copy pathcheck_stack.cpp
File metadata and controls
256 lines (178 loc) · 7.13 KB
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
#include <math.h>
#include "log.h"
#include <assert.h>
#include "check_stack.hpp"
Elem_t * create_data_canary (Stack_t * stack)
{
assert (stack);
int * stack_data = (int *) calloc (1, (sizeof (LEFT_DATA_CANARY) + stack->capacity*sizeof(Elem_t) + sizeof (RIGHT_DATA_CANARY)));
stack_data[0] = LEFT_DATA_CANARY;
stack_data++;
stack_data[stack->capacity*sizeof(Elem_t)/sizeof(int)] = RIGHT_DATA_CANARY;
return (Elem_t * ) stack_data;
}
Elem_t * recreate_data_canary (Stack_t * stack)
{
assert (stack);
int * stack_data = (int *) stack->data;
stack_data--;
stack_data = (int*) recalloc (stack_data, sizeof (LEFT_DATA_CANARY) + stack->capacity*sizeof (Elem_t) + sizeof (RIGHT_DATA_CANARY), 1);
stack_data++;
stack_data[stack->capacity*sizeof (Elem_t)/sizeof(int)] = RIGHT_DATA_CANARY;
return (Elem_t *) stack_data;
}
void stack_dump_ (FILE *file, Stack_t *stack, DUMP_MODE mode, const char *func_name, const char *file_name, const int line)
{
const char * error = "ok";
if (stack_ok (stack) != 0)
error = "ERROR";
fprintf (file, "\n%s() at %s.cpp (%d)\n", func_name, file_name, line);
fprintf (file, "stack[%p] (%s) \"%s\" ", stack, error, stack->stack_info.stack_name);
fprintf (file, "at %s() at %s (%d)\n", stack->stack_info.func_name, stack->stack_info.file_name, stack->stack_info.line);
fprintf (file, "{\n size = %d\n capacity = %d\n\n", stack->size, stack->capacity);
if (mode == FULL or (stack->capacity <= 6 and mode == SHORT))
for (unsigned index = 0; index < stack->capacity; index++)
print_stack_data (file, index, stack->data[index]);
else if (mode == SHORT)
{
for (unsigned index = 0; index < 3; index++)
print_stack_data (file, index, stack->data[index]);
fprintf (file,"\n........\n\n");
for (unsigned index = stack->capacity - 3; index < stack->capacity; index++)
print_stack_data (file, index, stack->data[index]);
}
fprintf (file,"}\n\n");
}
void print_stack_data (FILE *file, int index, int stack_data)
{
if (stack_data != int_poison)
fprintf (file, "*[%d] = %d\n", index, stack_data);
else
fprintf (file, " [%d] = %X (poison)\n", index, stack_data);
}
void print_stack_data (FILE *file, int index, double stack_data)
{
if (!isnan (stack_data))
fprintf (file, "*[%d] = %lg\n", index, stack_data);
else
fprintf (file, " [%d] = NAN (poison)\n", index);
}
unsigned long stack_ok (Stack_t *stack)
{
unsigned long error = 0;
if (stack == NULL)
{
$
error = error | (1 << NULL_POINTER_ERROR);
return error;
}
if ((int) stack->size > (int) stack->capacity)
error = error | (1 << CAPACITY_SIZE_ERROR);
if (stack->data == NULL)
error = error | (1 << DATA_POINTER_ERROR);
#ifdef CANARYCHECK
else
error = error | (data_canary_ok (stack) << LEFT_DATA_CANARY_ERROR);
#endif
if (((int) stack->capacity < 0))
error = error | (1 << ZERO_CAPACITY_ERROR);
#ifdef HASHCHECK
if (!error )error = error | (check_stack_hash (stack) << HASH_ERROR);
#endif
#ifdef CANARYCHECK
error = error | (canary_ok (stack) << LEFT_CANARY_ERROR);
#endif
return error;
}
void calculate_stack_hash (Stack_t * stack)
{
assert (stack);
stack->hash = 0;
stack->data_hash = calculate_hash (stack->data - sizeof (LEFT_DATA_CANARY) , sizeof (LEFT_DATA_CANARY) +
stack->capacity*sizeof(Elem_t) +
sizeof (RIGHT_DATA_CANARY));
stack->hash = calculate_hash (&stack->left_canary, sizeof (stack));
stack->hash += calculate_hash ((char *) &stack->hash, sizeof(stack->hash));
}
int calculate_hash (void * obj, size_t size)
{
int hash = 0;
unsigned char * point = (unsigned char *) obj;
while (size--)
{
hash += ((point[size] << 5) + point[size]);
}
return hash;
}
bool check_stack_hash (Stack_t *stack)
{
assert (stack);
if (stack->hash == update_stack_hash (stack))
return 0;
if (stack->data_hash == update_data_hash (stack))
return 0;
return 1;
}
int update_stack_hash (Stack_t *stack)
{
assert (stack);
int stack_hash = calculate_hash (&stack->left_canary, sizeof (stack));
stack_hash += calculate_hash (stack->data - sizeof (LEFT_DATA_CANARY) , sizeof (LEFT_DATA_CANARY) +
stack->capacity*sizeof(Elem_t) +
sizeof (RIGHT_DATA_CANARY));
stack_hash += calculate_hash ((char *) &stack_hash, sizeof(stack_hash));
return stack_hash;
}
int update_data_hash (Stack_t *stack)
{
assert (stack);
int data_hash = calculate_hash (stack->data - sizeof (LEFT_DATA_CANARY) , sizeof (LEFT_DATA_CANARY) +
stack->capacity*sizeof(Elem_t) +
sizeof (RIGHT_DATA_CANARY));
return data_hash;
}
void print_stack_error (unsigned long errors_code)
{
if (errors_code >> CAPACITY_SIZE_ERROR & 1)
fprintf (output_file, "WRONG CAPACITY\n");
if (errors_code >> DATA_POINTER_ERROR & 1)
fprintf (output_file, "DATA POINTER TO NULL\n");
if (errors_code >> ZERO_CAPACITY_ERROR & 1)
fprintf (output_file, "ZERO CAPACITY\n");
if (errors_code >> LEFT_CANARY_ERROR & 1)
fprintf (output_file, "LEFT CANARY ERROR\n");
if (errors_code >> RIGHT_CANARY_ERROR & 1)
fprintf (output_file, "RIGHT CANARY ERROR\n");
if (errors_code >> LEFT_DATA_CANARY_ERROR & 1)
fprintf (output_file, "LEFT DATA CANARY ERROR\n");
if (errors_code >> RIGHT_DATA_CANARY_ERROR & 1)
fprintf (output_file, "RIGHT DATA CANARY ERROR\n");
if (errors_code >> NULL_POINTER_ERROR & 1)
{
fprintf (output_file, "NULL POINTER ERROR\n");
exit (0);
}
#ifdef HASHCHECK
if (errors_code >> HASH_ERROR & 1)
fprintf (output_file, "HASH ERROR\n");
#endif
}
unsigned short canary_ok (Stack_t *stack)
{
unsigned short error = 0;
if (stack->left_canary != LEFT_CANARY)
error = error | 1;
else if (stack->right_canary != RIGHT_CANARY)
error = error | 2;
return error;
}
unsigned short data_canary_ok (Stack_t *stack)
{
assert (stack);
unsigned short error = 0;
if (((int *)stack->data)[-1] != LEFT_DATA_CANARY)
error = error | 1;
if (((int *)stack->data)[stack->capacity*sizeof (Elem_t)/sizeof (int)] != RIGHT_DATA_CANARY)
error = error | 2;
return error;
}