Skip to content

Commit 416cf1f

Browse files
committed
kunit/fortify: Expand testing of __compiletime_strlen()
It seems that Clang thinks __builtin_constant_p() of undefined variables should return true[1]. This is being fixed separately[2], but in the meantime, expand the fortify tests to help track this kind of thing down faster in the future. Link: ClangBuiltLinux#2073 [1] Link: llvm/llvm-project#130713 [2] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent d985e43 commit 416cf1f

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

lib/tests/fortify_kunit.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ static int fortify_write_overflows;
6060

6161
static const char array_of_10[] = "this is 10";
6262
static const char *ptr_of_11 = "this is 11!";
63+
static const char * const unchanging_12 = "this is 12!!";
6364
static char array_unknown[] = "compiler thinks I might change";
6465

6566
void fortify_add_kunit_error(int write)
@@ -83,12 +84,28 @@ void fortify_add_kunit_error(int write)
8384

8485
static void fortify_test_known_sizes(struct kunit *test)
8586
{
87+
char stack[80] = "Test!";
88+
89+
KUNIT_EXPECT_FALSE(test, __is_constexpr(__builtin_strlen(stack)));
90+
KUNIT_EXPECT_EQ(test, __compiletime_strlen(stack), 5);
91+
92+
KUNIT_EXPECT_TRUE(test, __is_constexpr(__builtin_strlen("88888888")));
8693
KUNIT_EXPECT_EQ(test, __compiletime_strlen("88888888"), 8);
94+
95+
KUNIT_EXPECT_TRUE(test, __is_constexpr(__builtin_strlen(array_of_10)));
8796
KUNIT_EXPECT_EQ(test, __compiletime_strlen(array_of_10), 10);
97+
98+
KUNIT_EXPECT_FALSE(test, __is_constexpr(__builtin_strlen(ptr_of_11)));
8899
KUNIT_EXPECT_EQ(test, __compiletime_strlen(ptr_of_11), 11);
89100

101+
KUNIT_EXPECT_TRUE(test, __is_constexpr(__builtin_strlen(unchanging_12)));
102+
KUNIT_EXPECT_EQ(test, __compiletime_strlen(unchanging_12), 12);
103+
104+
KUNIT_EXPECT_FALSE(test, __is_constexpr(__builtin_strlen(array_unknown)));
90105
KUNIT_EXPECT_EQ(test, __compiletime_strlen(array_unknown), SIZE_MAX);
106+
91107
/* Externally defined and dynamically sized string pointer: */
108+
KUNIT_EXPECT_FALSE(test, __is_constexpr(__builtin_strlen(test->name)));
92109
KUNIT_EXPECT_EQ(test, __compiletime_strlen(test->name), SIZE_MAX);
93110
}
94111

0 commit comments

Comments
 (0)