Skip to content

Commit

Permalink
merging master @ f66d188 to resolve conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
b1bart committed Feb 26, 2018
2 parents 6fcd58f + f66d188 commit 7d65a18
Show file tree
Hide file tree
Showing 27 changed files with 387 additions and 952 deletions.
8 changes: 6 additions & 2 deletions CMakeModules/wasm.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ macro(add_wast_library)
endmacro(add_wast_library)

macro(add_wast_executable)
cmake_parse_arguments(ARG "NOWARNINGS" "TARGET;DESTINATION_FOLDER" "SOURCE_FILES;INCLUDE_FOLDERS;SYSTEM_INCLUDE_FOLDERS;LIBRARIES" ${ARGN})
cmake_parse_arguments(ARG "NOWARNINGS" "TARGET;DESTINATION_FOLDER;MAX_MEMORY" "SOURCE_FILES;INCLUDE_FOLDERS;SYSTEM_INCLUDE_FOLDERS;LIBRARIES" ${ARGN})
set(target ${ARG_TARGET})
set(DESTINATION_FOLDER ${ARG_DESTINATION_FOLDER})

Expand Down Expand Up @@ -142,9 +142,13 @@ macro(add_wast_executable)
)
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${target}.s)

if(ARG_MAX_MEMORY)
set(MAX_MEMORY_PARAM "-m" ${ARG_MAX_MEMORY})
endif()

add_custom_command(OUTPUT ${DESTINATION_FOLDER}/${target}.wast
DEPENDS ${target}.s
COMMAND "$<TARGET_FILE:s2wasm>" -o ${DESTINATION_FOLDER}/${target}.wast -s 4096 ${target}.s
COMMAND "$<TARGET_FILE:s2wasm>" -o ${DESTINATION_FOLDER}/${target}.wast -s 4096 ${MAX_MEMORY_PARAM} ${target}.s
COMMENT "Generating WAST ${target}.wast"
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
VERBATIM
Expand Down
26 changes: 16 additions & 10 deletions contracts/eosiolib/eosiolib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,39 @@ namespace eosio {

memory* next_active_heap()
{
constexpr uint32_t wasm_page_size = 64*1024;
memory* const current_memory = _available_heaps + _active_heap;

// make sure we will not exceed the 1M limit (needs to match wasm_interface.cpp _max_memory)
auto remaining = 1024 * 1024 - reinterpret_cast<int32_t>(sbrk(0));
if (remaining <= 0)
{
const uint32_t current_memory_size = reinterpret_cast<uint32_t>(sbrk(0));
if(static_cast<int32_t>(current_memory_size) < 0)
return nullptr;

//grab up to the end of the current WASM memory page provided that it has 1KiB remaining, otherwise
// grow to end of next page
uint32_t heap_adj;
if(current_memory_size % wasm_page_size <= wasm_page_size-1024)
heap_adj = (current_memory_size + wasm_page_size) - (current_memory_size % wasm_page_size) - current_memory_size;
else
heap_adj = (current_memory_size + wasm_page_size*2) - (current_memory_size % (wasm_page_size*2)) - current_memory_size;
char* new_memory_start = reinterpret_cast<char*>(sbrk(heap_adj));
if(reinterpret_cast<int32_t>(new_memory_start) == -1) {
// ensure that any remaining unallocated memory gets cleaned up
current_memory->cleanup_remaining();
++_active_heap;
_heaps_actual_size = _active_heap;
return nullptr;
}

const uint32_t new_heap_size = uint32_t(remaining) > _new_heap_size ? _new_heap_size : uint32_t(remaining);
char* new_memory_start = static_cast<char*>(sbrk(new_heap_size));
// if we can expand the current memory, keep working with it
if (current_memory->expand_memory(new_memory_start, new_heap_size))
if (current_memory->expand_memory(new_memory_start, heap_adj))
return current_memory;

// ensure that any remaining unallocated memory gets cleaned up
current_memory->cleanup_remaining();

++_active_heap;
memory* const next = _available_heaps + _active_heap;
next->init(new_memory_start, new_heap_size);
next->init(new_memory_start, heap_adj);

return next;
}
Expand Down Expand Up @@ -202,7 +210,6 @@ namespace eosio {
{
_heap_size = size;
_heap = mem_heap;
memset(_heap, 0, _heap_size);
}

uint32_t is_init() const
Expand Down Expand Up @@ -461,7 +468,6 @@ namespace eosio {
static const uint32_t _mem_block = 8;
static const uint32_t _rem_mem_block_mask = _mem_block - 1;
static const uint32_t _initial_heap_size = 8192;//32768;
static const uint32_t _new_heap_size = 65536;
// if sbrk is not called outside of this file, then this is the max times we can call it
static const uint32_t _heaps_size = 16;
char _initial_heap[_initial_heap_size];
Expand Down
3 changes: 3 additions & 0 deletions contracts/test_api/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#set the MAX_MEMORY to 1MB for these tests; there were lots of memory unit tests that assume such

add_wast_executable(TARGET test_api
INCLUDE_FOLDERS "${STANDARD_INCLUDE_FOLDERS}"
LIBRARIES libc++ libc eosiolib
DESTINATION_FOLDER ${CMAKE_CURRENT_BINARY_DIR}
MAX_MEMORY 1048576
)
3 changes: 3 additions & 0 deletions contracts/test_api_mem/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#set the MAX_MEMORY to 1MB for these tests; there were lots of memory unit tests that assume such

add_wast_executable( TARGET test_api_mem
INCLUDE_FOLDERS "${STANDARD_INCLUDE_FOLDERS}"
LIBRARIES eosiolib
DESTINATION_FOLDER ${CMAKE_CURRENT_BINARY_DIR}
MAX_MEMORY 1048576
)
4 changes: 2 additions & 2 deletions contracts/test_api_mem/test_extended_memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ void test_extended_memory::test_page_memory_exceeded() {
*/
prev = sbrk(0);
eosio_assert(reinterpret_cast<uint32_t>(prev) == (1024*1024), "Should have allocated 1M of memory");
sbrk(1);
eosio_assert(0, "Should have thrown exception for trying to allocate too much memory");

eosio_assert(reinterpret_cast<int32_t>(sbrk(1)) == -1, "sbrk should have failed for trying to allocate too much memory");
}

void test_extended_memory::test_page_memory_negative_bytes() {
Expand Down
4 changes: 1 addition & 3 deletions libraries/chain/apply_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ void apply_context::exec_one()
// get code from cache
auto code = mutable_controller.get_wasm_cache().checkout_scoped(a.code_version, a.code.data(),
a.code.size());

// get wasm_interface
auto &wasm = wasm_interface::get();
wasm.apply(code, *this);
}
}
}
} FC_CAPTURE_AND_RETHROW((_pending_console_output.str()));

Expand Down Expand Up @@ -95,7 +94,6 @@ void apply_context::exec_one()
void apply_context::exec()
{
_notified.push_back(act.account);

for( uint32_t i = 0; i < _notified.size(); ++i ) {
receiver = _notified[i];
exec_one();
Expand Down
2 changes: 1 addition & 1 deletion libraries/chain/include/eosio/chain/apply_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ class apply_context {
if (itr->t_id != tab->id) return -1;

itr_cache.cache_table(*tab);
return itr_cache(*itr);
return itr_cache.add(*itr);
}

int next_primary( int iterator, uint64_t& primary ) {
Expand Down
2 changes: 1 addition & 1 deletion libraries/chain/include/eosio/chain/authority_checker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace detail {
bool operator()(const meta_permission& a, const meta_permission& b) const {
get_weight_visitor scale;
if (a.visit(scale) > b.visit(scale)) return true;
return a.contains<key_weight>() && !b.contains<permission_level_weight>();
return a.contains<key_weight>() && b.contains<permission_level_weight>();
}
};

Expand Down
18 changes: 18 additions & 0 deletions libraries/chain/include/eosio/chain/chain_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ struct chain_config {
uint32_t max_generated_transaction_size;

static chain_config get_median_values( vector<chain_config> votes );

template<typename Stream>
friend Stream& operator << ( Stream& out, const chain_config& c ) {
return out << "Target Block Size: " << c.target_block_size << ", "
<< "Max Block Size: " << c.max_block_size << ", "
<< "Target Block Acts Per Scope: " << c.target_block_acts_per_scope << ", "
<< "Max Block Acts Per Scope: " << c.max_block_acts_per_scope << ", "
<< "Target Block Acts: " << c.target_block_acts << ", "
<< "Max Block Acts: " << c.max_block_acts << ", "
<< "Real Threads: " << c.real_threads << ", "
<< "Max Storage Size: " << c.max_storage_size << ", "
<< "Max Transaction Lifetime: " << c.max_transaction_lifetime << ", "
<< "Max Authority Depth: " << c.max_authority_depth << ", "
<< "Max Transaction Exec Time: " << c.max_transaction_exec_time << ", "
<< "Max Inline Depth: " << c.max_inline_depth << ", "
<< "Max Inline Action Size: " << c.max_inline_action_size << ", "
<< "Max Generated Transaction Size: " << c.max_generated_transaction_size << "\n";
}
};

bool operator==(const chain_config& a, const chain_config& b);
Expand Down
1 change: 0 additions & 1 deletion libraries/chain/include/eosio/chain/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ namespace eosio { namespace chain {
using std::make_pair;
using std::enable_shared_from_this;
using std::tie;
using std::make_pair;
using std::move;
using std::forward;
using std::to_string;
Expand Down
16 changes: 11 additions & 5 deletions libraries/chain/include/eosio/chain/wasm_eosio_constraints.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ namespace IR {
namespace eosio { namespace chain {

namespace wasm_constraints {
//Be aware that some of these are required to be a multiple of some internal number
constexpr unsigned maximum_linear_memory = 1024*1024; //bytes
constexpr unsigned maximum_mutable_globals = 1024; //bytes
constexpr unsigned maximum_table_elements = 1024; //elements
constexpr unsigned maximum_linear_memory_init = 64*1024; //bytes
constexpr unsigned maximum_linear_memory = 33*1024*1024;//bytes
constexpr unsigned maximum_mutable_globals = 1024; //bytes
constexpr unsigned maximum_table_elements = 1024; //elements
constexpr unsigned maximum_linear_memory_init = 64*1024; //bytes

static constexpr unsigned wasm_page_size = 64*1024;

static_assert(maximum_linear_memory%wasm_page_size == 0, "maximum_linear_memory must be mulitple of wasm page size");
static_assert(maximum_mutable_globals%4 == 0, "maximum_mutable_globals must be mulitple of 4");
static_assert(maximum_table_elements*8%4096 == 0, "maximum_table_elements*8 must be mulitple of 4096");
static_assert(maximum_linear_memory_init%wasm_page_size == 0, "maximum_linear_memory_init must be mulitple of wasm page size");
}

//Throws if something in the module violates
Expand Down
19 changes: 10 additions & 9 deletions libraries/chain/wasm_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1031,17 +1031,17 @@ class memory_api : public context_aware_api {
return (char *)::memset( dest, value, length );
}

uint32_t sbrk(int num_bytes) {
int sbrk(int num_bytes) {
// sbrk should only allow for memory to grow
if (num_bytes < 0)
throw eosio::chain::page_memory_error();
// 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 MAX_MEM = wasm_constraints::maximum_linear_memory;

MemoryInstance* default_mem = Runtime::getDefaultMemory(code.instance);
if(!default_mem)
throw eosio::chain::page_memory_error();
return -1;

const uint32_t num_pages = Runtime::getMemoryNumPages(default_mem);
const uint32_t min_bytes = (num_pages << NBPPL2) > UINT32_MAX ? UINT32_MAX : num_pages << NBPPL2;
Expand All @@ -1051,20 +1051,21 @@ class memory_api : public context_aware_api {
num_bytes = (num_bytes + 7) & ~7;

if ((num_bytes > 0) && (prev_num_bytes > (MAX_MEM - num_bytes))) // test if allocating too much memory (overflowed)
throw eosio::chain::page_memory_error();
return -1;
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
sbrk_bytes += num_bytes;
const uint32_t num_desired_pages = (sbrk_bytes + IR::numBytesPerPage - 1) >> NBPPL2;
const uint32_t num_desired_pages = (sbrk_bytes + num_bytes + IR::numBytesPerPage - 1) >> NBPPL2;

// grow or shrink the memory to the desired number of pages
if (num_desired_pages > num_pages)
Runtime::growMemory(default_mem, num_desired_pages - num_pages);
if(Runtime::growMemory(default_mem, num_desired_pages - num_pages) == -1)
return -1;
else if (num_desired_pages < num_pages)
Runtime::shrinkMemory(default_mem, num_pages - num_desired_pages);
if(Runtime::shrinkMemory(default_mem, num_pages - num_desired_pages) == -1)
return -1;

sbrk_bytes += num_bytes;
return prev_num_bytes;
}
};
Expand Down
1 change: 1 addition & 0 deletions libraries/testing/include/eosio/testing/tester.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ namespace eosio { namespace testing {
void create_account( account_name name, account_name creator = config::system_account_name, bool multisig = false );

transaction_trace push_reqauth( account_name from, const vector<permission_level>& auths, const vector<private_key_type>& keys );
transaction_trace push_reqauth(account_name from, string role, bool multi_sig = false);
transaction_trace push_nonce( account_name from, const string& v = "blah" );
transaction_trace transfer( account_name from, account_name to, asset amount, string memo, account_name currency );
transaction_trace transfer( account_name from, account_name to, string amount, string memo, account_name currency );
Expand Down
10 changes: 10 additions & 0 deletions libraries/testing/tester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ namespace eosio { namespace testing {
return push_transaction( trx );
}

transaction_trace base_tester::push_reqauth(account_name from, string role, bool multi_sig) {
if (!multi_sig) {
return push_reqauth(from, vector<permission_level>{{from, config::owner_name}},
{get_private_key(from, role)});
} else {
return push_reqauth(from, vector<permission_level>{{from, config::owner_name}},
{get_private_key(from, role), get_private_key( config::system_account_name, "active" )} );
}
}

transaction_trace base_tester::push_nonce(account_name from, const string& v) {
variant pretty_trx = fc::mutable_variant_object()
("actions", fc::variants({
Expand Down
2 changes: 1 addition & 1 deletion libraries/wasm-jit/Source/WASM/WASMSerialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ namespace WASM
Serialization::OutputStream& byteStream;
FunctionDef& functionDef;
};

template<typename Injection>
struct WasmSerializationImpl
{
Expand Down
Loading

0 comments on commit 7d65a18

Please sign in to comment.