@@ -85,7 +85,7 @@ memories_deinstantiate(WASMModuleInstance *module_inst,
8585{
8686 uint32 i ;
8787 if (memories ) {
88- for (i = 0 ; i < count ; i ++ )
88+ for (i = 0 ; i < count ; i ++ ) {
8989 if (memories [i ]) {
9090#if WASM_ENABLE_MULTI_MODULE != 0
9191 if (memories [i ]-> owner != module_inst )
@@ -109,8 +109,10 @@ memories_deinstantiate(WASMModuleInstance *module_inst,
109109 mem_allocator_destroy (memories [i ]-> heap_handle );
110110 memories [i ]-> heap_handle = NULL ;
111111 }
112+ wasm_runtime_free (memories [i ]-> memory_data );
112113 wasm_runtime_free (memories [i ]);
113114 }
115+ }
114116 wasm_runtime_free (memories );
115117 }
116118 (void )module_inst ;
@@ -125,7 +127,7 @@ memory_instantiate(WASMModuleInstance *module_inst,
125127{
126128 WASMModule * module = module_inst -> module ;
127129 WASMMemoryInstance * memory ;
128- uint64 total_size , memory_data_size ;
130+ uint64 memory_data_size ;
129131 uint32 heap_offset = num_bytes_per_page * init_page_count ;
130132 uint32 inc_page_count , aux_heap_base , global_idx ;
131133 uint32 bytes_of_last_page , bytes_to_page_end ;
@@ -239,15 +241,17 @@ memory_instantiate(WASMModuleInstance *module_inst,
239241 }
240242#endif
241243
242- total_size = offsetof(WASMMemoryInstance , memory_data )
243- + memory_data_size ;
244-
245244 /* Allocate memory space, addr data and global data */
246- if (!(memory = runtime_malloc (total_size ,
245+ if (!(memory = runtime_malloc (( uint64 ) sizeof ( WASMMemoryInstance ) ,
247246 error_buf , error_buf_size ))) {
248247 return NULL ;
249248 }
250249
250+ if (!(memory -> memory_data =
251+ runtime_malloc (memory_data_size , error_buf , error_buf_size ))) {
252+ goto fail1 ;
253+ }
254+
251255 memory -> module_type = Wasm_Module_Bytecode ;
252256 memory -> num_bytes_per_page = num_bytes_per_page ;
253257 memory -> cur_page_count = init_page_count ;
@@ -257,21 +261,18 @@ memory_instantiate(WASMModuleInstance *module_inst,
257261 memory -> heap_data_end = memory -> heap_data + heap_size ;
258262 memory -> memory_data_end = memory -> memory_data + (uint32 )memory_data_size ;
259263
260- bh_assert ((uint32 )(memory -> memory_data_end - (uint8 * )memory )
261- == (uint32 )total_size );
262-
263264 /* Initialize heap */
264265 if (heap_size > 0
265266 && !(memory -> heap_handle =
266267 mem_allocator_create (memory -> heap_data , heap_size ))) {
267268 set_error_buf (error_buf , error_buf_size , "init app heap failed" );
268- goto fail1 ;
269+ goto fail2 ;
269270 }
270271
271272#if WASM_ENABLE_SHARED_MEMORY != 0
272273 if (0 != os_mutex_init (& memory -> mem_lock )) {
273274 set_error_buf (error_buf , error_buf_size , "init mutex failed" );
274- goto fail2 ;
275+ goto fail3 ;
275276 }
276277 if (is_shared_memory ) {
277278 memory -> is_shared = true;
@@ -280,18 +281,20 @@ memory_instantiate(WASMModuleInstance *module_inst,
280281 (WASMMemoryInstanceCommon * )memory )) {
281282 set_error_buf (error_buf , error_buf_size ,
282283 "allocate memory failed" );
283- goto fail3 ;
284+ goto fail4 ;
284285 }
285286 }
286287#endif
287288 return memory ;
288289#if WASM_ENABLE_SHARED_MEMORY != 0
289- fail3 :
290+ fail4 :
290291 os_mutex_destroy (& memory -> mem_lock );
291- fail2 :
292+ fail3 :
292293 if (heap_size > 0 )
293294 mem_allocator_destroy (memory -> heap_handle );
294295#endif
296+ fail2 :
297+ wasm_runtime_free (memory -> memory_data );
295298fail1 :
296299 wasm_runtime_free (memory );
297300 return NULL ;
@@ -1760,12 +1763,12 @@ wasm_get_native_addr_range(WASMModuleInstance *module_inst,
17601763bool
17611764wasm_enlarge_memory (WASMModuleInstance * module , uint32 inc_page_count )
17621765{
1763- WASMMemoryInstance * memory = module -> default_memory , * new_memory ;
1766+ WASMMemoryInstance * memory = module -> default_memory ;
1767+ uint8 * new_memory_data , * memory_data = memory -> memory_data ;
17641768 uint32 heap_size = memory -> heap_data_end - memory -> heap_data ;
1765- uint32 total_size_old = memory -> memory_data_end - ( uint8 * ) memory ;
1769+ uint32 total_size_old = memory -> memory_data_end - memory_data ;
17661770 uint32 total_page_count = inc_page_count + memory -> cur_page_count ;
1767- uint64 total_size = offsetof(WASMMemoryInstance , memory_data )
1768- + memory -> num_bytes_per_page * (uint64 )total_page_count ;
1771+ uint64 total_size = memory -> num_bytes_per_page * (uint64 )total_page_count ;
17691772 void * heap_handle_old = memory -> heap_handle ;
17701773 uint8 * heap_data_old = memory -> heap_data ;
17711774
@@ -1796,40 +1799,39 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
17961799 we cannot access its lock again. */
17971800 mem_allocator_destroy_lock (memory -> heap_handle );
17981801 }
1799- if (!(new_memory = wasm_runtime_realloc (memory , (uint32 )total_size ))) {
1800- if (!(new_memory = wasm_runtime_malloc ((uint32 )total_size ))) {
1802+ if (!(new_memory_data = wasm_runtime_realloc (memory_data , (uint32 )total_size ))) {
1803+ if (!(new_memory_data = wasm_runtime_malloc ((uint32 )total_size ))) {
18011804 if (heap_size > 0 ) {
18021805 /* Restore heap's lock if memory re-alloc failed */
18031806 mem_allocator_reinit_lock (memory -> heap_handle );
18041807 }
18051808 return false;
18061809 }
1807- bh_memcpy_s (( uint8 * ) new_memory , (uint32 )total_size ,
1808- ( uint8 * ) memory , total_size_old );
1809- wasm_runtime_free (memory );
1810+ bh_memcpy_s (new_memory_data , (uint32 )total_size ,
1811+ memory_data , total_size_old );
1812+ wasm_runtime_free (memory_data );
18101813 }
18111814
1812- memset (( uint8 * ) new_memory + total_size_old ,
1815+ memset (new_memory_data + total_size_old ,
18131816 0 , (uint32 )total_size - total_size_old );
18141817
18151818 if (heap_size > 0 ) {
1816- new_memory -> heap_handle = (uint8 * )heap_handle_old +
1817- (( uint8 * ) new_memory - ( uint8 * ) memory );
1818- if (mem_allocator_migrate (new_memory -> heap_handle ,
1819+ memory -> heap_handle = (uint8 * )heap_handle_old +
1820+ ( new_memory_data - memory_data );
1821+ if (mem_allocator_migrate (memory -> heap_handle ,
18191822 heap_handle_old ) != 0 ) {
18201823 return false;
18211824 }
18221825 }
18231826
1824- new_memory -> cur_page_count = total_page_count ;
1825- new_memory -> heap_data = heap_data_old +
1826- (( uint8 * ) new_memory - ( uint8 * ) memory );
1827- new_memory -> heap_data_end = new_memory -> heap_data + heap_size ;
1828- new_memory -> memory_data_end = new_memory -> memory_data
1829- + new_memory -> num_bytes_per_page
1830- * total_page_count ;
1827+ memory -> memory_data = new_memory_data ;
1828+ memory -> cur_page_count = total_page_count ;
1829+ memory -> heap_data = heap_data_old + ( new_memory_data - memory_data );
1830+ memory -> heap_data_end = memory -> heap_data + heap_size ;
1831+ memory -> memory_data_end = memory -> memory_data
1832+ + memory -> num_bytes_per_page
1833+ * total_page_count ;
18311834
1832- module -> memories [0 ] = module -> default_memory = new_memory ;
18331835 return true;
18341836}
18351837
@@ -2039,7 +2041,7 @@ wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst,
20392041 * module_inst -> memory_count ;
20402042 for (i = 0 ; i < module_inst -> memory_count ; i ++ ) {
20412043 WASMMemoryInstance * memory = module_inst -> memories [i ];
2042- size = offsetof (WASMMemoryInstance , memory_data )
2044+ size = sizeof (WASMMemoryInstance )
20432045 + memory -> num_bytes_per_page * memory -> cur_page_count ;
20442046 mem_conspn -> memories_size += size ;
20452047 mem_conspn -> app_heap_size += memory -> heap_data_end
@@ -2079,3 +2081,40 @@ wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst,
20792081}
20802082#endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0)
20812083 || (WASM_ENABLE_MEMORY_TRACING != 0) */
2084+
2085+ #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
2086+ void
2087+ wasm_interp_dump_call_stack (struct WASMExecEnv * exec_env )
2088+ {
2089+ WASMModuleInstance * module_inst =
2090+ (WASMModuleInstance * )wasm_exec_env_get_module_inst (exec_env );
2091+ WASMInterpFrame * cur_frame =
2092+ wasm_exec_env_get_cur_frame (exec_env );
2093+ WASMFunctionInstance * func_inst ;
2094+ const char * func_name = NULL ;
2095+ uint32 n ;
2096+
2097+ os_printf ("\n" );
2098+ for (n = 0 ; cur_frame && cur_frame -> function ; n ++ ) {
2099+ func_inst = cur_frame -> function ;
2100+
2101+ if (func_inst -> is_import_func ) {
2102+ func_name = func_inst -> u .func_import -> field_name ;
2103+ }
2104+ else {
2105+ func_name = func_inst -> u .func -> field_name ;
2106+ }
2107+
2108+ /* function name not exported, print number instead */
2109+ if (func_name == NULL ) {
2110+ os_printf ("#%02d $f%d \n" , n , func_inst - module_inst -> functions );
2111+ }
2112+ else {
2113+ os_printf ("#%02d %s \n" , n , func_name );
2114+ }
2115+
2116+ cur_frame = cur_frame -> prev_frame ;
2117+ }
2118+ os_printf ("\n" );
2119+ }
2120+ #endif /* end of WASM_ENABLE_CUSTOM_NAME_SECTION */
0 commit comments