nativeview-codegen 0.1.0-rc.1

Multi-language ABI, policy, flat-op codec, and hybrid Direct-FFI code generator for Native View Engine.
Documentation
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
//! Zero-allocation `CountingAlloc` guards (with positive control probes) for generated Direct-First
//! Hybrid FFI clients (`Direct*Client`), zero-waste `wire::Wire*Client` + `dispatch_*_wire`, and
//! `> 512 B` heap-spill exact-1-allocation proofs.
//!
//! Compiles `direct_services.rs` -> `direct_services_hybrid.generated.rs` with `rustc -O` and
//! executes a standalone binary that installs `nativeview_test_support`'s counting global allocator, so
//! measurements observe real optimized release codegen with zero test-harness allocator noise.

mod common;

use std::path::PathBuf;
use std::process::Command;

use common::rustc::Rustc;
use nativeview_codegen::{HybridTarget, generate_hybrid_target, parse_hybrid_rust_spec};

const DIRECT_SERVICES_SPEC: &str = include_str!("fixtures/direct_services.rs");
const BATCH_SERVICES_SPEC: &str = include_str!("fixtures/batch_services.rs");

fn temp_test_dir(tag: &str) -> PathBuf {
    let dir = std::env::temp_dir().join(format!(
        "nv_direct_alloc_guards_{tag}_{}",
        std::process::id()
    ));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).expect("create temp test dir");
    dir
}

const ALLOC_GUARD_MAIN_RS: &str = r#"
use std::hint::black_box;

use direct_services::{
    ClipboardBackend, ColorScheme, DirectClipboardClient, DirectSystemThemeClient,
    MotionPreference, SystemThemeBackend, ThemeSnapshot, box_clipboard_host_ops,
    box_system_theme_host_ops,
    wire::{
        WireClipboardClient, WireSystemThemeClient, dispatch_clipboard_wire,
        dispatch_system_theme_wire,
    },
};
use nativeview_abi::codec::{ByteTransport, TransportDoneFn, TransportError};
use nativeview_abi::{NvModStatus, SurfaceId, SurfaceToken};
use nativeview_test_support::count_allocs;

nativeview_test_support::install_counting_allocator!();

struct ZeroAllocTheme;
impl SystemThemeBackend for ZeroAllocTheme {
    fn current(&self) -> Result<ThemeSnapshot, NvModStatus> {
        Ok(ThemeSnapshot {
            scheme: ColorScheme::Dark,
            high_contrast: true,
            motion: MotionPreference::Reduced,
            pad: 0,
            accent_rgba: 0x1122_3344,
            font_scale: 1.5,
        })
    }
    fn set_preferred_scheme(&self, _scheme: ColorScheme) -> Result<(), NvModStatus> {
        Ok(())
    }
}

struct ZeroAllocClip;
impl ClipboardBackend for ZeroAllocClip {
    fn read_text(&self) -> Result<Option<String>, NvModStatus> { Ok(None) }
    fn write_text(&self, _text: &str) -> Result<(), NvModStatus> { Ok(()) }
    fn read_bytes(&self, _mime: &str) -> Result<Option<Vec<u8>>, NvModStatus> { Ok(None) }
    fn write_bytes(&self, _mime: &str, _data: &[u8]) -> Result<(), NvModStatus> { Ok(()) }
    fn clear(&self) -> Result<(), NvModStatus> { Ok(()) }
}

struct DirectDispatchTransport {
    theme: ZeroAllocTheme,
    clip: ZeroAllocClip,
}

impl ByteTransport for DirectDispatchTransport {
    fn call_sync(
        &self,
        service_id: u32,
        method_id: u32,
        req: &[u8],
        on_reply: &mut dyn FnMut(&[u8]),
    ) -> Result<(), TransportError> {
        if service_id == direct_services::NV_MOD_SYSTEM_THEME_SERVICE_ID.raw() {
            dispatch_system_theme_wire(&self.theme, method_id, req, on_reply)
        } else if service_id == direct_services::NV_MOD_CLIPBOARD_SERVICE_ID.raw() {
            dispatch_clipboard_wire(&self.clip, method_id, req, on_reply)
        } else {
            Err(TransportError::Unsupported)
        }
    }

    fn call_async(
        &self,
        _service_id: u32,
        _method_id: u32,
        _req: &[u8],
        done: TransportDoneFn,
    ) {
        done(Err(TransportError::Unsupported));
    }
}

fn main() {
    // 1. Positive control probe: CountingAlloc MUST observe Box::new(1u8).
    let (_, probe) = count_allocs(|| black_box(Box::new(1u8)));
    assert!(probe >= 1, "CountingAlloc positive control failed (got {probe})");

    // 2. Direct C-ABI `impl = "host"` hot calls = 0 heap allocations.
    let surface = SurfaceToken::new(SurfaceId::new(1, 1), 1);
    let owned_theme = box_system_theme_host_ops(std::sync::Arc::new(ZeroAllocTheme));
    let owned_clip = box_clipboard_host_ops(std::sync::Arc::new(ZeroAllocClip));
    let direct_theme = unsafe {
        DirectSystemThemeClient::from_static_ops(owned_theme.as_ptr().cast(), surface)
    }
    .expect("direct_theme");
    let direct_clip = unsafe {
        DirectClipboardClient::from_static_ops(owned_clip.as_ptr().cast(), surface)
    }
    .expect("direct_clip");

    let (snap, a_curr) = count_allocs(|| black_box(direct_theme.current().expect("current")));
    assert_eq!(snap.scheme, ColorScheme::Dark);
    assert_eq!(a_curr, 0, "DirectSystemThemeClient::current must be 0 allocs (got {a_curr})");

    let (_, a_scheme) = count_allocs(|| {
        black_box(direct_theme.set_preferred_scheme(ColorScheme::Light).expect("set_scheme"))
    });
    assert_eq!(a_scheme, 0, "DirectSystemThemeClient::set_preferred_scheme must be 0 allocs (got {a_scheme})");

    let (_, a_wt) = count_allocs(|| black_box(direct_clip.write_text("hello").expect("write_text")));
    assert_eq!(a_wt, 0, "DirectClipboardClient::write_text must be 0 allocs (got {a_wt})");

    let (_, a_clr) = count_allocs(|| black_box(direct_clip.clear().expect("clear")));
    assert_eq!(a_clr, 0, "DirectClipboardClient::clear must be 0 allocs (got {a_clr})");

    let (none_txt, a_rt) = count_allocs(|| black_box(direct_clip.read_text().expect("read_text")));
    assert_eq!(none_txt, None);
    assert_eq!(a_rt, 0, "DirectClipboardClient::read_text(Ok(None)) must be 0 allocs (got {a_rt})");

    let small_payload = [0x42u8; 256];
    let (_, a_wb) = count_allocs(|| {
        black_box(direct_clip.write_bytes("application/octet-stream", &small_payload).expect("write_bytes"))
    });
    assert_eq!(a_wb, 0, "DirectClipboardClient::write_bytes must be 0 allocs (got {a_wb})");

    // 3. Zero-waste `wire` (`ByteTransport`) client + dispatcher hot calls = 0 heap allocations.
    let transport = DirectDispatchTransport { theme: ZeroAllocTheme, clip: ZeroAllocClip };
    let wire_theme = WireSystemThemeClient::new(&transport);
    let wire_clip = WireClipboardClient::new(&transport);

    let (wsnap, wa_curr) = count_allocs(|| black_box(wire_theme.current().expect("wire current")));
    assert_eq!(wsnap.scheme, ColorScheme::Dark);
    assert_eq!(wa_curr, 0, "WireSystemThemeClient::current ([u8; 12]) must be 0 allocs (got {wa_curr})");

    let (_, wa_scheme) = count_allocs(|| {
        black_box(wire_theme.set_preferred_scheme(ColorScheme::Light).expect("wire scheme"))
    });
    assert_eq!(wa_scheme, 0, "WireSystemThemeClient::set_preferred_scheme ([u8; 1]) must be 0 allocs (got {wa_scheme})");

    let (_, wa_clr) = count_allocs(|| black_box(wire_clip.clear().expect("wire clear")));
    assert_eq!(wa_clr, 0, "WireClipboardClient::clear (&[]) must be 0 allocs (got {wa_clr})");

    let (_, wa_wt) = count_allocs(|| black_box(wire_clip.write_text("hello").expect("wire write_text")));
    assert_eq!(wa_wt, 0, "WireClipboardClient::write_text (raw &[u8]) must be 0 allocs (got {wa_wt})");

    let (wnone, wa_rt) = count_allocs(|| black_box(wire_clip.read_text().expect("wire read_text")));
    assert_eq!(wnone, None);
    assert_eq!(wa_rt, 0, "WireClipboardClient::read_text (None) must be 0 allocs (got {wa_rt})");

    // Multi-field payload <= 512 B fits in StackWriter::<512> with 0 heap allocations!
    let fit_payload = [0x33u8; 400];
    let (_, wa_fit) = count_allocs(|| {
        black_box(wire_clip.write_bytes("text/plain", &fit_payload).expect("wire write_bytes <= 512B"))
    });
    assert_eq!(wa_fit, 0, "WireClipboardClient::write_bytes (<= 512 B via StackWriter) must be 0 allocs (got {wa_fit})");

    // 4. Spill proof: multi-field payload > 512 B spills to heap with EXACTLY 1 allocation!
    let spill_payload = [0x99u8; 1024];
    let (_, wa_spill) = count_allocs(|| {
        black_box(wire_clip.write_bytes("text/plain", &spill_payload).expect("wire write_bytes > 512B"))
    });
    assert_eq!(
        wa_spill, 1,
        "WireClipboardClient::write_bytes (> 512 B) must spill with exactly 1 heap allocation (got {wa_spill})"
    );

    println!("DIRECT_ALLOC_GUARDS_OK");
}
"#;

#[test]
fn direct_ffi_and_zero_waste_wire_hot_paths_are_zero_alloc_with_exact_spill() {
    let dir = temp_test_dir("run");
    let spec = parse_hybrid_rust_spec(DIRECT_SERVICES_SPEC).expect("parse direct_services");
    let out = generate_hybrid_target(&spec, HybridTarget::Rust).expect("generate Rust");
    out.write_to_dir(&dir).expect("write Rust");

    let rustc = Rustc::locate();
    let gen_rs = dir.join(&out.files[0].relative_path);
    let rlib = dir.join("libdirect_services.rlib");
    rustc.build(
        &dir,
        &["--crate-type=lib", "--crate-name=direct_services", "-C", "opt-level=2"],
        &[],
        &gen_rs,
        &rlib,
    );

    let main_rs = dir.join("alloc_guard_main.rs");
    std::fs::write(&main_rs, ALLOC_GUARD_MAIN_RS).unwrap();
    let exe = dir.join("direct_alloc_guard_bin");
    rustc.build(
        &dir,
        &["--crate-type=bin", "-C", "opt-level=2"],
        &[
            ("direct_services", rlib.as_path()),
            ("nativeview_test_support", rustc.dependency_rlib("nativeview_test_support").as_path()),
        ],
        &main_rs,
        &exe,
    );

    let run = Command::new(&exe).output().expect("run direct alloc guard bin");
    assert!(
        run.status.success() && String::from_utf8_lossy(&run.stdout).contains("DIRECT_ALLOC_GUARDS_OK"),
        "direct alloc guard failed:\nstdout:\n{}\nstderr:\n{}",
        String::from_utf8_lossy(&run.stdout),
        String::from_utf8_lossy(&run.stderr)
    );
    let _ = std::fs::remove_dir_all(&dir);
}

const ALLOC_GUARD_BATCH_MAIN_RS: &str = r#"
use std::hint::black_box;
use std::sync::Arc;

use batch_services::{
    BatchRecord, DesktopMenuBackend, DesktopMenuBatch, DirectDesktopMenuClient,
    DirectNoteVaultClient, MenuError, MenuItemKind, MenuRect, NoteVaultAsyncBackend,
    NoteVaultBackend, NoteVaultBatch, box_desktop_menu_host_ops, box_note_vault_host_ops,
    wire::{
        WireDesktopMenuClient, WireNoteVaultClient, dispatch_desktop_menu_wire,
        dispatch_note_vault_wire,
    },
};
use nativeview_abi::codec::{ByteTransport, TransportDoneFn, TransportError};
use nativeview_abi::{SurfaceId, SurfaceToken};
use nativeview_test_support::count_allocs;

nativeview_test_support::install_counting_allocator!();

struct ZeroAllocMenu;
impl DesktopMenuBackend for ZeroAllocMenu {
    fn create_submenu(&self, _: u32, _: u32, _: &str) -> Result<(), MenuError> { Ok(()) }
    fn add_item(&self, _: u32, _: u32, _: MenuItemKind, _: &str) -> Result<(), MenuError> { Ok(()) }
    fn set_item_checked(&self, _: u32, _: bool) -> Result<(), MenuError> { Ok(()) }
    fn set_rect(&self, _: u32, _: MenuRect) -> Result<(), MenuError> { Ok(()) }
}

struct ZeroAllocVault;
impl NoteVaultBackend for ZeroAllocVault {
    fn put_note(&self, _: &str, _: &str) -> Result<(), String> { Ok(()) }
    fn get_note(&self, _: &str) -> Result<Option<String>, String> { Ok(None) }
    fn delete_note(&self, _: &str) -> Result<bool, String> { Ok(false) }
    fn echo_record(&self, rec: BatchRecord) -> Result<BatchRecord, String> { Ok(rec) }
    fn flush_sync(&self) -> Result<(), String> { Ok(()) }
}

impl NoteVaultAsyncBackend for ZeroAllocVault {
    fn sync_remote(&self, _: &str, done: Box<dyn FnOnce(Result<u32, String>) + Send>) -> Option<u64> {
        done(Ok(0));
        None
    }
}

struct BatchDispatchTransport {
    menu: ZeroAllocMenu,
    vault: ZeroAllocVault,
}

impl ByteTransport for BatchDispatchTransport {
    fn call_sync(
        &self,
        service_id: u32,
        method_id: u32,
        req: &[u8],
        on_reply: &mut dyn FnMut(&[u8]),
    ) -> Result<(), TransportError> {
        if service_id == batch_services::NV_MOD_DESKTOP_MENU_SERVICE_ID.raw() {
            dispatch_desktop_menu_wire(&self.menu, method_id, req, on_reply)
        } else if service_id == batch_services::NV_MOD_NOTE_VAULT_SERVICE_ID.raw() {
            dispatch_note_vault_wire(&self.vault, method_id, req, on_reply)
        } else {
            Err(TransportError::Unsupported)
        }
    }

    fn call_async(&self, _: u32, _: u32, _: &[u8], done: TransportDoneFn) {
        done(Err(TransportError::Unsupported));
    }
}

fn main() {
    // 1. Positive control probe: CountingAlloc MUST observe Box::new(1u8).
    let (_, probe) = count_allocs(|| black_box(Box::new(1u8)));
    assert!(probe >= 1, "CountingAlloc positive control failed (got {probe})");

    let surface = SurfaceToken::new(SurfaceId::new(1, 1), 1);
    let owned_menu = box_desktop_menu_host_ops(Arc::new(ZeroAllocMenu));
    let owned_vault = box_note_vault_host_ops(Arc::new(ZeroAllocVault));
    let direct_menu = DirectDesktopMenuClient::from_owned_ops(&owned_menu, surface).unwrap();
    let direct_vault = DirectNoteVaultClient::from_owned_ops(&owned_vault, surface).unwrap();

    let transport = BatchDispatchTransport { menu: ZeroAllocMenu, vault: ZeroAllocVault };
    let wire_menu = WireDesktopMenuClient::new(&transport);
    let wire_vault = WireNoteVaultClient::new(&transport);

    // 2. Void-only stack batch (<= 256 B: 16 ops = 16 + 16*7 = 128 B) across Direct & Wire = 0 allocs.
    let (_, a_direct_stack) = count_allocs(|| {
        direct_menu
            .batch(|b| {
                for i in 0..16u32 {
                    b.set_item_checked(black_box(i), (i & 1) == 0);
                }
            })
            .expect("direct void stack batch");
    });
    assert_eq!(a_direct_stack, 0, "Direct void stack batch <= 256B must do 0 allocs (got {a_direct_stack})");

    let (_, a_wire_stack) = count_allocs(|| {
        wire_menu
            .batch(|b| {
                for i in 0..16u32 {
                    b.set_item_checked(black_box(i), (i & 1) == 0);
                }
            })
            .expect("wire void stack batch");
    });
    assert_eq!(a_wire_stack, 0, "Wire void stack batch <= 256B must do 0 allocs (got {a_wire_stack})");

    // 3. Void-only reusable batch (256 ops = 1808 B) after 1 warmup + reset_batch() = 0 allocs.
    let mut reusable_menu = DesktopMenuBatch::with_capacity(2048);
    for i in 0..256u32 {
        reusable_menu.set_item_checked(i, (i & 1) == 0);
    }
    direct_menu.submit_batch(&reusable_menu).unwrap();
    wire_menu.submit_batch(&reusable_menu).unwrap();

    let (_, a_direct_reuse) = count_allocs(|| {
        reusable_menu.reset_batch();
        for i in 0..256u32 {
            reusable_menu.set_item_checked(black_box(i), (i & 1) == 0);
        }
        direct_menu.submit_batch(&reusable_menu).expect("direct reusable 256");
    });
    assert_eq!(a_direct_reuse, 0, "Direct reusable 256-op batch must do 0 allocs (got {a_direct_reuse})");

    let (_, a_wire_reuse) = count_allocs(|| {
        reusable_menu.reset_batch();
        for i in 0..256u32 {
            reusable_menu.set_item_checked(black_box(i), (i & 1) == 0);
        }
        wire_menu.submit_batch(&reusable_menu).expect("wire reusable 256");
    });
    assert_eq!(a_wire_reuse, 0, "Wire reusable 256-op batch must do 0 allocs (got {a_wire_reuse})");

    // 4. Value-returning batch_with / submit_batch_with (None + bool) across Direct & Wire = 0 allocs.
    let ((g1, d1), a_direct_val) = count_allocs(|| {
        direct_vault
            .batch_with(
                |b| {
                    b.put_note(black_box("k"), black_box("v"));
                    let t_get = b.get_note(black_box("k"));
                    let t_del = b.delete_note(black_box("k"));
                    (t_get, t_del)
                },
                |tbl, (tg, td)| Ok((tbl.read(tg)?, tbl.read(td)?)),
            )
            .expect("direct batch_with")
    });
    assert_eq!(g1, None);
    assert!(!d1);
    assert_eq!(a_direct_val, 0, "Direct batch_with (None + bool) must do 0 allocs (got {a_direct_val})");

    let ((g2, d2), a_wire_val) = count_allocs(|| {
        wire_vault
            .batch_with(
                |b| {
                    b.put_note(black_box("k"), black_box("v"));
                    let t_get = b.get_note(black_box("k"));
                    let t_del = b.delete_note(black_box("k"));
                    (t_get, t_del)
                },
                |tbl, (tg, td)| Ok((tbl.read(tg)?, tbl.read(td)?)),
            )
            .expect("wire batch_with")
    });
    assert_eq!(g2, None);
    assert!(!d2);
    assert_eq!(a_wire_val, 0, "Wire batch_with (None + bool) must do 0 allocs (got {a_wire_val})");

    let mut reusable_vault = NoteVaultBatch::with_capacity(256);
    let _ = reusable_vault.get_note("warm");
    let (_, a_sub_with) = count_allocs(|| {
        reusable_vault.reset_batch();
        let tg = reusable_vault.get_note(black_box("k"));
        let td = reusable_vault.delete_note(black_box("k"));
        let (v, del) = direct_vault
            .submit_batch_with(&reusable_vault, |tbl| Ok((tbl.read(tg)?, tbl.read(td)?)))
            .expect("direct submit_batch_with");
        assert!(v.is_none() && !del);
        let (v2, del2) = wire_vault
            .submit_batch_with(&reusable_vault, |tbl| Ok((tbl.read(tg)?, tbl.read(td)?)))
            .expect("wire submit_batch_with");
        assert!(v2.is_none() && !del2);
    });
    assert_eq!(a_sub_with, 0, "Reusable submit_batch_with (None + bool) must do 0 allocs (got {a_sub_with})");

    // 5. Spill proof: 1-shot stack batch > 256 B (40 ops = 16 + 40*7 = 296 B) spills with EXACT 1 alloc.
    let (_, a_direct_spill) = count_allocs(|| {
        direct_menu
            .batch(|b| {
                for i in 0..40u32 {
                    b.set_item_checked(black_box(i), (i & 1) == 0);
                }
            })
            .expect("direct spill batch");
    });
    assert_eq!(
        a_direct_spill, 1,
        "Direct 1-shot stack batch > 256B must spill with exact 1 heap alloc (got {a_direct_spill})"
    );

    let (_, a_wire_spill) = count_allocs(|| {
        wire_menu
            .batch(|b| {
                for i in 0..40u32 {
                    b.set_item_checked(black_box(i), (i & 1) == 0);
                }
            })
            .expect("wire spill batch");
    });
    assert_eq!(
        a_wire_spill, 1,
        "Wire 1-shot stack batch > 256B must spill with exact 1 heap alloc (got {a_wire_spill})"
    );

    println!("DIRECT_BATCH_ALLOC_GUARDS_OK");
}
"#;

#[test]
fn direct_batch_hot_paths_are_zero_alloc_with_exact_spill() {
    let dir = temp_test_dir("batch");
    let spec = parse_hybrid_rust_spec(BATCH_SERVICES_SPEC).expect("parse batch_services");
    let out = generate_hybrid_target(&spec, HybridTarget::Rust).expect("generate Rust");
    out.write_to_dir(&dir).expect("write Rust");

    let rustc = Rustc::locate();
    let gen_rs = dir.join(&out.files[0].relative_path);
    let rlib = dir.join("libbatch_services.rlib");
    rustc.build(
        &dir,
        &["--crate-type=lib", "--crate-name=batch_services", "-C", "opt-level=2"],
        &[],
        &gen_rs,
        &rlib,
    );

    let main_rs = dir.join("alloc_guard_batch_main.rs");
    std::fs::write(&main_rs, ALLOC_GUARD_BATCH_MAIN_RS).unwrap();
    let exe = dir.join("direct_batch_alloc_guard_bin");
    rustc.build(
        &dir,
        &["--crate-type=bin", "-C", "opt-level=2"],
        &[
            ("batch_services", rlib.as_path()),
            ("nativeview_test_support", rustc.dependency_rlib("nativeview_test_support").as_path()),
        ],
        &main_rs,
        &exe,
    );

    let run = Command::new(&exe).output().expect("run direct batch alloc guard bin");
    assert!(
        run.status.success()
            && String::from_utf8_lossy(&run.stdout).contains("DIRECT_BATCH_ALLOC_GUARDS_OK"),
        "direct batch alloc guard failed:\nstdout:\n{}\nstderr:\n{}",
        String::from_utf8_lossy(&run.stdout),
        String::from_utf8_lossy(&run.stderr)
    );
    let _ = std::fs::remove_dir_all(&dir);
}