@@ -882,15 +882,22 @@ impl Bindgen for FunctionBindgen<'_, '_> {
882882 ) ;
883883 let ret_area = self . locals . tmp ( "retArea" ) ;
884884
885+ let array_size = if align > 1 {
886+ // Add one additional element in case the starting address is not aligned
887+ format ! ( "{array_size} * {list}.Count + 1" )
888+ } else {
889+ format ! ( "{array_size} * {list}.Count" )
890+ } ;
891+
885892 match realloc {
886893 None => {
887894 self . needs_cleanup = true ;
888895 uwrite ! ( self . src,
889896 "
890897 void* {address};
891898 if (({size} * {list}.Count) < 1024) {{
892- var {ret_area} = stackalloc {element_type}[{array_size}*{list}.Count ];
893- {address} = (void*)((int){ret_area});
899+ var {ret_area} = stackalloc {element_type}[{array_size}];
900+ {address} = (void*)((( int){ret_area}) + ({align} - 1) & -{align });
894901 }}
895902 else
896903 {{
@@ -1288,10 +1295,17 @@ impl Bindgen for FunctionBindgen<'_, '_> {
12881295 // to align the allocation via the stackalloc command, unlike with a fixed array where the pointer will be aligned.
12891296 // We get the final ptr to pass to the wasm runtime by shifting to the
12901297 // correctly aligned pointer (sometimes it can be already aligned).
1298+ let array_size = if self . import_return_pointer_area_align > 1 {
1299+ // Add one additional element in case the starting address is not aligned
1300+ array_size + 1
1301+ } else {
1302+ array_size
1303+ } ;
1304+
12911305 uwrite ! (
12921306 self . src,
12931307 "
1294- var {ret_area} = stackalloc {element_type}[{array_size}+1 ];
1308+ var {ret_area} = stackalloc {element_type}[{array_size}];
12951309 var {ptr} = ((int){ret_area}) + ({align} - 1) & -{align};
12961310 " ,
12971311 align = align. align_wasm32( )
0 commit comments