-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgstack.h
More file actions
711 lines (556 loc) · 22.4 KB
/
gstack.h
File metadata and controls
711 lines (556 loc) · 22.4 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
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
#include "gstack-header.h"
//===========================================
// Advanced debug functions
#ifndef STACK_FUNC_GUARD
#define STACK_FUNC_GUARD
static bool ptrValid(const void* ptr)
{
if (ptr == NULL) {
return false;
}
#ifdef STACK_USE_PTR_POISON
if ((size_t)ptr>>4 == (size_t)STACK_BAD_PTR_MASK) {
return false;
}
#endif
#ifdef STACK_USE_PTR_SYS_CHECK
#ifdef __unix__
size_t page_size = sysconf(_SC_PAGESIZE);
void *base = (void *)((((size_t)ptr) / page_size) * page_size);
return msync(base, page_size, MS_ASYNC) == 0;
#else
#ifdef _WIN32
MEMORY_BASIC_INFORMATION mbi = {};
if (!VirtualQuery(ptr, &mbi, sizeof (mbi)))
return false;
if (mbi.Protect & (PAGE_GUARD | PAGE_NOACCESS))
return false; // Guard page -> bad ptr
DWORD readRights = PAGE_READONLY | PAGE_READWRITE | PAGE_WRITECOPY
| PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY;
return (mbi.Protect & readRights) != 0;
#else
fprintf(stderr, "WARNING: your OS is unsupported, system pointer checks are diabled!\n");
#endif
#endif
#endif
return true;
}
#ifdef STACK_USE_CANARY
static bool stack_isCanaryVal(void* ptr)
{
assert(ptrValid(ptr));
if (*(STACK_CANARY_TYPE*)ptr == STACK_LEFT_CANARY_POISON)
return true;
if (*(STACK_CANARY_TYPE*)ptr == STACK_RIGHT_CANARY_POISON)
return true;
return false;
}
#endif
#endif /* STACK_FUNC_GUARD */
#ifdef STACK_USE_POISON
static bool GENERIC(stack_isPoisoned)(const STACK_TYPE *elem)
{
assert(ptrValid(elem));
return !memcmp(elem, &STACK_REFERENCE_POISONED_ELEM, sizeof(STACK_TYPE));
}
#endif
#ifdef STACK_USE_CAPACITY_SYS_CHECK
static size_t GENERIC(stack_getRealCapacity)(void *ptr)
{
if (!ptrValid(ptr)) {
fprintf(stderr, "WARNING: getRealCapacity got a bad pointer!\n");
return STACK_SIZE_T_POISON;
}
size_t allocatedSize = 0;
#ifdef __unix__
allocatedSize = malloc_usable_size(ptr);
#else
#ifdef _WIN32
allocatedSize = _msize(ptr);
#else
fprintf(stderr, "WARNING: your OS is unsupported, real capacity check is skipped!\n");
return STACK_SIZE_T_POISON;
#endif
#endif
return (allocatedSize - 2 * STACK_CANARY_WRAPPER_LEN * sizeof(STACK_CANARY_TYPE)) / sizeof(STACK_TYPE);
}
#endif
//===========================================
// Auxiliary stack functions
static size_t GENERIC(stack_expandFactorCalc)(size_t capacity)
{
#ifdef STACK_USE_CANARY
if (capacity <= 1)
return 2 * sizeof(STACK_CANARY_TYPE);
size_t newCapacity = (((size_t)(capacity * STACK_EXPAND_FACTOR) - 1) / sizeof(STACK_CANARY_TYPE) + 1) * sizeof(STACK_CANARY_TYPE); // formula for least denominator of sizeof(STACK_CANARY_TYPE), that is > new capacity
if (newCapacity <= capacity)
return capacity + 1;
return newCapacity;
#else
if (capacity <= 1)
return 2;
size_t newCapacity = capacity * STACK_EXPAND_FACTOR;
if (newCapacity <= capacity)
return capacity + 1;
return newCapacity;
#endif
}
static size_t GENERIC(stack_shrinkageFactorCalc)(size_t capacity)
{
#ifdef STACK_USE_CANARY
if (capacity <= 1)
return 2 * sizeof(STACK_CANARY_TYPE);
size_t newCapacity = (size_t)(capacity * STACK_SHRINKAGE_FACTOR) / sizeof(STACK_CANARY_TYPE) * sizeof(STACK_CANARY_TYPE); // formula for greatest denominator of sizeof(STACK_CANARY_TYPE), that is < new capacity
if (newCapacity >= capacity)
return capacity - 1;
return newCapacity;
#else
if (capacity <= 1)
return 2;
size_t newCapacity = capacity * STACK_SHRINKAGE_FACTOR;
if (newCapacity >= capacity)
return capacity - 1;
return newCapacity;
#endif
}
static size_t GENERIC(stack_allocated_size)(size_t capacity)
{
return (capacity * sizeof(STACK_TYPE) + 2 * STACK_CANARY_WRAPPER_LEN * sizeof(STACK_CANARY_TYPE));
}
//===========================================
// Stack implementation
static stack_status GENERIC(stack_ctor)(GENERIC(stack) *this_)
{
STACK_PTR_VALIDATE(this_);
this_->capacity = STACK_SIZE_T_POISON;
this_->len = STACK_SIZE_T_POISON;
this_->logStream = stdout; //TODO
this_->dataWrapper = (STACK_CANARY_TYPE*)calloc(GENERIC(stack_allocated_size)(STACK_STARTING_CAPACITY), sizeof(char));
if (!this_->dataWrapper) {
#ifdef STACK_USE_PTR_POISON
this_->dataWrapper = (STACK_CANARY_TYPE*)STACK_DEAD_STRUCT_PTR;
this_->data = (STACK_TYPE*)STACK_DEAD_STRUCT_PTR;
#endif
this_->status = STACK_BAD_MEM_ALLOC;
return this_->status;
}
this_->data = (STACK_TYPE*)(this_->dataWrapper + STACK_CANARY_WRAPPER_LEN);
this_->capacity = STACK_STARTING_CAPACITY;
this_->len = 0;
this_->status = STACK_OK;
#ifdef STACK_USE_CANARY
for (size_t i = 0; i < STACK_CANARY_WRAPPER_LEN; ++i) {
LEFT_CANARY_WRAPPER[i] = STACK_LEFT_CANARY_POISON;
RIGHT_CANARY_WRAPPER[i] = STACK_RIGHT_CANARY_POISON;
this_-> leftCanary[i] = STACK_LEFT_CANARY_POISON;
this_->rightCanary[i] = STACK_RIGHT_CANARY_POISON;
}
#endif
#ifdef STACK_USE_POISON
memset((char*)(&STACK_REFERENCE_POISONED_ELEM), STACK_ELEM_POISON, sizeof(STACK_TYPE));
memset((char*)this_->data, STACK_ELEM_POISON, this_->capacity * sizeof(STACK_TYPE));
#endif
#ifdef STACK_USE_DATA_HASH
this_->dataHash = GENERIC(stack_calculateDataHash)(this_);
#endif
#ifdef STACK_USE_STRUCT_HASH
this_->structHash = GENERIC(stack_calculateStructHash)(this_);
#endif
return STACK_HEALTH_CHECK(this_);
}
static stack_status GENERIC(stack_dtor)(GENERIC(stack) *this_)
{
STACK_PTR_VALIDATE(this_);
STACK_HEALTH_CHECK(this_);
#ifdef STACK_USE_CAPACITY_SYS_CHECK
size_t newCapacity = GENERIC(stack_getRealCapacity)(this_->dataWrapper);
if (newCapacity != STACK_SIZE_T_POISON)
this_->capacity = newCapacity;
#endif
#ifdef STACK_USE_POISON
memset((char*)this_->dataWrapper, STACK_FREED_POISON, GENERIC(stack_allocated_size)(this_->capacity));
#endif
this_->capacity = STACK_SIZE_T_POISON;
this_->len = STACK_SIZE_T_POISON;
if (!ptrValid(this_->dataWrapper)) {
fprintf(this_->logStream, "ERROR: pointer to the data is invalid!\n"); //TODO think if dtor should be silent here too?
return STACK_BAD_DATA_PTR;
}
free(this_->dataWrapper);
#ifdef STACK_USE_PTR_POISON
this_->dataWrapper = (STACK_CANARY_TYPE*)STACK_FREED_PTR;
this_->data = (STACK_TYPE*)STACK_FREED_PTR;
#endif
return STACK_HEALTH_CHECK(this_);
}
static stack_status GENERIC(stack_push)(GENERIC(stack) *this_, STACK_TYPE item)
{
STACK_PTR_VALIDATE(this_);
if (STACK_HEALTH_CHECK(this_))
return this_->status;
FILE *out = this_->logStream; //TODO
if (this_->len == this_->capacity) {
this_->status |= GENERIC(stack_reallocate)(this_, GENERIC(stack_expandFactorCalc)(this_->capacity));
}
#ifdef STACK_USE_POISON
if (!GENERIC(stack_isPoisoned)(&this_->data[this_->len])) {
STACK_LOG_TO_STREAM(this_, out, "Stack structure corrupt, element was modified!");
this_->status |= STACK_DATA_INTEGRITY_VIOLATED;
}
#endif
#if defined(STACK_USE_CANARY) && defined(STACK_USE_POISON)
if ((STACK_CANARY_TYPE)this_->data[this_->len] == STACK_RIGHT_CANARY_POISON) {
STACK_LOG_TO_STREAM(this_, out, "WARNING: Requested elem in wrapper, stack didn't reallocate?");
this_->status |= STACK_BAD_MEM_ALLOC;
}
#endif
this_->data[this_->len] = item;
this_->len += 1;
#ifdef STACK_USE_DATA_HASH
this_->dataHash = GENERIC(stack_calculateDataHash)(this_);
#endif
#ifdef STACK_USE_STRUCT_HASH
this_->structHash = GENERIC(stack_calculateStructHash)(this_);
#endif
return STACK_HEALTH_CHECK(this_);
}
static stack_status GENERIC(stack_pop)(GENERIC(stack) *this_, STACK_TYPE *item)
{
STACK_PTR_VALIDATE(this_);
if (STACK_HEALTH_CHECK(this_))
return this_->status;
if (this_->len == 0) {
STACK_LOG_TO_STREAM(this_, this_->logStream, "WARNING: trying to pop from empty stack!");
}
this_->len -= 1;
if (ptrValid(item)) {
*item = this_->data[this_->len];
#ifdef STACK_USE_POISON
if (GENERIC(stack_isPoisoned)(item)) {
STACK_LOG_TO_STREAM(this_, this_->logStream, "WARNING: accessed uninitilized element!");
}
#endif
}
#ifdef STACK_USE_POISON
memset((char*)(&this_->data[this_->len]), STACK_ELEM_POISON, sizeof(STACK_TYPE));
#endif
#ifdef AUTO_SHRINK
size_t newCapacity = GENERIC(stack_shrinkageFactorCalc)(this_->capacity);
if (this_->len < newCapacity && this_->capacity > newCapacity)
{
stack_reallocate(this_, newCapacity);
}
#endif
#ifdef STACK_USE_DATA_HASH
this_->dataHash = GENERIC(stack_calculateDataHash)(this_);
#endif
#ifdef STACK_USE_STRUCT_HASH
this_->structHash = GENERIC(stack_calculateStructHash)(this_);
#endif
return STACK_HEALTH_CHECK(this_);
}
static stack_status GENERIC(stack_top)(GENERIC(stack) *this_, STACK_TYPE **item)
{
STACK_PTR_VALIDATE(this_);
if (STACK_HEALTH_CHECK(this_))
return this_->status;
if (this_->len == 0) {
STACK_LOG_TO_STREAM(this_, this_->logStream, "WARNING: trying to pop from empty stack!");
}
if (ptrValid(item)) {
*item = &this_->data[this_->len - 1];
}
return STACK_HEALTH_CHECK(this_);
}
static stack_status GENERIC(stack_get)(GENERIC(stack) *this_, size_t pos, STACK_TYPE **item)
{
STACK_PTR_VALIDATE(this_);
if (STACK_HEALTH_CHECK(this_))
return this_->status;
if (pos >= this_->len) {
STACK_LOG_TO_STREAM(this_, this_->logStream, "ERROR: bad position provided to stack_get!");
*item = NULL;
}
if (ptrValid(item)) {
*item = &this_->data[pos];
}
return STACK_HEALTH_CHECK(this_);
}
static stack_status GENERIC(stack_reallocate)(GENERIC(stack) *this_, const size_t newCapacity)
{
STACK_HEALTH_CHECK(this_);
#ifdef STACK_USE_POISON
if (newCapacity < this_->capacity)
{
memset((char*)(this_->data + newCapacity), STACK_FREED_POISON, (this_->capacity - newCapacity) * sizeof(STACK_TYPE));
}
#endif
STACK_CANARY_TYPE *newDataWrapper = (STACK_CANARY_TYPE*)realloc(this_->dataWrapper, GENERIC(stack_allocated_size)(newCapacity));
if (newDataWrapper == NULL) // reallocation failed
{
#ifdef STACK_USE_PTR_POISON
newDataWrapper = (STACK_CANARY_TYPE*)STACK_INVALID_PTR;
#endif
return STACK_BAD_MEM_ALLOC;
}
if (this_->dataWrapper != newDataWrapper) {
this_->dataWrapper = newDataWrapper;
this_->data = (STACK_TYPE*)(this_->dataWrapper + STACK_CANARY_WRAPPER_LEN);
}
#ifdef STACK_USE_POISON
if (newCapacity > this_->capacity)
memset((char*)(this_->data + this_->capacity), STACK_ELEM_POISON, (newCapacity - this_->capacity) * sizeof(STACK_TYPE));
#endif
this_->capacity = newCapacity;
#ifdef STACK_USE_CANARY
for (size_t i = 0; i < STACK_CANARY_WRAPPER_LEN; ++i) {
RIGHT_CANARY_WRAPPER[i] = STACK_RIGHT_CANARY_POISON;
}
#endif
#ifdef STACK_USE_DATA_HASH
this_->dataHash = GENERIC(stack_calculateDataHash)(this_);
#endif
#ifdef STACK_USE_STRUCT_HASH
this_->structHash = GENERIC(stack_calculateStructHash)(this_);
#endif
return STACK_HEALTH_CHECK(this_);
}
static stack_status GENERIC(stack_clear)(GENERIC(stack) *this_)
{
stack_status status = GENERIC(stack_dtor)(this_);
if (status != 0)
return status;
status = GENERIC(stack_ctor)(this_);
return status;
}
static stack_status GENERIC(stack_dumpToStream)(const GENERIC(stack) *this_, FILE *out)
{
STACK_PTR_VALIDATE(this_);
if (!ptrValid(out)) {
fprintf(stderr, "WARNING: Bad log stream provided, outputing to stderr.\n");
out = stderr;
}
fprintf(out, "%s\n", STACK_LOG_DELIM);
fprintf(out, "| Stack [%p] :\n", this_);
fprintf(out, "|----------------\n");
fprintf(out, "| Current status = %d\n", this_->status);
if (this_->status & STACK_BAD_STRUCT_PTR)
fprintf(out, "| Bad self ptr \n");
if (this_->status & STACK_BAD_MEM_ALLOC)
fprintf(out, "| Bad memory allocation \n");
if (this_->status & STACK_INTEGRITY_VIOLATED)
fprintf(out, "| Stack integrity violated \n");
if (this_->status & STACK_DATA_INTEGRITY_VIOLATED)
fprintf(out, "| Data integrity violated \n");
if (this_->status & STACK_LEFT_STRUCT_CANARY_CORRUPT)
fprintf(out, "| Left structure canary corrupted \n");
if (this_->status & STACK_RIGHT_STRUCT_CANARY_CORRUPT)
fprintf(out, "| Right structure canary corrupted \n");
if (this_->status & STACK_LEFT_DATA_CANARY_CORRUPT)
fprintf(out, "| Left data canary corrupted \n");
if (this_->status & STACK_RIGHT_DATA_CANARY_CORRUPT)
fprintf(out, "| Right data canary corrupted \n");
if (this_->status & STACK_BAD_STRUCT_HASH)
fprintf(out, "| Bad structure hash, stack may be corrupted \n");
if (this_->status & STACK_BAD_DATA_HASH)
fprintf(out, "| Bad data hash, stack data may be corrupted \n");
if (this_->status & STACK_BAD_CAPACITY)
fprintf(out, "| Bad capacity, capacity value differs from the allocated one\n");
size_t capacity = this_->capacity;
#ifdef STACK_USE_CAPACITY_SYS_CHECK
if (this_->status & STACK_BAD_CAPACITY) {
capacity = GENERIC(stack_getRealCapacity)(this_->dataWrapper);
if (capacity == STACK_SIZE_T_POISON) {
capacity = this_->capacity;
}
}
#endif
if (STACK_VERBOSE >= 1) {
fprintf(out, "|----------------\n");
fprintf(out, "| Capacity = %zu\n", this_->capacity);
#ifdef STACK_USE_CAPACITY_SYS_CHECK
fprintf(out, "| Real capacity = %zu\n", capacity);
#endif
fprintf(out, "| Len = %zu\n", this_->len);
fprintf(out, "| Data wrapper ptr = %p\n", this_->dataWrapper);
fprintf(out, "| Data ptr = %p\n", this_->data);
fprintf(out, "| Elem size = %zu\n", sizeof(STACK_TYPE));
#ifdef STACK_USE_STRUCT_HASH
fprintf(out, "| Struct hash = %zu\n", this_->structHash);
#endif
#ifdef STACK_USE_DATA_HASH
fprintf(out, "| Data hash = %zu\n", this_->dataHash);
#endif
fprintf(out, "| {\n");
#ifdef STACK_USE_CANARY //TODO read about graphviz
for (size_t i = 0; i < STACK_CANARY_WRAPPER_LEN; ++i) {
fprintf(out, "| l %llx\n", LEFT_CANARY_WRAPPER[i]); // `l` for left canary
}
#endif
size_t cap = fmin(this_->len, capacity); // in case structure is corrupt and len > capacity
for (size_t i = 0; i < cap; ++i) {
fprintf(out, "| * " ELEM_PRINTF_FORM "\n", this_->data[i]); // `*` for in-use cells
}
bool printAll = true;
#ifdef STACK_USE_POISON
if (capacity == this_->capacity) {
printAll = false;
for (size_t i = this_->len; i < capacity; ++i) {
if (!GENERIC(stack_isPoisoned)(&this_->data[i]))
printAll = true;
}
}
#endif
if (capacity < this_->len)
printAll = true;
if (capacity - this_->len > 10 && !printAll) { // shortens outp of the same poison
fprintf(out, "| %x\n", this_->data[this_->len]);
fprintf(out, "| %x\n", this_->data[this_->len]);
fprintf(out, "| %x\n", this_->data[this_->len]);
fprintf(out, "| ...\n");
fprintf(out, "| %x\n", this_->data[this_->len]);
fprintf(out, "| %x\n", this_->data[this_->len]);
}
else {
for (size_t i = this_->len; i < capacity; ++i) {
fprintf(out, "| %x\n", this_->data[i]);
}
}
#ifdef STACK_USE_CANARY
#ifdef STACK_USE_CAPACITY_SYS_CHECK
for (size_t i = 0; i < STACK_CANARY_WRAPPER_LEN; ++i) {
fprintf(out, "| r %llx\n", ((STACK_CANARY_TYPE*)((char*)this_->dataWrapper + STACK_CANARY_WRAPPER_LEN * sizeof(STACK_CANARY_TYPE) + capacity * sizeof(STACK_TYPE)))[i]);
}
#else
for (size_t i = 0; i < STACK_CANARY_WRAPPER_LEN; ++i) {
fprintf(out, "| r %llx\n", RIGHT_CANARY_WRAPPER[i]); // `r` for right canary
}
#endif
#endif
fprintf(out, "| }\n");
}
fprintf(out, "%s\n", STACK_LOG_DELIM);
return this_->status;
}
static stack_status GENERIC(stack_dump)(const GENERIC(stack) *this_)
{
return GENERIC(stack_dumpToStream)(this_, this_->logStream);
}
#ifdef STACK_USE_CAPACITY_SYS_CHECK
static stack_status GENERIC(stack_healthCheck)(GENERIC(stack) *this_) // healthcheck changes this_->capacity to realCapacity if the current value is definetly wrong
#else
static stack_status GENERIC(stack_healthCheck)(const GENERIC(stack) *this_)
#endif
{
STACK_PTR_VALIDATE(this_);
FILE *out = this_->logStream;
if ((this_->capacity == 0 || this_->capacity == STACK_SIZE_T_POISON) && // checks if properly empty
(this_->len == 0 || this_->len == STACK_SIZE_T_POISON))
{
#ifdef STACK_USE_PTR_POISON
if (this_->dataWrapper == (STACK_CANARY_TYPE*)STACK_FREED_PTR &&
this_->data == (STACK_TYPE*)STACK_FREED_PTR) //TODO think if invalid would fit here
{
this_->status = STACK_OK;
return STACK_OK;
}
#else
this_->status = STACK_OK;
return STACK_OK;
#endif
}
uint64_t hash = 0;
#ifdef STACK_USE_STRUCT_HASH
hash = GENERIC(stack_calculateStructHash)(this_);
if (this_->structHash != hash)
this_->status |= STACK_BAD_STRUCT_HASH;
#endif
if (this_->len > this_->capacity || this_->capacity > 1e20)
this_->status |= STACK_INTEGRITY_VIOLATED;
#ifdef STACK_USE_CANARY
for (size_t i = 0; i < STACK_CANARY_WRAPPER_LEN; ++i) {
if (this_->leftCanary[i] != STACK_LEFT_CANARY_POISON) {
this_->status |= STACK_LEFT_STRUCT_CANARY_CORRUPT;
}
if (this_->rightCanary[i] != STACK_RIGHT_CANARY_POISON) {
this_->status |= STACK_RIGHT_STRUCT_CANARY_CORRUPT;
}
}
#endif
if (!ptrValid(this_->dataWrapper)) {
this_->status |= STACK_BAD_DATA_PTR;
}
#ifdef STACK_USE_CAPACITY_SYS_CHECK
size_t capacity = GENERIC(stack_getRealCapacity)(this_->dataWrapper);
if (capacity != STACK_SIZE_T_POISON) {
if ((capacity < this_->capacity)) {
STACK_LOG_TO_STREAM(this_, stderr, "Capacity != RealCapacity");
this_->status |= STACK_BAD_CAPACITY;
this_->capacity = capacity;
}
else {
#if defined(__SANITIZE_ADDRESS__)
if (capacity != this_->capacity) {
STACK_LOG_TO_STREAM(this_, stderr, "Capacity != RealCapacity");
this_->status |= STACK_BAD_CAPACITY;
this_->capacity = capacity;
}
#endif
}
}
#endif
/// All stack struct checks should happen above here
/// All stack data chechs should happen below here
#ifdef STACK_USE_DATA_HASH
hash = GENERIC(stack_calculateDataHash)(this_);
if (this_->dataHash != hash)
this_->status |= STACK_BAD_DATA_HASH;
#endif
#ifdef STACK_USE_CANARY
for (size_t i = 0; i < STACK_CANARY_WRAPPER_LEN; ++i) {
if (LEFT_CANARY_WRAPPER[i] != STACK_LEFT_CANARY_POISON) {
this_->status |= STACK_LEFT_DATA_CANARY_CORRUPT;
}
if (RIGHT_CANARY_WRAPPER[i] != STACK_RIGHT_CANARY_POISON) {
this_->status |= STACK_RIGHT_DATA_CANARY_CORRUPT;
}
}
#endif
#ifdef STACK_USE_POISON
for (size_t i = this_->len; i < this_->capacity; ++i) {
if (!GENERIC(stack_isPoisoned)(&this_->data[i])) {
this_->status |= STACK_DATA_INTEGRITY_VIOLATED;
}
}
#endif
if (this_->status)
STACK_LOG_TO_STREAM(this_, out, "Problems found during healthcheck!");
return this_->status;
}
#ifdef STACK_USE_STRUCT_HASH
static uint64_t GENERIC(stack_calculateStructHash)(const GENERIC(stack) *this_)
{
assert(ptrValid(this_));
uint64_t hash = 0;
hash = _mm_crc32_u64(hash, (uint64_t)(this_->dataWrapper));
hash = _mm_crc32_u64(hash, (uint64_t)(this_->data));
hash = _mm_crc32_u64(hash, (uint64_t)(this_->capacity));
hash = _mm_crc32_u64(hash, (uint64_t)(this_->len));
hash = _mm_crc32_u64(hash, (uint64_t)(this_->logStream));
#ifdef STACK_USE_DATA_HASH
hash = _mm_crc32_u64(hash, (uint64_t)(this_->dataHash));
#endif
return hash;
}
#endif
#ifdef STACK_USE_DATA_HASH
static uint64_t GENERIC(stack_calculateDataHash)(const GENERIC(stack) *this_)
{
assert(ptrValid(this_));
uint64_t hash = 0;
for (char* iter = (char*)(this_->data); iter < (char*)(this_->data + this_->capacity); ++iter) {
hash = _mm_crc32_u8(hash, *iter);
}
return hash;
}
#endif