FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
kernelscript/tests/test_userspace_struct_flexibility.ml at main · multikernel/kernelscript · GitHub
multikernel
/
kernelscript
Public
Notifications
You must be signed in to change notification settings
Fork
24
Star
508
Code
Issues
2
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
kernelscript
/
tests
/
test_userspace_struct_flexibility.ml
Copy path
More file actions
More file actions
Latest commit
History
History
History
283 lines (232 loc) · 9.88 KB
Breadcrumbs
kernelscript
/
tests
/
test_userspace_struct_flexibility.ml
Copy path
File metadata and controls
283 lines (232 loc) · 9.88 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
(*
* 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.Parse
(*
* Helper function to check if generated code contains a pattern
*)
let
contains_pattern
code
pattern
=
try
let
regex
=
Str.
regexp pattern
in
ignore (
Str.
search_forward regex code
0
);
true
with
Not_found
->
false
(*
* Helper function to generate userspace code from a program
*)
let
generate_userspace_code_from_program
program_text
filename
=
let
ast
=
parse_string program_text
in
let
symbol_table
=
Kernelscript.Symbol_table.
build_symbol_table ast
in
let
(annotated_ast, _typed_programs)
=
Kernelscript.Type_checker.
type_check_and_annotate_ast ast
in
let
ir
=
Kernelscript.Ir_generator.
generate_ir annotated_ast symbol_table filename
in
let
temp_dir
=
Filename.
temp_file
"
test_userspace_struct
"
"
"
in
Unix.
unlink temp_dir;
Unix.
mkdir temp_dir
0o755
;
let
_output_file
=
Kernelscript.Userspace_codegen.
generate_userspace_code_from_ir
ir
~output_dir:
temp_dir filename
in
let
generated_file
=
Filename.
concat temp_dir (filename
^
"
.c
"
)
in
if
Sys.
file_exists generated_file
then
(
let
ic
=
open_in generated_file
in
let
content
=
really_input_string ic (in_channel_length ic)
in
close_in ic;
(*
Cleanup
*)
Unix.
unlink generated_file;
Unix.
rmdir temp_dir;
content
)
else
(
failwith
"
Failed to generate userspace code file
"
)
(*
* Test 1: Ensure global function main works with custom struct name "ServerConfig"
*)
let
test_global_function_main_with_different_struct_name
()
=
let
program_text
=
{
|
var
server_stats
:
hash
<
u32,
u64
>
(
32
)
@
xdp
fn
server_monitor
(
ctx
:
*
xdp_md) ->
xdp_action
{
return
2
}
struct
ServerConfig
{
max_connections
:
u64
,
enable_logging
:
u32
,
port_number
:
u32
,
}
fn main(settings:
ServerConfig
) -> i32 {
if
(settings.enable_logging
>
0
) {
return settings.port_number
}
return
0
}
|
}
in
try
let
result
=
generate_userspace_code_from_program program_text
"
test_server_config
"
in
(*
Check struct definition uses ServerConfig
*)
check
bool
"
struct ServerConfig defined
"
true
(contains_pattern result
"
struct ServerConfig
"
);
(*
Check function signature uses ServerConfig
*)
check
bool
"
parse_arguments returns struct ServerConfig
"
true
(contains_pattern result
"
struct ServerConfig parse_arguments
"
);
(*
Check variable uses settings parameter name
*)
check
bool
"
variable declared as struct ServerConfig settings
"
true
(contains_pattern result
"
struct ServerConfig settings
"
);
(*
Check getopt options include ServerConfig fields
*)
check
bool
"
max_connections option exists
"
true
(contains_pattern result
"
\"
max_connections
\"
"
);
check
bool
"
enable_logging option exists
"
true
(contains_pattern result
"
\"
enable_logging
\"
"
);
check
bool
"
port_number option exists
"
true
(contains_pattern result
"
\"
port_number
\"
"
);
(*
Check field access uses settings parameter name
*)
check
bool
"
field access uses settings parameter name
"
true
(contains_pattern result
"
settings
\\
.enable_logging
"
);
check
bool
"
field assignment uses settings parameter name
"
true
(contains_pattern result
"
settings
\\
.max_connections
"
);
(*
Ensure NO hardcoded "Args", "args", "config", or "MyConfiguration"
*)
check
bool
"
no hardcoded Args struct
"
false
(contains_pattern result
"
struct Args
"
);
check
bool
"
no hardcoded MyConfiguration struct
"
false
(contains_pattern result
"
struct MyConfiguration
"
);
check
bool
"
no hardcoded args variable
"
false
(contains_pattern result
"
Args args
"
);
check
bool
"
no hardcoded config variable
"
false
(contains_pattern result
"
MyConfiguration config
"
);
with
|
exn
-> fail (
"
Test failed with exception:
"
^
Printexc.
to_string
exn
)
(*
* Test 2: Ensure global function main works with single-letter struct name
*)
let
test_global_function_main_with_minimal_struct_name
()
=
let
program_text
=
{
|
var
minimal_map
:
hash
<
u32,
u64
>
(
8
)
@
xdp
fn
minimal_prog
(
ctx
:
*
xdp_md) ->
xdp_action
{
return
2
}
struct
X
{
a
:
u32
,
b
:
u32
,
}
fn main(x:
X
) -> i32 {
return x.a
+
x.b
}
|
}
in
try
let
result
=
generate_userspace_code_from_program program_text
"
test_minimal_struct
"
in
(*
Check struct definition uses X
*)
check
bool
"
struct X defined
"
true
(contains_pattern result
"
struct X
"
);
(*
Check function signature uses X
*)
check
bool
"
parse_arguments returns struct X
"
true
(contains_pattern result
"
struct X parse_arguments
"
);
(*
Check variable uses x parameter name
*)
check
bool
"
variable declared as struct X x
"
true
(contains_pattern result
"
struct X x
"
);
(*
Check getopt options include X fields
*)
check
bool
"
field a option exists
"
true
(contains_pattern result
"
\"
a
\"
"
);
check
bool
"
field b option exists
"
true
(contains_pattern result
"
\"
b
\"
"
);
(*
Check field access uses x parameter name
*)
check
bool
"
field access uses x.a
"
true
(contains_pattern result
"
x
\\
.a
"
);
check
bool
"
field access uses x.b
"
true
(contains_pattern result
"
x
\\
.b
"
);
with
|
exn
-> fail (
"
Test failed with exception:
"
^
Printexc.
to_string
exn
)
(*
* Test 3: Ensure compilation and validation still works with custom struct names
*)
let
test_global_function_main_validation_with_custom_struct
()
=
let
program_text
=
{
|
var
validation_map
:
hash
<
u32,
u64
>
(
16
)
@
xdp
fn
validation_prog
(
ctx
:
*
xdp_md) ->
xdp_action
{
return
2
}
struct
CustomArgs
{
debug_level
:
u32
,
output_file
:
u32
,
}
fn main(custom_args:
CustomArgs
) -> i32 {
if
(custom_args.debug_level
>
0
) {
return
1
}
return
0
}
|
}
in
try
(*
Test that parsing, type checking, and IR generation all work
*)
let
ast
=
parse_string program_text
in
let
symbol_table
=
Kernelscript.Symbol_table.
build_symbol_table ast
in
let
(annotated_ast, _typed_programs)
=
Kernelscript.Type_checker.
type_check_and_annotate_ast ast
in
let
_ir
=
Kernelscript.Ir_generator.
generate_ir annotated_ast symbol_table
"
test_validation
"
in
()
;
with
|
exn
-> fail (
"
Validation failed with custom struct:
"
^
Printexc.
to_string
exn
)
(*
* Test 4: Verify argument parsing and assignment to IR variables works correctly
*)
let
test_argument_parsing_assignment_bug_fix
()
=
let
program_text
=
{
|
@
xdp fn packet_filter(ctx:
*xdp_md
) -> xdp_action {
return
2
}
struct
Args
{
enable_debug
:
u32
,
interface
:
str
(
16
)
}
fn main(args:
Args
) -> i32 {
if
(args.enable_debug
>
0
) {
print(
"
Debug mode enabled
"
)
}
var prog
=
load(packet_filter)
attach(prog, args.interface,
0
)
return
0
}
|
}
in
try
let
result
=
generate_userspace_code_from_program program_text
"
test_arg_assignment
"
in
(*
1. Check that arguments are parsed correctly
*)
check
bool
"
parse_arguments generates struct Args
"
true
(contains_pattern result
"
struct Args parse_arguments
"
);
check
bool
"
args variable declared correctly
"
true
(contains_pattern result
"
struct Args args = parse_arguments
"
);
(*
2. Check that function parameters are used directly (no unnecessary copying)
*)
check
bool
"
no unnecessary struct assignment to var_0
"
false
(contains_pattern result
"
var_0 = args;
"
);
(*
3. Check that function parameter fields are used directly
*)
check
bool
"
args.interface used for attach
"
true
(contains_pattern result
"
args
\\
.interface
"
);
check
bool
"
args.enable_debug used directly
"
true
(contains_pattern result
"
args
\\
.enable_debug
"
);
(*
4. Check that string argument parsing uses strncpy (not atoi)
*)
check
bool
"
interface uses strncpy not atoi
"
true
(contains_pattern result
"
strncpy(args
\\
.interface, optarg
"
);
check
bool
"
interface does not use atoi
"
false
(contains_pattern result
"
args
\\
.interface.*atoi
"
);
(*
5. Check that no unnecessary assignment bridge exists (cleaner approach)
*)
check
bool
"
no assignment bridge from args to var_0
"
false
(contains_pattern result
"
// Copy parsed arguments to function variable
"
);
(*
6. Ensure function parameters are used appropriately
*)
let
args_usage_count
=
let
rec
count_matches
pattern
text
start
acc
=
try
let
pos
=
Str.
search_forward (
Str.
regexp pattern) text start
in
count_matches pattern text (pos
+
1
) (acc
+
1
)
with
Not_found
->
acc
in
count_matches
"
args
\\
.
"
result
0
0
in
check
bool
"
args parameter is used at least twice (enable_debug and interface)
"
true
(args_usage_count
>
=
2
);
with
|
exn
-> fail (
"
Argument parsing assignment test failed:
"
^
Printexc.
to_string
exn
)
(*
* All global function struct flexibility tests
*)
let
global_function_struct_flexibility_tests
=
[
"
global_function_main_with_different_struct_name
"
,
`Quick
, test_global_function_main_with_different_struct_name;
"
global_function_main_with_minimal_struct_name
"
,
`Quick
, test_global_function_main_with_minimal_struct_name;
"
global_function_main_validation_with_custom_struct
"
,
`Quick
, test_global_function_main_validation_with_custom_struct;
"
argument_parsing_assignment_bug_fix
"
,
`Quick
, test_argument_parsing_assignment_bug_fix;
]
let
()
=
run
"
KernelScript Global Function Struct Flexibility Tests
"
[
"
global_function_struct_flexibility
"
, global_function_struct_flexibility_tests;
]
Back
|
FazBrowse Home
|
New Git URL