From 725c4e73ea7699ea86c0ad95253203a1567b0718 Mon Sep 17 00:00:00 2001 From: Matias Romeo Date: Mon, 26 Jun 2017 05:38:14 -0300 Subject: [PATCH] Fix dereference of DataStream struct and use memoryArrayPtr for strings with length --- libraries/chain/wasm_interface.cpp | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/libraries/chain/wasm_interface.cpp b/libraries/chain/wasm_interface.cpp index 80d10a2f18b..38868a64453 100644 --- a/libraries/chain/wasm_interface.cpp +++ b/libraries/chain/wasm_interface.cpp @@ -29,8 +29,8 @@ DEFINE_INTRINSIC_FUNCTION4(env,store,store,none,i32,keyptr,i32,keylen,i32,valuep auto& db = wasm.current_apply_context->mutable_db; auto& scope = wasm.current_apply_context->scope; auto mem = wasm.current_memory; - char* key = &memoryRef( mem, keyptr ); - char* value = &memoryRef( mem, valueptr ); + char* key = memoryArrayPtr( mem, keyptr, keylen); + char* value = memoryArrayPtr( mem, valueptr, valuelen); string keystr( key, key+keylen); // if( valuelen == 8 ) idump(( *((int64_t*)value))); @@ -60,7 +60,7 @@ DEFINE_INTRINSIC_FUNCTION2(env,remove,remove,i32,i32,keyptr,i32,keylen) { auto& db = wasm.current_apply_context->mutable_db; auto& scope = wasm.current_apply_context->scope; auto mem = wasm.current_memory; - char* key = &memoryRef( mem, keyptr ); + char* key = memoryArrayPtr( mem, keyptr, keylen); string keystr( key, key+keylen); const auto* obj = db.find( boost::make_tuple(scope, keystr) ); @@ -95,7 +95,7 @@ DEFINE_INTRINSIC_FUNCTION2(env,Varint_unpack,Varint_unpack,none,i32,streamptr,i3 auto& wasm = wasm_interface::get(); auto mem = wasm.current_memory; - uint32_t* stream = &memoryRef( mem, streamptr ); + uint32_t* stream = memoryArrayPtr( mem, streamptr, 3 ); const char* pos = &memoryRef( mem, stream[1] ); const char* end = &memoryRef( mem, stream[2] ); uint32_t& value = memoryRef( mem, valueptr ); @@ -113,7 +113,7 @@ DEFINE_INTRINSIC_FUNCTION2(env,AccountName_unpack,AccountName_unpack,none,i32,st auto mem = wasm.current_memory; - uint32_t* stream = &memoryRef( mem, streamptr ); + uint32_t* stream = memoryArrayPtr( mem, streamptr, 3 ); const char* pos = &memoryRef( mem, stream[1] ); const char* end = &memoryRef( mem, stream[2] ); AccountName* name = &memoryRef( mem, accountptr ); @@ -139,8 +139,8 @@ DEFINE_INTRINSIC_FUNCTION4(env,load,load,i32,i32,keyptr,i32,keylen,i32,valueptr, auto& db = wasm.current_apply_context->mutable_db; auto& scope = wasm.current_apply_context->scope; auto mem = wasm.current_memory; - char* key = &memoryRef( mem, keyptr ); - char* value = &memoryRef( mem, valueptr ); + char* key = memoryArrayPtr( mem, keyptr, keylen ); + char* value = memoryArrayPtr( mem, valueptr, valuelen ); string keystr( key, key+keylen); const auto* obj = db.find( boost::make_tuple(scope, keystr) ); @@ -156,8 +156,7 @@ DEFINE_INTRINSIC_FUNCTION2(env,readMessage,readMessage,i32,i32,destptr,i32,dests FC_ASSERT( destsize > 0 ); wasm_interface& wasm = wasm_interface::get(); auto mem = wasm.current_memory; - char* begin = &Runtime::memoryRef( mem, destptr ); - Runtime::memoryRef( mem, destptr + destsize ); + char* begin = memoryArrayPtr( mem, destptr, destsize ); int minlen = std::min(wasm.current_validate_context->msg.data.size(), destsize); memcpy( begin, wasm.current_validate_context->msg.data.data(), minlen ); @@ -192,8 +191,12 @@ DEFINE_INTRINSIC_FUNCTION1(env,printi64,printi64,none,i64,val) { DEFINE_INTRINSIC_FUNCTION2(env,print,print,none,i32,charptr,i32,size) { FC_ASSERT( size > 0 ); - const char* str = &Runtime::memoryRef( Runtime::getDefaultMemory(wasm_interface::get().current_module), charptr); - const char* end = &Runtime::memoryRef( Runtime::getDefaultMemory(wasm_interface::get().current_module), charptr+size); + + auto& wasm = wasm_interface::get(); + auto mem = wasm.current_memory; + + const char* str = memoryArrayPtr( mem, charptr, size ); + edump((charptr)(size)); wlog( std::string( str, size ) ); }