relon-eval-api 0.1.0-rc2

Public types and Evaluator trait shared across Relon evaluation backends
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
//! Property-based write→read round-trip over the binary handshake
//! buffer, exercising the recursive host reader landed in `a1660b62`
//! (`BufferReader::read_list_record` / `read_list_list` + the per-field
//! decode the cranelift / llvm evaluators drive through
//! `read_value_from_reader`).
//!
//! The generators build values of the **return-side target shapes** —
//! `List<Schema>` (fields covering every scalar + `List<scalar>`),
//! `List<List<scalar>>`, plus the scalar / String / `List<scalar>`
//! leaves — write each value into a `BufferBuilder` through the same
//! typed writers the host marshaller uses, read it back through the
//! matching readers, and assert the decoded `Value` equals the input.
//! Strings cover empty / multi-byte (CJK, via `\u{...}` escapes) / long
//! / alignment-edge lengths; lists cover 0 / 1 / many; ints cover `MIN`
//! / `MAX`.
//!
//! Every successful round-trip is *also* fed through
//! [`relon_eval_api::verifier::verify_record`] over the whole-buffer
//! region — a clean verify is asserted, so the verifier and the reader
//! agree on "this buffer is well-formed" for the entire generated value
//! space (proptest auto-shrinks any counterexample).
//!
//! This is the S0 round-trip gate: it does **not** exercise the
//! compiled backends (no codegen output exists for these return shapes
//! yet — see the S0 load-bearing-wall proof). It validates the host
//! reader / writer pair and the verifier in isolation.

use std::sync::Arc;

use ordered_float::OrderedFloat;
use proptest::prelude::*;

use relon_eval_api::buffer::{BufferBuilder, BufferReader};
use relon_eval_api::layout::SchemaLayout;
use relon_eval_api::schema_canonical::{Field, Schema, TypeRepr};
use relon_eval_api::value::{Value, ValueDict};
use relon_eval_api::verifier::{verify_record, Region};

// --- scalar element generators ---------------------------------------

fn int_strat() -> impl Strategy<Value = i64> {
    prop_oneof![Just(i64::MIN), Just(i64::MAX), Just(0i64), any::<i64>(),]
}

fn float_strat() -> impl Strategy<Value = f64> {
    // Avoid NaN — `OrderedFloat` equality treats NaN specially and the
    // buffer round-trip is bit-preserving, but proptest's `==` on the
    // decoded `Value::Float` would otherwise need NaN-aware handling.
    prop_oneof![
        Just(0.0f64),
        Just(-0.0f64),
        Just(f64::MIN),
        Just(f64::MAX),
        (-1e9f64..1e9f64),
    ]
}

/// Strings covering the alignment / encoding edges the brief calls out:
/// empty, multi-byte CJK, long, and odd/aligned lengths.
fn string_strat() -> impl Strategy<Value = String> {
    prop_oneof![
        Just(String::new()),
        Just("a".to_string()),
        Just("ab".to_string()),
        Just("abc".to_string()),
        Just("abcd".to_string()),
        // Multi-byte UTF-8 (3-byte CJK codepoints) via escapes so the
        // source file stays ASCII while the payload still exercises the
        // non-1-byte-per-char alignment edges.
        Just("\u{4e2d}".to_string()),
        Just("\u{4e2d}\u{6587}\u{5b57}\u{7b26}\u{4e32}".to_string()),
        Just("x".repeat(257)),
        "[a-z]{0,40}".prop_map(|s| s),
        // Mixed ASCII + multi-byte so the byte length is not a multiple
        // of the char count.
        "[a-z]{0,12}".prop_map(|s| {
            // Append a multi-byte codepoint half the time to break the
            // byte-len == char-len assumption.
            if s.len() % 2 == 0 {
                format!("{s}\u{4e2d}")
            } else {
                s
            }
        }),
    ]
}

// --- inner-list (List<scalar>) generators ----------------------------

fn list_int_strat() -> impl Strategy<Value = Vec<i64>> {
    prop::collection::vec(int_strat(), 0..6)
}

fn list_float_strat() -> impl Strategy<Value = Vec<f64>> {
    prop::collection::vec(float_strat(), 0..6)
}

fn list_bool_strat() -> impl Strategy<Value = Vec<bool>> {
    prop::collection::vec(any::<bool>(), 0..6)
}

fn list_string_strat() -> impl Strategy<Value = Vec<String>> {
    prop::collection::vec(string_strat(), 0..5)
}

// --- helpers ----------------------------------------------------------

fn field(name: &str, ty: TypeRepr) -> Field {
    Field {
        name: name.into(),
        ty,
        default: None,
    }
}

fn list(inner: TypeRepr) -> TypeRepr {
    TypeRepr::List {
        element: Box::new(inner),
    }
}

/// Wrap a single field + value into a one-field schema, write it, read
/// it back, assert equality, and run the verifier clean. The single
/// shared helper keeps each property body to "generate → call".
fn roundtrip_one(field: Field, value: Value) {
    let schema = Schema {
        name: "Root".into(),
        generics: vec![],
        is_tuple: false,
        fields: vec![field.clone()],
    };
    let layout = SchemaLayout::offsets_for(&schema).expect("layout");
    let mut builder = BufferBuilder::new(&layout, &schema.fields);
    write_field(&mut builder, &field, &value);
    let bytes = builder.finish();

    // Verifier must pass on the whole-buffer region.
    let region = Region::new(0, bytes.len()).expect("region");
    verify_record(&bytes, &layout, &schema.fields, 0, region)
        .unwrap_or_else(|e| panic!("verifier rejected a legal buffer: {e}\n value={value:?}"));

    let reader = BufferReader::new(&layout, &schema.fields, &bytes).expect("reader");
    let decoded = read_field(&reader, &field);
    assert_eq!(
        decoded, value,
        "round-trip mismatch for field `{}`",
        field.name
    );
}

/// Write `value` into `builder` for `field` using the typed buffer
/// writers — mirrors `write_value_into_builder` in the evaluators but
/// also reaches the `List<Schema>` / `List<List<_>>` writers the input
/// path does not exercise (those are return-only shapes here).
fn write_field(builder: &mut BufferBuilder<'_>, field: &Field, value: &Value) {
    match (&field.ty, value) {
        (TypeRepr::Int, Value::Int(v)) => builder.write_int(&field.name, *v).unwrap(),
        (TypeRepr::Float, Value::Float(v)) => {
            builder.write_float(&field.name, v.into_inner()).unwrap()
        }
        (TypeRepr::Bool, Value::Bool(v)) => builder.write_bool(&field.name, *v).unwrap(),
        (TypeRepr::String, Value::String(s)) => {
            builder.write_string(&field.name, s.as_str()).unwrap()
        }
        (TypeRepr::List { element }, Value::List(items)) => {
            write_list_field(builder, &field.name, element, items)
        }
        other => panic!("write_field: unhandled ({:?}, value)", other.0),
    }
}

fn write_list_field(
    builder: &mut BufferBuilder<'_>,
    name: &str,
    element: &TypeRepr,
    items: &[Value],
) {
    match element {
        TypeRepr::Int => {
            let v: Vec<i64> = items
                .iter()
                .map(|x| match x {
                    Value::Int(i) => *i,
                    _ => panic!("expected Int element"),
                })
                .collect();
            builder.write_list_int(name, &v).unwrap();
        }
        TypeRepr::Float => {
            let v: Vec<f64> = items
                .iter()
                .map(|x| match x {
                    Value::Float(f) => f.into_inner(),
                    _ => panic!("expected Float element"),
                })
                .collect();
            builder.write_list_float(name, &v).unwrap();
        }
        TypeRepr::Bool => {
            let v: Vec<bool> = items
                .iter()
                .map(|x| match x {
                    Value::Bool(b) => *b,
                    _ => panic!("expected Bool element"),
                })
                .collect();
            builder.write_list_bool(name, &v).unwrap();
        }
        TypeRepr::String => {
            let v: Vec<&str> = items
                .iter()
                .map(|x| match x {
                    Value::String(s) => s.as_str(),
                    _ => panic!("expected String element"),
                })
                .collect();
            builder.write_list_string(name, &v).unwrap();
        }
        TypeRepr::Schema { schema } => {
            let elem_layout = SchemaLayout::offsets_for(schema).expect("elem layout");
            let mut writer = builder
                .list_record_writer(name, &elem_layout, schema)
                .expect("list_record_writer");
            for it in items {
                let Value::Dict(dict) = it else {
                    panic!("expected branded Dict element");
                };
                let mut child = writer.start_entry();
                for f in &schema.fields {
                    let fv = dict.map.get(f.name.as_str()).expect("dict field present");
                    write_field(&mut child, f, fv);
                }
                writer.finish_entry(builder, child).expect("finish_entry");
            }
            builder
                .finish_list_record(writer)
                .expect("finish_list_record");
        }
        TypeRepr::List { element: inner } => {
            // List<List<scalar>>: drive `write_nested_scalar_list`.
            relon_eval_api::buffer::write_nested_scalar_list(builder, name, inner, items)
                .expect("write_nested_scalar_list");
        }
        other => panic!("write_list_field: unhandled element {other:?}"),
    }
}

/// Read `field` back as a `Value`, mirroring `read_value_from_reader`
/// in the evaluators (the a1660b62 host reader).
fn read_field(reader: &BufferReader<'_>, field: &Field) -> Value {
    match &field.ty {
        TypeRepr::Int => Value::Int(reader.read_int(&field.name).unwrap()),
        TypeRepr::Float => Value::Float(OrderedFloat(reader.read_float(&field.name).unwrap())),
        TypeRepr::Bool => Value::Bool(reader.read_bool(&field.name).unwrap()),
        TypeRepr::String => Value::String(reader.read_string(&field.name).unwrap().into()),
        TypeRepr::List { element } => read_list_field(reader, &field.name, element),
        other => panic!("read_field: unhandled {other:?}"),
    }
}

fn read_list_field(reader: &BufferReader<'_>, name: &str, element: &TypeRepr) -> Value {
    match element {
        TypeRepr::Int => Value::List(Arc::new(
            reader
                .read_list_int(name)
                .unwrap()
                .into_iter()
                .map(Value::Int)
                .collect(),
        )),
        TypeRepr::Float => Value::List(Arc::new(
            reader
                .read_list_float(name)
                .unwrap()
                .into_iter()
                .map(|f| Value::Float(OrderedFloat(f)))
                .collect(),
        )),
        TypeRepr::Bool => Value::List(Arc::new(
            reader
                .read_list_bool(name)
                .unwrap()
                .into_iter()
                .map(Value::Bool)
                .collect(),
        )),
        TypeRepr::String => Value::List(Arc::new(
            reader
                .read_list_string(name)
                .unwrap()
                .into_iter()
                .map(|s| Value::String(s.into()))
                .collect(),
        )),
        TypeRepr::Schema { schema } => {
            let elem_layout = SchemaLayout::offsets_for(schema).expect("elem layout");
            let subs = reader
                .read_list_record(name, &elem_layout, schema)
                .expect("read_list_record");
            let mut items = Vec::with_capacity(subs.len());
            for sub in &subs {
                let mut entries: Vec<(String, Value)> = Vec::new();
                for f in &schema.fields {
                    entries.push((f.name.clone(), read_field(sub, f)));
                }
                items.push(Value::branded_dict(entries, Some(schema.name.clone())));
            }
            Value::List(Arc::new(items))
        }
        TypeRepr::List { .. } => Value::List(Arc::new(
            reader
                .read_list_list(name)
                .expect("read_list_list")
                .into_iter()
                .map(|row| Value::List(Arc::new(row)))
                .collect(),
        )),
        other => panic!("read_list_field: unhandled element {other:?}"),
    }
}

// --- List<Schema> element generator ----------------------------------

/// A schema whose fields cover every scalar + each `List<scalar>` —
/// the "fields contain Int/Float/Bool/String/List" shape the brief
/// asks for in the `List<Schema>` generator.
fn elem_schema() -> Schema {
    Schema {
        name: "Elem".into(),
        generics: vec![],
        is_tuple: false,
        fields: vec![
            field("i", TypeRepr::Int),
            field("f", TypeRepr::Float),
            field("b", TypeRepr::Bool),
            field("s", TypeRepr::String),
            field("li", list(TypeRepr::Int)),
            field("ls", list(TypeRepr::String)),
        ],
    }
}

fn elem_value_strat() -> impl Strategy<Value = Value> {
    (
        int_strat(),
        float_strat(),
        any::<bool>(),
        string_strat(),
        list_int_strat(),
        list_string_strat(),
    )
        .prop_map(|(i, f, b, s, li, ls)| {
            let entries: Vec<(String, Value)> = vec![
                ("i".into(), Value::Int(i)),
                ("f".into(), Value::Float(OrderedFloat(f))),
                ("b".into(), Value::Bool(b)),
                ("s".into(), Value::String(s.into())),
                (
                    "li".into(),
                    Value::List(Arc::new(li.into_iter().map(Value::Int).collect())),
                ),
                (
                    "ls".into(),
                    Value::List(Arc::new(
                        ls.into_iter().map(|x| Value::String(x.into())).collect(),
                    )),
                ),
            ];
            Value::branded_dict(entries, Some("Elem".to_string()))
        })
}

// --- List<List<scalar>> generator ------------------------------------

fn list_list_int_strat() -> impl Strategy<Value = Value> {
    prop::collection::vec(list_int_strat(), 0..4).prop_map(|rows| {
        Value::List(Arc::new(
            rows.into_iter()
                .map(|r| Value::List(Arc::new(r.into_iter().map(Value::Int).collect())))
                .collect(),
        ))
    })
}

fn list_list_bool_strat() -> impl Strategy<Value = Value> {
    prop::collection::vec(list_bool_strat(), 0..4).prop_map(|rows| {
        Value::List(Arc::new(
            rows.into_iter()
                .map(|r| Value::List(Arc::new(r.into_iter().map(Value::Bool).collect())))
                .collect(),
        ))
    })
}

fn list_list_float_strat() -> impl Strategy<Value = Value> {
    prop::collection::vec(list_float_strat(), 0..4).prop_map(|rows| {
        Value::List(Arc::new(
            rows.into_iter()
                .map(|r| {
                    Value::List(Arc::new(
                        r.into_iter()
                            .map(|f| Value::Float(OrderedFloat(f)))
                            .collect(),
                    ))
                })
                .collect(),
        ))
    })
}

// Suppress an unused-import warning for `ValueDict` on toolchains that
// don't need it for inference; the type is part of the value surface we
// round-trip.
#[allow(dead_code)]
fn _value_dict_marker(_: &ValueDict) {}

proptest! {
    #![proptest_config(ProptestConfig::with_cases(256))]

    #[test]
    fn roundtrip_scalar_int(v in int_strat()) {
        roundtrip_one(field("i", TypeRepr::Int), Value::Int(v));
    }

    #[test]
    fn roundtrip_scalar_float(v in float_strat()) {
        roundtrip_one(field("f", TypeRepr::Float), Value::Float(OrderedFloat(v)));
    }

    #[test]
    fn roundtrip_scalar_bool(v in any::<bool>()) {
        roundtrip_one(field("b", TypeRepr::Bool), Value::Bool(v));
    }

    #[test]
    fn roundtrip_string(s in string_strat()) {
        roundtrip_one(field("s", TypeRepr::String), Value::String(s.into()));
    }

    #[test]
    fn roundtrip_list_int(v in list_int_strat()) {
        let val = Value::List(Arc::new(v.into_iter().map(Value::Int).collect()));
        roundtrip_one(field("li", list(TypeRepr::Int)), val);
    }

    #[test]
    fn roundtrip_list_float(v in list_float_strat()) {
        let val = Value::List(Arc::new(
            v.into_iter().map(|f| Value::Float(OrderedFloat(f))).collect(),
        ));
        roundtrip_one(field("lf", list(TypeRepr::Float)), val);
    }

    #[test]
    fn roundtrip_list_bool(v in list_bool_strat()) {
        let val = Value::List(Arc::new(v.into_iter().map(Value::Bool).collect()));
        roundtrip_one(field("lb", list(TypeRepr::Bool)), val);
    }

    #[test]
    fn roundtrip_list_string(v in list_string_strat()) {
        let val = Value::List(Arc::new(
            v.into_iter().map(|s| Value::String(s.into())).collect(),
        ));
        roundtrip_one(field("ls", list(TypeRepr::String)), val);
    }

    #[test]
    fn roundtrip_list_schema(items in prop::collection::vec(elem_value_strat(), 0..4)) {
        let val = Value::List(Arc::new(items));
        let fld = field(
            "rows",
            list(TypeRepr::Schema {
                schema: Box::new(elem_schema()),
            }),
        );
        roundtrip_one(fld, val);
    }

    #[test]
    fn roundtrip_list_list_int(val in list_list_int_strat()) {
        roundtrip_one(field("m", list(list(TypeRepr::Int))), val);
    }

    #[test]
    fn roundtrip_list_list_bool(val in list_list_bool_strat()) {
        roundtrip_one(field("m", list(list(TypeRepr::Bool))), val);
    }

    #[test]
    fn roundtrip_list_list_float(val in list_list_float_strat()) {
        roundtrip_one(field("m", list(list(TypeRepr::Float))), val);
    }
}

// --- F0 cross-region object generator (multi-region verifier) ---------
//
// The F1 target shape is `-> Dict { servers: List<Cfg>, n: Int }`: an
// object head built in `out_buf` whose `servers` field points at a
// parameter-sourced `List<Cfg>` whose header / entries / sub-records /
// String fields all live in `in_buf`. F0 ships no cap release, so we
// hand-assemble that cross-region arena (object head in `out`, field data
// in `in`, every pointer arena-absolute) and drive the multi-region
// verifier over the whole value space. Legal arenas must verify clean;
// a corrupted pointer must be rejected loudly (never a panic / over-read).

use relon_eval_api::layout::OffsetTable;
use relon_eval_api::verifier::{verify_record_multi, MultiRegion};

/// `Cfg { name: String, port: Int }` — the cross-region element schema.
fn xr_cfg_schema() -> Schema {
    Schema {
        name: "Cfg".into(),
        generics: vec![],
        is_tuple: false,
        fields: vec![
            field("name", TypeRepr::String),
            field("port", TypeRepr::Int),
        ],
    }
}

/// `Out { servers: List<Cfg>, n: Int }` — the cross-region object schema.
fn xr_outer_schema() -> Schema {
    Schema {
        name: "Out".into(),
        generics: vec![],
        is_tuple: false,
        fields: vec![
            field(
                "servers",
                list(TypeRepr::Schema {
                    schema: Box::new(xr_cfg_schema()),
                }),
            ),
            field("n", TypeRepr::Int),
        ],
    }
}

fn le_u32(v: u32) -> [u8; 4] {
    v.to_le_bytes()
}

/// Assemble a cross-region arena for `Out { servers: [Cfg{name,port}..], n }`:
/// the `in` region holds the whole `List<Cfg>` graph with arena-absolute
/// pointers; the `out` region holds the object head whose `servers` slot
/// points (cross-region) at the `in`-region list header. Returns
/// `(arena, multi, outer_layout, outer_schema, out_record_base, pointer_slot_abs_offsets)`.
/// `pointer_slot_abs_offsets` lists every arena-absolute byte position that
/// holds a pointer the verifier follows, so a proptest can corrupt one.
#[allow(clippy::type_complexity)]
fn build_xr_arena(
    servers: &[(String, i64)],
    n: i64,
) -> (Vec<u8>, MultiRegion, OffsetTable, Schema, usize, Vec<usize>) {
    let cfg = xr_cfg_schema();
    let cfg_layout = SchemaLayout::offsets_for(&cfg).expect("cfg layout");
    let outer = xr_outer_schema();
    let outer_layout = SchemaLayout::offsets_for(&outer).expect("outer layout");

    let name_fo = cfg_layout.fields.iter().find(|f| f.name == "name").unwrap();
    let port_fo = cfg_layout.fields.iter().find(|f| f.name == "port").unwrap();

    // Region-local layout of the `in` buffer:
    //   [list header len][off_0..off_{k-1}][cfg_0 ..][cfg_{k-1}][name strings..]
    let k = servers.len();
    let header_rel = 0usize;
    let entries_rel = header_rel + 4;
    let cfgs_rel = entries_rel + k * 4;
    let cfg_rel = |i: usize| cfgs_rel + i * cfg_layout.root_size;
    let names_rel = cfgs_rel + k * cfg_layout.root_size;
    // Compute each name's region-local offset.
    let mut name_rel = Vec::with_capacity(k);
    let mut cursor = names_rel;
    for (s, _) in servers {
        name_rel.push(cursor);
        cursor += 4 + s.len();
    }
    let in_len = cursor.max(4);

    // Region geometry (disjoint, padded windows).
    let const_len = 16usize;
    let in_start = const_len;
    let in_end = in_start + in_len;
    let out_start = in_end + 8;
    let out_len = outer_layout.root_size;
    let out_end = out_start + out_len;
    let scratch_start = out_end + 8;
    let arena_size = scratch_start + 16;
    let mut arena = vec![0u8; arena_size];

    let mut ptr_slots: Vec<usize> = Vec::new();
    let put32 = |arena: &mut [u8], abs: usize, v: u32| {
        arena[abs..abs + 4].copy_from_slice(&le_u32(v));
    };
    let put64 = |arena: &mut [u8], abs: usize, v: i64| {
        arena[abs..abs + 8].copy_from_slice(&v.to_le_bytes());
    };

    // List header + entry pointers (arena-absolute).
    put32(&mut arena, in_start + header_rel, k as u32);
    for i in 0..k {
        let slot = in_start + entries_rel + i * 4;
        put32(&mut arena, slot, (in_start + cfg_rel(i)) as u32);
        ptr_slots.push(slot);
    }
    // Each Cfg sub-record: name pointer (arena-absolute) + inline port.
    for (i, (s, port)) in servers.iter().enumerate() {
        let name_slot = in_start + cfg_rel(i) + name_fo.offset;
        put32(&mut arena, name_slot, (in_start + name_rel[i]) as u32);
        ptr_slots.push(name_slot);
        put64(&mut arena, in_start + cfg_rel(i) + port_fo.offset, *port);
        // String record [len][utf8].
        let rec = in_start + name_rel[i];
        put32(&mut arena, rec, s.len() as u32);
        arena[rec + 4..rec + 4 + s.len()].copy_from_slice(s.as_bytes());
    }

    // Object head in `out`: `servers` -> list header in `in` (cross-region).
    let servers_fo = outer_layout
        .fields
        .iter()
        .find(|f| f.name == "servers")
        .unwrap();
    let n_fo = outer_layout.fields.iter().find(|f| f.name == "n").unwrap();
    let servers_slot = out_start + servers_fo.offset;
    put32(&mut arena, servers_slot, (in_start + header_rel) as u32);
    ptr_slots.push(servers_slot);
    put64(&mut arena, out_start + n_fo.offset, n);

    let multi = MultiRegion::new(
        (0, const_len),
        (in_start, in_end),
        (out_start, out_end),
        (scratch_start, arena_size),
    )
    .expect("multi region");

    (arena, multi, outer_layout, outer, out_start, ptr_slots)
}

fn xr_servers_strat() -> impl Strategy<Value = Vec<(String, i64)>> {
    prop::collection::vec((string_strat(), int_strat()), 0..4)
}

proptest! {
    #![proptest_config(ProptestConfig::with_cases(256))]

    /// A legal cross-region `Dict { servers: List<Cfg>, n }` arena must
    /// verify clean under the multi-region map for the whole value space.
    #[test]
    fn xr_legal_object_verifies(servers in xr_servers_strat(), n in int_strat()) {
        let (arena, multi, layout, schema, base, _slots) = build_xr_arena(&servers, n);
        verify_record_multi(&arena, &layout, &schema.fields, base, multi)
            .map_err(|e| TestCaseError::fail(format!("legal cross-region object rejected: {e}")))?;
    }

    /// Corrupting any one followed pointer to a far out-of-arena offset
    /// must make the multi-region verifier reject loudly — never panic,
    /// never over-read.
    #[test]
    fn xr_corrupt_pointer_rejected(
        servers in xr_servers_strat(),
        n in int_strat(),
        which in any::<prop::sample::Index>(),
    ) {
        let (mut arena, multi, layout, schema, base, slots) = build_xr_arena(&servers, n);
        prop_assume!(!slots.is_empty());
        let idx = which.index(slots.len());
        let slot = slots[idx];
        let bogus = (arena.len() as u32) + 4096;
        arena[slot..slot + 4].copy_from_slice(&le_u32(bogus));
        let res = verify_record_multi(&arena, &layout, &schema.fields, base, multi);
        prop_assert!(res.is_err(), "a corrupt cross-region pointer must be rejected, got Ok");
    }
}