Skip to content

Commit ed41b4e

Browse files
committed
fix(vm): skip Named host schemas in nostd and reject host type args
nostd skip_host_schema now walks v13 HostTypeSchema::Named (tag 13) with the std name/field-count/nested-schema layout and existing depth/count/blob budgets. Host Named lookup and callable schema match fail closed on nonempty type args without changing guest generic lookup. Generic stream rustdocs describe Named/Object results and SSE's exact SseCallbackAction contract.
1 parent d2acd75 commit ed41b4e

6 files changed

Lines changed: 218 additions & 12 deletions

File tree

‎pd-vm-nostd/src/vmbc.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,15 @@ fn skip_host_schema(cursor: &mut Cursor<'_>, depth: usize) -> Result<(), WireErr
234234
skip_host_schema(cursor, depth + 1)
235235
}
236236
12 => cursor.skip_string(),
237+
13 => {
238+
cursor.skip_string()?;
239+
let field_count = cursor.read_count_with_overhead("host named struct fields", 1, 1)?;
240+
for _ in 0..field_count {
241+
cursor.skip_string()?;
242+
skip_host_schema(cursor, depth + 1)?;
243+
}
244+
Ok(())
245+
}
237246
value => Err(WireError::InvalidValueType(value)),
238247
}
239248
}

‎pd-vm-nostd/tests/embedded_vmbc.rs‎

Lines changed: 163 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use pd_vm_nostd::{
55
use vm::compiler::TypeSchema;
66
use vm::{
77
HostApiBuilder, HostFunctionSchema, HostImport, HostImportSchema, HostParamPassing,
8-
HostParamSchema, HostTypeSchema, OpCode, Program, ReplLocalBinding, ResourceTypeKey,
9-
ResourceTypeSchema, TypeMap, Value, ValueType, compile_source, compile_source_for_repl,
10-
compile_source_for_repl_with_locals, encode_program,
8+
HostParamSchema, HostStructField, HostStructSchema, HostTypeSchema, OpCode, Program,
9+
ReplLocalBinding, ResourceTypeKey, ResourceTypeSchema, TypeMap, Value, ValueType,
10+
compile_source, compile_source_for_repl, compile_source_for_repl_with_locals, encode_program,
1111
};
1212

1313
fn encoded_scalar_program() -> Vec<u8> {
@@ -89,6 +89,62 @@ fn embedded_decoder_skips_full_host_schema_metadata() {
8989
assert_eq!(decoded.imports().len(), 1);
9090
}
9191

92+
fn point_named_schema() -> HostTypeSchema {
93+
HostTypeSchema::named_struct(
94+
"Point",
95+
vec![
96+
HostStructField::new("x", HostTypeSchema::Int),
97+
HostStructField::new("y", HostTypeSchema::Int),
98+
],
99+
)
100+
}
101+
102+
fn envelope_named_schema() -> HostTypeSchema {
103+
HostTypeSchema::named_struct(
104+
"Envelope",
105+
vec![HostStructField::new("inner", point_named_schema())],
106+
)
107+
}
108+
109+
#[test]
110+
fn embedded_decoder_skips_named_host_schema_from_std_encode() {
111+
let function = HostFunctionSchema::with_return(
112+
"embedded::named",
113+
vec![HostParamSchema::value("req", envelope_named_schema())],
114+
point_named_schema(),
115+
);
116+
let mut builder = HostApiBuilder::new();
117+
builder.named_struct(HostStructSchema::new(
118+
"Point",
119+
vec![
120+
HostStructField::new("x", HostTypeSchema::Int),
121+
HostStructField::new("y", HostTypeSchema::Int),
122+
],
123+
));
124+
builder.named_struct(HostStructSchema::new(
125+
"Envelope",
126+
vec![HostStructField::new("inner", point_named_schema())],
127+
));
128+
builder.function(function.clone());
129+
let catalog = builder.build().expect("catalog");
130+
let schema = HostImportSchema::from_function(&catalog, &function);
131+
132+
let mut program = Program::new(Vec::new(), vec![OpCode::Ret as u8]);
133+
program.imports.push(HostImport {
134+
name: "embedded::named".to_string(),
135+
arity: 1,
136+
return_type: ValueType::Map,
137+
});
138+
let program = program
139+
.with_host_import_schemas(vec![schema])
140+
.expect("schema alignment");
141+
let bytes = encode_program(&program).expect("named host schema should encode");
142+
assert_eq!(u16::from_le_bytes([bytes[4], bytes[5]]), 13);
143+
let decoded = decode_program(&bytes).expect("embedded decoder should skip Named host schemas");
144+
assert_eq!(decoded.imports().len(), 1);
145+
assert_eq!(decoded.imports()[0].name, "embedded::named");
146+
}
147+
92148
#[test]
93149
fn embedded_decoder_reads_legacy_v11_without_schema_markers() {
94150
let program = Program::new(
@@ -468,6 +524,110 @@ fn append_wire_string(out: &mut Vec<u8>, value: &str) {
468524
out.extend_from_slice(value.as_bytes());
469525
}
470526

527+
fn v12_with_named_host_return_schema(schema: &[u8]) -> Vec<u8> {
528+
let mut bytes = minimal_vmbc_prefix(0, &[EmbeddedOpCode::Ret as u8], 1);
529+
bytes.extend_from_slice(&1u32.to_le_bytes());
530+
bytes.push(b'h');
531+
bytes.extend_from_slice(&[0, 0, 1]);
532+
bytes.extend_from_slice(&1u32.to_le_bytes());
533+
bytes.push(b'h');
534+
bytes.extend_from_slice(&0u32.to_le_bytes());
535+
bytes.extend_from_slice(schema);
536+
bytes.extend_from_slice(&0u64.to_le_bytes());
537+
bytes.push(0);
538+
bytes.push(0);
539+
bytes.extend_from_slice(&0u32.to_le_bytes());
540+
bytes.extend_from_slice(&0u32.to_le_bytes());
541+
bytes.extend_from_slice(&0u32.to_le_bytes());
542+
bytes.extend_from_slice(&0u32.to_le_bytes());
543+
bytes.extend_from_slice(&0u32.to_le_bytes());
544+
bytes
545+
}
546+
547+
fn empty_named_host_schema(name: &str) -> Vec<u8> {
548+
let mut schema = vec![13];
549+
append_wire_string(&mut schema, name);
550+
schema.extend_from_slice(&0u32.to_le_bytes());
551+
schema
552+
}
553+
554+
fn nested_named_host_schema(depth: usize) -> Vec<u8> {
555+
let mut schema = empty_named_host_schema("Leaf");
556+
for _ in 0..depth {
557+
let mut outer = vec![13];
558+
append_wire_string(&mut outer, "Wrap");
559+
outer.extend_from_slice(&1u32.to_le_bytes());
560+
append_wire_string(&mut outer, "inner");
561+
outer.extend_from_slice(&schema);
562+
schema = outer;
563+
}
564+
schema
565+
}
566+
567+
#[test]
568+
fn embedded_decoder_accepts_empty_named_host_schema() {
569+
let bytes = v12_with_named_host_return_schema(&empty_named_host_schema("Point"));
570+
decode_program(&bytes).expect("empty Named host schema should skip");
571+
}
572+
573+
#[test]
574+
fn embedded_decoder_rejects_truncated_named_host_schema() {
575+
let mut schema = vec![13];
576+
append_wire_string(&mut schema, "Point");
577+
schema.extend_from_slice(&1u32.to_le_bytes());
578+
assert_eq!(
579+
decode_program(&v12_with_named_host_return_schema(&schema)),
580+
Err(WireError::UnexpectedEof)
581+
);
582+
}
583+
584+
#[test]
585+
fn embedded_decoder_rejects_oversized_named_host_field_count() {
586+
const TOO_MANY: u32 = 1_000_001;
587+
let mut schema = vec![13];
588+
append_wire_string(&mut schema, "Point");
589+
schema.extend_from_slice(&TOO_MANY.to_le_bytes());
590+
assert!(matches!(
591+
decode_program(&v12_with_named_host_return_schema(&schema)),
592+
Err(WireError::LengthTooLarge("host named struct fields", count))
593+
if count == TOO_MANY as usize
594+
));
595+
}
596+
597+
#[test]
598+
fn embedded_decoder_rejects_malformed_nested_named_host_schema() {
599+
let mut schema = vec![13];
600+
append_wire_string(&mut schema, "Outer");
601+
schema.extend_from_slice(&1u32.to_le_bytes());
602+
append_wire_string(&mut schema, "inner");
603+
schema.push(99);
604+
assert_eq!(
605+
decode_program(&v12_with_named_host_return_schema(&schema)),
606+
Err(WireError::InvalidValueType(99))
607+
);
608+
}
609+
610+
#[test]
611+
fn embedded_decoder_rejects_truncated_nested_named_host_schema() {
612+
let mut schema = vec![13];
613+
append_wire_string(&mut schema, "Outer");
614+
schema.extend_from_slice(&1u32.to_le_bytes());
615+
append_wire_string(&mut schema, "inner");
616+
schema.push(13);
617+
append_wire_string(&mut schema, "Inner");
618+
schema.extend_from_slice(&1u32.to_le_bytes());
619+
assert_eq!(
620+
decode_program(&v12_with_named_host_return_schema(&schema)),
621+
Err(WireError::UnexpectedEof)
622+
);
623+
}
624+
625+
#[test]
626+
fn embedded_decoder_rejects_oversized_nested_named_host_depth() {
627+
let bytes = v12_with_named_host_return_schema(&nested_named_host_schema(64));
628+
assert_eq!(decode_program(&bytes), Err(WireError::SchemaTooDeep));
629+
}
630+
471631
#[test]
472632
fn embedded_decoder_reads_v13_guest_named_struct_payload() {
473633
let compiled = compile_source(

‎src/vm/async_host/stream.rs‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ pub(crate) struct HostStreamAdmissionError {
6666
///
6767
/// The VM always validates the callback's callable provenance and arity before
6868
/// installing a driver. When its metadata is [`TypeSchema::Callable`], it also
69-
/// validates the argument and result schemas against `fn(map) -> map`. HTTP SSE
70-
/// additionally requires an `SseCallbackAction`-compatible named or object
71-
/// result rather than an arbitrary map. Scripts receive ordinary callback items
72-
/// and a final value; they never receive a stream handle or a producer poll API.
69+
/// validates a map argument and a map, Named, or Object result. HTTP SSE
70+
/// additionally requires the exact `SseCallbackAction` named type or a matching
71+
/// `{ action: string }` object rather than an arbitrary map. Scripts receive
72+
/// ordinary callback items and a final value; they never receive a stream
73+
/// handle or a producer poll API.
7374
///
7475
/// Implementors must observe these contracts:
7576
///
@@ -179,9 +180,12 @@ impl Vm {
179180
/// This Rust embedding API does not create a script-visible handle. The VM
180181
/// always validates that `callback` is a callable owned by this VM and has
181182
/// arity one. When its metadata is [`TypeSchema::Callable`], the VM also
182-
/// validates its argument and result schemas against `fn(map) -> map`. It
183-
/// then owns the callback and driver until completion, cancellation, reset,
184-
/// or error; removing the driver drops it to release producer resources.
183+
/// validates a map argument and a map, Named, or Object result. HTTP SSE
184+
/// uses [`Self::validate_sse_callback_value`] for the exact
185+
/// `SseCallbackAction` named/object contract rather than an arbitrary map.
186+
/// The VM then owns the callback and driver until completion, cancellation,
187+
/// reset, or error; removing the driver drops it to release producer
188+
/// resources.
185189
///
186190
/// The driver contract is documented on [`HostStreamDriver`]. In
187191
/// particular, producer polling and callback action application stay

‎src/vm/host.rs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,8 +1366,8 @@ fn callable_schema_matches(
13661366
name: expected_name,
13671367
..
13681368
},
1369-
TypeSchema::Named(actual_name, _),
1370-
) => expected_name == actual_name,
1369+
TypeSchema::Named(actual_name, args),
1370+
) => expected_name == actual_name && args.is_empty(),
13711371
(HostTypeSchema::Named { fields, .. }, TypeSchema::Object(actual_fields)) => {
13721372
named_fields_match_object(fields, actual_fields)
13731373
}
@@ -3734,6 +3734,10 @@ mod tests {
37343734
&expected,
37353735
&TypeSchema::Object(object_fields),
37363736
));
3737+
assert!(!callable_schema_matches(
3738+
&expected,
3739+
&TypeSchema::Named("SseEvent".to_string(), vec![TypeSchema::Int]),
3740+
));
37373741
}
37383742

37393743
#[test]

‎src/vm/mod.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,9 @@ impl<'a> NamedStructLookup<'a> {
506506
args: &[crate::compiler::TypeSchema],
507507
) -> Option<(crate::compiler::TypeSchema, NamedStructOrigin)> {
508508
if let Some(body) = self.host.get(name) {
509+
if !args.is_empty() {
510+
return None;
511+
}
509512
return Some((body.clone(), NamedStructOrigin::Host));
510513
}
511514
let decl = self.guest.get(name)?;

‎src/vm/tests.rs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3929,6 +3929,32 @@ mod callable_resource_schema_tests {
39293929
assert_resource_schema_error(error, "invalid_resource_handle");
39303930
}
39313931

3932+
#[test]
3933+
fn host_named_struct_rejects_nonempty_type_args() {
3934+
let (_schema, named) = handle_box_schema();
3935+
let schema = TypeSchema::Named("HandleBox".to_string(), vec![TypeSchema::Int]);
3936+
let (mut vm, callable) = callable_vm_with_named(
3937+
&[OpCode::Ret as u8],
3938+
Vec::new(),
3939+
schema,
3940+
TypeSchema::Null,
3941+
named,
3942+
);
3943+
let handle = resource_handle(&mut vm);
3944+
let error = vm
3945+
.invoke_callable(callable, &[named_handle_box(handle, None)])
3946+
.expect_err("host Named with type args must fail closed");
3947+
match error {
3948+
VmError::HostError(message) => {
3949+
assert!(
3950+
message.contains("unknown named struct"),
3951+
"unexpected host error: {message}"
3952+
);
3953+
}
3954+
other => panic!("expected HostError, got {other:?}"),
3955+
}
3956+
}
3957+
39323958
#[test]
39333959
fn unknown_named_struct_fails_closed() {
39343960
let schema = TypeSchema::Named("MissingBox".to_string(), Vec::new());

0 commit comments

Comments
 (0)