Jim Schaad | 49301d7 | 2016-01-03 21:56:21 -0800 | [diff] [blame] | 1 | #include <stdlib.h> |
Jim Schaad | 3683e6a | 2016-02-01 12:39:29 -0800 | [diff] [blame] | 2 | #ifdef _MSC_VER |
| 3 | #endif |
Jim Schaad | 49301d7 | 2016-01-03 21:56:21 -0800 | [diff] [blame] | 4 | #include <stdio.h> |
| 5 | #include <memory.h> |
| 6 | #include <assert.h> |
| 7 | |
Jim Schaad | 6f269f1 | 2016-01-03 22:01:27 -0800 | [diff] [blame] | 8 | #include <cn-cbor/cn-cbor.h> |
Jim Schaad | 49301d7 | 2016-01-03 21:56:21 -0800 | [diff] [blame] | 9 | |
Jim Schaad | fecd78d | 2016-01-03 22:07:22 -0800 | [diff] [blame] | 10 | #ifdef USE_CBOR_CONTEXT |
Jim Schaad | 49301d7 | 2016-01-03 21:56:21 -0800 | [diff] [blame] | 11 | #include "context.h" |
| 12 | |
| 13 | typedef unsigned char byte; |
| 14 | |
| 15 | typedef struct { |
| 16 | cn_cbor_context context; |
Carlos Gomes Martinho | 9223f01 | 2020-04-24 09:42:16 +0200 | [diff] [blame] | 17 | byte *pFirst; |
Jim Schaad | 0dcce25 | 2016-01-15 12:00:18 -0800 | [diff] [blame] | 18 | unsigned int iFailLeft; |
Jim Schaad | 49301d7 | 2016-01-03 21:56:21 -0800 | [diff] [blame] | 19 | } MyContext; |
| 20 | |
| 21 | typedef struct _MyItem { |
Carlos Gomes Martinho | 9223f01 | 2020-04-24 09:42:16 +0200 | [diff] [blame] | 22 | struct _MyItem *pNext; |
| 23 | size_t size; |
| 24 | byte pad[4]; |
| 25 | byte data[4]; |
Jim Schaad | 49301d7 | 2016-01-03 21:56:21 -0800 | [diff] [blame] | 26 | } MyItem; |
| 27 | |
Carlos Gomes Martinho | 9223f01 | 2020-04-24 09:42:16 +0200 | [diff] [blame] | 28 | bool CheckMemory(MyContext *pContext) |
Jim Schaad | 49301d7 | 2016-01-03 21:56:21 -0800 | [diff] [blame] | 29 | { |
Carlos Gomes Martinho | 9223f01 | 2020-04-24 09:42:16 +0200 | [diff] [blame] | 30 | MyItem *p = NULL; |
Jim Schaad | 49301d7 | 2016-01-03 21:56:21 -0800 | [diff] [blame] | 31 | // Walk memory and check every block |
| 32 | |
Carlos Gomes Martinho | 9223f01 | 2020-04-24 09:42:16 +0200 | [diff] [blame] | 33 | for (p = (MyItem *)pContext->pFirst; p != NULL; p = p->pNext) { |
| 34 | if (p->pad[0] == (byte)0xab) { |
Jim Schaad | 49301d7 | 2016-01-03 21:56:21 -0800 | [diff] [blame] | 35 | // Block has been freed |
Carlos Gomes Martinho | 2a14519 | 2020-04-22 16:25:59 +0200 | [diff] [blame] | 36 | for (unsigned i = 0; i < p->size + 8; i++) { |
Carlos Gomes Martinho | 9223f01 | 2020-04-24 09:42:16 +0200 | [diff] [blame] | 37 | if (p->pad[i] != (byte)0xab) { |
Jim Schaad | 49301d7 | 2016-01-03 21:56:21 -0800 | [diff] [blame] | 38 | fprintf(stderr, "Freed block is modified"); |
| 39 | assert(false); |
| 40 | } |
| 41 | } |
Carlos Gomes Martinho | 9223f01 | 2020-04-24 09:42:16 +0200 | [diff] [blame] | 42 | } else if (p->pad[0] == (byte)0xef) { |
Carlos Gomes Martinho | 2a14519 | 2020-04-22 16:25:59 +0200 | [diff] [blame] | 43 | for (unsigned i = 0; i < 4; i++) { |
Carlos Gomes Martinho | 9223f01 | 2020-04-24 09:42:16 +0200 | [diff] [blame] | 44 | if ((p->pad[i] != (byte)0xef) || |
| 45 | (p->pad[i + 4 + p->size] != (byte)0xef)) { |
Jim Schaad | 49301d7 | 2016-01-03 21:56:21 -0800 | [diff] [blame] | 46 | fprintf(stderr, "Curent block was overrun"); |
| 47 | assert(false); |
| 48 | } |
| 49 | } |
Carlos Gomes Martinho | 9223f01 | 2020-04-24 09:42:16 +0200 | [diff] [blame] | 50 | } else { |
Jim Schaad | 49301d7 | 2016-01-03 21:56:21 -0800 | [diff] [blame] | 51 | fprintf(stderr, "Incorrect pad value"); |
| 52 | assert(false); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return true; |
| 57 | } |
| 58 | |
Carlos Gomes Martinho | 9223f01 | 2020-04-24 09:42:16 +0200 | [diff] [blame] | 59 | void *MyCalloc(size_t count, size_t size, void *context) |
Jim Schaad | 49301d7 | 2016-01-03 21:56:21 -0800 | [diff] [blame] | 60 | { |
Carlos Gomes Martinho | 9223f01 | 2020-04-24 09:42:16 +0200 | [diff] [blame] | 61 | MyItem *pb = NULL; |
| 62 | MyContext *myContext = (MyContext *)context; |
Jim Schaad | 49301d7 | 2016-01-03 21:56:21 -0800 | [diff] [blame] | 63 | |
| 64 | CheckMemory(myContext); |
| 65 | |
Carlos Gomes Martinho | 9223f01 | 2020-04-24 09:42:16 +0200 | [diff] [blame] | 66 | if (myContext->iFailLeft == 0) |
| 67 | return NULL; |
Jim Schaad | e565d0b | 2016-01-20 17:26:23 -0800 | [diff] [blame] | 68 | myContext->iFailLeft--; |
| 69 | |
Carlos Gomes Martinho | 9223f01 | 2020-04-24 09:42:16 +0200 | [diff] [blame] | 70 | pb = (MyItem *)malloc(sizeof(MyItem) + count * size); |
Jim Schaad | 0dcce25 | 2016-01-15 12:00:18 -0800 | [diff] [blame] | 71 | |
Carlos Gomes Martinho | 9223f01 | 2020-04-24 09:42:16 +0200 | [diff] [blame] | 72 | memset(pb, 0xef, sizeof(MyItem) + count * size); |
| 73 | memset(&pb->data, 0, count * size); |
Jim Schaad | 49301d7 | 2016-01-03 21:56:21 -0800 | [diff] [blame] | 74 | |
Carlos Gomes Martinho | 9223f01 | 2020-04-24 09:42:16 +0200 | [diff] [blame] | 75 | pb->pNext = (struct _MyItem *)myContext->pFirst; |
Jim Schaad | e565d0b | 2016-01-20 17:26:23 -0800 | [diff] [blame] | 76 | myContext->pFirst = (byte *)pb; |
Carlos Gomes Martinho | 9223f01 | 2020-04-24 09:42:16 +0200 | [diff] [blame] | 77 | pb->size = count * size; |
Jim Schaad | 49301d7 | 2016-01-03 21:56:21 -0800 | [diff] [blame] | 78 | |
Jim Schaad | e565d0b | 2016-01-20 17:26:23 -0800 | [diff] [blame] | 79 | return &pb->data; |
Jim Schaad | 49301d7 | 2016-01-03 21:56:21 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Carlos Gomes Martinho | 9223f01 | 2020-04-24 09:42:16 +0200 | [diff] [blame] | 82 | void MyFree(void *ptr, void *context) |
Jim Schaad | 49301d7 | 2016-01-03 21:56:21 -0800 | [diff] [blame] | 83 | { |
Carlos Gomes Martinho | 9223f01 | 2020-04-24 09:42:16 +0200 | [diff] [blame] | 84 | MyItem *pb = (MyItem *)((byte *)ptr - sizeof(MyItem) + 4); |
| 85 | MyContext *myContext = (MyContext *)context; |
Jim Schaad | 49301d7 | 2016-01-03 21:56:21 -0800 | [diff] [blame] | 86 | |
| 87 | CheckMemory(myContext); |
Carlos Gomes Martinho | 9223f01 | 2020-04-24 09:42:16 +0200 | [diff] [blame] | 88 | if (ptr == NULL) |
| 89 | return; |
Jim Schaad | 49301d7 | 2016-01-03 21:56:21 -0800 | [diff] [blame] | 90 | |
| 91 | memset(&pb->pad, 0xab, pb->size + 8); |
| 92 | } |
| 93 | |
Carlos Gomes Martinho | 9223f01 | 2020-04-24 09:42:16 +0200 | [diff] [blame] | 94 | cn_cbor_context *CreateContext(unsigned int iFailPoint) |
Jim Schaad | 49301d7 | 2016-01-03 21:56:21 -0800 | [diff] [blame] | 95 | { |
Carlos Gomes Martinho | 9223f01 | 2020-04-24 09:42:16 +0200 | [diff] [blame] | 96 | MyContext *p = malloc(sizeof(MyContext)); |
Jim Schaad | 49301d7 | 2016-01-03 21:56:21 -0800 | [diff] [blame] | 97 | |
| 98 | p->context.calloc_func = MyCalloc; |
| 99 | p->context.free_func = MyFree; |
| 100 | p->context.context = p; |
| 101 | p->pFirst = NULL; |
Jim Schaad | 0dcce25 | 2016-01-15 12:00:18 -0800 | [diff] [blame] | 102 | p->iFailLeft = iFailPoint; |
Jim Schaad | 49301d7 | 2016-01-03 21:56:21 -0800 | [diff] [blame] | 103 | |
| 104 | return &p->context; |
| 105 | } |
| 106 | |
Carlos Gomes Martinho | 9223f01 | 2020-04-24 09:42:16 +0200 | [diff] [blame] | 107 | void FreeContext(cn_cbor_context *pContext) |
Jim Schaad | 49301d7 | 2016-01-03 21:56:21 -0800 | [diff] [blame] | 108 | { |
Carlos Gomes Martinho | 9223f01 | 2020-04-24 09:42:16 +0200 | [diff] [blame] | 109 | MyContext *myContext = (MyContext *)pContext; |
| 110 | MyItem *pItem; |
| 111 | MyItem *pItem2; |
Jim Schaad | 49301d7 | 2016-01-03 21:56:21 -0800 | [diff] [blame] | 112 | |
| 113 | CheckMemory(myContext); |
| 114 | |
Carlos Gomes Martinho | 9223f01 | 2020-04-24 09:42:16 +0200 | [diff] [blame] | 115 | for (pItem = (MyItem *)myContext->pFirst; pItem != NULL; pItem = pItem2) { |
Jim Schaad | 49301d7 | 2016-01-03 21:56:21 -0800 | [diff] [blame] | 116 | pItem2 = pItem->pNext; |
| 117 | free(pItem); |
| 118 | } |
| 119 | |
| 120 | free(myContext); |
| 121 | |
| 122 | return; |
| 123 | } |
Jim Schaad | bff945a | 2016-01-03 22:04:53 -0800 | [diff] [blame] | 124 | |
Carlos Gomes Martinho | 9223f01 | 2020-04-24 09:42:16 +0200 | [diff] [blame] | 125 | #endif // USE_CBOR_CONTEXT |