@@ -5,9 +5,9 @@ use pd_vm_nostd::{
55use vm:: compiler:: TypeSchema ;
66use 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
1313fn 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]
93149fn 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]
472632fn embedded_decoder_reads_v13_guest_named_struct_payload ( ) {
473633 let compiled = compile_source (
0 commit comments