-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Limiting WASM exectuion time #67
Changes from 3 commits
1e5dba3
acca2df
06998dc
ae02208
b988ed1
c62d525
096c223
dca8fc3
952d39f
6b47360
bc962d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
#include "IR/Validate.h" | ||
#include <eos/chain/key_value_object.hpp> | ||
#include <eos/chain/account_object.hpp> | ||
#include <chrono> | ||
|
||
namespace eos { namespace chain { | ||
using namespace IR; | ||
|
@@ -17,6 +18,22 @@ namespace eos { namespace chain { | |
wasm_interface::wasm_interface() { | ||
} | ||
|
||
std::chrono::time_point<std::chrono::system_clock> checktimeStart; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This global needs to be moved into the wasm_interface member data There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also it should be an fc::time_point |
||
|
||
#ifdef NDEBUG | ||
const int CHECKTIME_LIMIT = 2000; | ||
#else | ||
const int CHECKTIME_LIMIT = 12000; | ||
#endif | ||
|
||
DEFINE_INTRINSIC_FUNCTION0(env,checktime,checktime,none) { | ||
auto dur = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now() - checktimeStart); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fc::time_point::now() is a shorter way to write this. |
||
if (dur.count() > CHECKTIME_LIMIT) { | ||
wlog("checktime called ${d}", ("d", dur.count())); | ||
throw checktime_exceeded(); | ||
} | ||
} | ||
|
||
DEFINE_INTRINSIC_FUNCTION4(env,store,store,none,i32,keyptr,i32,keylen,i32,valueptr,i32,valuelen ) { | ||
// ilog( "store ${keylen} ${vallen}", ("keylen",keylen)("vallen",valuelen) ); | ||
/* | ||
|
@@ -245,6 +262,11 @@ DEFINE_INTRINSIC_FUNCTION1(env,toUpper,toUpper,none,i32,charptr) { | |
// return 0; | ||
} | ||
|
||
DEFINE_INTRINSIC_FUNCTION1(env,loopControl,loopControl,i32,i32,i) { | ||
usleep(50); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this all about? We shouldn't be sleeping. I think this entire intrinsic can be removed. |
||
return i < 40; | ||
} | ||
|
||
wasm_interface& wasm_interface::get() { | ||
static wasm_interface* wasm = nullptr; | ||
if( !wasm ) | ||
|
@@ -279,6 +301,8 @@ DEFINE_INTRINSIC_FUNCTION1(env,toUpper,toUpper,none,i32,charptr) { | |
std::vector<Value> invokeArgs(1); | ||
invokeArgs[0] = U32(bytes); | ||
|
||
checktimeStart = std::chrono::system_clock::now(); | ||
|
||
auto result = Runtime::invokeFunction(alloc_function,invokeArgs); | ||
|
||
return &memoryRef<char>( current_memory, result.i32 ); | ||
|
@@ -309,6 +333,8 @@ DEFINE_INTRINSIC_FUNCTION1(env,toUpper,toUpper,none,i32,charptr) { | |
memset( memstart + state.mem_end, 0, ((1<<16) - state.mem_end) ); | ||
memcpy( memstart, state.init_memory.data(), state.mem_end); | ||
|
||
checktimeStart = std::chrono::system_clock::now(); | ||
|
||
Runtime::invokeFunction(call,args); | ||
} catch( const Runtime::Exception& e ) { | ||
edump((std::string(describeExceptionCause(e.cause)))); | ||
|
@@ -331,6 +357,8 @@ DEFINE_INTRINSIC_FUNCTION1(env,toUpper,toUpper,none,i32,charptr) { | |
return; /// if not found then it is a no-op | ||
} | ||
|
||
checktimeStart = std::chrono::system_clock::now(); | ||
|
||
const FunctionType* functionType = getFunctionType(apply); | ||
FC_ASSERT( functionType->parameters.size() == 0 ); | ||
|
||
|
@@ -407,7 +435,7 @@ DEFINE_INTRINSIC_FUNCTION1(env,toUpper,toUpper,none,i32,charptr) { | |
{ | ||
wlog( "LOADING CODE" ); | ||
Serialization::MemoryInputStream stream((const U8*)recipient.code.data(),recipient.code.size()); | ||
WASM::serialize(stream,*state.module); | ||
WASM::serializeWithInjection(stream,*state.module); | ||
|
||
RootResolver rootResolver; | ||
LinkResult linkResult = linkModule(*state.module,rootResolver); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should define exceptions in eos/chain/exceptions.hpp