@@ -2635,17 +2635,7 @@ impl ComponentState {
26352635 CanonicalOption :: Realloc ( idx) => {
26362636 realloc = match realloc {
26372637 None => {
2638- let ty_id = self . core_function_at ( * idx, offset) ?;
2639- let func_ty = types[ ty_id] . unwrap_func ( ) ;
2640- if func_ty. params ( )
2641- != [ ValType :: I32 , ValType :: I32 , ValType :: I32 , ValType :: I32 ]
2642- || func_ty. results ( ) != [ ValType :: I32 ]
2643- {
2644- return Err ( BinaryReaderError :: new (
2645- "canonical option `realloc` uses a core function with an incorrect signature" ,
2646- offset,
2647- ) ) ;
2648- }
2638+ // Validation deferred because it may depend on the memory option.
26492639 Some ( * idx)
26502640 }
26512641 Some ( _) => {
@@ -2771,6 +2761,29 @@ impl ComponentState {
27712761 bail ! ( offset, "cannot specify `core-type` without `gc`" )
27722762 }
27732763
2764+ // Validate `realloc`
2765+ if let Some ( realloc_idx) = realloc {
2766+ let addr_type = match memory {
2767+ // If a memory was specified, `realloc` must match its address type.
2768+ Some ( memory_idx) => match self . memory_at ( memory_idx, offset) ?. memory64 {
2769+ true => ValType :: I64 ,
2770+ false => ValType :: I32 ,
2771+ } ,
2772+ // Backwards compatibility: Assume `i32` memory if none was specified.
2773+ None => ValType :: I32 ,
2774+ } ;
2775+ let ty_id = self . core_function_at ( realloc_idx, offset) ?;
2776+ let func_ty = types[ ty_id] . unwrap_func ( ) ;
2777+ if func_ty. params ( ) != [ addr_type, addr_type, addr_type, addr_type]
2778+ || func_ty. results ( ) != [ addr_type]
2779+ {
2780+ return Err ( BinaryReaderError :: new (
2781+ "canonical option `realloc` uses a core function with an incorrect signature" ,
2782+ offset,
2783+ ) ) ;
2784+ }
2785+ }
2786+
27742787 Ok ( CanonicalOptions {
27752788 string_encoding,
27762789 memory,
0 commit comments