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

@ -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);