Skip to content

Commit f9035c3

Browse files
committed
Update function parameter types from usize to size_t across multiple files to ensure consistency with the new type alias.
Signed-off-by: Cong Wang <cwang@multikernel.io>
1 parent 659849f commit f9035c3

File tree

9 files changed

+43
-27
lines changed

9 files changed

+43
-27
lines changed

SPEC.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ KernelScript automatically extracts kernel function signatures from BTF (BPF Typ
217217
```kernelscript
218218
// Function entrance probe (uses fprobe)
219219
@probe("sys_read")
220-
fn function_entrance(fd: u32, buf: *u8, count: usize) -> i32 {
220+
fn function_entrance(fd: u32, buf: *u8, count: size_t) -> i32 {
221221
// Direct access to function parameters with correct types
222222
// Compiler automatically extracts signature from BTF:
223223
// long sys_read(unsigned int fd, char __user *buf, size_t count)
@@ -2575,7 +2575,7 @@ pin var global_counters : array<u32, GlobalCounter>(256)
25752575
pin var event_stream : hash<u32, Event>(1024)
25762576
25772577
@probe("sys_read")
2578-
fn producer(fd: u32, buf: *u8, count: usize) -> i32 {
2578+
fn producer(fd: u32, buf: *u8, count: size_t) -> i32 {
25792579
var pid = bpf_get_current_pid_tgid() as u32
25802580
25812581
// Update global counter (accessible by other programs)
@@ -2595,7 +2595,7 @@ fn producer(fd: u32, buf: *u8, count: usize) -> i32 {
25952595
}
25962596
25972597
@probe("sys_write")
2598-
fn consumer(fd: u32, buf: *u8, count: usize) -> i32 {
2598+
fn consumer(fd: u32, buf: *u8, count: size_t) -> i32 {
25992599
var pid = bpf_get_current_pid_tgid() as u32
26002600
26012601
// Access global counter (same map as producer program)

examples/map_operations_demo.ks

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ fn event_logger(ctx: *trace_event_raw_sys_enter) -> i32 {
207207

208208
// Program 4: Sequential access pattern demonstration
209209
@probe("vfs_read")
210-
fn data_processor(file: *file, buf: *u8, count: usize, pos: *i64) -> i32 {
210+
fn data_processor(file: *file, buf: *u8, count: size_t, pos: *i64) -> i32 {
211211
// Sequential access pattern - will be detected and optimized
212212
for (i in 0..32) {
213213
var element = sequential_data[i]

src/btf_stubs.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,14 @@ static char* resolve_type_to_string(struct btf *btf, int type_id) {
272272
return strdup("*u8");
273273
}
274274

275+
if (kind == BTF_KIND_TYPEDEF) {
276+
/* Check for special typedef names like size_t */
277+
const char *typedef_name = btf__name_by_offset(btf, t->name_off);
278+
if (typedef_name && strcmp(typedef_name, "size_t") == 0) {
279+
return strdup("size_t"); /* Map size_t to KernelScript size_t type alias */
280+
}
281+
}
282+
275283
t = btf__type_by_id(btf, t->type);
276284
if (!t) break;
277285
kind = btf_kind(t);
@@ -408,6 +416,14 @@ value btf_resolve_type_stub(value btf_handle, value type_id) {
408416
CAMLreturn(caml_copy_string("*u8"));
409417
}
410418

419+
if (kind == BTF_KIND_TYPEDEF) {
420+
/* Check for special typedef names like size_t */
421+
const char *typedef_name = btf__name_by_offset(btf, t->name_off);
422+
if (typedef_name && strcmp(typedef_name, "size_t") == 0) {
423+
CAMLreturn(caml_copy_string("size_t")); /* Map size_t to KernelScript size_t type alias */
424+
}
425+
}
426+
411427
/* Follow the type chain */
412428
t = btf__type_by_id(btf, t->type);
413429
if (!t) break;

src/ir.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,6 @@ let rec ast_type_to_ir_type = function
716716
| Xdp_md -> IRContext XdpCtx
717717
| TracepointContext -> IRContext TracepointCtx
718718
| Xdp_action -> IRAction Xdp_actionType
719-
| UserType "usize" -> IRU64 (* usize maps to 64-bit unsigned integer *)
720719
| UserType name -> IRStruct (name, []) (* Resolved by type checker *)
721720
| Function (param_types, return_type) ->
722721
(* Function types are represented as proper function pointers *)

src/stdlib.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ let get_kernel_implementation name =
250250

251251
(** Builtin type definitions *)
252252
let builtin_types = [
253+
(* Standard C types as type aliases *)
254+
TypeDef (TypeAlias ("size_t", U64)); (* size_t maps to 64-bit unsigned integer *)
255+
253256
(* Kernel allocation flags enum *)
254257
TypeDef (EnumDef ("gfp_flag", [
255258
("GFP_KERNEL", Some 0x0001);

src/type_checker.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,6 @@ let rec resolve_user_type ctx = function
220220
| UserType "xdp_md" -> Xdp_md
221221
| UserType "xdp_action" -> Xdp_action
222222
| UserType "__sk_buff" -> Struct "__sk_buff"
223-
224-
| UserType "usize" -> U64 (* usize maps to 64-bit unsigned integer *)
225223
| UserType name ->
226224
(* Look up type alias in the context *)
227225
(try

tests/test_probe.ml

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@ module MockProbeBTF = struct
5454
let mock_kernel_functions = [
5555
{
5656
name = "sys_read";
57-
parameters = [("fd", "u32"); ("buf", "*u8"); ("count", "usize")];
57+
parameters = [("fd", "u32"); ("buf", "*u8"); ("count", "size_t")];
5858
return_type = "isize";
5959
};
6060
{
6161
name = "vfs_write";
62-
parameters = [("file", "*file"); ("buf", "*u8"); ("count", "usize"); ("pos", "*i64")];
62+
parameters = [("file", "*file"); ("buf", "*u8"); ("count", "size_t"); ("pos", "*i64")];
6363
return_type = "isize";
6464
};
6565
{
6666
name = "tcp_sendmsg";
67-
parameters = [("sk", "*sock"); ("msg", "*msghdr"); ("size", "usize")];
67+
parameters = [("sk", "*sock"); ("msg", "*msghdr"); ("size", "size_t")];
6868
return_type = "i32";
6969
};
7070
]
@@ -75,7 +75,7 @@ end
7575
(* 1. Parser Tests *)
7676
let test_probe_attribute_parsing _ =
7777
let source = "@probe(\"sys_read\")
78-
fn sys_read_handler(fd: u32, buf: *u8, count: usize) -> i32 {
78+
fn sys_read_handler(fd: u32, buf: *u8, count: size_t) -> i32 {
7979
return 0
8080
}" in
8181
let ast = parse_string source in
@@ -92,7 +92,7 @@ fn sys_read_handler(fd: u32, buf: *u8, count: usize) -> i32 {
9292

9393
let test_probe_multiple_parameters _ =
9494
let source = "@probe(\"vfs_write\")
95-
fn vfs_write_handler(file: *file, buf: *u8, count: usize, pos: *i64) -> i32 {
95+
fn vfs_write_handler(file: *file, buf: *u8, count: size_t, pos: *i64) -> i32 {
9696
return 0
9797
}" in
9898
let ast = parse_string source in
@@ -138,7 +138,7 @@ fn empty_target_handler(fd: u32) -> i32 {
138138
(* 2. Type Checking Tests *)
139139
let test_probe_type_checking _ =
140140
let source = "@probe(\"sys_read\")
141-
fn sys_read_handler(fd: u32, buf: *u8, count: usize) -> i32 {
141+
fn sys_read_handler(fd: u32, buf: *u8, count: size_t) -> i32 {
142142
return 0
143143
}" in
144144
let ast = parse_string source in
@@ -147,7 +147,7 @@ fn sys_read_handler(fd: u32, buf: *u8, count: usize) -> i32 {
147147

148148
let test_probe_parameter_validation _ =
149149
let source = "@probe(\"sys_read\")
150-
fn sys_read_handler(fd: u32, buf: *u8, count: usize) -> i32 {
150+
fn sys_read_handler(fd: u32, buf: *u8, count: size_t) -> i32 {
151151
return 0
152152
}" in
153153
let ast = parse_string source in
@@ -166,8 +166,8 @@ fn sys_read_handler(fd: u32, buf: *u8, count: usize) -> i32 {
166166
(match fd_type with U32 -> true | _ -> false);
167167
check bool "Second parameter type should be Pointer" true
168168
(match buf_type with Pointer _ -> true | _ -> false);
169-
check bool "Third parameter type should be UserType usize" true
170-
(match count_type with UserType "usize" -> true | _ -> false)
169+
check bool "Third parameter type should be UserType size_t" true
170+
(match count_type with UserType "size_t" -> true | _ -> false)
171171
| _ -> fail "Expected exactly three parameters")
172172
| _ -> fail "Expected AttributedFunction"
173173

@@ -232,7 +232,7 @@ fn invalid_handler(ctx: *pt_regs) -> i32 {
232232
(* 3. IR Generation Tests *)
233233
let test_probe_ir_generation _ =
234234
let source = "@probe(\"sys_read\")
235-
fn sys_read_handler(fd: u32, buf: *u8, count: usize) -> i32 {
235+
fn sys_read_handler(fd: u32, buf: *u8, count: size_t) -> i32 {
236236
return 0
237237
}" in
238238
let ast = parse_string source in
@@ -247,7 +247,7 @@ fn sys_read_handler(fd: u32, buf: *u8, count: usize) -> i32 {
247247

248248
let test_probe_complex_parameters _ =
249249
let source = "@probe(\"tcp_sendmsg\")
250-
fn tcp_sendmsg_handler(sk: *sock, msg: *msghdr, size: usize) -> i32 {
250+
fn tcp_sendmsg_handler(sk: *sock, msg: *msghdr, size: size_t) -> i32 {
251251
return 0
252252
}" in
253253
let ast = parse_string source in
@@ -262,7 +262,7 @@ fn tcp_sendmsg_handler(sk: *sock, msg: *msghdr, size: usize) -> i32 {
262262

263263
let test_probe_function_signature_validation _ =
264264
let source = "@probe(\"vfs_write\")
265-
fn vfs_write_handler(file: *file, buf: *u8, count: usize, pos: *i64) -> i32 {
265+
fn vfs_write_handler(file: *file, buf: *u8, count: size_t, pos: *i64) -> i32 {
266266
return 0
267267
}" in
268268
let ast = parse_string source in
@@ -279,7 +279,7 @@ fn vfs_write_handler(file: *file, buf: *u8, count: usize, pos: *i64) -> i32 {
279279
(* 4. Code Generation Tests *)
280280
let test_fprobe_section_name_generation _ =
281281
let source = "@probe(\"sys_read\")
282-
fn sys_read_handler(fd: u32, buf: *u8, count: usize) -> i32 {
282+
fn sys_read_handler(fd: u32, buf: *u8, count: size_t) -> i32 {
283283
return 0
284284
}" in
285285
let ast = parse_string source in
@@ -315,7 +315,7 @@ fn vfs_read_handler(ctx: *pt_regs) -> i32 {
315315

316316
let test_fprobe_complex_section_generation _ =
317317
let source = "@probe(\"tcp_sendmsg\")
318-
fn tcp_sendmsg_handler(sk: *sock, msg: *msghdr, size: usize) -> i32 {
318+
fn tcp_sendmsg_handler(sk: *sock, msg: *msghdr, size: size_t) -> i32 {
319319
return 0
320320
}" in
321321
let ast = parse_string source in
@@ -338,7 +338,7 @@ fn tcp_sendmsg_handler(sk: *sock, msg: *msghdr, size: usize) -> i32 {
338338

339339
let test_fprobe_ebpf_codegen _ =
340340
let source = "@probe(\"sys_read\")
341-
fn sys_read_handler(fd: u32, buf: *u8, count: usize) -> i32 {
341+
fn sys_read_handler(fd: u32, buf: *u8, count: size_t) -> i32 {
342342
return 0
343343
}" in
344344
let ast = parse_string source in
@@ -379,7 +379,7 @@ fn vfs_read_handler(ctx: *pt_regs) -> i32 {
379379

380380
let test_fprobe_includes_generation _ =
381381
let source = "@probe(\"sys_read\")
382-
fn sys_read_handler(fd: u32, buf: *u8, count: usize) -> i32 {
382+
fn sys_read_handler(fd: u32, buf: *u8, count: size_t) -> i32 {
383383
return 0
384384
}" in
385385
let ast = parse_string source in
@@ -541,7 +541,7 @@ fn sys_open_handler(ctx: *pt_regs) -> i32 {
541541

542542
let test_fprobe_network_function _ =
543543
let source = "@probe(\"tcp_sendmsg\")
544-
fn tcp_sendmsg_handler(sk: *sock, msg: *msghdr, size: usize) -> i32 {
544+
fn tcp_sendmsg_handler(sk: *sock, msg: *msghdr, size: size_t) -> i32 {
545545
return 0
546546
}" in
547547
let ast = parse_string source in
@@ -559,7 +559,7 @@ fn tcp_sendmsg_handler(sk: *sock, msg: *msghdr, size: usize) -> i32 {
559559

560560
let test_probe_multiple_functions _ =
561561
let source = "@probe(\"sys_read\")
562-
fn sys_read_handler(fd: u32, buf: *u8, count: usize) -> i32 {
562+
fn sys_read_handler(fd: u32, buf: *u8, count: size_t) -> i32 {
563563
return 0
564564
}
565565

tests/test_program_ref.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fn main() -> i32 {
4747
(** Test program reference with different program types *)
4848
let test_different_program_types () =
4949
let program_text = {|
50-
@probe("sys_read") fn kprobe_tracer(fd: u32, buf: *u8, count: usize) -> i32 {
50+
@probe("sys_read") fn kprobe_tracer(fd: u32, buf: *u8, count: size_t) -> i32 {
5151
return 0
5252
}
5353

tests/test_ringbuf.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ var security_events : ringbuf<SecurityEvent>(8192)
502502
return XDP_PASS
503503
}
504504

505-
@probe("sys_read") fn security_prog(fd: u32, buf: *u8, count: usize) -> i32 {
505+
@probe("sys_read") fn security_prog(fd: u32, buf: *u8, count: size_t) -> i32 {
506506
var sec_event = security_events.reserve()
507507
if (sec_event != null) {
508508
security_events.submit(sec_event)

0 commit comments

Comments
 (0)