Fix data/elem drop (#2747)
Currently, `data.drop` instruction is implemented by directly modifying the underlying module. It breaks use cases where you have multiple instances sharing a single loaded module. `elem.drop` has the same problem too. This PR fixes the issue by keeping track of which data/elem segments have been dropped by using bitmaps for each module instances separately, and add a sample to demonstrate the issue and make the CI run it. Also add a missing check of dropped elements to the fast-jit `table.init`. Fixes: https://github.com/bytecodealliance/wasm-micro-runtime/issues/2735 Fixes: https://github.com/bytecodealliance/wasm-micro-runtime/issues/2772
This commit is contained in:
27
core/shared/utils/bh_bitmap.c
Normal file
27
core/shared/utils/bh_bitmap.c
Normal file
@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright (C) 2021 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "bh_bitmap.h"
|
||||
|
||||
bh_bitmap *
|
||||
bh_bitmap_new(uintptr_t begin_index, unsigned bitnum)
|
||||
{
|
||||
bh_bitmap *bitmap;
|
||||
uint32 bitmap_size = (bitnum + 7) / 8;
|
||||
uint32 total_size = offsetof(bh_bitmap, map) + bitmap_size;
|
||||
|
||||
if (bitnum > UINT32_MAX - 7 || total_size < offsetof(bh_bitmap, map)
|
||||
|| (total_size - offsetof(bh_bitmap, map)) != bitmap_size) {
|
||||
return NULL; /* integer overflow */
|
||||
}
|
||||
|
||||
if ((bitmap = BH_MALLOC(total_size)) != NULL) {
|
||||
memset(bitmap, 0, total_size);
|
||||
bitmap->begin_index = begin_index;
|
||||
bitmap->end_index = begin_index + bitnum;
|
||||
}
|
||||
|
||||
return bitmap;
|
||||
}
|
||||
114
core/shared/utils/bh_bitmap.h
Normal file
114
core/shared/utils/bh_bitmap.h
Normal file
@ -0,0 +1,114 @@
|
||||
/*
|
||||
* Copyright (C) 2021 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#ifndef _BH_BITMAP_H
|
||||
#define _BH_BITMAP_H
|
||||
|
||||
#include "bh_platform.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* A simple fixed size bitmap.
|
||||
*/
|
||||
typedef struct bh_bitmap {
|
||||
/* The first valid bit index. */
|
||||
uintptr_t begin_index;
|
||||
|
||||
/* The last valid bit index plus one. */
|
||||
uintptr_t end_index;
|
||||
|
||||
/* The bitmap. */
|
||||
uint8 map[1];
|
||||
} bh_bitmap;
|
||||
|
||||
/**
|
||||
* Create a new bitmap.
|
||||
*
|
||||
* @param begin_index the first valid bit index
|
||||
* @param bitnum maximal bit number of the bitmap.
|
||||
*
|
||||
* @return the new bitmap if succeeds, NULL otherwise.
|
||||
*/
|
||||
bh_bitmap *
|
||||
bh_bitmap_new(uintptr_t begin_index, unsigned bitnum);
|
||||
|
||||
/**
|
||||
* Delete a bitmap.
|
||||
*
|
||||
* @param bitmap the bitmap to be deleted
|
||||
*/
|
||||
static inline void
|
||||
bh_bitmap_delete(bh_bitmap *bitmap)
|
||||
{
|
||||
if (bitmap != NULL)
|
||||
BH_FREE(bitmap);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the given index is in the range of the bitmap.
|
||||
*
|
||||
* @param bitmap the bitmap
|
||||
* @param n the bit index
|
||||
*
|
||||
* @return true if the index is in range, false otherwise
|
||||
*/
|
||||
static inline bool
|
||||
bh_bitmap_is_in_range(bh_bitmap *bitmap, unsigned n)
|
||||
{
|
||||
return n >= bitmap->begin_index && n < bitmap->end_index;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a bit in the bitmap
|
||||
*
|
||||
* @param bitmap the bitmap
|
||||
* @param n the n-th bit to be get
|
||||
*
|
||||
* @return value of the bit
|
||||
*/
|
||||
static inline int
|
||||
bh_bitmap_get_bit(bh_bitmap *bitmap, unsigned n)
|
||||
{
|
||||
unsigned idx = n - bitmap->begin_index;
|
||||
bh_assert(n >= bitmap->begin_index && n < bitmap->end_index);
|
||||
return (bitmap->map[idx / 8] >> (idx % 8)) & 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a bit in the bitmap.
|
||||
*
|
||||
* @param bitmap the bitmap
|
||||
* @param n the n-th bit to be set
|
||||
*/
|
||||
static inline void
|
||||
bh_bitmap_set_bit(bh_bitmap *bitmap, unsigned n)
|
||||
{
|
||||
unsigned idx = n - bitmap->begin_index;
|
||||
bh_assert(n >= bitmap->begin_index && n < bitmap->end_index);
|
||||
bitmap->map[idx / 8] |= 1 << (idx % 8);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear a bit in the bitmap.
|
||||
*
|
||||
* @param bitmap the bitmap
|
||||
* @param n the n-th bit to be cleared
|
||||
*/
|
||||
static inline void
|
||||
bh_bitmap_clear_bit(bh_bitmap *bitmap, unsigned n)
|
||||
{
|
||||
unsigned idx = n - bitmap->begin_index;
|
||||
bh_assert(n >= bitmap->begin_index && n < bitmap->end_index);
|
||||
bitmap->map[idx / 8] &= ~(1 << (idx % 8));
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user