-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtest_map_syntax.ml
More file actions
497 lines (436 loc) · 16.5 KB
/
Copy pathtest_map_syntax.ml
File metadata and controls
497 lines (436 loc) · 16.5 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
(*
* 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.Ast
open Kernelscript.Parse
open Alcotest
(** Test suite for Map Syntax and Operations *)
let _test_position = make_position 1 1 "test.ks"
(** Helper function to parse string with builtin types loaded via symbol table *)
let parse_string_with_builtins code =
let ast = parse_string code in
(* Create symbol table with test builtin types *)
let symbol_table = Test_utils.Helpers.create_test_symbol_table ast in
(* Run type checking with builtin types loaded *)
let (typed_ast, _) = Kernelscript.Type_checker.type_check_and_annotate_ast ~symbol_table:(Some symbol_table) ast in
(typed_ast, symbol_table)
(** Helper function to check if string contains substring *)
let contains_substr str substr =
try
let _ = Str.search_forward (Str.regexp_string substr) str 0 in
true
with Not_found -> false
(** Test map declaration parsing *)
let test_map_declaration_parsing () =
let test_cases = [
(* Basic Hash *)
("var test_map : hash<u32, u64>(1024)", true);
(* Array map *)
("var array_map : array<u32, u32>(512)", true);
(* Percpu_hash *)
("var percpu_map : percpu_hash<u64, u64>(256)", true);
(* Invalid syntax - wrong order *)
("var bad_map : hash<u32, u64>(1024)", true);
(* Invalid syntax - missing max_entries *)
("var default_map : hash<u32, u64>()", false);
(* Old syntax with blocks - should fail *)
("var old_map : hash<u32, u64>(1024) { }", false);
] in
List.iter (fun (code, should_succeed) ->
try
let program = Printf.sprintf "%s\n@xdp fn test() -> u32 { return 0 }" code in
let _ = parse_string program in
check bool ("parsing: " ^ code) should_succeed true
with
| _ ->
check bool ("parsing: " ^ code) should_succeed false
) test_cases
(** Test new block-less map declaration syntax *)
let test_blockless_map_declaration () =
let test_cases = [
(* Basic block-less Hash *)
("var simple_map : hash<u32, u64>(1024)", true);
(* Block-less Array *)
("var array_map : array<u32, u32>(512)", true);
(* Block-less Percpu_hash *)
("var percpu_map : percpu_hash<u64, u64>(256)", true);
(* Block-less Lru_hash *)
("var lru_map : lru_hash<u32, u64>(128)", true);
(* Pinned map *)
("pin var pinned_map : hash<u32, u64>(1024)", true);
(* Map with flags *)
("@flags(no_prealloc) var flags_map : hash<u32, u64>(1024)", true);
(* Combined pin and flags *)
("@flags(rdonly) pin var combined_map : hash<u32, u64>(1024)", true);
(* Invalid - old syntax with blocks *)
("var invalid_map : hash<u32, u64>(1024) { }", false);
] in
List.iter (fun (code, should_succeed) ->
try
let program = Printf.sprintf "%s\n@xdp fn test() -> u32 { return 0 }" code in
let _ = parse_string program in
check bool ("blockless parsing: " ^ code) should_succeed true
with
| _ ->
check bool ("blockless parsing: " ^ code) should_succeed false
) test_cases
(** Test map declarations with new attributes *)
let test_map_attributes_syntax () =
let test_cases = [
(* Pinned map *)
("pin var pinned_map : hash<u32, u64>(1024)", true);
(* Map with flags *)
("@flags(no_prealloc) var flags_map : hash<u32, u64>(1024)", true);
(* Combined attributes *)
("@flags(rdonly) pin var combined_map : hash<u32, u64>(1024)", true);
(* Multiple flags *)
("@flags(no_prealloc | rdonly) var multi_flags_map : hash<u32, u64>(1024)", true);
(* Regular map without attributes *)
("var regular_map : hash<u32, u64>(1024)", true);
(* Invalid - old syntax with blocks *)
("var invalid_map : hash<u32, u64>(1024) { pinned: \"/path\" }", false);
(* Invalid - old syntax with empty blocks *)
("var invalid_map : hash<u32, u64>(1024) { }", false);
] in
List.iter (fun (code, should_succeed) ->
try
let program = Printf.sprintf "%s\n@xdp fn test() -> u32 { return 0 }" code in
let _ = parse_string program in
check bool ("attributes parsing: " ^ code) should_succeed true
with
| _ ->
check bool ("attributes parsing: " ^ code) should_succeed false
) test_cases
(** Test comprehensive map syntax variations *)
let test_comprehensive_map_syntax () =
let program = {|
// Block-less maps
var simple_counter : hash<u32, u64>(512)
var lookup_array : array<u32, u32>(256)
var percpu_stats : percpu_hash<u64, u64>(128)
// Pinned maps
pin var pinned_global : hash<u32, u64>(2048)
pin var pinned_local : hash<u32, u64>(512)
// Maps with flags
@flags(no_prealloc) var efficient_map : hash<u32, u64>(1024)
@flags(rdonly) var readonly_map : hash<u32, u64>(256)
// Combined attributes
@flags(no_prealloc | rdonly) pin var combined_map : hash<u32, u64>(1024)
@xdp fn test_syntax(ctx: *xdp_md) -> xdp_action {
// Test all map types can be used
simple_counter[42] = 100
lookup_array[10] = 200
percpu_stats[123] = 300
pinned_global[1] = 400
pinned_local[2] = 500
efficient_map[3] = 600
readonly_map[4] = 700
combined_map[5] = 800
return XDP_PASS
}
|} in
try
let (typed_ast, _) = parse_string_with_builtins program in
check bool "comprehensive syntax parsing" true (List.length typed_ast > 0)
with
| exn ->
Printf.printf "Comprehensive syntax parsing failed with: %s\n" (Printexc.to_string exn);
check bool "comprehensive syntax parsing" true false
(** Test map syntax type checking *)
let test_new_syntax_type_checking () =
let program = {|
var blockless_map : hash<u32, u64>(512)
pin var pinned_map : hash<u32, u64>(1024)
@xdp fn test(ctx: *xdp_md) -> xdp_action {
// Test type checking works with new syntax
var key: u32 = 42
var value1: u64 = blockless_map[key]
var value2: u64 = pinned_map[key]
blockless_map[key] = value1 + 1
pinned_map[key] = value2 + 1
return XDP_PASS
}
|} in
try
let (ast, _) = parse_string_with_builtins program in
check bool "new syntax type checking" true (List.length ast > 0)
with
| exn ->
Printf.printf "New syntax type checking failed with: %s\n" (Printexc.to_string exn);
check bool "new syntax type checking" true false
(** Test IR generation with new syntax *)
let test_new_syntax_ir_generation () =
let program = {|
var simple_map : hash<u32, u64>(512)
pin var pinned_map : hash<u32, u64>(1024)
@xdp fn test(ctx: *xdp_md) -> xdp_action {
simple_map[42] = 100
pinned_map[42] = 200
var val1 = simple_map[42]
var val2 = pinned_map[42]
return XDP_PASS
}
|} in
try
(* Follow the complete compiler pipeline *)
let (typed_ast, symbol_table) = parse_string_with_builtins program in
(* Test that IR generation completes without errors *)
let ir = Kernelscript.Ir_generator.generate_ir typed_ast symbol_table "test" in
check bool "IR generation produces programs" true (List.length (Kernelscript.Ir.get_programs ir) > 0)
with
| exn -> fail ("IR generation failed: " ^ Printexc.to_string exn)
(** Test C code generation with new syntax *)
let test_new_syntax_c_generation () =
let program = {|
var blockless_counter : hash<u32, u64>(512)
pin var pinned_stats : hash<u32, u64>(1024)
@xdp fn counter(ctx: *xdp_md) -> xdp_action {
var key = 42
blockless_counter[key] = blockless_counter[key] + 1
pinned_stats[key] = pinned_stats[key] + 1
return XDP_PASS
}
|} in
try
let (typed_ast, symbol_table) = parse_string_with_builtins program in
let ir = Kernelscript.Ir_generator.generate_ir typed_ast symbol_table "test" in
let c_code = Kernelscript.Ebpf_c_codegen.generate_c_multi_program ir in
(* Verify both maps are generated *)
let has_blockless = contains_substr c_code "blockless_counter" in
let has_pinned = contains_substr c_code "pinned_stats" in
let has_map_ops = contains_substr c_code "bpf_map_lookup_elem" &&
contains_substr c_code "bpf_map_update_elem" in
let _ = has_blockless && has_pinned && has_map_ops in
check bool "C code generation test" true (has_blockless && has_pinned && has_map_ops)
with
| exn ->
Printf.printf "C generation failed with: %s\n" (Printexc.to_string exn);
check bool "C code generation test" true false
(** Test error cases for new syntax *)
let test_new_syntax_error_cases () =
let invalid_cases = [
(* Old syntax with blocks - should fail *)
"var invalid : hash<u32, u64>(512) { }";
(* Old syntax with attributes - should fail *)
"var invalid : hash<u32, u64>(512) { pinned: \"/path\" }";
(* Missing colon *)
"var bad_map hash<u32, u64>(1024)";
(* Invalid flags *)
"@flags(invalid_flag) var invalid : hash<u32, u64>(512)";
] in
let all_failed_as_expected = List.for_all (fun invalid_code ->
try
let program = Printf.sprintf "%s\n@xdp fn test() -> u32 { return 0 }" invalid_code in
let _ = parse_string program in
false (* Should have failed *)
with
| _ -> true (* Expected to fail *)
) invalid_cases in
check bool "all invalid cases failed as expected" true all_failed_as_expected
(** Test map operations parsing *)
let test_map_operations_parsing () =
let test_cases = [
("map[key] = value", true);
("var result = map[key]", true);
("delete map[key]", true);
("var inner_key = inner_map[key]\nvar result = outer_map[inner_key]", true);
] in
List.iter (fun (input, should_pass) ->
try
let _ = parse_string input in
if not should_pass then
Printf.printf "ERROR: Expected %s to fail\n" input
with
| _ when should_pass ->
Printf.printf "ERROR: Expected %s to pass\n" input
| _ -> () (* Expected failure *)
) test_cases
(** Test complete map program parsing *)
let test_complete_map_program_parsing () =
let program = {|
var packet_counts : hash<u32, u64>(1024)
@xdp fn rate_limiter(ctx: *xdp_md) -> xdp_action {
var src_ip = 0x08080808
var current_count = packet_counts[src_ip]
var new_count = current_count + 1
packet_counts[src_ip] = new_count
if (new_count > 100) {
return XDP_DROP
}
return XDP_PASS
}
|} in
try
let (typed_ast, _) = parse_string_with_builtins program in
check bool "complete map program parsing" true (List.length typed_ast > 0)
with
| exn -> fail ("Complete map program parsing failed: " ^ Printexc.to_string exn)
(** Test map type checking *)
let test_map_type_checking () =
let program = {|
var test_map : hash<u32, u64>(1024)
@xdp fn test(ctx: *xdp_md) -> xdp_action {
var key = 42
var value = test_map[key]
test_map[key] = value + 1
return XDP_PASS
}
|} in
try
let (ast, _) = parse_string_with_builtins program in
check bool "map type checking" true (List.length ast > 0)
with
| exn -> fail ("Map type checking failed: " ^ Printexc.to_string exn)
(** Test map type validation *)
let test_map_type_validation () =
let test_cases = [
(* Valid: u32 key with u32 access *)
({|
var valid_map : hash<u32, u64>(1024)
@xdp fn test(ctx: *xdp_md) -> xdp_action {
var key: u32 = 42
var value = valid_map[key]
return XDP_PASS
}
|}, true);
(* Invalid: string key with u32 map *)
({|
var invalid_map : hash<u32, u64>(1024)
@xdp fn test(ctx: *xdp_md) -> xdp_action {
var key = "invalid"
var value = invalid_map[key]
return XDP_PASS
}
|}, false)
] in
let all_validation_passed = List.for_all (fun (code, should_succeed) ->
try
let (_ast, _) = parse_string_with_builtins code in
should_succeed
with
| _ -> not should_succeed
) test_cases in
check bool "all map type validation cases passed" true all_validation_passed
(** Test map identifier resolution *)
let test_map_identifier_resolution () =
let program = {|
var global_map : hash<u32, u64>(1024)
@xdp fn test(ctx: *xdp_md) -> xdp_action {
var value = global_map[42]
return XDP_PASS
}
|} in
try
let (typed_ast, _) = parse_string_with_builtins program in
check bool "map identifier resolution" true (List.length typed_ast > 0)
with
| _ ->
check bool "map identifier resolution" true false
(** Test IR generation for maps *)
let test_map_ir_generation () =
let program = {|
var test_map : hash<u32, u64>(1024)
@xdp fn test(ctx: *xdp_md) -> xdp_action {
var key = 42
var value = test_map[key]
test_map[key] = value + 1
return XDP_PASS
}
|} in
try
(* Follow the complete compiler pipeline *)
let (typed_ast, symbol_table) = parse_string_with_builtins program in
(* Test that IR generation completes without errors *)
let ir = Kernelscript.Ir_generator.generate_ir typed_ast symbol_table "test" in
check bool "map IR generation produces programs" true (List.length (Kernelscript.Ir.get_programs ir) > 0)
with
| exn -> fail ("Map IR generation failed: " ^ Printexc.to_string exn)
(** Test C code generation for maps *)
let test_map_c_generation () =
let program = {|
var packet_counter : hash<u32, u64>(1024)
@xdp fn test(ctx: *xdp_md) -> xdp_action {
var src_ip = 0x12345678
var count = packet_counter[src_ip]
packet_counter[src_ip] = count + 1
return XDP_PASS
}
|} in
try
(* Follow the complete compiler pipeline *)
let (typed_ast, symbol_table) = parse_string_with_builtins program in
(* Test that C code generation completes and produces expected output *)
let ir = Kernelscript.Ir_generator.generate_ir typed_ast symbol_table "test" in
let c_code = Kernelscript.Ebpf_c_codegen.generate_c_multi_program ir in
let contains_map_decl = contains_substr c_code "BPF_MAP_TYPE_HASH" &&
contains_substr c_code "packet_counter" in
let contains_lookup = contains_substr c_code "bpf_map_lookup_elem" in
let contains_update = contains_substr c_code "bpf_map_update_elem" in
check bool "C code generation test" true (contains_map_decl && contains_lookup && contains_update)
with
| exn ->
Printf.printf "Map C generation failed with: %s\n" (Printexc.to_string exn);
check bool "C code generation test" true false
(** Test different map types *)
let test_different_map_types () =
let map_types = [
("hash", "BPF_MAP_TYPE_HASH");
("array", "BPF_MAP_TYPE_ARRAY");
("percpu_hash", "BPF_MAP_TYPE_PERCPU_HASH");
("percpu_array", "BPF_MAP_TYPE_PERCPU_ARRAY");
("lru_hash", "BPF_MAP_TYPE_LRU_HASH");
] in
let all_map_types_work = List.for_all (fun (ks_type, c_type) ->
let program = Printf.sprintf {|
var test_map : %s<u32, u64>(1024)
@xdp fn test(ctx: *xdp_md) -> xdp_action {
var key = 42
var value = test_map[key]
return XDP_PASS
}
|} ks_type in
try
(* Follow the complete compiler pipeline *)
let (typed_ast, symbol_table) = parse_string_with_builtins program in
(* Test compilation and C code generation *)
let ir = Kernelscript.Ir_generator.generate_ir typed_ast symbol_table "test" in
let c_code = Kernelscript.Ebpf_c_codegen.generate_c_multi_program ir in
contains_substr c_code c_type
with
| _ -> false
) map_types in
check bool "all different map types work correctly" true all_map_types_work
let map_syntax_tests = [
"map_declaration_parsing", `Quick, test_map_declaration_parsing;
"blockless_map_declaration", `Quick, test_blockless_map_declaration;
"map_attributes_syntax", `Quick, test_map_attributes_syntax;
"comprehensive_map_syntax", `Quick, test_comprehensive_map_syntax;
"new_syntax_type_checking", `Quick, test_new_syntax_type_checking;
"new_syntax_ir_generation", `Quick, test_new_syntax_ir_generation;
"new_syntax_c_generation", `Quick, test_new_syntax_c_generation;
"new_syntax_error_cases", `Quick, test_new_syntax_error_cases;
"map_operations_parsing", `Quick, test_map_operations_parsing;
"complete_map_program_parsing", `Quick, test_complete_map_program_parsing;
"map_type_checking", `Quick, test_map_type_checking;
"map_type_validation", `Quick, test_map_type_validation;
"map_identifier_resolution", `Quick, test_map_identifier_resolution;
"map_ir_generation", `Quick, test_map_ir_generation;
"map_c_generation", `Quick, test_map_c_generation;
"different_map_types", `Quick, test_different_map_types;
]
let () =
run "KernelScript Map Syntax Tests" [
"map_syntax", map_syntax_tests;
]