1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
3 #include <test_progs.h>
4 
5 #include "struct_ops_maybe_null.skel.h"
6 #include "struct_ops_maybe_null_fail.skel.h"
7 
8 /* Test that the verifier accepts a program that access a nullable pointer
9  * with a proper check.
10  */
maybe_null(void)11 static void maybe_null(void)
12 {
13 	struct struct_ops_maybe_null *skel;
14 
15 	skel = struct_ops_maybe_null__open_and_load();
16 	if (!ASSERT_OK_PTR(skel, "struct_ops_module_open_and_load"))
17 		return;
18 
19 	struct_ops_maybe_null__destroy(skel);
20 }
21 
22 /* Test that the verifier rejects a program that access a nullable pointer
23  * without a check beforehand.
24  */
maybe_null_fail(void)25 static void maybe_null_fail(void)
26 {
27 	struct struct_ops_maybe_null_fail *skel;
28 
29 	skel = struct_ops_maybe_null_fail__open_and_load();
30 	if (ASSERT_ERR_PTR(skel, "struct_ops_module_fail__open_and_load"))
31 		return;
32 
33 	struct_ops_maybe_null_fail__destroy(skel);
34 }
35 
test_struct_ops_maybe_null(void)36 void test_struct_ops_maybe_null(void)
37 {
38 	/* The verifier verifies the programs at load time, so testing both
39 	 * programs in the same compile-unit is complicated. We run them in
40 	 * separate objects to simplify the testing.
41 	 */
42 	if (test__start_subtest("maybe_null"))
43 		maybe_null();
44 	if (test__start_subtest("maybe_null_fail"))
45 		maybe_null_fail();
46 }
47