Skip to content

Commit

Permalink
Issues with unresolvable intrinsic
Browse files Browse the repository at this point in the history
  • Loading branch information
larryk85 committed Mar 12, 2018
1 parent 748f75d commit 361abbe
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
30 changes: 25 additions & 5 deletions libraries/chain/include/eosio/chain/wasm_eosio_injection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,18 @@ namespace eosio { namespace chain { namespace wasm_injections {
}
}

static int last_imported_index( Module& module ) {
return module.functions.imports.size() - 1;
}

template <ResultType Result, ValueType... Params>
static void add_import(Module& module, const char* scope, const char* func_name, int32_t& index ) {
if (module.functions.imports.size() == 0 || module.functions.imports.back().exportName.compare(func_name) != 0) {
add_type_slot<Result, Params...>( module );

const uint32_t func_type_index = type_slots[{ FromResultType<Result>::value, FromValueType<Params>::value... }];
for ( auto e : { FromValueType<Params>::value...} )
std::cout << "\n";
module.functions.imports.push_back({{func_type_index}, std::move(scope), std::move(func_name)});
index = module.functions.imports.size()-1;
// shift all exported functions by 1
Expand Down Expand Up @@ -132,22 +138,37 @@ namespace eosio { namespace chain { namespace wasm_injections {
}
static int32_t checktime_idx;
};

struct increment_call_index {
static constexpr bool kills = false;
static void init() {}
static void accept( wasm_ops::instr* inst, wasm_ops::visitor_arg& arg ) {
// TODO this is hardcoded for various injections, refactor to allow the system to deduce this
wasm_ops::op_types<>::call_t* call_inst = reinterpret_cast<wasm_ops::op_types<>::call_t*>(inst);
const int offset = 1;
if ( call_inst->field >= injector_utils::last_imported_index( *(arg.module) ) )
call_inst->field += offset;
}

};
// add opcode specific constraints here
// so far we only black list
struct op_injectors : wasm_ops::op_types<pass_injector> {
using block_t = wasm_ops::block <debug_printer, instruction_counter, checktime_injector>;
using loop_t = wasm_ops::loop <debug_printer, instruction_counter, checktime_injector>;
using if__t = wasm_ops::if_ <debug_printer, instruction_counter, checktime_injector>;
using else__t = wasm_ops::else_ <debug_printer, instruction_counter, checktime_injector>;
using if__t = wasm_ops::if_ <debug_printer, instruction_counter>;
using else__t = wasm_ops::else_ <debug_printer, instruction_counter>;

//using if__t = wasm_ops::if_ <debug_printer, instruction_counter, checktime_injector>;
//using else__t = wasm_ops::else_ <debug_printer, instruction_counter, checktime_injector>;

using end_t = wasm_ops::end <debug_printer, instruction_counter>;
using unreachable_t = wasm_ops::unreachable <debug_printer, instruction_counter>;
using br_t = wasm_ops::br <debug_printer, instruction_counter>;
using br_if_t = wasm_ops::br_if <debug_printer, instruction_counter>;
using br_table_t = wasm_ops::br_table <debug_printer, instruction_counter>;
using return__t = wasm_ops::return_ <debug_printer, instruction_counter>;
using call_t = wasm_ops::call <debug_printer, instruction_counter>;
using call_t = wasm_ops::call <debug_printer, instruction_counter, increment_call_index>;
using call_indirect_t = wasm_ops::call_indirect <debug_printer, instruction_counter>;
using drop_t = wasm_ops::drop <debug_printer, instruction_counter>;
using select_t = wasm_ops::select <debug_printer, instruction_counter>;
Expand Down Expand Up @@ -286,7 +307,7 @@ namespace eosio { namespace chain { namespace wasm_injections {
wasm_binary_injection() {
_module_injectors.init();
// initialize static fields of injectors
//injector_utils::init();
injector_utils::init();
instruction_counter::init();
checktime_injector::init();
}
Expand All @@ -300,7 +321,6 @@ namespace eosio { namespace chain { namespace wasm_injections {
auto op = decoder.decodeOp();
op->visit( { &mod, &new_code, &fd, decoder.index() } );
}

fd.code = new_code;
}
}
Expand Down
27 changes: 20 additions & 7 deletions libraries/chain/wasm_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,9 @@ namespace eosio { namespace chain {
WASM::serializeWithInjection(stream2, *module2);

wasm_constraints::wasm_binary_validation::validate( *module );
#if 0
wasm_injections::wasm_binary_injection::inject( *module );
/*

std::cout << "INJECTED\n";
wasm_injections::wasm_binary_injection::inject( *module );
std::cout << "INJECTEDSS\n";
Expand Down Expand Up @@ -236,12 +237,12 @@ namespace eosio { namespace chain {
std::cout << "\n";

std::cout << "FUNCTION_Import A \n";
for (auto e : module->functions.imports)
std::cout << e.exportName << "\n";
for (int i=0; i < module->functions.imports.size(); i++)
std::cout << module->functions.imports[i].exportName << " : " << i << "\n";
std::cout << "\n";
std::cout << "FUNCTION_Import B \n";
for (auto e : module2->functions.imports)
std::cout << e.exportName << "\n";
for (int i=0; i < module->functions.imports.size(); i++)
std::cout << module->functions.imports[i].exportName << " : " << i << "\n";

std::cout << "\n";
std::cout << "TABLES_Import A \n";
Expand Down Expand Up @@ -283,7 +284,20 @@ namespace eosio { namespace chain {
for (auto e : module2->exports)
std::cout << e.name << " " << uint32_t(e.index) << "\n";
std::cout << "\n";
*/

std::cout << "DATASEG A \n";
for (auto e : module->dataSegments)
std::cout << e.memoryIndex << "\n";

std::cout << "\n";
std::cout << "DATSEG B \n";
for (auto e : module2->dataSegments)
std::cout << e.memoryIndex << "\n";
std::cout << "\n";

std::cout << "start index A " << module->startFunctionIndex << "\n";
std::cout << "start index B " << module2->startFunctionIndex << "\n";
#endif

Serialization::ArrayOutputStream outstream;
WASM::serialize(outstream, *module);
Expand Down Expand Up @@ -595,7 +609,6 @@ class checktime_api : public context_aware_api {
using context_aware_api::context_aware_api;

void checktime(uint32_t instruction_count) {
std::cout << "CHECKTIME\n";
context.checktime(instruction_count);
}
};
Expand Down
3 changes: 2 additions & 1 deletion libraries/chain/webassembly/wavm.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <eosio/chain/webassembly/wavm.hpp>
#include <eosio/chain/wasm_eosio_constraints.hpp>
#include <eosio/chain/wasm_eosio_injection.hpp>
#include <eosio/chain/apply_context.hpp>

#include "IR/Module.h"
Expand Down Expand Up @@ -121,7 +122,7 @@ entry entry::build(const char* wasm_binary, size_t wasm_binary_size) {
Module* module = new Module();
Serialization::MemoryInputStream stream((const U8 *) wasm_binary, wasm_binary_size);
WASM::serialize(stream, *module);

wasm_injections::wasm_binary_injection::inject( *module );
root_resolver resolver;
LinkResult link_result = linkModule(*module, resolver);
ModuleInstance *instance = instantiateModule(*module, std::move(link_result.resolvedImports));
Expand Down

0 comments on commit 361abbe

Please sign in to comment.