Skip to content

Commit

Permalink
Merge pull request EOSIO#395 from EOSIO/feature/auto
Browse files Browse the repository at this point in the history
Feature/auto
  • Loading branch information
larryk85 authored Feb 1, 2019
2 parents ce2e35f + ebc146b commit d0553e3
Show file tree
Hide file tree
Showing 24 changed files with 1,670 additions and 130 deletions.
18 changes: 12 additions & 6 deletions libraries/eosiolib/malloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
#include "system.hpp"
#include "print.hpp"

namespace eosio {
#ifdef EOSIO_NATIVE
extern "C" {
size_t __builtin_wasm_current_memory();
size_t __builtin_wasm_grow_memory(size_t);
size_t _current_memory();
size_t _grow_memory(size_t);
}
#define CURRENT_MEMORY _current_memory()
#define GROW_MEMORY(X) _grow_memory(X)
#else
#define CURRENT_MEMORY __builtin_wasm_current_memory()
#define GROW_MEMORY(X) __builtin_wasm_grow_memory(X)
#endif

namespace eosio {
extern "C" uintptr_t __get_heap_base();
void* sbrk(size_t num_bytes) {
constexpr uint32_t NBPPL2 = 16U;
Expand All @@ -18,15 +24,15 @@ namespace eosio {
static bool initialized;
static uint32_t sbrk_bytes;
if(!initialized) {
sbrk_bytes = __builtin_wasm_current_memory() * NBBP;
sbrk_bytes = CURRENT_MEMORY * NBBP;
initialized = true;
}

if(num_bytes > INT32_MAX)
return reinterpret_cast<void*>(-1);

const uint32_t prev_num_bytes = sbrk_bytes;
const uint32_t current_pages = __builtin_wasm_current_memory();
const uint32_t current_pages = CURRENT_MEMORY;

// round the absolute value of num_bytes to an alignment boundary
num_bytes = (num_bytes + 7U) & ~7U;
Expand All @@ -35,7 +41,7 @@ namespace eosio {
const uint32_t num_desired_pages = (sbrk_bytes + num_bytes + NBBP - 1) >> NBPPL2;

if(num_desired_pages > current_pages) {
if (__builtin_wasm_grow_memory(num_desired_pages - current_pages) == -1)
if (GROW_MEMORY(num_desired_pages - current_pages) == -1)
return reinterpret_cast<void*>(-1);
}

Expand Down
2 changes: 0 additions & 2 deletions libraries/eosiolib/string.hpp → libraries/eosiolib/rope.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#pragma once

#include <variant>
#include <set>
#include <stack>
#include "system.hpp"

namespace eosio {
Expand Down
14 changes: 10 additions & 4 deletions libraries/eosiolib/simple_malloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@

#ifdef EOSIO_NATIVE
extern "C" {
size_t __builtin_wasm_current_memory();
size_t __builtin_wasm_grow_memory(size_t);
size_t _current_memory();
size_t _grow_memory(size_t);
}
#define CURRENT_MEMORY _current_memory()
#define GROW_MEMORY(X) _grow_memory(X)
#else
#define CURRENT_MEMORY __builtin_wasm_current_memory()
#define GROW_MEMORY(X) __builtin_wasm_grow_memory(X)
#endif

namespace eosio {
Expand All @@ -24,7 +29,8 @@ namespace eosio {
volatile uintptr_t heap_base = 0; // linker places this at address 0
heap = align(*(char**)heap_base, 8);
last_ptr = heap;
next_page = __builtin_wasm_current_memory();

next_page = CURRENT_MEMORY;
}

char* operator()(size_t sz, uint8_t align_amt=8) {
Expand All @@ -40,7 +46,7 @@ namespace eosio {
next_page++;
pages_to_alloc++;
}
eosio::check(__builtin_wasm_grow_memory(pages_to_alloc) != -1, "failed to allocate pages");
eosio::check(GROW_MEMORY(pages_to_alloc) != -1, "failed to allocate pages");
return ret;
}

Expand Down
9 changes: 2 additions & 7 deletions libraries/native/crt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ eosio::cdt::output_stream std_out;
eosio::cdt::output_stream std_err;

extern "C" {
#ifdef __APPLE__
//void* alloca(size_t s) {
// return malloc(s);
//}
#endif
int main(int, char**);
char* _mmap();

Expand All @@ -35,11 +30,11 @@ extern "C" {
return ___heap_base_ptr;
}

size_t __builtin_wasm_current_memory() {
size_t _current_memory() {
return ___pages;
}

size_t __builtin_wasm_grow_memory(size_t size) {
size_t _grow_memory(size_t size) {
if ((___heap_ptr + (size*64*1024)) > (___heap_ptr + 100*1024*1024))
eosio_assert(false, "__builtin_wasm_grow_memory");
___heap_ptr += (size*64*1024);
Expand Down
2 changes: 1 addition & 1 deletion modules/ClangExternalProject.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ include(GNUInstallDirs)

ExternalProject_Add(
EosioClang
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}/llvm -DCMAKE_BUILD_TYPE=Release -DEOSIO_VER_MAJOR=${VERSION_MAJOR} -DEOSIO_VER_MINOR=${VERSION_MINOR} -DEOSIO_VER_REVISION=${VERSION_PATCH} -DEOSIO_STACK_SIZE=8192 -DCOMPILER_RT_DEFAULT_TARGET_TRIPLE=wasm32-unknown-unknown-wasm -DCOMPILER_RT_BAREMETAL_BUILD=TRUE -DCOMPILER_RT_EXCLUDE_ATOMIC_BUILTIN=TRUE -DCAN_TARGET_wasm32=ON -DLLVM_BUILD_EXTERNAL_COMPILER_RT=ON -DCOMPILER_RT_BUILD_BUILTINS=ON -DCOMPILER_RT_BUILD_XRAY=OFF -DCOMPILER_RT_BUILD_SANITIZERS=OFF -DCOMPILER_RT_BUILD_LIBFUZZER=OFF -DCOMPILER_RT_BUILD_PROFILE=OFF
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}/llvm -DCMAKE_BUILD_TYPE=Release -DEOSIO_TOOL_DIR=${CMAKE_SOURCE_DIR}/tools

SOURCE_DIR "${CMAKE_SOURCE_DIR}/eosio_llvm"
BINARY_DIR "${CMAKE_BINARY_DIR}/eosio_llvm"
Expand Down
86 changes: 86 additions & 0 deletions tests/integration/codegen_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <boost/test/unit_test.hpp>
#include <eosio/testing/tester.hpp>
#include <eosio/chain/abi_serializer.hpp>

#include <Runtime/Runtime.h>

#include <fc/variant_object.hpp>

#include <contracts.hpp>

using namespace eosio;
using namespace eosio::testing;
using namespace eosio::chain;
using namespace eosio::testing;
using namespace fc;

using mvo = fc::mutable_variant_object;

BOOST_AUTO_TEST_SUITE(codegen_tests)

BOOST_FIXTURE_TEST_CASE( simple_tests, tester ) try {
create_accounts( { N(test), N(eosio.token), N(someone) } );
produce_block();

set_code( N(eosio.token), contracts::transfer_wasm() );
set_abi( N(eosio.token), contracts::transfer_abi().data() );

set_code( N(someone), contracts::transfer_wasm() );
set_abi( N(someone), contracts::transfer_abi().data() );

set_code( N(test), contracts::simple_wasm() );
set_abi( N(test), contracts::simple_abi().data() );
produce_blocks();
push_action(N(test), N(test1), N(test),
mvo()
("nm", "bucky"));

BOOST_CHECK_THROW(push_action(N(test), N(test1), N(test), mvo()("nm", "notbucky")),
fc::exception);

push_action(N(test), N(test2), N(test),
mvo()
("arg0", 33)
("arg1", "some string"));
BOOST_CHECK_THROW(push_action(N(test), N(test2), N(test), mvo() ("arg0", 30)("arg1", "some string")), fc::exception);
BOOST_CHECK_THROW(push_action(N(test), N(test2), N(test), mvo() ("arg0", 33)("arg1", "not some string")), fc::exception);

set_abi( N(test), contracts::simple_wrong_abi().data() );
produce_blocks();

BOOST_CHECK_THROW(push_action(N(test), N(test3), N(test), mvo() ("arg0", 33) ("arg1", "some string")), fc::exception);

set_abi( N(test), contracts::simple_abi().data() );
produce_blocks();

push_action(N(test), N(test4), N(test), mvo() ("to", "someone"));
push_action(N(test), N(test5), N(test), mvo() ("to", "someone"));
push_action(N(test), N(testa), N(test), mvo() ("to", "someone"));
BOOST_CHECK_THROW(push_action(N(test), N(testb), N(test), mvo() ("to", "someone")), fc::exception);

} FC_LOG_AND_RETHROW()

BOOST_FIXTURE_TEST_CASE( simple_eosio_tests, tester ) try {
set_code( N(eosio), contracts::simple_wasm() );
set_abi( N(eosio), contracts::simple_wrong_abi().data() );
produce_blocks();
push_action(N(eosio), N(test1), N(eosio),
mvo()
("nm", "bucky"));

BOOST_CHECK_THROW(push_action(N(eosio), N(test1), N(eosio), mvo()("nm", "notbucky")),
fc::exception);

push_action(N(eosio), N(test2), N(eosio),
mvo()
("arg0", 33)
("arg1", "some string"));
BOOST_CHECK_THROW(push_action(N(eosio), N(test2), N(eosio), mvo() ("arg0", 30)("arg1", "some string")), fc::exception);
BOOST_CHECK_THROW(push_action(N(eosio), N(test2), N(eosio), mvo() ("arg0", 33)("arg1", "not some string")), fc::exception);

push_action(N(eosio), N(test3), N(eosio),
mvo()
("arg0", 33)
("arg1", "some string"));

} FC_LOG_AND_RETHROW() }
10 changes: 9 additions & 1 deletion tests/integration/contracts.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ struct contracts {
static std::vector<char> malloc_tests_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/malloc_tests.abi"); }
static std::vector<uint8_t> old_malloc_tests_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../unit/test_contracts/old_malloc_tests.wasm"); }
static std::vector<char> old_malloc_tests_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/old_malloc_tests.abi"); }
};

static std::vector<uint8_t> simple_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../unit/test_contracts/simple_tests.wasm"); }
static std::vector<char> simple_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/simple_tests.abi"); }
static std::vector<char> simple_wrong_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/simple_wrong.abi"); }

static std::vector<uint8_t> transfer_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../unit/test_contracts/transfer_contract.wasm"); }
static std::vector<char> transfer_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/transfer_contract.abi"); }

};

}} //ns eosio::testing
3 changes: 2 additions & 1 deletion tests/integration/memory_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ BOOST_AUTO_TEST_SUITE(memory_tests)
BOOST_FIXTURE_TEST_CASE( malloc_tests, tester ) try {
create_accounts( { N(test) } );
produce_block();

/*
set_code( N(test), contracts::malloc_tests_wasm() );
set_abi( N(test), contracts::malloc_tests_abi().data() );
produce_blocks();
Expand All @@ -33,4 +33,5 @@ BOOST_FIXTURE_TEST_CASE( malloc_tests, tester ) try {
BOOST_CHECK_EXCEPTION( push_action(N(test), N(mallocfail), N(test), {}),
eosio_assert_message_exception,
eosio_assert_message_is("failed to allocate pages") );
*/
} FC_LOG_AND_RETHROW() }
2 changes: 1 addition & 1 deletion tests/unit/rope_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <eosiolib/eosio.hpp>
#include <eosiolib/string.hpp>
#include <eosiolib/rope.hpp>
#include <eosio/native/tester.hpp>
#include <string>

Expand Down
4 changes: 4 additions & 0 deletions tests/unit/test_contracts/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
add_contract(malloc_tests malloc_tests malloc_tests.cpp)
add_contract(malloc_tests old_malloc_tests malloc_tests.cpp)
add_contract(simple_tests simple_tests simple_tests.cpp)
add_contract(transfer_contract transfer_contract transfer.cpp)

configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/simple_wrong.abi ${CMAKE_CURRENT_BINARY_DIR}/simple_wrong.abi COPYONLY )

target_link_libraries(old_malloc_tests PUBLIC --use-freeing-malloc)
73 changes: 73 additions & 0 deletions tests/unit/test_contracts/simple_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <eosiolib/eosio.hpp>
#include "transfer.hpp"

using namespace eosio;

class [[eosio::contract]] simple_tests : public contract {
public:
using contract::contract;

[[eosio::action]]
void test1(name nm) {
check(nm == "bucky"_n, "not bucky");
}

[[eosio::action]]
void test2(int arg0, std::string arg1) {
check(arg0 == 33, "33 does not match");
check(arg1 == "some string", "some string does not match");
}

[[eosio::action("test4")]]
void test4(name to) {
transfer_contract::transfer_action trans("eosio.token"_n, {_self, "active"_n});
trans.send(_self, to, asset{100, {"TST", 4}}, "memo");
}

[[eosio::action]]
void test5(name to) {
transfer_contract::transfer_action trans("someone"_n, {_self, "active"_n});
trans.send(_self, to, asset{100, {"TST", 4}}, "memo");
}

[[eosio::action]]
void testa(name to) {
transfer_contract::transfer2_action trans("someone"_n, {_self, "active"_n});
trans.send(_self, to, asset{100, {"TST", 4}}, "memo");
}

[[eosio::action]]
void testb(name to) {
transfer_contract::transfer3_action trans("someone"_n, {_self, "active"_n});
trans.send(_self, to, asset{100, {"TST", 4}}, "memo");
}

[[eosio::on_notify("eosio.token::transfer")]]
void on_transfer(name from, name to, asset quant, std::string memo) {
check(_code == "eosio.token"_n, "should be eosio.token");
print_f("On notify : % % % %", from, to, quant, memo);
}

[[eosio::on_notify("*::transfer")]]
void on_transfer2(name from, name to, asset quant, std::string memo) {
check(_code != "eosio.token"_n, "should not be eosio.token");
print_f("On notify 2 : % % % %", from, to, quant, memo);
}

[[eosio::on_notify("*::transfer2")]]
void on_transfer3(name from, name to, asset quant, std::string memo) {
print_f("On notify 3 : % % % %", from, to, quant, memo);
}

};

extern "C" void pre_dispatch(name self, name original_receiver, name action) {
print_f("pre_dispatch : % % %\n", self, original_receiver, action);
}

extern "C" void post_dispatch(name self, name original_receiver, name action) {
print_f("post_dispatch : % % %\n", self, original_receiver, action);
std::set<name> valid_actions = {"test1"_n, "test2"_n, "test4"_n, "test5"_n};
check(valid_actions.count(action) == 0, "valid action should have dispatched");
check(self == "eosio"_n, "should only be eosio for action failures");
}
52 changes: 52 additions & 0 deletions tests/unit/test_contracts/simple_wrong.abi
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"____comment": "This file was generated with eosio-abigen. DO NOT EDIT Tue Jan 22 18:06:02 2019",
"version": "eosio::abi/1.1",
"types": [],
"structs": [
{
"name": "test1",
"base": "",
"fields": [
{
"name": "nm",
"type": "name"
}
]
},
{
"name": "test2",
"base": "",
"fields": [
{
"name": "arg0",
"type": "int32"
},
{
"name": "arg1",
"type": "string"
}
]
}
],
"actions": [
{
"name": "test1",
"type": "test1",
"ricardian_contract": ""
},
{
"name": "test2",
"type": "test2",
"ricardian_contract": ""
},
{
"name": "test3",
"type": "test2",
"ricardian_contract": ""
}

],
"tables": [],
"ricardian_clauses": [],
"variants": []
}
Loading

0 comments on commit d0553e3

Please sign in to comment.