1212#include "wasm_export.h"
1313#include <stdio.h>
1414
15- bool parse_single_alias (const uint8_t * * payload , const uint8_t * end , WASMComponentAliasDefinition * out , char * error_buf , uint32_t error_buf_size ) {
15+ bool
16+ parse_single_alias (const uint8_t * * payload , const uint8_t * end ,
17+ WASMComponentAliasDefinition * out , char * error_buf ,
18+ uint32_t error_buf_size )
19+ {
1620 const uint8_t * p = * payload ;
1721
1822 out -> sort = wasm_runtime_malloc (sizeof (WASMComponentSort ));
1923 if (!out -> sort ) {
20- set_error_buf_ex (error_buf , error_buf_size , "Failed to allocate memory for alias sort" );
24+ set_error_buf_ex (error_buf , error_buf_size ,
25+ "Failed to allocate memory for alias sort" );
2126 return false;
2227 }
2328
@@ -31,9 +36,11 @@ bool parse_single_alias(const uint8_t **payload, const uint8_t *end, WASMCompone
3136
3237 // Parse alias target using switch
3338 switch (tag ) {
34- case WASM_COMP_ALIAS_TARGET_EXPORT : {
39+ case WASM_COMP_ALIAS_TARGET_EXPORT :
40+ {
3541 uint64_t instance_idx = 0 ;
36- if (!read_leb ((uint8_t * * )& p , end , 32 , false, & instance_idx , error_buf , error_buf_size )) {
42+ if (!read_leb ((uint8_t * * )& p , end , 32 , false, & instance_idx ,
43+ error_buf , error_buf_size )) {
3744 return false;
3845 }
3946 WASMComponentCoreName * name = NULL ;
@@ -45,27 +52,34 @@ bool parse_single_alias(const uint8_t **payload, const uint8_t *end, WASMCompone
4552 out -> target .exported .name = name ;
4653 break ;
4754 }
48- case WASM_COMP_ALIAS_TARGET_CORE_EXPORT : {
55+ case WASM_COMP_ALIAS_TARGET_CORE_EXPORT :
56+ {
4957 uint64_t core_instance_idx = 0 ;
50- if (!read_leb ((uint8_t * * )& p , end , 32 , false, & core_instance_idx , error_buf , error_buf_size )) {
58+ if (!read_leb ((uint8_t * * )& p , end , 32 , false, & core_instance_idx ,
59+ error_buf , error_buf_size )) {
5160 return false;
5261 }
5362 WASMComponentCoreName * core_name = NULL ;
54- if (!parse_core_name (& p , end , & core_name , error_buf , error_buf_size )) {
63+ if (!parse_core_name (& p , end , & core_name , error_buf ,
64+ error_buf_size )) {
5565 return false;
5666 }
5767 out -> alias_target_type = WASM_COMP_ALIAS_TARGET_CORE_EXPORT ;
58- out -> target .core_exported .instance_idx = (uint32_t )core_instance_idx ;
68+ out -> target .core_exported .instance_idx =
69+ (uint32_t )core_instance_idx ;
5970 out -> target .core_exported .name = core_name ;
6071 break ;
6172 }
62- case WASM_COMP_ALIAS_TARGET_OUTER : {
73+ case WASM_COMP_ALIAS_TARGET_OUTER :
74+ {
6375 uint64_t outer_ct = 0 ;
64- if (!read_leb ((uint8_t * * )& p , end , 32 , false, & outer_ct , error_buf , error_buf_size )) {
76+ if (!read_leb ((uint8_t * * )& p , end , 32 , false, & outer_ct , error_buf ,
77+ error_buf_size )) {
6578 return false;
6679 }
6780 uint64_t outer_idx = 0 ;
68- if (!read_leb ((uint8_t * * )& p , end , 32 , false, & outer_idx , error_buf , error_buf_size )) {
81+ if (!read_leb ((uint8_t * * )& p , end , 32 , false, & outer_idx , error_buf ,
82+ error_buf_size )) {
6983 return false;
7084 }
7185 out -> alias_target_type = WASM_COMP_ALIAS_TARGET_OUTER ;
@@ -78,13 +92,16 @@ bool parse_single_alias(const uint8_t **payload, const uint8_t *end, WASMCompone
7892 || (out -> sort -> sort == WASM_COMP_SORT_CORE_SORT
7993 && out -> sort -> core_sort == WASM_COMP_CORE_SORT_MODULE );
8094 if (!valid_outer_sort ) {
81- set_error_buf_ex (error_buf , error_buf_size , "Outer alias sort must be type, component, or core module" );
95+ set_error_buf_ex (
96+ error_buf , error_buf_size ,
97+ "Outer alias sort must be type, component, or core module" );
8298 return false;
8399 }
84100 break ;
85101 }
86102 default :
87- snprintf (error_buf , error_buf_size , "Unknown alias target type: 0x%02X" , tag );
103+ snprintf (error_buf , error_buf_size ,
104+ "Unknown alias target type: 0x%02X" , tag );
88105 return false;
89106 }
90107
@@ -93,10 +110,18 @@ bool parse_single_alias(const uint8_t **payload, const uint8_t *end, WASMCompone
93110}
94111
95112// Section 6: alias section
96- bool wasm_component_parse_alias_section (const uint8_t * * payload , uint32_t payload_len , WASMComponentAliasSection * out , char * error_buf , uint32_t error_buf_size , uint32_t * consumed_len ) {
113+ bool
114+ wasm_component_parse_alias_section (const uint8_t * * payload ,
115+ uint32_t payload_len ,
116+ WASMComponentAliasSection * out ,
117+ char * error_buf , uint32_t error_buf_size ,
118+ uint32_t * consumed_len )
119+ {
97120 if (!payload || !* payload || payload_len == 0 || !out ) {
98- set_error_buf_ex (error_buf , error_buf_size , "Invalid payload or output pointer" );
99- if (consumed_len ) * consumed_len = 0 ;
121+ set_error_buf_ex (error_buf , error_buf_size ,
122+ "Invalid payload or output pointer" );
123+ if (consumed_len )
124+ * consumed_len = 0 ;
100125 return false;
101126 }
102127
@@ -106,54 +131,66 @@ bool wasm_component_parse_alias_section(const uint8_t **payload, uint32_t payloa
106131
107132 // Read alias count
108133 uint64_t alias_count_leb = 0 ;
109- if (!read_leb ((uint8_t * * )& p , end , 32 , false, & alias_count_leb , error_buf , error_buf_size )) {
110- if (consumed_len ) * consumed_len = (uint32_t )(p - * payload );
134+ if (!read_leb ((uint8_t * * )& p , end , 32 , false, & alias_count_leb , error_buf ,
135+ error_buf_size )) {
136+ if (consumed_len )
137+ * consumed_len = (uint32_t )(p - * payload );
111138 return false;
112139 }
113140
114141 alias_count = (uint32_t )alias_count_leb ;
115142
116143 out -> count = alias_count ;
117144 if (alias_count > 0 ) {
118- out -> aliases = wasm_runtime_malloc (sizeof (WASMComponentAliasDefinition ) * alias_count );
145+ out -> aliases = wasm_runtime_malloc (sizeof (WASMComponentAliasDefinition )
146+ * alias_count );
119147 if (!out -> aliases ) {
120- if (consumed_len ) * consumed_len = (uint32_t )(p - * payload );
148+ if (consumed_len )
149+ * consumed_len = (uint32_t )(p - * payload );
121150 return false;
122151 }
123152 // Zero-initialize the aliases array
124- memset (out -> aliases , 0 , sizeof (WASMComponentAliasDefinition ) * alias_count );
153+ memset (out -> aliases , 0 ,
154+ sizeof (WASMComponentAliasDefinition ) * alias_count );
125155
126156 for (uint32_t i = 0 ; i < alias_count ; ++ i ) {
127157 // Allocate memory for the sort field
128- if (!parse_single_alias (& p , end , & out -> aliases [i ], error_buf , error_buf_size )) {
129- if (consumed_len ) * consumed_len = (uint32_t )(p - * payload );
130- set_error_buf_ex (error_buf , error_buf_size , "Failed to parse alias %d" , i );
158+ if (!parse_single_alias (& p , end , & out -> aliases [i ], error_buf ,
159+ error_buf_size )) {
160+ if (consumed_len )
161+ * consumed_len = (uint32_t )(p - * payload );
162+ set_error_buf_ex (error_buf , error_buf_size ,
163+ "Failed to parse alias %d" , i );
131164 return false;
132165 }
133166 }
134167 }
135168
136- if (consumed_len ) * consumed_len = (uint32_t )(p - * payload );
169+ if (consumed_len )
170+ * consumed_len = (uint32_t )(p - * payload );
137171
138172 // If binaries use alias ids, this parser will need to be extended.
139173 return true;
140174}
141175
142176// Individual section free functions
143- void wasm_component_free_alias_section (WASMComponentSection * section ) {
144- if (!section || !section -> parsed .alias_section ) return ;
145-
177+ void
178+ wasm_component_free_alias_section (WASMComponentSection * section )
179+ {
180+ if (!section || !section -> parsed .alias_section )
181+ return ;
182+
146183 WASMComponentAliasSection * alias_sec = section -> parsed .alias_section ;
147184 if (alias_sec -> aliases ) {
148185 for (uint32_t j = 0 ; j < alias_sec -> count ; ++ j ) {
149186 WASMComponentAliasDefinition * alias = & alias_sec -> aliases [j ];
150-
187+
151188 // Free sort
152189 if (alias -> sort ) {
153190 wasm_runtime_free (alias -> sort );
154191 alias -> sort = NULL ;
155192 }
156-
193+
157194 // Free target-specific data
158195 switch (alias -> alias_target_type ) {
159196 case WASM_COMP_ALIAS_TARGET_EXPORT :
@@ -180,4 +217,4 @@ void wasm_component_free_alias_section(WASMComponentSection *section) {
180217 }
181218 wasm_runtime_free (alias_sec );
182219 section -> parsed .alias_section = NULL ;
183- }
220+ }
0 commit comments