-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtest_struct_initialization.ml
More file actions
370 lines (314 loc) · 11.6 KB
/
Copy pathtest_struct_initialization.ml
File metadata and controls
370 lines (314 loc) · 11.6 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
(*
* 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 Alcotest
open Kernelscript.Ast
open Kernelscript.Type_checker
open Kernelscript.Ir_generator
open Kernelscript.Ebpf_c_codegen
open Kernelscript.Ir
(** Helper functions *)
let dummy_pos = { line = 1; column = 1; filename = "test_struct_init.ks" }
let parse_string s =
let lexbuf = Lexing.from_string s in
Kernelscript.Parser.program Kernelscript.Lexer.token lexbuf
(** Helper 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
(** Helper to generate IR and C code from program text *)
let generate_c_from_program program_text program_name =
(* Initialize context codegens first *)
Kernelscript_context.Xdp_codegen.register ();
Kernelscript_context.Tc_codegen.register ();
let ast = parse_string program_text in
let symbol_table = Test_utils.Helpers.create_test_symbol_table ast in
let (annotated_ast, _) = type_check_and_annotate_ast ~symbol_table:(Some symbol_table) ast in
let ir_multi_prog = generate_ir annotated_ast symbol_table program_name in
(* Generate C code *)
let c_code = generate_c_multi_program ir_multi_prog in
c_code
(** Test 1: Basic struct initialization with simple types *)
let test_basic_struct_initialization () =
let program_text = {|
struct PacketInfo {
size: u64,
action: u32,
}
@xdp fn packet_filter(ctx: *xdp_md) -> xdp_action {
var packet_size = ctx->data_end - ctx->data
var info = PacketInfo {
size: packet_size,
action: 2,
}
if (info.size > 1500) {
return 1
}
return info.action
}
fn main() -> i32 {
return 0
}
|} in
try
let c_code = generate_c_from_program program_text "packet_filter" in
(* Verify struct definition is generated *)
check bool "struct definition generated" true (contains_substr c_code "struct PacketInfo");
check bool "size field defined" true (contains_substr c_code "__u64 size");
check bool "action field defined" true (contains_substr c_code "__u32 action");
(* Verify struct initialization syntax *)
check bool "struct literal assignment found" true (contains_substr c_code "(struct PacketInfo){");
check bool "field initialization syntax" true (contains_substr c_code ".size =");
check bool "action field initialization" true (contains_substr c_code ".action = 2");
(* Verify field access works *)
check bool "field access generated" true (contains_substr c_code ".size");
check bool "return field access" true (contains_substr c_code ".action")
with
| exn -> fail ("Basic struct initialization test failed: " ^ Printexc.to_string exn)
(** Test 2: Struct initialization with different data types *)
let test_struct_with_different_types () =
let program_text = {|
struct ConfigData {
mode: u64,
flags: u32,
}
@xdp fn config_filter(ctx: *xdp_md) -> xdp_action {
var packet_size = ctx->data_end - ctx->data
var info = ConfigData {
mode: packet_size,
flags: 42,
}
if (info.mode > 1500) {
return 1
}
return info.flags
}
fn main() -> i32 {
return 0
}
|} in
try
let c_code = generate_c_from_program program_text "config_filter" in
(* Verify all data types are correctly generated *)
check bool "struct ConfigData defined" true (contains_substr c_code "struct ConfigData");
check bool "u64 mode field defined" true (contains_substr c_code "__u64 mode");
check bool "u32 flags field defined" true (contains_substr c_code "__u32 flags");
(* Verify struct initialization syntax *)
check bool "struct literal syntax" true (contains_substr c_code "(struct ConfigData){");
check bool "flags literal assignment" true (contains_substr c_code ".flags = 42")
with
| exn -> fail ("Different types struct test failed: " ^ Printexc.to_string exn)
(** Test 3: Struct initialization with variables *)
let test_struct_initialization_with_variables () =
let program_text = {|
struct VariableTest {
size: u64,
action: u32,
}
@xdp fn variable_test(ctx: *xdp_md) -> xdp_action {
var packet_size = ctx->data_end - ctx->data
var info = VariableTest {
size: packet_size,
action: 3,
}
if (info.size > 1500) {
return 1
}
return info.action
}
fn main() -> i32 {
return 0
}
|} in
try
let c_code = generate_c_from_program program_text "variable_test" in
(* Verify struct definition *)
check bool "VariableTest struct defined" true (contains_substr c_code "struct VariableTest");
check bool "__u64 size field" true (contains_substr c_code "__u64 size");
check bool "__u32 action field" true (contains_substr c_code "__u32 action");
(* Verify struct compound literal syntax *)
check bool "compound literal syntax" true (contains_substr c_code "(struct VariableTest){");
check bool "literal field assignment" true (contains_substr c_code ".action = 3")
with
| exn -> fail ("Variable struct initialization test failed: " ^ Printexc.to_string exn)
(** Test 4: Multiple struct definitions and initializations *)
let test_multiple_struct_definitions () =
let program_text = {|
struct Header {
version: u8,
flags: u8,
}
struct Payload {
size: u32,
data_type: u16,
}
@xdp fn multi_struct(ctx: *xdp_md) -> xdp_action {
var hdr = Header {
version: 1,
flags: 0,
}
var payload = Payload {
size: 1024,
data_type: 42,
}
if (hdr.version == 1 && payload.size > 0) {
return 2
}
return 1
}
fn main() -> i32 {
return 0
}
|} in
try
let c_code = generate_c_from_program program_text "multi_struct" in
(* Verify both struct definitions are generated *)
check bool "Header struct defined" true (contains_substr c_code "struct Header");
check bool "Payload struct defined" true (contains_substr c_code "struct Payload");
(* Verify both struct initializations *)
check bool "Header initialization" true (contains_substr c_code "(struct Header){");
check bool "Payload initialization" true (contains_substr c_code "(struct Payload){");
(* Verify field assignments for both structs *)
check bool "Header version assignment" true (contains_substr c_code ".version = 1");
check bool "Header flags assignment" true (contains_substr c_code ".flags = 0");
check bool "Payload size assignment" true (contains_substr c_code ".size = 1024");
check bool "Payload data_type assignment" true (contains_substr c_code ".data_type = 42")
with
| exn -> fail ("Multiple struct definitions test failed: " ^ Printexc.to_string exn)
(** Test 5: Nested struct usage (assignment and field access) *)
let test_nested_struct_usage () =
let program_text = {|
struct FieldTest {
size: u64,
action: u32,
}
@xdp fn field_test(ctx: *xdp_md) -> xdp_action {
var packet_size = ctx->data_end - ctx->data
var info = FieldTest {
size: packet_size,
action: 4,
}
if (info.size > 1500) {
return 1
}
return info.action
}
fn main() -> i32 {
return 0
}
|} in
try
let c_code = generate_c_from_program program_text "field_test" in
(* Verify struct definition *)
check bool "FieldTest struct defined" true (contains_substr c_code "struct FieldTest");
check bool "__u64 size field" true (contains_substr c_code "__u64 size");
check bool "__u32 action field" true (contains_substr c_code "__u32 action");
(* Verify struct initialization *)
check bool "struct literal syntax" true (contains_substr c_code "(struct FieldTest){");
check bool "field initialization" true (contains_substr c_code ".action = 4")
with
| exn -> fail ("Nested struct usage test failed: " ^ Printexc.to_string exn)
(** Test 6: IR generation verification for struct literals *)
let test_ir_struct_literal_generation () =
let program_text = {|
struct TestStruct {
field1: u32,
field2: u64,
}
@xdp fn test_ir(ctx: *xdp_md) -> xdp_action {
var test_obj = TestStruct {
field1: 42,
field2: 1000,
}
return test_obj.field1
}
fn main() -> i32 {
return 0
}
|} in
try
let ast = parse_string program_text in
let symbol_table = Test_utils.Helpers.create_test_symbol_table ast in
let (annotated_ast, _) = type_check_and_annotate_ast ~symbol_table:(Some symbol_table) ast in
let ir_multi_prog = generate_ir annotated_ast symbol_table "test_ir" in
(* Extract the main function from IR *)
let test_program = List.find (fun prog -> prog.name = "test_ir") (get_programs ir_multi_prog) in
let main_func = test_program.entry_function in
(* Look for IRStructLiteral in the instructions *)
let has_struct_literal = ref false in
let check_instruction instr =
match instr.instr_desc with
| IRAssign (_, expr) ->
(match expr.expr_desc with
| IRStructLiteral (struct_name, _) ->
if struct_name = "TestStruct" then has_struct_literal := true
| _ -> ())
| IRVariableDecl (_, _, Some expr) ->
(match expr.expr_desc with
| IRStructLiteral (struct_name, _) ->
if struct_name = "TestStruct" then has_struct_literal := true
| _ -> ())
| _ -> ()
in
List.iter (fun block ->
List.iter check_instruction block.instructions
) main_func.basic_blocks;
check bool "IRStructLiteral generated in IR" true !has_struct_literal
with
| exn -> fail ("IR struct literal generation test failed: " ^ Printexc.to_string exn)
(** Test 7: Struct initialization in function parameters and returns *)
let test_struct_as_function_parameter () =
let program_text = {|
struct Parameter {
size: u64,
action: u32,
}
@xdp fn param_test(ctx: *xdp_md) -> xdp_action {
var packet_size = ctx->data_end - ctx->data
var info = Parameter {
size: packet_size,
action: 5,
}
if (info.size > 1500) {
return 1
}
return info.action
}
fn main() -> i32 {
return 0
}
|} in
try
let c_code = generate_c_from_program program_text "param_test" in
(* Verify struct definition *)
check bool "Parameter struct defined" true (contains_substr c_code "struct Parameter");
check bool "__u64 size field" true (contains_substr c_code "__u64 size");
check bool "__u32 action field" true (contains_substr c_code "__u32 action");
(* Verify struct initialization and field access *)
check bool "struct initialization" true (contains_substr c_code "(struct Parameter){");
check bool "action field assignment" true (contains_substr c_code ".action = 5")
with
| exn -> fail ("Struct as function parameter test failed: " ^ Printexc.to_string exn)
(** All struct initialization tests *)
let tests = [
"test_basic_struct_initialization", `Quick, test_basic_struct_initialization;
"test_multiple_struct_definitions", `Quick, test_multiple_struct_definitions;
"test_ir_struct_literal_generation", `Quick, test_ir_struct_literal_generation;
]
let () = Alcotest.run "Struct Initialization Tests" [
"struct_initialization", tests
]