1
0
Fork 0
mirror of https://github.com/ton-blockchain/ton synced 2025-03-09 15:40:10 +00:00

updated fift/func

This commit is contained in:
ton 2019-12-12 19:16:25 +04:00
parent b6f6788532
commit d41ce55305
31 changed files with 717 additions and 66 deletions

View file

@ -713,8 +713,8 @@ gas_limits#_ remaining:int64 _:^[ max_limit:int64 cur_limit:int64 credit:int64 ]
= VmGasLimits;
_ libraries:(HashmapE 256 ^Cell) = VmLibraries;
vm_ctl_data$_ nargs:int14 stack:(Maybe VmStack) save:VmSaveList
cp:int16 = VmControlData;
vm_ctl_data$_ nargs:(Maybe uint13) stack:(Maybe VmStack) save:VmSaveList
cp:(Maybe int16) = VmControlData;
vmc_std$00 cdata:VmControlData code:VmCellSlice = VmCont;
vmc_envelope$01 cdata:VmControlData next:^VmCont = VmCont;
vmc_quit$1000 exit_code:int32 = VmCont;

View file

@ -199,10 +199,13 @@ void usage() {
int main(int argc, char* const argv[]) {
int i;
int new_verbosity_level = VERBOSITY_NAME(INFO);
bool dump_state = false;
bool dump_state = false, dump_vmcont = false;
auto zerostate = std::make_unique<block::ZerostateInfo>();
while ((i = getopt(argc, argv, "Shv:")) != -1) {
while ((i = getopt(argc, argv, "CShv:")) != -1) {
switch (i) {
case 'C':
dump_vmcont = true;
break;
case 'S':
dump_state = true;
break;
@ -230,12 +233,13 @@ int main(int argc, char* const argv[]) {
vm::CellSlice cs{vm::NoVm(), boc};
cs.print_rec(std::cout);
std::cout << std::endl;
auto& type = dump_state ? (const tlb::TLB&)block::gen::t_ShardStateUnsplit : block::gen::t_Block;
std::string type_name = dump_state ? "ShardState" : "Block";
auto& type = !dump_vmcont
? (dump_state ? (const tlb::TLB&)block::gen::t_ShardStateUnsplit : block::gen::t_Block)
: block::gen::t_VmCont;
type.print_ref(std::cout, boc);
std::cout << std::endl;
bool ok = type.validate_ref(boc);
std::cout << "(" << (ok ? "" : "in") << "valid " << type_name << ")" << std::endl;
std::cout << "(" << (ok ? "" : "in") << "valid " << type << ")" << std::endl;
}
}
if (!done) {

View file

@ -310,14 +310,14 @@ x{A938} @Defop(8u+1) MODPOW2#
x{A984} @Defop MULDIV
x{A985} @Defop MULDIVR
x{A98C} @Defop MULDIVMOD
x{A9A4} @Defop MULRSHIFT
x{A9A5} @Defop MULRSHIFTR
x{A9B4} @Defop(8u+1) MULRSHIFT#
x{A9B5} @Defop(8u+1) MULRSHIFTR#
x{A9C4} @Defop LSHIFTDIV
x{A9C5} @Defop LSHIFTDIVR
x{A9D4} @Defop(8u+1) LSHIFT#DIV
x{A9D5} @Defop(8u+1) LSHIFT#DIVR
x{A9E4} @Defop MULRSHIFT
x{A9E5} @Defop MULRSHIFTR
x{A9F4} @Defop(8u+1) MULRSHIFT#
x{A9F5} @Defop(8u+1) MULRSHIFTR#
x{AA} @Defop(8u+1) LSHIFT#
x{AB} @Defop(8u+1) RSHIFT#
x{AC} @Defop LSHIFT
@ -972,12 +972,17 @@ x{F800} @Defop ACCEPT
x{F801} @Defop SETGASLIMIT
x{F80F} @Defop COMMIT
x{F810} @Defop RANDU256
x{F811} @Defop RAND
x{F814} @Defop SETRAND
x{F815} dup @Defop ADDRAND @Defop RANDOMIZE
x{F82} @Defop(4u) GETPARAM
x{F823} @Defop NOW
x{F824} @Defop BLOCKLT
x{F825} @Defop LTIME
x{F826} @Defop BALANCE
x{F827} @Defop RANDSEED
x{F826} @Defop RANDSEED
x{F827} @Defop BALANCE
x{F828} @Defop MYADDR
x{F829} @Defop CONFIGROOT
x{F830} @Defop CONFIGDICT

View file

@ -26,9 +26,14 @@ recursive list-delete-range {
} : $pfx?
// ( s -- ? ) checks whether s is an option (a string beginning with '-')
{ dup $len 1 > { "-" $pfx? } { drop false } cond } : is-opt?
// ( s -- ? ) checks whether s is a digit option
{ 2 $| drop 1 $| nip $>B 8 B>u@ dup 57 <= swap 48 >= and } : is-digit-opt?
0 box constant disable-digit-opts
// ( l -- s i or 0 ) finds first string in l beginning with '-'
{ 0 { 1+ over null? { 2drop 0 true } {
swap uncons over is-opt? { drop swap true } { nip swap false } cond
swap uncons over is-opt?
{ disable-digit-opts @ { over is-digit-opt? not } { true } cond } { false } cond
{ drop swap true } { nip swap false } cond
} cond } until
} : list-find-opt
// ( -- s i or 0 ) finds first option in cmdline args
@ -51,11 +56,15 @@ recursive list-delete-range {
// ( s -- s' null or s' s'' ) Splits long option --opt=arg at '='
{ dup "=" $pos 1+ ?dup { tuck $| swap rot 1- $| drop swap } { null } cond
} : split-longopt
// ( l -- f or 0 ) Extracts global option flags from first entry of l
{ dup null? { drop 0 } { car get-opt-flags -256 and } cond
} : get-global-option-flags
variable options-list
// ( l -- i or 0 )
// parses command line arguments according to option description list l
// and returns index i of first incorrect option
{ options-list !
{ dup options-list ! get-global-option-flags
256 and disable-digit-opts !
{ first-opt dup 0= { true } {
swap dup "--" $pfx? { // i s
dup $len 2 = { drop dup 1 $*del.. 0 true } {
@ -116,5 +125,7 @@ anon constant opt-list-marker
{ 6 2swap 4 tuple } : short-long-option-?arg
// ( o s -- s' ) Adds help message to option
' , : option-help
// ( s -- o ) Creates a generic help message
{ 'nop 8 "" 3 roll 4 tuple } : generic-help
// ( s f -- o ) Creates a generic help message
{ swap 'nop rot "" 3 roll 4 tuple } : generic-help-setopt
{ 0 generic-help-setopt } : generic-help
256 constant disable-digit-options

View file

@ -2356,6 +2356,25 @@ void interpret_db_run_vm_parallel(IntCtx& ctx) {
do_interpret_db_run_vm_parallel(ctx.error_stream, ctx.stack, ctx.ton_db, threads_n, tasks_n);
}
void interpret_store_vm_cont(vm::Stack& stack) {
auto vmcont = stack.pop_cont();
auto cb = stack.pop_builder();
if (!vmcont->serialize(cb.write())) {
throw IntError{"cannot serialize vm continuation"};
}
stack.push_builder(std::move(cb));
}
void interpret_fetch_vm_cont(vm::Stack& stack) {
auto cs = stack.pop_cellslice();
auto vmcont = vm::Continuation::deserialize(cs.write());
if (vmcont.is_null()) {
throw IntError{"cannot deserialize vm continuation"};
}
stack.push_cellslice(std::move(cs));
stack.push_cont(std::move(vmcont));
}
Ref<vm::Box> cmdline_args{true};
void interpret_get_fixed_cmdline_arg(vm::Stack& stack, int n) {
@ -2858,6 +2877,8 @@ void init_words_vm(Dictionary& d) {
d.def_ctx_word("gasrunvmctx ", std::bind(interpret_run_vm_c7, _1, true));
d.def_ctx_word("dbrunvm ", interpret_db_run_vm);
d.def_ctx_word("dbrunvm-parallel ", interpret_db_run_vm_parallel);
d.def_stack_word("vmcont, ", interpret_store_vm_cont);
d.def_stack_word("vmcont@ ", interpret_fetch_vm_cont);
}
void import_cmdline_args(Dictionary& d, std::string arg0, int n, const char* const argv[]) {

View file

@ -431,26 +431,25 @@ bool Op::compute_used_vars(const CodeBlob& code, bool edit) {
}
case _While: {
// while (block0 || left) block1;
// ... { block0 left block1 } block0 left next
VarDescrList after_cond_first{next_var_info};
after_cond_first += left;
code.compute_used_code_vars(block0, after_cond_first, false);
VarDescrList new_var_info{block0->var_info};
// ... block0 left { block1 block0 left } next
VarDescrList new_var_info{next_var_info};
bool changes = false;
do {
code.compute_used_code_vars(block1, block0->var_info, changes);
VarDescrList after_cond{block1->var_info};
VarDescrList after_cond{new_var_info};
after_cond += left;
code.compute_used_code_vars(block0, after_cond, changes);
code.compute_used_code_vars(block1, block0->var_info, changes);
std::size_t n = new_var_info.size();
new_var_info += block0->var_info;
new_var_info += block1->var_info;
new_var_info.clear_last();
if (changes) {
break;
}
changes = (new_var_info.size() == n);
} while (changes <= edit);
return set_var_info(std::move(new_var_info));
new_var_info += left;
code.compute_used_code_vars(block0, new_var_info, edit);
return set_var_info(block0->var_info);
}
case _Until: {
// until (block0 || left);

9
crypto/func/test/a10.fc Normal file
View file

@ -0,0 +1,9 @@
_ f(int a, int x) {
int y = 0;
int z = 0;
while ((y = x * x) > a) {
x -= 1;
z = 1;
}
return (y, z);
}

20
crypto/func/test/a6_4.fc Normal file
View file

@ -0,0 +1,20 @@
var calc_root(m) {
int base = 1;
repeat(70) { base *= 10; }
var (a, b, c) = (1, 0, - m);
var (p1, q1, p2, q2) = (1, 0, 0, 1);
do {
int k = -1;
var (a1, b1, c1) = (0, 0, 0);
do {
k += 1;
(a1, b1, c1) = (a, b, c);
c += b;
c += b += a;
} until (c > 0);
(a, b, c) = (- c1, - b1, - a1);
(p1, q1) = (k * p1 + q1, p1);
(p2, q2) = (k * p2 + q2, p2);
} until (p1 > base);
return (p1, q1, p2, q2);
}

22
crypto/func/test/w8.fc Normal file
View file

@ -0,0 +1,22 @@
int check_signatures(msg_hash, signatures, signers, bitmask_size) impure {
var bitmask = 0;
var id = -1;
do {
(id, var signature, var f) = signatures.udict_get_next?(32, id);
if (f){
var sig = signature.preload_bits(512);
var public_key = -1;
do {
(public_key, var cs, var _found) = signers.udict_get_next?(256, public_key);
if (_found){
if (check_signature(msg_hash, sig, public_key)){
var signer_index = cs~load_uint(bitmask_size);
bitmask = bitmask | (1 << (signer_index - 1));
}
}
} until (~ _found);
;; signature~touch();
}
} until (~ f);
return bitmask;
}

8
crypto/func/test/w9.fc Normal file
View file

@ -0,0 +1,8 @@
_ g(s) {
var (z, t) = (17, s);
while (z > 0) {
t = s;
z -= 1;
}
return ~ t;
}

View file

@ -14,7 +14,7 @@ begin-options
+"Creates a request with up to 254 orders loaded from <order-file> to high-load (sub)wallet created by new-highload-v2-wallet.fif, with private key loaded from file <filename-base>.pk "
+"and address from <filename-base><subwallet-id>.addr, and saves it into <savefile>.boc ('wallet-query.boc' by default)" +cr
+"<order-file> is a text file with lines `SEND <dest-addr> <amount>`"
generic-help
disable-digit-options generic-help-setopt
"n" "--no-bounce" { false =: allow-bounce } short-long-option
"Clears bounce flag" option-help
"b" "--force-bounce" { true =: force-bounce } short-long-option

View file

@ -14,7 +14,7 @@ begin-options
+"Creates a request with up to 254 orders loaded from <order-file> to high-load (sub)wallet created by new-highload-wallet.fif, with private key loaded from file <filename-base>.pk "
+"and address from <filename-base><subwallet-id>.addr, and saves it into <savefile>.boc ('wallet-query.boc' by default)" +cr
+"<order-file> is a text file with lines `SEND <dest-addr> <amount>`"
generic-help
disable-digit-options generic-help-setopt
"n" "--no-bounce" { false =: allow-bounce } short-long-option
"Clears bounce flag" option-help
"b" "--force-bounce" { true =: force-bounce } short-long-option

View file

@ -15,7 +15,7 @@
throw_unless(33, msg_seqno == stored_seqno);
throw_unless(34, check_signature(slice_hash(in_msg), signature, public_key));
accept_message();
cs~touch_slice();
cs~touch();
if (cs.slice_refs()) {
var mode = cs~load_uint(8);
send_raw_message(cs~load_ref(), mode);

View file

@ -30,7 +30,7 @@ slice do_verify_message(slice in_msg, int seqno, int public_key) {
(int stored_seqno, int public_key) = load_state();
var cs = do_verify_message(in_msg, stored_seqno, public_key);
accept_message();
cs~touch_slice();
cs~touch();
if (cs.slice_refs()) {
var mode = cs~load_uint(8);
send_raw_message(cs~load_ref(), mode);
@ -57,11 +57,7 @@ cell prepare_send_message(int mode, cell msg) method_id {
return prepare_send_message_with_seqno(mode, msg, seqno());
}
slice verify_message(slice msg) method_id {
var (stored_seqno, public_key) = load_state();
return do_verify_message(msg, stored_seqno, public_key);
}

View file

@ -21,6 +21,8 @@ forall X -> X null() asm "PUSHNULL";
int now() asm "NOW";
slice my_address() asm "MYADDR";
tuple get_balance() asm "BALANCE";
int cur_lt() asm "LTIME";
int block_lt() asm "BLOCKLT";
int cell_hash(cell c) asm "HASHCU";
int slice_hash(slice s) asm "HASHSU";
@ -154,5 +156,9 @@ int cell_null?(cell c) asm "ISNULL";
() send_raw_message(cell msg, int mode) impure asm "SENDRAWMSG";
() set_code(cell new_code) impure asm "SETCODE";
slice touch_slice(slice s) asm "NOP";
(slice,()) ~touch_slice(slice s) asm "NOP";
int random() impure asm "RANDU256";
int rand(int range) impure asm "RAND";
int get_seed() impure asm "RANDSEED";
int set_seed() impure asm "SETRAND";
() randomize(int x) impure asm "ADDRAND";
() randomize_lt() impure asm "LTIME" "ADDRAND";

View file

@ -14,7 +14,7 @@ begin-options
" <filename-base> <dest-addr> <seqno> <amount> [-n|-b] [-t<timeout>] [-B <body-boc>] [-C <comment>] [<savefile>]" +cr +tab
+"Creates a request to advanced wallet created by new-wallet-v2.fif, with private key loaded from file <filename-base>.pk "
+"and address from <filename-base>.addr, and saves it into <savefile>.boc ('wallet-query.boc' by default)"
generic-help
disable-digit-options generic-help-setopt
"n" "--no-bounce" { false =: allow-bounce } short-long-option
"Clears bounce flag" option-help
"b" "--force-bounce" { true =: force-bounce } short-long-option

View file

@ -14,7 +14,7 @@ begin-options
" <filename-base> <dest-addr> <subwallet-id> <seqno> <amount> [-n|-b] [-t<timeout>] [-B <body-boc>] [-C <comment>] [<savefile>]" +cr +tab
+"Creates a request to advanced wallet created by new-wallet-v3.fif, with private key loaded from file <filename-base>.pk "
+"and address from <filename-base>.addr, and saves it into <savefile>.boc ('wallet-query.boc' by default)"
generic-help
disable-digit-options generic-help-setopt
"n" "--no-bounce" { false =: allow-bounce } short-long-option
"Clears bounce flag" option-help
"b" "--force-bounce" { true =: force-bounce } short-long-option

View file

@ -13,7 +13,7 @@ begin-options
" <filename-base> <dest-addr> <seqno> <amount> [-n|-b] [-B <body-boc>] [-C <comment>] [<savefile>]" +cr +tab
+"Creates a request to simple wallet created by new-wallet.fif, with private key loaded from file <filename-base>.pk "
+"and address from <filename-base>.addr, and saves it into <savefile>.boc ('wallet-query.boc' by default)"
generic-help
disable-digit-options generic-help-setopt
"n" "--no-bounce" { false =: allow-bounce } short-long-option
"Clears bounce flag" option-help
"b" "--force-bounce" { true =: force-bounce } short-long-option

View file

@ -102,13 +102,15 @@ unsigned long long get_special_value(std::string str) {
unsigned long long val = 0;
int bits = 0;
if (str[0] == '#') {
while (i < n) {
int c = str[i++];
for (; i < n; i++) {
int c = str[i];
if (c == '_') {
break;
}
if (c >= '0' && c <= '9') {
c -= '0';
} else if (c >= 'A' && c <= 'F') {
c -= 'A' - 10;
} else if (c >= 'a' && c <= 'f') {
c -= 'a' - 10;
} else {
@ -146,6 +148,9 @@ unsigned long long get_special_value(std::string str) {
while (bits && !((val >> (64 - bits)) & 1)) {
--bits;
}
if (bits) {
--bits;
}
}
if (bits == 64) {
return 0;

View file

@ -33,6 +33,12 @@ const NatWidth t_Nat{32};
const Anything t_Anything;
const RefAnything t_RefCell;
std::string TLB::get_type_name() const {
std::ostringstream os;
print_type(os);
return os.str();
}
bool Bool::print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const {
int t = get_tag(cs);
return cs.advance(1) && pp.out(t ? "bool_true" : "bool_false");

View file

@ -196,6 +196,7 @@ class TLB {
virtual std::ostream& print_type(std::ostream& os) const {
return os << "<unknown-TLB-type>";
}
std::string get_type_name() const;
virtual bool print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const;
virtual bool print(PrettyPrinter& pp, const vm::CellSlice& cs) const {
vm::CellSlice cs_copy{cs};

View file

@ -32,6 +32,17 @@ bool Continuation::has_c0() const {
return cont_data && cont_data->save.c[0].not_null();
}
bool ControlRegs::clear() {
for (unsigned i = 0; i < creg_num; i++) {
c[i].clear();
}
for (unsigned i = 0; i < dreg_num; i++) {
d[i].clear();
}
c7.clear();
return true;
}
StackEntry ControlRegs::get(unsigned idx) const {
if (idx < creg_num) {
return get_c(idx);
@ -108,6 +119,7 @@ ControlRegs& ControlRegs::operator&=(const ControlRegs& save) {
}
bool ControlRegs::serialize(CellBuilder& cb) const {
// _ cregs:(HashmapE 4 VmStackValue) = VmSaveList;
Dictionary dict{4};
CellBuilder cb2;
for (int i = 0; i < creg_num; i++) {
@ -126,8 +138,34 @@ bool ControlRegs::serialize(CellBuilder& cb) const {
std::move(dict).append_dict_to_bool(cb);
}
bool ControlRegs::deserialize(CellSlice& cs, int mode) {
// _ cregs:(HashmapE 4 VmStackValue) = VmSaveList;
Ref<Cell> root;
return cs.fetch_maybe_ref(root) && deserialize(std::move(root), mode);
}
bool ControlRegs::deserialize(Ref<Cell> root, int mode) {
try {
clear();
Dictionary dict{std::move(root), 4};
return dict.check_for_each([this, mode](Ref<CellSlice> val, td::ConstBitPtr key, int n) {
StackEntry value;
return value.deserialize(val.write(), mode) && val->empty_ext() && set((int)key.get_uint(4), std::move(value));
});
} catch (VmError&) {
return false;
}
}
bool ControlData::clear() {
stack.clear();
save.clear();
nargs = cp = -1;
return true;
}
bool ControlData::serialize(CellBuilder& cb) const {
// vm_ctl_data$_ nargs:(Maybe int13) stack:(Maybe VmStack) save:VmSaveList
// vm_ctl_data$_ nargs:(Maybe uint13) stack:(Maybe VmStack) save:VmSaveList
// cp:(Maybe int16) = VmControlData;
return cb.store_bool_bool(nargs >= 0) // vm_ctl_data$_ nargs:(Maybe ...
&& (nargs < 0 || cb.store_long_bool(nargs, 13)) // ... int13)
@ -138,16 +176,88 @@ bool ControlData::serialize(CellBuilder& cb) const {
&& (cp == -1 || cb.store_long_bool(cp, 16)); // ... int16)
}
bool ControlData::deserialize(CellSlice& cs, int mode) {
// vm_ctl_data$_ nargs:(Maybe uint13) stack:(Maybe VmStack) save:VmSaveList
// cp:(Maybe int16) = VmControlData;
nargs = cp = -1;
stack.clear();
bool f;
return cs.fetch_bool_to(f) && (!f || cs.fetch_uint_to(13, nargs)) // nargs:(Maybe uint13)
&& cs.fetch_bool_to(f) && (!f || Stack::deserialize_to(cs, stack, mode)) // stack:(Maybe VmStack)
&& save.deserialize(cs, mode) // save:VmSaveList
&& cs.fetch_bool_to(f) && (!f || (cs.fetch_int_to(16, cp) && cp != -1)); // cp:(Maybe int16)
}
bool Continuation::serialize_ref(CellBuilder& cb) const {
vm::CellBuilder cb2;
return serialize(cb2) && cb.store_ref_bool(cb2.finalize());
}
Ref<Continuation> Continuation::deserialize(CellSlice& cs, int mode) {
if (mode & 0x1002) {
return {};
}
mode |= 0x1000;
switch (cs.bselect_ext(6, 0x100f011100010001ULL)) {
case 0:
// vmc_std$00 cdata:VmControlData code:VmCellSlice = VmCont;
return OrdCont::deserialize(cs, mode);
case 1:
// vmc_envelope$01 cdata:VmControlData next:^VmCont = VmCont;
return ArgContExt::deserialize(cs, mode);
case 2:
// vmc_quit$1000 exit_code:int32 = VmCont;
return QuitCont::deserialize(cs, mode);
case 3:
// vmc_quit_exc$1001 = VmCont;
return ExcQuitCont::deserialize(cs, mode);
case 4:
// vmc_repeat$10100 count:uint63 body:^VmCont after:^VmCont = VmCont;
return RepeatCont::deserialize(cs, mode);
case 5:
// vmc_until$110000 body:^VmCont after:^VmCont = VmCont;
return UntilCont::deserialize(cs, mode);
case 6:
// vmc_again$110001 body:^VmCont = VmCont;
return AgainCont::deserialize(cs, mode);
case 7:
// vmc_while_cond$110010 cond:^VmCont body:^VmCont after:^VmCont = VmCont;
return WhileCont::deserialize(cs, mode | 0x2000);
case 8:
// vmc_while_body$110011 cond:^VmCont body:^VmCont after:^VmCont = VmCont;
return WhileCont::deserialize(cs, mode & ~0x2000);
case 9:
// vmc_pushint$1111 value:int32 next:^VmCont = VmCont;
return PushIntCont::deserialize(cs, mode);
default:
return {};
}
}
bool Continuation::deserialize_to(Ref<Cell> cell, Ref<Continuation>& cont, int mode) {
if (cell.is_null()) {
cont.clear();
return false;
}
CellSlice cs = load_cell_slice(std::move(cell));
return deserialize_to(cs, cont, mode & ~0x1000) && cs.empty_ext();
}
bool QuitCont::serialize(CellBuilder& cb) const {
// vmc_quit$1000 exit_code:int32 = VmCont;
return cb.store_long_bool(8, 4) && cb.store_long_bool(exit_code, 32);
}
Ref<QuitCont> QuitCont::deserialize(CellSlice& cs, int mode) {
// vmc_quit$1000 exit_code:int32 = VmCont;
int exit_code;
if (cs.fetch_ulong(4) == 8 && cs.fetch_int_to(32, exit_code)) {
return Ref<QuitCont>{true, exit_code};
} else {
return {};
}
}
int ExcQuitCont::jump(VmState* st) const & {
int n = 0;
try {
@ -164,6 +274,11 @@ bool ExcQuitCont::serialize(CellBuilder& cb) const {
return cb.store_long_bool(9, 4);
}
Ref<ExcQuitCont> ExcQuitCont::deserialize(CellSlice& cs, int mode) {
// vmc_quit_exc$1001 = VmCont;
return cs.fetch_ulong(4) == 9 ? Ref<ExcQuitCont>{true} : Ref<ExcQuitCont>{};
}
int PushIntCont::jump(VmState* st) const & {
VM_LOG(st) << "execute implicit PUSH " << push_val << " (slow)";
st->get_stack().push_smallint(push_val);
@ -181,6 +296,19 @@ bool PushIntCont::serialize(CellBuilder& cb) const {
return cb.store_long_bool(15, 4) && cb.store_long_bool(push_val, 32) && next->serialize_ref(cb);
}
Ref<PushIntCont> PushIntCont::deserialize(CellSlice& cs, int mode) {
// vmc_pushint$1111 value:int32 next:^VmCont = VmCont;
int value;
Ref<Cell> ref;
Ref<Continuation> next;
if (cs.fetch_ulong(4) == 15 && cs.fetch_int_to(32, value) && cs.fetch_ref_to(ref) &&
deserialize_to(std::move(ref), next, mode)) {
return Ref<PushIntCont>{true, value, std::move(next)};
} else {
return {};
}
}
int ArgContExt::jump(VmState* st) const & {
st->adjust_cr(data.save);
if (data.cp != -1) {
@ -202,6 +330,18 @@ bool ArgContExt::serialize(CellBuilder& cb) const {
return cb.store_long_bool(1, 2) && data.serialize(cb) && ext->serialize_ref(cb);
}
Ref<ArgContExt> ArgContExt::deserialize(CellSlice& cs, int mode) {
// vmc_envelope$01 cdata:VmControlData next:^VmCont = VmCont;
ControlData cdata;
Ref<Cell> ref;
Ref<Continuation> next;
mode &= ~0x1000;
return cs.fetch_ulong(2) == 1 && cdata.deserialize(cs, mode) && cs.fetch_ref_to(ref) &&
deserialize_to(std::move(ref), next, mode)
? Ref<ArgContExt>{true, std::move(next), std::move(cdata)}
: Ref<ArgContExt>{};
}
int RepeatCont::jump(VmState* st) const & {
VM_LOG(st) << "repeat " << count << " more times (slow)\n";
if (count <= 0) {
@ -236,6 +376,20 @@ bool RepeatCont::serialize(CellBuilder& cb) const {
after->serialize_ref(cb);
}
Ref<RepeatCont> RepeatCont::deserialize(CellSlice& cs, int mode) {
// vmc_repeat$10100 count:uint63 body:^VmCont after:^VmCont = VmCont;
long long count;
Ref<Cell> ref;
Ref<Continuation> body, after;
if (cs.fetch_ulong(5) == 0x14 && cs.fetch_uint_to(63, count) && cs.fetch_ref_to(ref) &&
deserialize_to(std::move(ref), body, mode) && cs.fetch_ref_to(ref) &&
deserialize_to(std::move(ref), after, mode)) {
return Ref<RepeatCont>{true, std::move(body), std::move(after), count};
} else {
return {};
}
}
int VmState::repeat(Ref<Continuation> body, Ref<Continuation> after, long long count) {
if (count <= 0) {
body.clear();
@ -268,6 +422,17 @@ bool AgainCont::serialize(CellBuilder& cb) const {
return cb.store_long_bool(0x31, 6) && body->serialize_ref(cb);
}
Ref<AgainCont> AgainCont::deserialize(CellSlice& cs, int mode) {
// vmc_again$110001 body:^VmCont = VmCont;
Ref<Cell> ref;
Ref<Continuation> body;
if (cs.fetch_ulong(6) == 0x31 && cs.fetch_ref_to(ref) && deserialize_to(std::move(ref), body, mode)) {
return Ref<AgainCont>{true, std::move(body)};
} else {
return {};
}
}
int VmState::again(Ref<Continuation> body) {
return jump(Ref<AgainCont>{true, std::move(body)});
}
@ -305,6 +470,18 @@ bool UntilCont::serialize(CellBuilder& cb) const {
return cb.store_long_bool(0x30, 6) && body->serialize_ref(cb) && after->serialize_ref(cb);
}
Ref<UntilCont> UntilCont::deserialize(CellSlice& cs, int mode) {
// vmc_until$110000 body:^VmCont after:^VmCont = VmCont;
Ref<Cell> ref;
Ref<Continuation> body, after;
if (cs.fetch_ulong(6) == 0x30 && cs.fetch_ref_to(ref) && deserialize_to(std::move(ref), body, mode) &&
cs.fetch_ref_to(ref) && deserialize_to(std::move(ref), after, mode)) {
return Ref<UntilCont>{true, std::move(body), std::move(after)};
} else {
return {};
}
}
int VmState::until(Ref<Continuation> body, Ref<Continuation> after) {
if (!body->has_c0()) {
set_c0(Ref<UntilCont>{true, body, std::move(after)});
@ -371,6 +548,22 @@ bool WhileCont::serialize(CellBuilder& cb) const {
body->serialize_ref(cb) && after->serialize_ref(cb);
}
Ref<WhileCont> WhileCont::deserialize(CellSlice& cs, int mode) {
// vmc_while_cond$110010 cond:^VmCont body:^VmCont after:^VmCont = VmCont;
// vmc_while_body$110011 cond:^VmCont body:^VmCont after:^VmCont = VmCont;
bool at_body;
Ref<Cell> ref;
Ref<Continuation> cond, body, after;
if (cs.fetch_ulong(5) == 0x19 && cs.fetch_bool_to(at_body) && cs.fetch_ref_to(ref) &&
deserialize_to(std::move(ref), cond, mode) && cs.fetch_ref_to(ref) &&
deserialize_to(std::move(ref), body, mode) && cs.fetch_ref_to(ref) &&
deserialize_to(std::move(ref), after, mode)) {
return Ref<WhileCont>{true, std::move(cond), std::move(body), std::move(after), !at_body};
} else {
return {};
}
}
int VmState::loop_while(Ref<Continuation> cond, Ref<Continuation> body, Ref<Continuation> after) {
if (!cond->has_c0()) {
set_c0(Ref<WhileCont>{true, cond, std::move(body), std::move(after), true});
@ -392,7 +585,18 @@ int OrdCont::jump_w(VmState* st) & {
bool OrdCont::serialize(CellBuilder& cb) const {
// vmc_std$00 cdata:VmControlData code:VmCellSlice = VmCont;
return cb.store_long_bool(1, 2) && data.serialize(cb) && StackEntry{code}.serialize(cb);
return cb.store_long_bool(0, 2) && data.serialize(cb) && StackEntry{code}.serialize(cb, 0x1000);
}
Ref<OrdCont> OrdCont::deserialize(CellSlice& cs, int mode) {
// vmc_std$00 cdata:VmControlData code:VmCellSlice = VmCont;
ControlData cdata;
StackEntry val;
mode &= ~0x1000;
return cs.fetch_ulong(2) == 0 && cdata.deserialize(cs, mode) && val.deserialize(cs, 0x4000) &&
val.is(StackEntry::t_slice)
? Ref<OrdCont>{true, std::move(val).as_slice(), std::move(cdata)}
: Ref<OrdCont>{};
}
void VmState::init_cregs(bool same_c3, bool push_0) {

View file

@ -37,6 +37,7 @@ struct ControlRegs {
Ref<Continuation> c[creg_num]; // c0..c3
Ref<Cell> d[dreg_num]; // c4..c5
Ref<Tuple> c7; // c7
bool clear();
Ref<Continuation> get_c(unsigned idx) const {
return idx < creg_num ? c[idx] : Ref<Continuation>{};
}
@ -136,6 +137,8 @@ struct ControlRegs {
ControlRegs& operator^=(const ControlRegs& save); // sets c[i]=save.c[i] for all save.c[i] != 0
ControlRegs& operator^=(ControlRegs&& save);
bool serialize(CellBuilder& cb) const;
bool deserialize(CellSlice& cs, int mode = 0);
bool deserialize(Ref<Cell> root, int mode = 0);
};
struct ControlData {
@ -151,7 +154,9 @@ struct ControlData {
}
ControlData(int _cp, Ref<Stack> _stack, int _nargs = -1) : stack(std::move(_stack)), nargs(_nargs), cp(_cp) {
}
bool clear();
bool serialize(CellBuilder& cb) const;
bool deserialize(CellSlice& cs, int mode = 0);
};
class Continuation : public td::CntObject {
@ -164,10 +169,6 @@ class Continuation : public td::CntObject {
virtual const ControlData* get_cdata() const {
return 0;
}
virtual bool serialize(CellBuilder& cb) const {
return false;
}
bool serialize_ref(CellBuilder& cb) const;
bool has_c0() const;
Continuation() {
}
@ -181,6 +182,15 @@ class Continuation : public td::CntObject {
return *this;
}
~Continuation() override = default;
virtual bool serialize(CellBuilder& cb) const {
return false;
}
bool serialize_ref(CellBuilder& cb) const;
static Ref<Continuation> deserialize(CellSlice& cs, int mode = 0);
static bool deserialize_to(CellSlice& cs, Ref<Continuation>& cont, int mode = 0) {
return (cont = deserialize(cs, mode)).not_null();
}
static bool deserialize_to(Ref<Cell> cell, Ref<Continuation>& cont, int mode = 0);
};
class QuitCont : public Continuation {
@ -194,6 +204,7 @@ class QuitCont : public Continuation {
return ~exit_code;
}
bool serialize(CellBuilder& cb) const override;
static Ref<QuitCont> deserialize(CellSlice& cs, int mode = 0);
};
class ExcQuitCont : public Continuation {
@ -202,6 +213,7 @@ class ExcQuitCont : public Continuation {
~ExcQuitCont() override = default;
int jump(VmState* st) const & override;
bool serialize(CellBuilder& cb) const override;
static Ref<ExcQuitCont> deserialize(CellSlice& cs, int mode = 0);
};
class PushIntCont : public Continuation {
@ -215,6 +227,7 @@ class PushIntCont : public Continuation {
int jump(VmState* st) const & override;
int jump_w(VmState* st) & override;
bool serialize(CellBuilder& cb) const override;
static Ref<PushIntCont> deserialize(CellSlice& cs, int mode = 0);
};
class RepeatCont : public Continuation {
@ -229,6 +242,7 @@ class RepeatCont : public Continuation {
int jump(VmState* st) const & override;
int jump_w(VmState* st) & override;
bool serialize(CellBuilder& cb) const override;
static Ref<RepeatCont> deserialize(CellSlice& cs, int mode = 0);
};
class AgainCont : public Continuation {
@ -241,6 +255,7 @@ class AgainCont : public Continuation {
int jump(VmState* st) const & override;
int jump_w(VmState* st) & override;
bool serialize(CellBuilder& cb) const override;
static Ref<AgainCont> deserialize(CellSlice& cs, int mode = 0);
};
class UntilCont : public Continuation {
@ -253,6 +268,7 @@ class UntilCont : public Continuation {
int jump(VmState* st) const & override;
int jump_w(VmState* st) & override;
bool serialize(CellBuilder& cb) const override;
static Ref<UntilCont> deserialize(CellSlice& cs, int mode = 0);
};
class WhileCont : public Continuation {
@ -267,6 +283,7 @@ class WhileCont : public Continuation {
int jump(VmState* st) const & override;
int jump_w(VmState* st) & override;
bool serialize(CellBuilder& cb) const override;
static Ref<WhileCont> deserialize(CellSlice& cs, int mode = 0);
};
class ArgContExt : public Continuation {
@ -274,9 +291,13 @@ class ArgContExt : public Continuation {
Ref<Continuation> ext;
public:
ArgContExt(Ref<Continuation> _ext) : data(), ext(_ext) {
ArgContExt(Ref<Continuation> _ext) : data(), ext(std::move(_ext)) {
}
ArgContExt(Ref<Continuation> _ext, Ref<Stack> _stack) : data(_stack), ext(_ext) {
ArgContExt(Ref<Continuation> _ext, Ref<Stack> _stack) : data(std::move(_stack)), ext(std::move(_ext)) {
}
ArgContExt(Ref<Continuation> _ext, const ControlData& _cdata) : data(_cdata), ext(std::move(_ext)) {
}
ArgContExt(Ref<Continuation> _ext, ControlData&& _cdata) : data(std::move(_cdata)), ext(std::move(_ext)) {
}
ArgContExt(const ArgContExt&) = default;
ArgContExt(ArgContExt&&) = default;
@ -293,6 +314,7 @@ class ArgContExt : public Continuation {
return new ArgContExt{*this};
}
bool serialize(CellBuilder& cb) const override;
static Ref<ArgContExt> deserialize(CellSlice& cs, int mode = 0);
};
class OrdCont : public Continuation {
@ -310,6 +332,10 @@ class OrdCont : public Continuation {
OrdCont(Ref<CellSlice> _code, int _cp, Ref<Stack> _stack, int nargs = -1)
: data(_cp, std::move(_stack), nargs), code(std::move(_code)) {
}
OrdCont(Ref<CellSlice> _code, const ControlData& _cdata) : data(_cdata), code(std::move(_code)) {
}
OrdCont(Ref<CellSlice> _code, ControlData&& _cdata) : data(std::move(_cdata)), code(std::move(_code)) {
}
OrdCont(const OrdCont&) = default;
OrdCont(OrdCont&&) = default;
~OrdCont() override = default;
@ -342,6 +368,7 @@ class OrdCont : public Continuation {
return Ref<OrdCont>{true, *this};
}
bool serialize(CellBuilder& cb) const override;
static Ref<OrdCont> deserialize(CellSlice& cs, int mode = 0);
};
struct GasLimits {

View file

@ -688,7 +688,7 @@ bool StackEntry::serialize(vm::CellBuilder& cb, int mode) const {
return cb.store_long_bool(0x02ff, 16);
} else if (!(mode & 1) && val->signed_fits_bits(64)) {
// vm_stk_tinyint#01 value:int64 = VmStackValue;
return cb.store_long_bool(1, 8) && cb.store_int256_bool(std::move(val), 256);
return cb.store_long_bool(1, 8) && cb.store_int256_bool(std::move(val), 64);
} else {
// vm_stk_int#0201_ value:int257 = VmStackValue;
return cb.store_long_bool(0x0200 / 2, 15) && cb.store_int256_bool(std::move(val), 257);
@ -701,7 +701,7 @@ bool StackEntry::serialize(vm::CellBuilder& cb, int mode) const {
// _ cell:^Cell st_bits:(## 10) end_bits:(## 10) { st_bits <= end_bits }
// st_ref:(#<= 4) end_ref:(#<= 4) { st_ref <= end_ref } = VmCellSlice;
const auto& cs = *static_cast<Ref<CellSlice>>(ref);
return cb.store_long_bool(4, 8) // vm_stk_slice#04 _:VmCellSlice = VmStackValue;
return ((mode & 0x1000) || cb.store_long_bool(4, 8)) // vm_stk_slice#04 _:VmCellSlice = VmStackValue;
&& cb.store_ref_bool(cs.get_base_cell()) // _ cell:^Cell
&& cb.store_long_bool(cs.cur_pos(), 10) // st_bits:(## 10)
&& cb.store_long_bool(cs.cur_pos() + cs.size(), 10) // end_bits:(## 10)
@ -738,6 +738,110 @@ bool StackEntry::serialize(vm::CellBuilder& cb, int mode) const {
}
}
bool StackEntry::deserialize(CellSlice& cs, int mode) {
clear();
int t = (mode & 0xf000) ? ((mode >> 12) & 15) : (int)cs.prefetch_ulong(8);
switch (t) {
case 0:
// vm_stk_null#00 = VmStackValue;
return cs.advance(8);
case 1: {
// vm_stk_tinyint#01 value:int64 = VmStackValue;
td::RefInt256 val;
return !(mode & 1) && cs.advance(8) && cs.fetch_int256_to(64, val) && set_int(std::move(val));
}
case 2: {
t = (int)cs.prefetch_ulong(16) & 0x1ff;
if (t == 0xff) {
// vm_stk_nan#02ff = VmStackValue;
return cs.advance(16) && set_int(td::RefInt256{true});
} else {
// vm_stk_int#0201_ value:int257 = VmStackValue;
td::RefInt256 val;
return cs.fetch_ulong(15) == 0x0200 / 2 && cs.fetch_int256_to(257, val) && set_int(std::move(val));
}
}
case 3: {
// vm_stk_cell#03 cell:^Cell = VmStackValue;
return cs.have_refs() && cs.advance(8) && set(t_cell, cs.fetch_ref());
}
case 4: {
// _ cell:^Cell st_bits:(## 10) end_bits:(## 10) { st_bits <= end_bits }
// st_ref:(#<= 4) end_ref:(#<= 4) { st_ref <= end_ref } = VmCellSlice;
// vm_stk_slice#04 _:VmCellSlice = VmStackValue;
unsigned st_bits, end_bits, st_ref, end_ref;
Ref<Cell> cell;
Ref<CellSlice> csr;
return ((mode & 0xf000) || cs.advance(8)) // vm_stk_slice#04
&& cs.fetch_ref_to(cell) // cell:^Cell
&& cs.fetch_uint_to(10, st_bits) // st_bits:(## 10)
&& cs.fetch_uint_to(10, end_bits) // end_bits:(## 10)
&& st_bits <= end_bits // { st_bits <= end_bits }
&& cs.fetch_uint_to(3, st_ref) // st_ref:(#<= 4)
&& cs.fetch_uint_to(3, end_ref) // end_ref:(#<= 4)
&& st_ref <= end_ref && end_ref <= 4 // { st_ref <= end_ref }
&& (csr = load_cell_slice_ref(std::move(cell))).not_null() // load cell slice
&& csr->have(end_bits, end_ref) &&
csr.write().skip_last(csr->size() - end_bits, csr->size_refs() - end_ref) &&
csr.write().skip_first(st_bits, st_ref) && set(t_slice, std::move(csr));
}
case 5: {
// vm_stk_builder#05 cell:^Cell = VmStackValue;
Ref<Cell> cell;
Ref<CellSlice> csr;
Ref<CellBuilder> cb{true};
return cs.advance(8) && cs.fetch_ref_to(cell) && (csr = load_cell_slice_ref(std::move(cell))).not_null() &&
cb.write().append_cellslice_bool(std::move(csr)) && set(t_builder, std::move(cb));
}
case 6: {
// vm_stk_cont#06 cont:VmCont = VmStackValue;
Ref<Continuation> cont;
return !(mode & 2) && cs.advance(8) && Continuation::deserialize_to(cs, cont, mode) &&
set(t_vmcont, std::move(cont));
}
case 7: {
// vm_stk_tuple#07 len:(## 16) data:(VmTuple len) = VmStackValue;
int n;
if (!(cs.advance(8) && cs.fetch_uint_to(16, n))) {
return false;
}
Ref<Tuple> tuple{true, n};
auto& t = tuple.write();
if (n > 1) {
Ref<Cell> head, tail;
n--;
if (!(cs.fetch_ref_to(head) && cs.fetch_ref_to(tail) && t[n].deserialize(std::move(tail), mode))) {
return false;
}
vm::CellSlice cs2;
while (--n > 0) {
if (!(cs2.load(std::move(head)) && cs2.fetch_ref_to(head) && cs2.fetch_ref_to(tail) && cs2.empty_ext() &&
t[n].deserialize(std::move(tail), mode))) {
return false;
}
}
if (!t[0].deserialize(std::move(head), mode)) {
return false;
}
} else if (n == 1) {
return cs.have_refs() && t[0].deserialize(cs.fetch_ref(), mode);
}
return set(t_tuple, std::move(tuple));
}
default:
return false;
}
}
bool StackEntry::deserialize(Ref<Cell> cell, int mode) {
if (cell.is_null()) {
clear();
return false;
}
CellSlice cs = load_cell_slice(std::move(cell));
return deserialize(cs, mode) && cs.empty_ext();
}
bool Stack::serialize(vm::CellBuilder& cb, int mode) const {
// vm_stack#_ depth:(## 24) stack:(VmStackList depth) = VmStack;
unsigned n = depth();
@ -758,4 +862,45 @@ bool Stack::serialize(vm::CellBuilder& cb, int mode) const {
return cb.store_ref_bool(std::move(rest)) && stack[n - 1].serialize(cb, mode);
}
bool Stack::deserialize(vm::CellSlice& cs, int mode) {
clear();
// vm_stack#_ depth:(## 24) stack:(VmStackList depth) = VmStack;
int n;
if (!cs.fetch_uint_to(24, n)) {
return false;
}
if (!n) {
return true;
}
stack.resize(n);
Ref<Cell> rest;
if (!(cs.fetch_ref_to(rest) && stack[n - 1].deserialize(cs, mode))) {
clear();
return false;
}
for (int i = n - 2; i >= 0; --i) {
// vm_stk_cons#_ {n:#} rest:^(VmStackList n) tos:VmStackValue = VmStackList (n + 1);
vm::CellSlice cs2 = load_cell_slice(std::move(rest));
if (!(cs2.fetch_ref_to(rest) && stack[i].deserialize(cs2, mode) && cs2.empty_ext())) {
clear();
return false;
}
}
if (!load_cell_slice(std::move(rest)).empty_ext()) {
clear();
return false;
}
return true;
}
bool Stack::deserialize_to(vm::CellSlice& cs, Ref<Stack>& stack, int mode) {
stack = Ref<Stack>{true};
if (stack.unique_write().deserialize(cs, mode)) {
return true;
} else {
stack.clear();
return false;
}
}
} // namespace vm

View file

@ -135,6 +135,9 @@ class StackEntry {
tp = t_null;
return *this;
}
bool set_int(td::RefInt256 value) {
return set(t_int, std::move(value));
}
bool empty() const {
return tp == t_null;
}
@ -168,6 +171,8 @@ class StackEntry {
}
// mode: +1 = disable short ints, +2 = disable continuations
bool serialize(vm::CellBuilder& cb, int mode = 0) const;
bool deserialize(vm::CellSlice& cs, int mode = 0);
bool deserialize(Ref<Cell> cell, int mode = 0);
private:
static bool is_list(const StackEntry* se);
@ -195,6 +200,11 @@ class StackEntry {
Ref<T> move_as() & {
return tp == tag ? Ref<T>{td::static_cast_ref(), std::move(ref)} : td::Ref<T>{};
}
bool set(Type _tp, RefAny _ref) {
tp = _tp;
ref = std::move(_ref);
return ref.not_null() || tp == t_null;
}
public:
static StackEntry make_list(std::vector<StackEntry>&& elems);
@ -511,6 +521,8 @@ class Stack : public td::CntObject {
// mode: +1 = add eoln, +2 = Lisp-style lists
void dump(std::ostream& os, int mode = 1) const;
bool serialize(vm::CellBuilder& cb, int mode = 0) const;
bool deserialize(vm::CellSlice& cs, int mode = 0);
static bool deserialize_to(vm::CellSlice& cs, Ref<Stack>& stack, int mode = 0);
};
} // namespace vm

View file

@ -165,6 +165,8 @@ int exec_set_global_common(VmState* st, unsigned idx) {
if (idx >= 255) {
throw VmError{Excno::range_chk, "tuple index out of range"};
}
static auto empty_tuple = Ref<Tuple>{true};
st->set_c7(empty_tuple); // optimization; use only if no exception can be thrown until true set_c7()
auto tpay = tuple_extend_set_index(tuple, idx, std::move(x));
if (tpay > 0) {
st->consume_tuple_gas(tpay);
@ -193,7 +195,8 @@ void register_ton_config_ops(OpcodeTable& cp0) {
.insert(OpcodeInstr::mksimple(0xf823, 16, "NOW", std::bind(exec_get_param, _1, 3, "NOW")))
.insert(OpcodeInstr::mksimple(0xf824, 16, "BLOCKLT", std::bind(exec_get_param, _1, 4, "BLOCKLT")))
.insert(OpcodeInstr::mksimple(0xf825, 16, "LTIME", std::bind(exec_get_param, _1, 5, "LTIME")))
.insert(OpcodeInstr::mkfixedrange(0xf826, 0xf828, 16, 4, instr::dump_1c("GETPARAM "), exec_get_var_param))
.insert(OpcodeInstr::mksimple(0xf826, 16, "RANDSEED", std::bind(exec_get_param, _1, 6, "RANDSEED")))
.insert(OpcodeInstr::mksimple(0xf827, 16, "BALANCE", std::bind(exec_get_param, _1, 7, "BALANCE")))
.insert(OpcodeInstr::mksimple(0xf828, 16, "MYADDR", std::bind(exec_get_param, _1, 8, "MYADDR")))
.insert(OpcodeInstr::mksimple(0xf829, 16, "CONFIGROOT", std::bind(exec_get_param, _1, 9, "CONFIGROOT")))
.insert(OpcodeInstr::mkfixedrange(0xf82a, 0xf830, 16, 4, instr::dump_1c("GETPARAM "), exec_get_var_param))
@ -206,6 +209,112 @@ void register_ton_config_ops(OpcodeTable& cp0) {
.insert(OpcodeInstr::mkfixedrange(0xf861, 0xf880, 16, 5, instr::dump_1c_and(31, "SETGLOB "), exec_set_global));
}
static constexpr int randseed_idx = 6;
td::RefInt256 generate_randu256(VmState* st) {
auto tuple = st->get_c7();
auto t1 = tuple_index(*tuple, 0).as_tuple_range(255);
if (t1.is_null()) {
throw VmError{Excno::type_chk, "intermediate value is not a tuple"};
}
auto seedv = tuple_index(*t1, randseed_idx).as_int();
if (seedv.is_null()) {
throw VmError{Excno::type_chk, "random seed is not an integer"};
}
unsigned char seed[32];
if (!seedv->export_bytes(seed, 32, false)) {
throw VmError{Excno::range_chk, "random seed out of range"};
}
unsigned char hash[64];
digest::hash_str<digest::SHA512>(hash, seed, 32);
if (!seedv.write().import_bytes(hash, 32, false)) {
throw VmError{Excno::range_chk, "cannot store new random seed"};
}
td::RefInt256 res{true};
if (!res.write().import_bytes(hash + 32, 32, false)) {
throw VmError{Excno::range_chk, "cannot store new random number"};
}
static auto empty_tuple = Ref<Tuple>{true};
st->set_c7(empty_tuple); // optimization; use only if no exception can be thrown until true set_c7()
tuple.write()[0].clear();
t1.write().at(randseed_idx) = std::move(seedv);
st->consume_tuple_gas(t1);
tuple.write().at(0) = std::move(t1);
st->consume_tuple_gas(tuple);
st->set_c7(std::move(tuple));
return res;
}
int exec_randu256(VmState* st) {
VM_LOG(st) << "execute RANDU256";
st->get_stack().push_int(generate_randu256(st));
return 0;
}
int exec_rand_int(VmState* st) {
VM_LOG(st) << "execute RAND";
auto& stack = st->get_stack();
stack.check_underflow(1);
auto x = stack.pop_int_finite();
auto y = generate_randu256(st);
typename td::BigInt256::DoubleInt tmp{0};
tmp.add_mul(*x, *y);
tmp.rshift(256, -1).normalize();
stack.push_int(td::RefInt256{true, tmp});
return 0;
}
int exec_set_rand(VmState* st, bool mix) {
VM_LOG(st) << "execute " << (mix ? "ADDRAND" : "SETRAND");
auto& stack = st->get_stack();
stack.check_underflow(1);
auto x = stack.pop_int_finite();
if (!x->unsigned_fits_bits(256)) {
throw VmError{Excno::range_chk, "new random seed out of range"};
}
auto tuple = st->get_c7();
auto t1 = tuple_index(*tuple, 0).as_tuple_range(255);
if (t1.is_null()) {
throw VmError{Excno::type_chk, "intermediate value is not a tuple"};
}
if (mix) {
auto seedv = tuple_index(*t1, randseed_idx).as_int();
if (seedv.is_null()) {
throw VmError{Excno::type_chk, "random seed is not an integer"};
}
unsigned char buffer[64], hash[32];
if (!std::move(seedv)->export_bytes(buffer, 32, false)) {
throw VmError{Excno::range_chk, "random seed out of range"};
}
if (!x->export_bytes(buffer + 32, 32, false)) {
throw VmError{Excno::range_chk, "mixed seed value out of range"};
}
digest::hash_str<digest::SHA256>(hash, buffer, 64);
if (!x.write().import_bytes(hash, 32, false)) {
throw VmError{Excno::range_chk, "new random seed value out of range"};
}
}
static auto empty_tuple = Ref<Tuple>{true};
st->set_c7(empty_tuple); // optimization; use only if no exception can be thrown until true set_c7()
tuple.write()[0].clear();
auto tpay = tuple_extend_set_index(t1, randseed_idx, std::move(x));
if (tpay > 0) {
st->consume_tuple_gas(tpay);
}
tuple.unique_write()[0] = std::move(t1);
st->consume_tuple_gas(tuple);
st->set_c7(std::move(tuple));
return 0;
}
void register_prng_ops(OpcodeTable& cp0) {
using namespace std::placeholders;
cp0.insert(OpcodeInstr::mksimple(0xf810, 16, "RANDU256", exec_randu256))
.insert(OpcodeInstr::mksimple(0xf811, 16, "RAND", exec_rand_int))
.insert(OpcodeInstr::mksimple(0xf814, 16, "SETRAND", std::bind(exec_set_rand, _1, false)))
.insert(OpcodeInstr::mksimple(0xf815, 16, "ADDRAND", std::bind(exec_set_rand, _1, true)));
}
int exec_compute_hash(VmState* st, int mode) {
VM_LOG(st) << "execute HASH" << (mode & 1 ? 'S' : 'C') << 'U';
Stack& stack = st->get_stack();
@ -677,7 +786,7 @@ int exec_set_lib_code(VmState* st) {
}
return install_output_action(st, cb.finalize());
}
int exec_change_lib(VmState* st) {
VM_LOG(st) << "execute CHANGELIB";
Stack& stack = st->get_stack();
@ -688,9 +797,9 @@ int exec_change_lib(VmState* st) {
throw VmError{Excno::range_chk, "library hash must be non-negative"};
}
CellBuilder cb;
if (!(cb.store_ref_bool(get_actions(st)) // out_list$_ {n:#} prev:^(OutList n)
&& cb.store_long_bool(0x26fa1dd4, 32) // action_change_library#26fa1dd4
&& cb.store_long_bool(mode * 2, 8) // mode:(## 7) { mode <= 2 }
if (!(cb.store_ref_bool(get_actions(st)) // out_list$_ {n:#} prev:^(OutList n)
&& cb.store_long_bool(0x26fa1dd4, 32) // action_change_library#26fa1dd4
&& cb.store_long_bool(mode * 2, 8) // mode:(## 7) { mode <= 2 }
&& cb.store_int256_bool(hash, 256, false))) { // libref:LibRef = OutAction;
throw VmError{Excno::cell_ov, "cannot serialize library hash into an output action cell"};
}
@ -710,6 +819,7 @@ void register_ton_message_ops(OpcodeTable& cp0) {
void register_ton_ops(OpcodeTable& cp0) {
register_basic_gas_ops(cp0);
register_ton_gas_ops(cp0);
register_prng_ops(cp0);
register_ton_config_ops(cp0);
register_ton_crypto_ops(cp0);
register_ton_currency_address_ops(cp0);