aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/kfuzztest/testdata/common.h
blob: 29e8b193e6bab919e219916ce8eaddb50cabadf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2025 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

// Common struct definitions that ressemble those sound in the kernel source
// under include/linux/kfuzztest.h. For testing purposes, it is only required
// that these have the same sizes and emitted metadata as the kernel
// definitions, and therefore there is no strict requirement that their fields
// match one-to-one.
#ifndef COMMON_H
#define COMMON_H

#include <stdint.h>

struct kfuzztest_target {
	const char *name;
	const char *arg_type_name;
	uintptr_t write_input_cb;
} __attribute__((aligned(32)));

enum kfuzztest_constraint_type {
	EXPECT_EQ,
	EXPECT_NE,
	EXPECT_LT,
	EXPECT_LE,
	EXPECT_GT,
	EXPECT_GE,
	EXPECT_IN_RANGE,
};

struct kfuzztest_constraint {
	const char *input_type;
	const char *field_name;
	uintptr_t value1;
	uintptr_t value2;
	enum kfuzztest_constraint_type type;
} __attribute__((aligned(64)));

enum kfuzztest_annotation_attribute {
	ATTRIBUTE_LEN,
	ATTRIBUTE_STRING,
	ATTRIBUTE_ARRAY,
};

struct kfuzztest_annotation {
	const char *input_type;
	const char *field_name;
	const char *linked_field_name;
	enum kfuzztest_annotation_attribute attrib;
} __attribute__((aligned(32)));

#define DEFINE_FUZZ_TARGET(test_name, test_arg_type)                        \
	struct kfuzztest_target __fuzz_test__##test_name                    \
		__attribute__((section(".kfuzztest_target"), __used__)) = { \
			.name = #test_name,                                 \
			.arg_type_name = #test_arg_type,                    \
		};                                                          \
	/* Avoid the compiler optimizing out the struct definition. */      \
	static test_arg_type arg;

#define DEFINE_CONSTRAINT(arg_type, field, val1, val2, tpe)                  \
	static struct kfuzztest_constraint __constraint_##arg_type##_##field \
		__attribute__((section(".kfuzztest_constraint"),             \
			       __used__)) = {                                \
			.input_type = "struct " #arg_type,                   \
			.field_name = #field,                                \
			.value1 = (uintptr_t)val1,                           \
			.value2 = (uintptr_t)val2,                           \
			.type = tpe,                                         \
		}

#define DEFINE_ANNOTATION(arg_type, field, linked_field, attribute)          \
	static struct kfuzztest_annotation __annotation_##arg_type##_##field \
		__attribute__((section(".kfuzztest_annotation"),             \
			       __used__)) = {                                \
			.input_type = "struct " #arg_type,                   \
			.field_name = #field,                                \
			.linked_field_name = #linked_field,                  \
			.attrib = attribute,                                 \
		}

#endif /* COMMON_H */