@@ -178,16 +178,16 @@ wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args)
178178 }
179179
180180 size = align_uint (size , os_getpagesize ());
181- heap -> size = size ;
182- heap -> start_off_mem64 = UINT64_MAX - heap -> size + 1 ;
183- heap -> start_off_mem32 = UINT32_MAX - heap -> size + 1 ;
184- heap -> attached_count = 0 ;
185-
186181 if (size > APP_HEAP_SIZE_MAX || size < APP_HEAP_SIZE_MIN ) {
187182 LOG_WARNING ("Invalid size of shared heap" );
188183 goto fail2 ;
189184 }
190185
186+ heap -> size = size ;
187+ heap -> start_off_mem64 = UINT64_MAX - heap -> size + 1 ;
188+ heap -> start_off_mem32 = UINT32_MAX - heap -> size + 1 ;
189+ heap -> attached_count = 0 ;
190+
191191 if (init_args -> pre_allocated_addr != NULL ) {
192192 /* Create shared heap from a pre allocated buffer, its size need to
193193 * align with system page */
@@ -275,6 +275,13 @@ wasm_runtime_chain_shared_heaps(WASMSharedHeap *head, WASMSharedHeap *body)
275275 os_mutex_unlock (& shared_heap_list_lock );
276276 return NULL ;
277277 }
278+ if (cur == head && cur -> chain_next ) {
279+ LOG_WARNING (
280+ "To create shared heap chain, the 'head' shared heap can't "
281+ "already be the 'head' in another a chain" );
282+ os_mutex_unlock (& shared_heap_list_lock );
283+ return NULL ;
284+ }
278285 }
279286 for (cur = body ; cur ; cur = cur -> chain_next ) {
280287 if (cur -> heap_handle && heap_handle_exist ) {
@@ -519,6 +526,10 @@ wasm_runtime_attach_shared_heap(WASMModuleInstanceCommon *module_inst,
519526void
520527wasm_runtime_detach_shared_heap_internal (WASMModuleInstanceCommon * module_inst )
521528{
529+ /* Reset shared_heap_end_off = UINT64/32_MAX - 1 to handling a corner case,
530+ app_offset >= shared_heap_start && app_offset <= shared_heap_end-bytes+1
531+ when bytes=1 and both e->shared_heap_start_off and e->shared_heap_end_off
532+ is 0xffffffff */
522533#if WASM_ENABLE_INTERP != 0
523534 if (module_inst -> module_type == Wasm_Module_Bytecode ) {
524535 WASMModuleInstanceExtra * e =
@@ -614,19 +625,17 @@ is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
614625 shared_heap_start =
615626 (uint64 )get_last_used_shared_heap_start_offset (module_inst );
616627 shared_heap_end = (uint64 )get_last_used_shared_heap_end_offset (module_inst );
617- if (app_offset >= shared_heap_start
618- && app_offset <= shared_heap_end - bytes + 1
619- && bytes - 1 <= shared_heap_end ) {
628+ if (bytes - 1 <= shared_heap_end && app_offset >= shared_heap_start
629+ && app_offset <= shared_heap_end - bytes + 1 ) {
620630 return true;
621631 }
622632
623633 /* Early stop for app start address not in the shared heap(chain) at all */
624634 shared_heap_start =
625635 is_memory64 ? heap -> start_off_mem64 : heap -> start_off_mem32 ;
626636 shared_heap_end = is_memory64 ? UINT64_MAX : UINT32_MAX ;
627- if (app_offset < shared_heap_start
628- || app_offset > shared_heap_end - bytes + 1
629- || bytes - 1 > shared_heap_end ) {
637+ if (bytes - 1 > shared_heap_end || app_offset < shared_heap_start
638+ || app_offset > shared_heap_end - bytes + 1 ) {
630639 goto fail ;
631640 }
632641
@@ -636,9 +645,8 @@ is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
636645 shared_heap_start =
637646 is_memory64 ? cur -> start_off_mem64 : cur -> start_off_mem32 ;
638647 shared_heap_end = shared_heap_start - 1 + cur -> size ;
639- if (app_offset >= shared_heap_start
640- && app_offset <= shared_heap_end - bytes + 1
641- && bytes - 1 <= shared_heap_end ) {
648+ if (bytes - 1 <= shared_heap_end && app_offset >= shared_heap_start
649+ && app_offset <= shared_heap_end - bytes + 1 ) {
642650 update_last_used_shared_heap (module_inst , cur , is_memory64 );
643651 return true;
644652 }
@@ -1075,7 +1083,7 @@ wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst_comm,
10751083 shared_heap_base_addr_adj =
10761084 (char * )get_last_used_shared_heap_base_addr_adj (module_inst_comm );
10771085 str = shared_heap_base_addr_adj + app_str_offset ;
1078- str_end = shared_heap_base_addr_adj + shared_heap_end_off ;
1086+ str_end = shared_heap_base_addr_adj + shared_heap_end_off + 1 ;
10791087 }
10801088 else
10811089#endif
@@ -1358,7 +1366,8 @@ wasm_check_app_addr_and_convert(WASMModuleInstance *module_inst, bool is_str,
13581366
13591367 /* The whole string must be in the shared heap */
13601368 str = (const char * )native_addr ;
1361- str_end = (const char * )shared_heap_base_addr_adj + shared_heap_end_off ;
1369+ str_end =
1370+ (const char * )shared_heap_base_addr_adj + shared_heap_end_off + 1 ;
13621371 while (str < str_end && * str != '\0' )
13631372 str ++ ;
13641373 if (str == str_end ) {
0 commit comments