1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* |
3 | * Runtime test cases for CONFIG_FORTIFY_SOURCE that aren't expected to |
4 | * Oops the kernel on success. (For those, see drivers/misc/lkdtm/fortify.c) |
5 | * |
6 | * For corner cases with UBSAN, try testing with: |
7 | * |
8 | * ./tools/testing/kunit/kunit.py run --arch=x86_64 \ |
9 | * --kconfig_add CONFIG_FORTIFY_SOURCE=y \ |
10 | * --kconfig_add CONFIG_UBSAN=y \ |
11 | * --kconfig_add CONFIG_UBSAN_TRAP=y \ |
12 | * --kconfig_add CONFIG_UBSAN_BOUNDS=y \ |
13 | * --kconfig_add CONFIG_UBSAN_LOCAL_BOUNDS=y \ |
14 | * --make_options LLVM=1 fortify |
15 | */ |
16 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
17 | |
18 | #include <kunit/test.h> |
19 | #include <linux/device.h> |
20 | #include <linux/slab.h> |
21 | #include <linux/string.h> |
22 | #include <linux/vmalloc.h> |
23 | |
24 | static const char array_of_10[] = "this is 10" ; |
25 | static const char *ptr_of_11 = "this is 11!" ; |
26 | static char array_unknown[] = "compiler thinks I might change" ; |
27 | |
28 | static void known_sizes_test(struct kunit *test) |
29 | { |
30 | KUNIT_EXPECT_EQ(test, __compiletime_strlen("88888888" ), 8); |
31 | KUNIT_EXPECT_EQ(test, __compiletime_strlen(array_of_10), 10); |
32 | KUNIT_EXPECT_EQ(test, __compiletime_strlen(ptr_of_11), 11); |
33 | |
34 | KUNIT_EXPECT_EQ(test, __compiletime_strlen(array_unknown), SIZE_MAX); |
35 | /* Externally defined and dynamically sized string pointer: */ |
36 | KUNIT_EXPECT_EQ(test, __compiletime_strlen(test->name), SIZE_MAX); |
37 | } |
38 | |
39 | /* This is volatile so the optimizer can't perform DCE below. */ |
40 | static volatile int pick; |
41 | |
42 | /* Not inline to keep optimizer from figuring out which string we want. */ |
43 | static noinline size_t want_minus_one(int pick) |
44 | { |
45 | const char *str; |
46 | |
47 | switch (pick) { |
48 | case 1: |
49 | str = "4444" ; |
50 | break; |
51 | case 2: |
52 | str = "333" ; |
53 | break; |
54 | default: |
55 | str = "1" ; |
56 | break; |
57 | } |
58 | return __compiletime_strlen(str); |
59 | } |
60 | |
61 | static void control_flow_split_test(struct kunit *test) |
62 | { |
63 | KUNIT_EXPECT_EQ(test, want_minus_one(pick), SIZE_MAX); |
64 | } |
65 | |
66 | #define KUNIT_EXPECT_BOS(test, p, expected, name) \ |
67 | KUNIT_EXPECT_EQ_MSG(test, __builtin_object_size(p, 1), \ |
68 | expected, \ |
69 | "__alloc_size() not working with __bos on " name "\n") |
70 | |
71 | #if !__has_builtin(__builtin_dynamic_object_size) |
72 | #define KUNIT_EXPECT_BDOS(test, p, expected, name) \ |
73 | /* Silence "unused variable 'expected'" warning. */ \ |
74 | KUNIT_EXPECT_EQ(test, expected, expected) |
75 | #else |
76 | #define KUNIT_EXPECT_BDOS(test, p, expected, name) \ |
77 | KUNIT_EXPECT_EQ_MSG(test, __builtin_dynamic_object_size(p, 1), \ |
78 | expected, \ |
79 | "__alloc_size() not working with __bdos on " name "\n") |
80 | #endif |
81 | |
82 | /* If the execpted size is a constant value, __bos can see it. */ |
83 | #define check_const(_expected, alloc, free) do { \ |
84 | size_t |
---|