-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtest_program_ref.ml
More file actions
293 lines (260 loc) · 10.2 KB
/
Copy pathtest_program_ref.ml
File metadata and controls
293 lines (260 loc) · 10.2 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
(*
* Copyright 2025 Multikernel Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*)
open Kernelscript.Parse
open Kernelscript.Type_checker
open Alcotest
(** Test program reference type checking *)
let test_program_reference_type () =
let program_text = {|
@xdp fn packet_filter(ctx: *xdp_md) -> xdp_action {
return 2
}
fn main() -> i32 {
var prog_handle = load(packet_filter)
var result = attach(prog_handle, "eth0", 0)
return 0
}
|} in
try
let ast = parse_string program_text in
let _ = Kernelscript.Symbol_table.build_symbol_table ast in
let (typed_ast, _) = Kernelscript.Type_checker.type_check_and_annotate_ast ast in
check bool "program reference type checking" true (List.length typed_ast > 0)
with
| e -> fail ("program reference type checking failed: " ^ Printexc.to_string e)
(** Test program reference with different program types *)
let test_different_program_types () =
let program_text = {|
@probe("sys_read") fn kprobe_tracer(fd: u32, buf: *u8, count: size_t) -> i32 {
return 0
}
@tc("ingress") fn tc_filter(ctx: *__sk_buff) -> i32 {
return 0
}
fn main() -> i32 {
var kprobe_handle = load(kprobe_tracer)
var tc_handle = load(tc_filter)
var kprobe_result = attach(kprobe_handle, "sys_read", 0)
var tc_result = attach(tc_handle, "eth0", 1)
return 0
}
|} in
try
let ast = parse_string program_text in
let _ = Kernelscript.Symbol_table.build_symbol_table ast in
let (typed_ast, _) = Kernelscript.Type_checker.type_check_and_annotate_ast ast in
check bool "different program types" true (List.length typed_ast > 0)
with
| e -> fail ("different program types failed: " ^ Printexc.to_string e)
(** Test invalid program reference *)
let test_invalid_program_reference () =
let program_text = {|
fn main() -> i32 {
var prog_handle = load(non_existent_program)
return 0
}
|} in
try
let ast = parse_string program_text in
let _ = Kernelscript.Symbol_table.build_symbol_table ast in
let (_, _) = Kernelscript.Type_checker.type_check_and_annotate_ast ast in
fail "should fail for non-existent program"
with
| Type_error _ -> ()
| Kernelscript.Symbol_table.Symbol_error _ -> ()
| e -> fail ("Expected Type_error or Symbol_error, got: " ^ Printexc.to_string e)
(** Test program reference as variable *)
let test_program_reference_as_variable () =
let program_text = {|
@xdp fn my_xdp(ctx: *xdp_md) -> xdp_action {
return 2
}
fn main() -> i32 {
var prog_ref = my_xdp // Should work - program reference as variable
var prog_handle = load(prog_ref)
return 0
}
|} in
try
let ast = parse_string program_text in
let _ = Kernelscript.Symbol_table.build_symbol_table ast in
let (typed_ast, _) = Kernelscript.Type_checker.type_check_and_annotate_ast ast in
check bool "program reference as variable" true (List.length typed_ast > 0)
with
| e -> fail ("program reference as variable failed: " ^ Printexc.to_string e)
(** Test wrong argument types for program functions *)
let test_wrong_argument_types () =
let program_text = {|
@xdp fn my_xdp(ctx: *xdp_md) -> xdp_action {
return 2
}
fn main() -> i32 {
var prog_handle = load("string_instead_of_program") // Should fail
return 0
}
|} in
try
let ast = parse_string program_text in
let _ = Kernelscript.Symbol_table.build_symbol_table ast in
let (_, _) = Kernelscript.Type_checker.type_check_and_annotate_ast ast in
fail "should fail for wrong argument type"
with
| Type_error _ -> ()
| e -> fail ("Expected Type_error, got: " ^ Printexc.to_string e)
(** Test stdlib integration *)
let test_stdlib_integration () =
(* Test that the built-in functions are properly recognized *)
check bool "load is builtin" true (Kernelscript.Stdlib.is_builtin_function "load");
check bool "attach is builtin" true (Kernelscript.Stdlib.is_builtin_function "attach");
(* Test getting function signatures *)
(match Kernelscript.Stdlib.get_builtin_function_signature "load" with
| Some (params, return_type) ->
check int "load parameter count" 1 (List.length params);
check bool "load return type is ProgramHandle" true (return_type = Kernelscript.Ast.ProgramHandle)
| None -> check bool "load function signature should exist" false true);
(match Kernelscript.Stdlib.get_builtin_function_signature "attach" with
| Some (params, return_type) ->
(* attach uses custom validation (param_types = []), so count is 0 *)
check int "attach parameter count" 0 (List.length params);
check bool "attach return type is U32" true (return_type = Kernelscript.Ast.U32)
| None -> check bool "attach function signature should exist" false true);
(match Kernelscript.Stdlib.get_builtin_function_signature "read" with
| Some (params, return_type) ->
check int "read parameter count" 0 (List.length params);
check bool "read return type is perf_read" true (return_type = Kernelscript.Ast.Struct "perf_read")
| None -> check bool "read function signature should exist" false true);
(* Verify that the custom validation function is wired up on the attach entry *)
(match Kernelscript.Stdlib.get_builtin_function "attach" with
| Some func ->
check bool "attach has custom validation wired up" true
(match func.validate with Some _ -> true | None -> false)
| None -> check bool "attach builtin should exist" false true)
(** Test perf attach returns an attachment value that can be read/detached. *)
let test_perf_attachment_value_flow () =
let program_text = {|
@perf_event fn on_cache_miss(ctx: *bpf_perf_event_data) -> i32 {
return 0
}
fn main() -> i32 {
var prog = load(on_cache_miss)
var att = attach(prog, perf_options {
event: cache_misses,
period: 1000000,
}, 0)
var snapshot = read(att)
var count = snapshot.scaled
detach(att)
print("count=%lld", count)
return 0
}
|} in
try
let ast = parse_string program_text in
let symbol_table =
Kernelscript.Symbol_table.build_symbol_table
~builtin_asts:[Kernelscript.Stdlib.get_builtin_types ()]
ast
in
let (typed_ast, _) = Kernelscript.Type_checker.type_check_and_annotate_ast ~symbol_table:(Some symbol_table) ast in
check bool "perf attachment value flow should type check" true (List.length typed_ast > 0)
with
| e -> fail ("perf attachment value flow failed: " ^ Printexc.to_string e)
(** Test that calling attach without load fails *)
let test_attach_without_load_fails () =
let program_text = {|
@xdp fn simple_xdp(ctx: *xdp_md) -> xdp_action {
return 2
}
fn main() -> i32 {
var result = attach(simple_xdp, "eth0", 0) // Should fail - program ref instead of handle
return 0
}
|} in
try
let ast = parse_string program_text in
let _ = Kernelscript.Symbol_table.build_symbol_table ast in
let (_, _) = Kernelscript.Type_checker.type_check_and_annotate_ast ast in
check bool "should fail when attach called with program reference" false true
with
| Type_error (msg, _) ->
check bool "should fail with type error" true (String.length msg > 0);
(* Error message is: "attach() requires (handle, target, flags) — ..." *)
check bool "error message starts with attach()" true
(String.length msg >= 8 && String.sub msg 0 8 = "attach()")
| _ ->
check bool "should fail when attach called with program reference" false true
(** Test multiple program handles with proper resource management *)
let test_multiple_program_handles () =
let program_text = {|
@xdp fn xdp_filter(ctx: *xdp_md) -> xdp_action {
return 2
}
@tc("ingress") fn tc_shaper(ctx: *__sk_buff) -> i32 {
return 0
}
fn main() -> i32 {
var xdp_handle = load(xdp_filter)
var tc_handle = load(tc_shaper)
var xdp_result = attach(xdp_handle, "eth0", 0)
var tc_result = attach(tc_handle, "eth0", 1)
return 0
}
|} in
try
let ast = parse_string program_text in
let _ = Kernelscript.Symbol_table.build_symbol_table ast in
let (typed_ast, _) = Kernelscript.Type_checker.type_check_and_annotate_ast ast in
check bool "multiple program handles should work" true (List.length typed_ast > 0)
with
| e -> fail ("multiple program handles failed: " ^ Printexc.to_string e)
(** Test that program handle variables can be named appropriately *)
let test_program_handle_naming () =
let program_text = {|
@xdp fn simple_xdp(ctx: *xdp_md) -> xdp_action {
return 2
}
fn main() -> i32 {
var program_handle = load(simple_xdp) // Clear, non-fd naming
var network_prog = load(simple_xdp) // Alternative naming
var result1 = attach(program_handle, "eth0", 0)
var result2 = attach(network_prog, "lo", 0)
return 0
}
|} in
try
let ast = parse_string program_text in
let _ = Kernelscript.Symbol_table.build_symbol_table ast in
let (typed_ast, _) = Kernelscript.Type_checker.type_check_and_annotate_ast ast in
check bool "program handle naming should work" true (List.length typed_ast > 0)
with
| e -> fail ("program handle naming failed: " ^ Printexc.to_string e)
(** Test suite *)
let program_ref_tests = [
"program_reference_type_checking", `Quick, test_program_reference_type;
"different_program_types", `Quick, test_different_program_types;
"invalid_program_reference", `Quick, test_invalid_program_reference;
"program_reference_as_variable", `Quick, test_program_reference_as_variable;
"wrong_argument_types", `Quick, test_wrong_argument_types;
"stdlib_integration", `Quick, test_stdlib_integration;
"attach_without_load_fails", `Quick, test_attach_without_load_fails;
"multiple_program_handles", `Quick, test_multiple_program_handles;
"program_handle_naming", `Quick, test_program_handle_naming;
"perf_attachment_value_flow", `Quick, test_perf_attachment_value_flow;
]
let () =
run "Program Reference Tests" [
"program_ref", program_ref_tests;
]