Skip to content

Commit 06d7746

Browse files
Andrew Boienashif
authored andcommitted
kernel: cover k_array_index_sanitize()
Needed an explicit test for this function for code coverage purposes; we were relying indirectly on other code using it. Signed-off-by: Andrew Boie <[email protected]>
1 parent a3a89ed commit 06d7746

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

tests/kernel/common/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ CONFIG_BOOT_DELAY=500
77
CONFIG_IRQ_OFFLOAD=y
88
CONFIG_TEST_USERSPACE=y
99
CONFIG_SMP=n
10+
CONFIG_BOUNDS_CHECK_BYPASS_MITIGATION=y

tests/kernel/common/src/main.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <ztest.h>
99
#include <kernel_version.h>
10+
#include <misc/speculation.h>
1011
#include "version.h"
1112

1213
extern void test_byteorder_memcpy_swap(void);
@@ -68,6 +69,24 @@ static void test_version(void)
6869

6970
}
7071

72+
static void test_bounds_check_mitigation(void)
73+
{
74+
/* Very hard to test against speculation attacks, but we can
75+
* at least assert that logically this function does
76+
* what it says it does.
77+
*/
78+
79+
int index = 17;
80+
81+
index = k_array_index_sanitize(index, 24);
82+
zassert_equal(index, 17, "bad index");
83+
84+
#ifdef CONFIG_USERSPACE
85+
index = k_array_index_sanitize(index, 5);
86+
zassert_equal(index, 0, "bad index");
87+
#endif
88+
}
89+
7190
void test_main(void)
7291
{
7392
ztest_test_suite(common,
@@ -86,7 +105,8 @@ void test_main(void)
86105
ztest_unit_test(test_clock_cycle),
87106
ztest_unit_test(test_version),
88107
ztest_unit_test(test_multilib),
89-
ztest_unit_test(test_thread_context)
108+
ztest_unit_test(test_thread_context),
109+
ztest_unit_test(test_bounds_check_mitigation)
90110
);
91111

92112
ztest_run_test_suite(common);

0 commit comments

Comments
 (0)