lib/tests/fortify_kunit.c
Source file repositories/reference/linux-study-clean/lib/tests/fortify_kunit.c
File Facts
- System
- Linux kernel
- Corpus path
lib/tests/fortify_kunit.c- Extension
.c- Size
- 38496 bytes
- Lines
- 1070
- Domain
- Kernel Services
- Bucket
- lib
- Inferred role
- Kernel Services: implementation source
- Status
- source implementation candidate
Why This File Exists
Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
kunit/device.hkunit/test.hkunit/test-bug.hlinux/device.hlinux/slab.hlinux/string.hlinux/vmalloc.h
Detected Declarations
struct fortify_paddingstruct fortify_zero_sizedfunction fortify_add_kunit_errorfunction fortify_test_known_sizesfunction want_minus_onefunction fortify_test_control_flow_splitfunction fortify_test_realloc_sizefunction fortify_test_strlenfunction fortify_test_strnlenfunction fortify_test_strcpyfunction fortify_test_strscpyfunction fortify_test_strcatfunction fortify_test_strncatfunction fortify_test_strlcatfunction fortify_test_memscanfunction fortify_test_memchrfunction fortify_test_memchr_invfunction fortify_test_memcmpfunction fortify_test_kmemdupfunction fortify_test_init
Annotated Snippet
struct fortify_padding {
unsigned long bytes_before;
char buf[32];
unsigned long bytes_after;
};
static void fortify_test_strlen(struct kunit *test)
{
struct fortify_padding pad = { };
int i, end = sizeof(pad.buf) - 1;
/* Fill 31 bytes with valid characters. */
for (i = 0; i < sizeof(pad.buf) - 1; i++)
pad.buf[i] = i + '0';
/* Trailing bytes are still %NUL. */
KUNIT_EXPECT_EQ(test, pad.buf[end], '\0');
KUNIT_EXPECT_EQ(test, pad.bytes_after, 0);
/* String is terminated, so strlen() is valid. */
KUNIT_EXPECT_EQ(test, strlen(pad.buf), end);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0);
/* Make string unterminated, and recount. */
pad.buf[end] = 'A';
end = sizeof(pad.buf);
KUNIT_EXPECT_EQ(test, strlen(pad.buf), end);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1);
}
static void fortify_test_strnlen(struct kunit *test)
{
struct fortify_padding pad = { };
int i, end = sizeof(pad.buf) - 1;
/* Fill 31 bytes with valid characters. */
for (i = 0; i < sizeof(pad.buf) - 1; i++)
pad.buf[i] = i + '0';
/* Trailing bytes are still %NUL. */
KUNIT_EXPECT_EQ(test, pad.buf[end], '\0');
KUNIT_EXPECT_EQ(test, pad.bytes_after, 0);
/* String is terminated, so strnlen() is valid. */
KUNIT_EXPECT_EQ(test, strnlen(pad.buf, sizeof(pad.buf)), end);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0);
/* A truncated strnlen() will be safe, too. */
KUNIT_EXPECT_EQ(test, strnlen(pad.buf, sizeof(pad.buf) / 2),
sizeof(pad.buf) / 2);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0);
/* Make string unterminated, and recount. */
pad.buf[end] = 'A';
end = sizeof(pad.buf);
/* Reading beyond will fail. */
KUNIT_EXPECT_EQ(test, strnlen(pad.buf, end + 1), end);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1);
KUNIT_EXPECT_EQ(test, strnlen(pad.buf, end + 2), end);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 2);
/* Early-truncated is safe still, though. */
KUNIT_EXPECT_EQ(test, strnlen(pad.buf, end), end);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 2);
end = sizeof(pad.buf) / 2;
KUNIT_EXPECT_EQ(test, strnlen(pad.buf, end), end);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 2);
}
static void fortify_test_strcpy(struct kunit *test)
{
struct fortify_padding pad = { };
char src[sizeof(pad.buf) + 1] = { };
int i;
/* Fill 31 bytes with valid characters. */
for (i = 0; i < sizeof(src) - 2; i++)
src[i] = i + '0';
/* Destination is %NUL-filled to start with. */
KUNIT_EXPECT_EQ(test, pad.bytes_before, 0);
KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0');
KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 2], '\0');
KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 3], '\0');
KUNIT_EXPECT_EQ(test, pad.bytes_after, 0);
/* Legitimate strcpy() 1 less than of max size. */
KUNIT_ASSERT_TRUE(test, strcpy(pad.buf, src)
== pad.buf);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0);
KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0);
/* Only last byte should be %NUL */
Annotation
- Immediate include surface: `kunit/device.h`, `kunit/test.h`, `kunit/test-bug.h`, `linux/device.h`, `linux/slab.h`, `linux/string.h`, `linux/vmalloc.h`.
- Detected declarations: `struct fortify_padding`, `struct fortify_zero_sized`, `function fortify_add_kunit_error`, `function fortify_test_known_sizes`, `function want_minus_one`, `function fortify_test_control_flow_split`, `function fortify_test_realloc_size`, `function fortify_test_strlen`, `function fortify_test_strnlen`, `function fortify_test_strcpy`.
- Atlas domain: Kernel Services / lib.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.