提交 45681a76 编写于 作者: B Bucky Kittinger

@DAWN-390 fixed issue with sbrk not clearing between actions, and added page...

@DAWN-390 fixed issue with sbrk not clearing between actions, and added page resizing to cache checkin to ensure page size goes back to default
上级 035f701b
......@@ -17,15 +17,23 @@ using wasm_double = boost::multiprecision::cpp_dec_float_50;
struct wasm_cache::entry {
entry(ModuleInstance *instance, Module *module)
: instance(instance), module(module) {}
: instance(instance), module(module)
{
}
ModuleInstance *instance;
Module *module;
ModuleInstance* instance;
Module* module;
};
struct wasm_context {
wasm_cache::entry &code;
apply_context &context;
wasm_context(wasm_cache::entry &code, apply_context& ctx) : code(code), context(ctx)
{
//initialize to minimum bytes and limit this to 32 bit space
sbrk_bytes = (1 << IR::numBytesPerPageLog2) > UINT32_MAX ? UINT32_MAX : 1 << IR::numBytesPerPageLog2;
}
wasm_cache::entry& code;
apply_context& context;
uint32_t sbrk_bytes;
};
struct wasm_interface_impl {
......
......@@ -342,18 +342,18 @@ namespace eosio { namespace chain {
wasm_cache::~wasm_cache() = default;
wasm_cache::entry &wasm_cache::checkout( const digest_type& code_id, const char* wasm_binary, size_t wasm_binary_size ) {
// see if there is an avaialble entry in the cache
// see if there is an available entry in the cache
auto result = _my->try_fetch_entry(code_id);
if (result) {
return (*result).get();
}
return _my->fetch_entry(code_id, wasm_binary, wasm_binary_size);
}
void wasm_cache::checkin(const digest_type& code_id, entry& code ) {
auto default_mem = Runtime::getDefaultMemory(code.instance);
Runtime::shrinkMemory(default_mem, Runtime::getMemoryNumPages(default_mem) - 1);
_my->return_entry(code_id, code);
}
......@@ -622,10 +622,12 @@ DEFINE_INTRINSIC_FUNCTION0(env,checktime,checktime,none) {
class context_aware_api {
public:
context_aware_api(wasm_interface& wasm)
:context(intrinsics_accessor::get_context(wasm).context), code(intrinsics_accessor::get_context(wasm).code)
:context(intrinsics_accessor::get_context(wasm).context), code(intrinsics_accessor::get_context(wasm).code),
sbrk_bytes(intrinsics_accessor::get_context(wasm).sbrk_bytes)
{}
protected:
uint32_t& sbrk_bytes;
wasm_cache::entry& code;
apply_context& context;
};
......@@ -811,8 +813,7 @@ class db_index_api : public context_aware_api {
class memory_api : public context_aware_api {
public:
using context_aware_api::context_aware_api;
char* memcpy( array_ptr<char> dest, array_ptr<const char> src, size_t length) {
return (char *)::memcpy(dest, src, length);
}
......@@ -827,27 +828,25 @@ class memory_api : public context_aware_api {
uint32_t sbrk(int num_bytes) {
// TODO: omitted checktime function from previous version of sbrk, may need to be put back in at some point
constexpr uint32_t NBPPL2 = IR::numBytesPerPageLog2;
constexpr uint32_t max_mem = 1024 * 1024;
constexpr uint32_t NBPPL2 = IR::numBytesPerPageLog2;
constexpr uint32_t MAX_MEM = 1024 * 1024;
const auto default_mem = Runtime::getDefaultMemory(code.instance);
const uint32_t num_pages = Runtime::getMemoryNumPages(default_mem);
// limit this min to 32 bit space
const uint32_t min_bytes = (num_pages << NBPPL2) > UINT32_MAX ? UINT32_MAX : num_pages << NBPPL2;
static uint32_t _num_bytes = min_bytes;
const uint32_t prev_num_bytes = _num_bytes;
const uint32_t prev_num_bytes = sbrk_bytes; //_num_bytes;
// round the absolute value of num_bytes to an alignment boundary
num_bytes = (num_bytes + 7) & ~7;
if ((num_bytes > 0) && (prev_num_bytes > (max_mem - num_bytes))) // test if allocating too much memory (overflowed)
if ((num_bytes > 0) && (prev_num_bytes > (MAX_MEM - num_bytes))) // test if allocating too much memory (overflowed)
throw eosio::chain::page_memory_error();
else if ((num_bytes < 0) && (prev_num_bytes < (min_bytes - num_bytes))) // test for underflow
throw eosio::chain::page_memory_error();
// update the number of bytes allocated, and compute the number of pages needed
_num_bytes += num_bytes;
const uint32_t num_desired_pages = (_num_bytes + IR::numBytesPerPage - 1) >> NBPPL2;
sbrk_bytes += num_bytes;
const uint32_t num_desired_pages = (sbrk_bytes + IR::numBytesPerPage - 1) >> NBPPL2;
// grow or shrink the memory to the desired number of pages
if (num_desired_pages > num_pages)
......@@ -856,7 +855,7 @@ class memory_api : public context_aware_api {
Runtime::shrinkMemory(default_mem, num_pages - num_desired_pages);
return prev_num_bytes;
}
}
};
class transaction_api : public context_aware_api {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册