Skip to content

Commit 1877e97

Browse files
committed
Reject nonzero perf attach flags
Signed-off-by: Cong Wang <cwang@multikernel.io>
1 parent d986963 commit 1877e97

5 files changed

Lines changed: 13 additions & 4 deletions

File tree

BUILTINS.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ fn main() -> i32 {
9999
- Perf event form:
100100
- `handle`: Program handle returned from `load()`
101101
- `opts`: `perf_options` value — only `perf_type` and `perf_config` are required; all other fields have defaults
102-
- `flags`: Reserved (pass `0`)
102+
- `flags`: Must be `0` for perf attaches; nonzero values are rejected
103103

104104
**Return Value:**
105105
- Standard form returns `0` on success and an error code on failure
@@ -114,7 +114,7 @@ if (result != 0) {
114114
}
115115
116116
// Minimal perf attach — all non-perf_type/perf_config fields use defaults:
117-
// pid=-1 (all procs), cpu=0, period=1_000_000, wakeup=1, flags=false
117+
// pid=-1 (all procs), cpu=0, period=1_000_000, wakeup=1; perf attach flags must be 0
118118
var perf_prog = load(on_branch_miss)
119119
var perf_att = attach(perf_prog, perf_options { perf_type: perf_type_hardware, perf_config: branch_misses }, 0)
120120
var count = read(perf_att)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ fn main() -> i32 {
307307
var prog = load(on_branch_miss)
308308
309309
// Minimal form — defaults: pid=-1 (all procs), cpu=0,
310-
// period=1_000_000, wakeup=1, all flags=false
310+
// period=1_000_000, wakeup=1; perf attach flags must be 0
311311
var att = attach(prog, perf_options { perf_type: perf_type_hardware, perf_config: branch_misses }, 0)
312312
var count = read(att)
313313
print("branch misses: %lld", count)

SPEC.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ For event families with a richer config space, such as `perf_type_hw_cache`, pro
555555

556556
**Compiler implementation:**
557557
- Detects `attach(prog, perf_options_value, flags)` (three-argument form with `perf_options` second arg) and routes to `ks_attach_perf_event`
558+
- Requires perf attach `flags` to be `0`; nonzero values are rejected instead of being silently ignored
558559
- Returns a first-class `PerfAttachment` value for perf attaches so one program can hold multiple live counters
559560
- `PerfAttachment` carries `perf_fd` plus an internal generation token; `read(attachment)` avoids global attachment-list scans and rejects copied handles after detach
560561
- Exposes omitted `perf_options` fields as language-level defaults (partial struct literal)

src/userspace_codegen.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4850,7 +4850,11 @@ PerfAttachment ks_attach_perf_event(int prog_fd, ks_perf_options opts, int flags
48504850
.link_id = -1,
48514851
.prog_fd = prog_fd,
48524852
};
4853-
(void)flags; /* reserved for future use */
4853+
4854+
if (flags != 0) {
4855+
fprintf(stderr, "ks_attach_perf_event: perf attach flags must be 0, got %d\n", flags);
4856+
return attachment;
4857+
}
48544858
48554859
if (prog_fd < 0) {
48564860
fprintf(stderr, "Invalid program file descriptor: %d\n", prog_fd);

tests/test_perf_event_attach.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,10 @@ let test_perf_attach_event_function_generated () =
332332
(contains_substr code "find_prog_by_fd");
333333
check bool "perf attach rejects wrong program type at runtime" true
334334
(contains_substr code "is not a @perf_event program");
335+
check bool "perf attach rejects nonzero flags" true
336+
(contains_substr code "perf attach flags must be 0");
337+
check bool "perf attach no longer ignores flags" false
338+
(contains_substr code "(void)flags");
335339
check bool "perf attach returns PerfAttachment" true
336340
(contains_substr code "PerfAttachment ks_attach_perf_event");
337341
check bool "attachment struct typedef emitted" true

0 commit comments

Comments
 (0)