@@ -85,7 +85,7 @@ memories_deinstantiate(WASMModuleInstance *module_inst,
85
85
{
86
86
uint32 i ;
87
87
if (memories ) {
88
- for (i = 0 ; i < count ; i ++ )
88
+ for (i = 0 ; i < count ; i ++ ) {
89
89
if (memories [i ]) {
90
90
#if WASM_ENABLE_MULTI_MODULE != 0
91
91
if (memories [i ]-> owner != module_inst )
@@ -109,8 +109,10 @@ memories_deinstantiate(WASMModuleInstance *module_inst,
109
109
mem_allocator_destroy (memories [i ]-> heap_handle );
110
110
memories [i ]-> heap_handle = NULL ;
111
111
}
112
+ wasm_runtime_free (memories [i ]-> memory_data );
112
113
wasm_runtime_free (memories [i ]);
113
114
}
115
+ }
114
116
wasm_runtime_free (memories );
115
117
}
116
118
(void )module_inst ;
@@ -125,7 +127,7 @@ memory_instantiate(WASMModuleInstance *module_inst,
125
127
{
126
128
WASMModule * module = module_inst -> module ;
127
129
WASMMemoryInstance * memory ;
128
- uint64 total_size , memory_data_size ;
130
+ uint64 memory_data_size ;
129
131
uint32 heap_offset = num_bytes_per_page * init_page_count ;
130
132
uint32 inc_page_count , aux_heap_base , global_idx ;
131
133
uint32 bytes_of_last_page , bytes_to_page_end ;
@@ -239,15 +241,17 @@ memory_instantiate(WASMModuleInstance *module_inst,
239
241
}
240
242
#endif
241
243
242
- total_size = offsetof(WASMMemoryInstance , memory_data )
243
- + memory_data_size ;
244
-
245
244
/* Allocate memory space, addr data and global data */
246
- if (!(memory = runtime_malloc (total_size ,
245
+ if (!(memory = runtime_malloc (( uint64 ) sizeof ( WASMMemoryInstance ) ,
247
246
error_buf , error_buf_size ))) {
248
247
return NULL ;
249
248
}
250
249
250
+ if (!(memory -> memory_data =
251
+ runtime_malloc (memory_data_size , error_buf , error_buf_size ))) {
252
+ goto fail1 ;
253
+ }
254
+
251
255
memory -> module_type = Wasm_Module_Bytecode ;
252
256
memory -> num_bytes_per_page = num_bytes_per_page ;
253
257
memory -> cur_page_count = init_page_count ;
@@ -257,21 +261,18 @@ memory_instantiate(WASMModuleInstance *module_inst,
257
261
memory -> heap_data_end = memory -> heap_data + heap_size ;
258
262
memory -> memory_data_end = memory -> memory_data + (uint32 )memory_data_size ;
259
263
260
- bh_assert ((uint32 )(memory -> memory_data_end - (uint8 * )memory )
261
- == (uint32 )total_size );
262
-
263
264
/* Initialize heap */
264
265
if (heap_size > 0
265
266
&& !(memory -> heap_handle =
266
267
mem_allocator_create (memory -> heap_data , heap_size ))) {
267
268
set_error_buf (error_buf , error_buf_size , "init app heap failed" );
268
- goto fail1 ;
269
+ goto fail2 ;
269
270
}
270
271
271
272
#if WASM_ENABLE_SHARED_MEMORY != 0
272
273
if (0 != os_mutex_init (& memory -> mem_lock )) {
273
274
set_error_buf (error_buf , error_buf_size , "init mutex failed" );
274
- goto fail2 ;
275
+ goto fail3 ;
275
276
}
276
277
if (is_shared_memory ) {
277
278
memory -> is_shared = true;
@@ -280,18 +281,20 @@ memory_instantiate(WASMModuleInstance *module_inst,
280
281
(WASMMemoryInstanceCommon * )memory )) {
281
282
set_error_buf (error_buf , error_buf_size ,
282
283
"allocate memory failed" );
283
- goto fail3 ;
284
+ goto fail4 ;
284
285
}
285
286
}
286
287
#endif
287
288
return memory ;
288
289
#if WASM_ENABLE_SHARED_MEMORY != 0
289
- fail3 :
290
+ fail4 :
290
291
os_mutex_destroy (& memory -> mem_lock );
291
- fail2 :
292
+ fail3 :
292
293
if (heap_size > 0 )
293
294
mem_allocator_destroy (memory -> heap_handle );
294
295
#endif
296
+ fail2 :
297
+ wasm_runtime_free (memory -> memory_data );
295
298
fail1 :
296
299
wasm_runtime_free (memory );
297
300
return NULL ;
@@ -1760,12 +1763,12 @@ wasm_get_native_addr_range(WASMModuleInstance *module_inst,
1760
1763
bool
1761
1764
wasm_enlarge_memory (WASMModuleInstance * module , uint32 inc_page_count )
1762
1765
{
1763
- WASMMemoryInstance * memory = module -> default_memory , * new_memory ;
1766
+ WASMMemoryInstance * memory = module -> default_memory ;
1767
+ uint8 * new_memory_data , * memory_data = memory -> memory_data ;
1764
1768
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 ;
1766
1770
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 ;
1769
1772
void * heap_handle_old = memory -> heap_handle ;
1770
1773
uint8 * heap_data_old = memory -> heap_data ;
1771
1774
@@ -1796,40 +1799,39 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
1796
1799
we cannot access its lock again. */
1797
1800
mem_allocator_destroy_lock (memory -> heap_handle );
1798
1801
}
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 ))) {
1801
1804
if (heap_size > 0 ) {
1802
1805
/* Restore heap's lock if memory re-alloc failed */
1803
1806
mem_allocator_reinit_lock (memory -> heap_handle );
1804
1807
}
1805
1808
return false;
1806
1809
}
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 );
1810
1813
}
1811
1814
1812
- memset (( uint8 * ) new_memory + total_size_old ,
1815
+ memset (new_memory_data + total_size_old ,
1813
1816
0 , (uint32 )total_size - total_size_old );
1814
1817
1815
1818
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 ,
1819
1822
heap_handle_old ) != 0 ) {
1820
1823
return false;
1821
1824
}
1822
1825
}
1823
1826
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 ;
1831
1834
1832
- module -> memories [0 ] = module -> default_memory = new_memory ;
1833
1835
return true;
1834
1836
}
1835
1837
@@ -2039,7 +2041,7 @@ wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst,
2039
2041
* module_inst -> memory_count ;
2040
2042
for (i = 0 ; i < module_inst -> memory_count ; i ++ ) {
2041
2043
WASMMemoryInstance * memory = module_inst -> memories [i ];
2042
- size = offsetof (WASMMemoryInstance , memory_data )
2044
+ size = sizeof (WASMMemoryInstance )
2043
2045
+ memory -> num_bytes_per_page * memory -> cur_page_count ;
2044
2046
mem_conspn -> memories_size += size ;
2045
2047
mem_conspn -> app_heap_size += memory -> heap_data_end
@@ -2079,3 +2081,40 @@ wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst,
2079
2081
}
2080
2082
#endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0)
2081
2083
|| (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