| 1 | //------------------------------------------------------------------------------ |
| 2 | // Copyright (c) 2016 by Lukasz Janyst <lukasz@jany.st> |
| 3 | //------------------------------------------------------------------------------ |
| 4 | // This file is part of thread-bites. |
| 5 | // |
| 6 | // thread-bites is free software: you can redistribute it and/or modify |
| 7 | // it under the terms of the GNU General Public License as published by |
| 8 | // the Free Software Foundation, either version 3 of the License, or |
| 9 | // (at your option) any later version. |
| 10 | // |
| 11 | // thread-bites is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | // |
| 16 | // You should have received a copy of the GNU General Public License |
| 17 | // along with thread-bites. If not, see <http://www.gnu.org/licenses/>. |
| 18 | //------------------------------------------------------------------------------ |
| 19 | |
| 20 | #include <tb.h> |
| 21 | #include <asm-generic/param.h> |
| 22 | |
| 23 | //------------------------------------------------------------------------------ |
| 24 | // Prototype for a hidden function |
| 25 | //------------------------------------------------------------------------------ |
| 26 | void tb_heap_state(uint64_t *total, uint64_t *allocated); |
| 27 | |
| 28 | //------------------------------------------------------------------------------ |
| 29 | // Check if the state of the heap memory is consistent |
| 30 | //------------------------------------------------------------------------------ |
| 31 | int memory_correct(void **addrs, uint32_t *sizes) |
| 32 | { |
| 33 | for(int i = 0; i < 256; ++i) { |
| 34 | unsigned char *c = addrs[i]; |
| 35 | for(int j = 0; j < sizes[i]; ++j, ++c) |
| 36 | if(*c != i) |
| 37 | return 0; |
| 38 | } |
| 39 | return 1; |
| 40 | } |
| 41 | |
| 42 | //------------------------------------------------------------------------------ |
| 43 | // Start the show |
| 44 | //------------------------------------------------------------------------------ |
| 45 | int main(int argc, char **argv) |
| 46 | { |
| 47 | uint32_t seed = tbtime(); |
| 48 | void *addrs[256]; |
| 49 | uint32_t sizes[256]; |
| 50 | |
| 51 | tbprint("Testing memory allocator. It may take a while...\n" ); |
| 52 | |
| 53 | //---------------------------------------------------------------------------- |
| 54 | // Allocate all the chunks |
| 55 | //---------------------------------------------------------------------------- |
| 56 | for(int i = 0; i < 256; ++i) { |
| 57 | sizes[i] = tbrandom(&seed) % (2*EXEC_PAGESIZE); |
| 58 | addrs[i] = calloc(sizes[i], 1); |
| 59 | unsigned char *c = addrs[i]; |
| 60 | for(int j = 0; j < sizes[i]; ++j, *c++ = i); |
| 61 | } |
| 62 | |
| 63 | if(!memory_correct(addrs, sizes)) { |
| 64 | tbprint("Memory corruption after initialization\n" ); |
| 65 | return 1; |
| 66 | } |
| 67 | |
| 68 | //---------------------------------------------------------------------------- |
| 69 | // In each iteration realloc 20% of each chunks, mark the memory and verify |
| 70 | // correctness of the whole thing. |
| 71 | //---------------------------------------------------------------------------- |
| 72 | for(int i = 0; i < 10000; ++i) { |
| 73 | for(int k = 0; k < 50; ++k) { |
| 74 | int ind = tbrandom(&seed) % 256; |
| 75 | uint32_t new_size = tbrandom(&seed) % (2*EXEC_PAGESIZE); |
| 76 | addrs[ind] = realloc(addrs[ind], new_size); |
| 77 | if(new_size > sizes[ind]) { |
| 78 | unsigned char *c = addrs[ind]; |
| 79 | c += sizes[ind]; |
| 80 | for(int l = sizes[ind]; l < new_size; ++l, *c++ = ind); |
| 81 | } |
| 82 | sizes[ind] = new_size; |
| 83 | } |
| 84 | if(!memory_correct(addrs, sizes)) { |
| 85 | tbprint("Memory corruption after iteration: %d\n" , i); |
| 86 | return 1; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | //---------------------------------------------------------------------------- |
| 91 | // Free all the chunks |
| 92 | //---------------------------------------------------------------------------- |
| 93 | for(int i = 0; i < 256; ++i) |
| 94 | free(addrs[i]); |
| 95 | |
| 96 | //---------------------------------------------------------------------------- |
| 97 | // Check up the heap |
| 98 | //---------------------------------------------------------------------------- |
| 99 | uint64_t total, allocated; |
| 100 | tb_heap_state(&total, &allocated); |
| 101 | tbprint("Total chunks on the heap: %llu, allocated: %llu\n" , |
| 102 | total, allocated); |
| 103 | return 0; |
| 104 | }; |
| 105 | |