|
| 1 | +/* |
| 2 | + * Copyright (c) 2019 BayLibre SAS |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +/* |
| 8 | + * This test demonstrates a serious bugs that exist in the mem-pool code |
| 9 | + * when the fix from PR #16966 is not applied. |
| 10 | + * |
| 11 | + * Expected output with the bug present: |
| 12 | + * |
| 13 | + * allocating boxes |
| 14 | + * init box 3 |
| 15 | + * show box 3: tic="tic" tac="tac" toe="toe" |
| 16 | + * init box 4 |
| 17 | + * show box 4: tic="tic" tac="tac" toe="toe" |
| 18 | + * show box 3: tic="tic" tac="tac" toe="tic" |
| 19 | + * init box 3 |
| 20 | + * show box 3: tic="tic" tac="tac" toe="toe" |
| 21 | + * show box 4: tic="toe" tac="tac" toe="toe" |
| 22 | + */ |
| 23 | + |
| 24 | +/** |
| 25 | + * @brief Memory Pool Bug Tests |
| 26 | + * @defgroup kernel_memory_pool_tests Memory Pool |
| 27 | + * @ingroup all_tests |
| 28 | + * @{ |
| 29 | + * @} |
| 30 | + */ |
| 31 | + |
| 32 | +#include <ztest.h> |
| 33 | +#include <zephyr.h> |
| 34 | +#include <misc/printk.h> |
| 35 | + |
| 36 | +K_MEM_POOL_DEFINE(my_pool, 8, 44, 9, 4); |
| 37 | + |
| 38 | +struct tic_tac_toe { char *tic; char *tac; char *toe; }; |
| 39 | + |
| 40 | +static struct tic_tac_toe *boxes[9]; |
| 41 | + |
| 42 | +static bool spooked; |
| 43 | + |
| 44 | +static void init_box(int n) |
| 45 | +{ |
| 46 | + printk("init box %d\n", n); |
| 47 | + boxes[n]->tic = "tic"; |
| 48 | + boxes[n]->tac = "tac"; |
| 49 | + boxes[n]->toe = "toe"; |
| 50 | +} |
| 51 | + |
| 52 | +static void show_box(int n) |
| 53 | +{ |
| 54 | + printk("show box %d: tic=\"%s\" tac=\"%s\" toe=\"%s\"\n", |
| 55 | + n, boxes[n]->tic, boxes[n]->tac, boxes[n]->toe); |
| 56 | + |
| 57 | + if (strcmp(boxes[n]->tic, "tic") != 0 || |
| 58 | + strcmp(boxes[n]->tac, "tac") != 0 || |
| 59 | + strcmp(boxes[n]->toe, "toe") != 0) { |
| 60 | + spooked = true; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +void test_mempool_spook(void) |
| 65 | +{ |
| 66 | + struct k_mem_block block[9]; |
| 67 | + int i, ret; |
| 68 | + |
| 69 | + printk("allocating boxes\n"); |
| 70 | + for (i = 0; i < 9; i++) { |
| 71 | + ret = k_mem_pool_alloc(&my_pool, &block[i], |
| 72 | + sizeof(struct tic_tac_toe), 0); |
| 73 | + zassert_false(ret != 0 || block[i].data == NULL, |
| 74 | + "memory allocation failure\n"); |
| 75 | + boxes[i] = block[i].data; |
| 76 | + } |
| 77 | + |
| 78 | + init_box(3); |
| 79 | + show_box(3); |
| 80 | + init_box(4); |
| 81 | + show_box(4); |
| 82 | + show_box(3); /* ! */ |
| 83 | + init_box(3); |
| 84 | + show_box(3); |
| 85 | + show_box(4); /* ! */ |
| 86 | + |
| 87 | + zassert_false(spooked, ""); |
| 88 | +} |
0 commit comments