Skip to content

Commit eb595df

Browse files
author
Nicolas Pitre
committed
test/mempool: bug demonstration test
This test demonstrates a serious bugs that exist in the mem-pool code as ovf Zephyr v1.14.0, and probably earlier versions too. This bug is fixed with PR zephyrproject-rtos#16966. Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
1 parent d0490fe commit eb595df

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

tests/kernel/mem_pool/mem_pool_concept/src/main.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ extern void test_mpool_alloc_wait_prio(void);
99
extern void test_mpool_alloc_size_roundup(void);
1010
extern void test_mpool_alloc_merge_failed_diff_size(void);
1111
extern void test_mpool_alloc_merge_failed_diff_parent(void);
12+
extern void test_mempool_spook(void);
1213

1314
/*test case main entry*/
1415
void test_main(void)
@@ -17,7 +18,8 @@ void test_main(void)
1718
ztest_unit_test(test_mpool_alloc_wait_prio),
1819
ztest_unit_test(test_mpool_alloc_size_roundup),
1920
ztest_unit_test(test_mpool_alloc_merge_failed_diff_size),
20-
ztest_unit_test(test_mpool_alloc_merge_failed_diff_parent));
21+
ztest_unit_test(test_mpool_alloc_merge_failed_diff_parent),
22+
ztest_unit_test(test_mempool_spook));
2123
ztest_run_test_suite(mpool_concept);
2224
}
2325

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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

Comments
 (0)