mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +00:00
TVM Upgrade (#686)
* New TVM instructions * Remove PREVBLOCKS * Separate target ton_crypto into TVM-related and -unrelared code * Add fine for failed "send message"; rework SENDMSG * Fix include * Fix bugs, improve action fines * Disable fines for special accounts * Handle msg_balance_remaining.grams == null in transaction.cpp * Bugfixes in SENDMSG * Fix fee calculation in SENDMSG * Fix CellStorageStat and transaction.cpp after merge * SETBOUNCEONACTIONPHASEFAIL instruction * ADDDIVMOD instructions * RUNVM, RUNVMX instructions * Changes in RUNVM * Tests for adddiv and runvm * HASHEXT instruction * Improve opcode-timing More iterations Don't measure preliminary run Remove logs and other excessive operations Add "error" to output * Increase RUNVM gas price * Optimize HASHEXT, adjust gas price * Add "bounce of action fail" flag to actions * Stack operations with unlimited arguments * Ristretto255 instructions * Adjust gas consumption * Optional fixed number of return values in RUNVM, fix exception handling * Adjust gas consumption * Simplify gas consumption logic * Support of secp256k1 and sodium libraries in builds (#11) * add support of secp256k1 library to the builds (linux, win) * add support of secp256k1 library to the builds (linux, win) * install secp256k1 via brew * install libsodium via brew; change sodium to upper case in FindSodium.cmake * install libsodium via brew; change sodium to upper case in FindSodium.cmake * simplify FindSodium.cmake * bug fixing * bug fixing * bug fixing * add macro SODIUM_STATIC * adjust build command for windows * put back original FindSodium.cmake * put back original FindSodium.cmake * fix sodium unzipped path for windows; add ninja * fix sodium unzipped path for windows; add ninja * fix sodium unzipped path for windows; add ninja * Win32 github build for secp256k1 * x64 architecture github build for secp256k1 * fix sodium linking on linux * enable docker buildx arm64 builds from forked repos * enable docker buildx arm64 builds from forked repos * enable docker buildx arm64 builds from forked repos * adjust mac builds for secp2561k and sodium * fix tonlib jni generation * minor fix * sync fixes across platforms * add libsodium build script for android and precompiled static libraries * build tonlib for android (fails) * FindSodium uppercase * remove system libsodium for android, use precompiled instead; specify SECP256K1_INCLUDE_DIR fir mac 12.6 * uppercase sodium * simplify FindSodium * fix windows build sodium path; use ninja for windows * simplify sodium 2 * adjust windows sodium paths; add paths to android jni * add ninja build windows * add ninja build windows * add ninja build windows 2 * remove win ninja * fix 1 * fix 2 * fix win 3 * fix linux compile 3 * fix jni 1 * fix jni 2 and mac * fix jni 3 * fix jni 4 * fix jni 5 * fix mac 6 * fix mac 7 and jni paths * fix jni 8 * rework sodium for android * rework sodium for android * rework sodium for android 2 * fixed sodium for android 2 * fixed sodium for android 3 * static secp256k1 for android * add precompiled arm secp256k1 * add precompiled arm secp256k1 * build native-lib with secp256k1 x86-64 (non arm) * update precompiled with NDK libsecp256k1.a * update precompiled with NDK libsecp256k1.a * update precompiled with NDK libsecp256k1.a * refactor llvm-strip location * refactor llvm-strip location * add native-lib.so for armv7a, armv8a * add native-lib.so for armv7a, armv8a * test armv7a, armv8a * armv7a - fails linking on sodium, test -> armv8a * works x86-64, armv7a - fails linking on sodium, armv8a - fails linking secp256k1 (incompatible with aarch64linux) * update libpsec256k1, sodium static libs * test x86 android native-lib * test armv7 android native-lib * test armv8 android native-lib * x86_64 and arm64 android native-lib works * x86_64 and arm64 android native-lib works * x86_64 and arm64 android native-lib works * test armv7 android native-lib * test all android native-libs * test all android native-libs * test all android native-libs * test all android native-libs - without SodiumAndroid * test all android native-libs - with FindSodiumAndroid.cmake * win, with Sodium via SODIUM_DIR * win, with Sodium via SODIUM_DIR env * win, with Sodium via SODIUM_DIR env * win, with Sodium via SODIUM_DIR env and SODIUM_USE_STATIC_LIBS * win, with Sodium via SODIUM_DIR, SODIUM_USE_STATIC_LIBS and SODIUM_INCLUDE_DIR * android, with FindSodium * android, with FindSodium with SODIUM_USE_STATIC_LIBS * remove if not apple * target_link_libraries(ton_crypto_core PUBLIC secp256k1) * android SECP256K1_INCLUDE_DIRS * android SECP256K1_INCLUDE_DIR * add libsecp256k1.a/so pre-compiled with ubuntu 22 x86-64 * add libsecp256k1.a/so pre-compiled with ubuntu 22 x86-64 * sodium dirs * sodium dirs * sodium dirs * remove NOT APPLE and SodiumAndroid * add NOT APPLE and remove SodiumAndroid * add NOT APPLE and remove SodiumAndroid * remove build scripts for 18.04, reduce CMakeLists.txt * remove build scripts for 18.04, reduce CMakeLists.txt * Fix cas consumption during library load * Fix fetch_config_params after merge * Add all ADDDIVMOD ops to Asm.fif * Save unpaid storage fee to due_payment * Add "set prev blocks info" to emulator * Adjusted builds (#13) * Update flake.nix Add libsodium * add libsecp256k1-dev and libsodium-dev into wasm build * make back emulator a shared library; put emulator to artifacts; compile wasm artifacts with sodium and secp256k1. * add secp256k1 to nix * compile emulator statically with nix * compile emulator statically with nix * compile emulator lib statically with nix * compile emulator lib statically with nix * add libemulator to artifacts * add shared libemulator library to artifacts * minor release fix * update set-output commands; add recent_changelog.md * releases fixes * releases fixes, multiline * releases fixes, multiline * releases fixes, multiline * put back multiline changelog * put back multiline changelog * ConfigParam 19 (global-id) and GLOBALID instruction * Fix gas consumption in HASHEXT * Add blst library * Add bls instructions * Allow passing long code to opcode-timing * Add bls testcase * More BLS instructions * Fix tests, add bls tests * Add more bls tests * Improve some bls operations * Adjust some BLS gas prices * Adjust BLS gas prices * Enable __BLST_PORTABLE__ flag only if PORTABLE flag is set * Add tests for BLS_PAIRING * GASCONSUMED instruction * Fix compilation against docker with blst library; (#14) * fix compilation against docker with blst library; add precompiled libblst.a to android builds * minor fix * Adjust BLKSWX gas * Fix comparison with NAN * Allow arbitrary integers for scalars in ristretto multiplication, fix test * Adjust nix builds according to PR 694 (#15) * integrate and test PR-694 * integrate and test PR-694, test 2 * Add P256_CHKSIGN (secp256r1) --------- Co-authored-by: SpyCheese <mikle98@yandex.ru> Co-authored-by: neodiX42 <namlem@gmail.com>
This commit is contained in:
parent
d6b11d9d36
commit
9f93888cf4
374 changed files with 25800 additions and 387 deletions
175
crypto/vm/vm.cpp
175
crypto/vm/vm.cpp
|
@ -389,7 +389,7 @@ int VmState::throw_exception(int excno) {
|
|||
stack_ref.push_smallint(0);
|
||||
stack_ref.push_smallint(excno);
|
||||
code.clear();
|
||||
gas.consume_chk(exception_gas_price);
|
||||
consume_gas_chk(exception_gas_price);
|
||||
return jump(get_c2());
|
||||
}
|
||||
|
||||
|
@ -399,7 +399,7 @@ int VmState::throw_exception(int excno, StackEntry&& arg) {
|
|||
stack_ref.push(std::move(arg));
|
||||
stack_ref.push_smallint(excno);
|
||||
code.clear();
|
||||
gas.consume_chk(exception_gas_price);
|
||||
consume_gas_chk(exception_gas_price);
|
||||
return jump(get_c2());
|
||||
}
|
||||
|
||||
|
@ -453,54 +453,41 @@ int VmState::step() {
|
|||
VM_LOG(this) << "execute implicit JMPREF";
|
||||
auto ref_cell = code->prefetch_ref();
|
||||
VM_LOG_MASK(this, vm::VmLog::ExecLocation) << "code cell hash: " << ref_cell->get_hash().to_hex() << " offset: 0";
|
||||
gas.consume_chk(implicit_jmpref_gas_price);
|
||||
consume_gas_chk(implicit_jmpref_gas_price);
|
||||
Ref<Continuation> cont = Ref<OrdCont>{true, load_cell_slice_ref(std::move(ref_cell)), get_cp()};
|
||||
return jump(std::move(cont));
|
||||
} else {
|
||||
VM_LOG(this) << "execute implicit RET";
|
||||
gas.consume_chk(implicit_ret_gas_price);
|
||||
consume_gas_chk(implicit_ret_gas_price);
|
||||
return ret();
|
||||
}
|
||||
}
|
||||
|
||||
int VmState::run() {
|
||||
if (code.is_null() || stack.is_null()) {
|
||||
// throw VmError{Excno::fatal, "cannot run an uninitialized VM"};
|
||||
return (int)Excno::fatal; // no ~ for unhandled exceptions
|
||||
}
|
||||
int VmState::run_inner() {
|
||||
int res;
|
||||
Guard guard(this);
|
||||
do {
|
||||
try {
|
||||
try {
|
||||
try {
|
||||
res = step();
|
||||
VM_LOG_MASK(this, vm::VmLog::GasRemaining) << "gas remaining: " << gas.gas_remaining;
|
||||
gas.check();
|
||||
} catch (vm::CellBuilder::CellWriteError) {
|
||||
throw VmError{Excno::cell_ov};
|
||||
} catch (vm::CellBuilder::CellCreateError) {
|
||||
throw VmError{Excno::cell_ov};
|
||||
} catch (vm::CellSlice::CellReadError) {
|
||||
throw VmError{Excno::cell_und};
|
||||
}
|
||||
} catch (const VmError& vme) {
|
||||
VM_LOG(this) << "handling exception code " << vme.get_errno() << ": " << vme.get_msg();
|
||||
try {
|
||||
++steps;
|
||||
res = throw_exception(vme.get_errno());
|
||||
} catch (const VmError& vme2) {
|
||||
VM_LOG(this) << "exception " << vme2.get_errno() << " while handling exception: " << vme.get_msg();
|
||||
return ~vme2.get_errno();
|
||||
}
|
||||
res = step();
|
||||
VM_LOG_MASK(this, vm::VmLog::GasRemaining) << "gas remaining: " << gas.gas_remaining;
|
||||
gas.check();
|
||||
} catch (vm::CellBuilder::CellWriteError) {
|
||||
throw VmError{Excno::cell_ov};
|
||||
} catch (vm::CellBuilder::CellCreateError) {
|
||||
throw VmError{Excno::cell_ov};
|
||||
} catch (vm::CellSlice::CellReadError) {
|
||||
throw VmError{Excno::cell_und};
|
||||
}
|
||||
} catch (const VmError& vme) {
|
||||
VM_LOG(this) << "handling exception code " << vme.get_errno() << ": " << vme.get_msg();
|
||||
try {
|
||||
++steps;
|
||||
res = throw_exception(vme.get_errno());
|
||||
} catch (const VmError& vme2) {
|
||||
VM_LOG(this) << "exception " << vme2.get_errno() << " while handling exception: " << vme.get_msg();
|
||||
return ~vme2.get_errno();
|
||||
}
|
||||
} catch (VmNoGas vmoog) {
|
||||
++steps;
|
||||
VM_LOG(this) << "unhandled out-of-gas exception: gas consumed=" << gas.gas_consumed()
|
||||
<< ", limit=" << gas.gas_limit;
|
||||
get_stack().clear();
|
||||
get_stack().push_smallint(gas.gas_consumed());
|
||||
return vmoog.get_errno(); // no ~ for unhandled exceptions (to make their faking impossible)
|
||||
}
|
||||
} while (!res);
|
||||
if ((res | 1) == -1 && !try_commit()) {
|
||||
|
@ -512,6 +499,34 @@ int VmState::run() {
|
|||
return res;
|
||||
}
|
||||
|
||||
int VmState::run() {
|
||||
if (code.is_null() || stack.is_null()) {
|
||||
// throw VmError{Excno::fatal, "cannot run an uninitialized VM"};
|
||||
return (int)Excno::fatal; // no ~ for unhandled exceptions
|
||||
}
|
||||
int res = 0;
|
||||
bool restore_parent = false;
|
||||
while (true) {
|
||||
try {
|
||||
if (restore_parent) {
|
||||
restore_parent_vm(~res);
|
||||
}
|
||||
res = run_inner();
|
||||
} catch (VmNoGas &vmoog) {
|
||||
++steps;
|
||||
VM_LOG(this) << "unhandled out-of-gas exception: gas consumed=" << gas.gas_consumed()
|
||||
<< ", limit=" << gas.gas_limit;
|
||||
get_stack().clear();
|
||||
get_stack().push_smallint(gas.gas_consumed());
|
||||
res = vmoog.get_errno(); // no ~ for unhandled exceptions (to make their faking impossible)
|
||||
}
|
||||
if (!parent) {
|
||||
return res;
|
||||
}
|
||||
restore_parent = true;
|
||||
}
|
||||
}
|
||||
|
||||
bool VmState::try_commit() {
|
||||
if (cr.d[0].not_null() && cr.d[1].not_null() && cr.d[0]->get_depth() <= max_data_depth &&
|
||||
cr.d[1]->get_depth() <= max_data_depth && cr.d[0]->get_level() == 0 && cr.d[1]->get_level() == 0) {
|
||||
|
@ -544,7 +559,8 @@ ControlRegs* force_cregs(Ref<Continuation>& cont) {
|
|||
}
|
||||
|
||||
int run_vm_code(Ref<CellSlice> code, Ref<Stack>& stack, int flags, Ref<Cell>* data_ptr, VmLog log, long long* steps,
|
||||
GasLimits* gas_limits, std::vector<Ref<Cell>> libraries, Ref<Tuple> init_c7, Ref<Cell>* actions_ptr) {
|
||||
GasLimits* gas_limits, std::vector<Ref<Cell>> libraries, Ref<Tuple> init_c7, Ref<Cell>* actions_ptr,
|
||||
int global_version) {
|
||||
VmState vm{code,
|
||||
std::move(stack),
|
||||
gas_limits ? *gas_limits : GasLimits{},
|
||||
|
@ -553,6 +569,7 @@ int run_vm_code(Ref<CellSlice> code, Ref<Stack>& stack, int flags, Ref<Cell>* da
|
|||
log,
|
||||
std::move(libraries),
|
||||
std::move(init_c7)};
|
||||
vm.set_global_version(global_version);
|
||||
int res = vm.run();
|
||||
stack = vm.get_stack_ref();
|
||||
if (vm.committed() && data_ptr) {
|
||||
|
@ -582,12 +599,13 @@ int run_vm_code(Ref<CellSlice> code, Ref<Stack>& stack, int flags, Ref<Cell>* da
|
|||
}
|
||||
|
||||
int run_vm_code(Ref<CellSlice> code, Stack& stack, int flags, Ref<Cell>* data_ptr, VmLog log, long long* steps,
|
||||
GasLimits* gas_limits, std::vector<Ref<Cell>> libraries, Ref<Tuple> init_c7, Ref<Cell>* actions_ptr) {
|
||||
GasLimits* gas_limits, std::vector<Ref<Cell>> libraries, Ref<Tuple> init_c7, Ref<Cell>* actions_ptr,
|
||||
int global_version) {
|
||||
Ref<Stack> stk{true};
|
||||
stk.unique_write().set_contents(std::move(stack));
|
||||
stack.clear();
|
||||
int res = run_vm_code(code, stk, flags, data_ptr, log, steps, gas_limits, std::move(libraries), std::move(init_c7),
|
||||
actions_ptr);
|
||||
actions_ptr, global_version);
|
||||
CHECK(stack.is_unique());
|
||||
if (stk.is_null()) {
|
||||
stack.clear();
|
||||
|
@ -608,7 +626,7 @@ int run_vm_code(Ref<CellSlice> code, Stack& stack, int flags, Ref<Cell>* data_pt
|
|||
Ref<Cell> VmState::load_library(td::ConstBitPtr hash) {
|
||||
std::unique_ptr<VmStateInterface> tmp_ctx;
|
||||
// install temporary dummy vm state interface to prevent charging for cell load operations during library lookup
|
||||
VmStateInterface::Guard(tmp_ctx.get());
|
||||
VmStateInterface::Guard guard{global_version >= 4 ? tmp_ctx.get() : VmStateInterface::get()};
|
||||
for (const auto& lib_collection : libraries) {
|
||||
auto lib = lookup_library_in(hash, lib_collection);
|
||||
if (lib.not_null()) {
|
||||
|
@ -632,9 +650,6 @@ void VmState::register_cell_load(const CellHash& cell_hash) {
|
|||
consume_gas(cell_load_gas_price);
|
||||
} else {
|
||||
auto ok = loaded_cells.insert(cell_hash); // check whether this is the first time this cell is loaded
|
||||
if (ok.second) {
|
||||
loaded_cells_count++;
|
||||
}
|
||||
consume_gas(ok.second ? cell_load_gas_price : cell_reload_gas_price);
|
||||
}
|
||||
}
|
||||
|
@ -681,4 +696,78 @@ Ref<vm::Cell> lookup_library_in(td::ConstBitPtr key, Ref<vm::Cell> lib_root) {
|
|||
return lookup_library_in(key, dict);
|
||||
}
|
||||
|
||||
void VmState::run_child_vm(VmState&& new_state, bool return_data, bool return_actions, bool return_gas,
|
||||
bool isolate_gas, int ret_vals) {
|
||||
new_state.log = std::move(log);
|
||||
new_state.libraries = std::move(libraries);
|
||||
new_state.stack_trace = stack_trace;
|
||||
new_state.max_data_depth = max_data_depth;
|
||||
if (!isolate_gas) {
|
||||
new_state.loaded_cells = std::move(loaded_cells);
|
||||
} else {
|
||||
consume_gas(std::min<long long>(chksgn_counter, chksgn_free_count) * chksgn_gas_price);
|
||||
chksgn_counter = 0;
|
||||
}
|
||||
new_state.chksgn_counter = chksgn_counter;
|
||||
|
||||
auto new_parent = std::make_unique<ParentVmState>();
|
||||
new_parent->return_data = return_data;
|
||||
new_parent->return_actions = return_actions;
|
||||
new_parent->return_gas = return_gas;
|
||||
new_parent->isolate_gas = isolate_gas;
|
||||
new_parent->ret_vals = ret_vals;
|
||||
new_parent->state = std::move(*this);
|
||||
new_state.parent = std::move(new_parent);
|
||||
*this = std::move(new_state);
|
||||
}
|
||||
|
||||
void VmState::restore_parent_vm(int res) {
|
||||
auto parent = std::move(this->parent);
|
||||
CHECK(parent);
|
||||
VmState child_state = std::move(*this);
|
||||
*this = std::move(parent->state);
|
||||
log = std::move(child_state.log);
|
||||
libraries = std::move(child_state.libraries);
|
||||
steps += child_state.steps;
|
||||
if (!parent->isolate_gas) {
|
||||
loaded_cells = std::move(child_state.loaded_cells);
|
||||
}
|
||||
chksgn_counter = child_state.chksgn_counter;
|
||||
VM_LOG(this) << "Child VM finished. res: " << res << ", steps: " << child_state.steps
|
||||
<< ", gas: " << child_state.gas_consumed();
|
||||
|
||||
consume_gas(std::min(child_state.gas_consumed(), child_state.gas.gas_limit + 1));
|
||||
Stack& cur_stack = get_stack();
|
||||
int ret_cnt;
|
||||
if (res == 0 || res == 1) {
|
||||
if (parent->ret_vals >= 0) {
|
||||
if (child_state.stack->depth() >= parent->ret_vals) {
|
||||
ret_cnt = parent->ret_vals;
|
||||
} else {
|
||||
ret_cnt = 0;
|
||||
res = ~(int)Excno::stk_und;
|
||||
cur_stack.push(td::zero_refint());
|
||||
}
|
||||
} else {
|
||||
ret_cnt = child_state.stack->depth();
|
||||
}
|
||||
} else {
|
||||
ret_cnt = std::min(child_state.stack->depth(), 1);
|
||||
}
|
||||
consume_stack_gas(ret_cnt);
|
||||
for (int i = ret_cnt - 1; i >= 0; --i) {
|
||||
cur_stack.push(std::move(child_state.stack->at(i)));
|
||||
}
|
||||
cur_stack.push_smallint(res);
|
||||
if (parent->return_data) {
|
||||
cur_stack.push_cell(child_state.get_committed_state().c4);
|
||||
}
|
||||
if (parent->return_actions) {
|
||||
cur_stack.push_cell(child_state.get_committed_state().c5);
|
||||
}
|
||||
if (parent->return_gas) {
|
||||
cur_stack.push_smallint(child_state.gas.gas_consumed());
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace vm
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue