This repository has been archived by the owner on Oct 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSRTL.cpp
233 lines (209 loc) · 7.94 KB
/
SRTL.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* Copyright (C) 2019 Zilliqa
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <any>
#include "ScillaRTL/Errors.h"
#include "ScillaRTL/Utils.h"
#include "SRTL.h"
#include "SafeInt.h"
#include "ScillaExecImpl.h"
#include "ScillaTypes.h"
#include "ScillaValue.h"
namespace {
void unquoteString(std::string &Input) {
if (Input.empty()) {
return;
}
if (Input.front() == '"') {
Input.erase(0, 1);
}
if (Input.back() == '"') {
Input.pop_back();
}
}
} // namespace
namespace ScillaRTL {
std::optional<std::string> remoteFieldType(const ScillaExecImpl *SJ,
const std::string &Addr,
const std::string &FName) {
ScillaParams::StateQuery Q = {FName, 0, {}, true};
std::any IgnoreVal;
std::string TypeS;
bool Found;
if (!SJ->SPs.fetchRemoteStateValue(Addr, Q, IgnoreVal, Found, TypeS)) {
CREATE_ERROR("Error querying " + Addr + " for " + FName);
}
return (Found ? std::make_optional(TypeS) : std::nullopt);
}
bool isContrAddr(const ScillaExecImpl *SJ, const std::string &Addr) {
return static_cast<bool>(remoteFieldType(SJ, Addr, "_this_address"));
}
bool isLibOrContrAddr(const ScillaExecImpl *SJ, const std::string &Addr) {
return static_cast<bool>(remoteFieldType(SJ, Addr, "_codehash"));
}
bool isLibAddr(const ScillaExecImpl *SJ, const std::string &Addr) {
return isLibOrContrAddr(SJ, Addr) && !isContrAddr(SJ, Addr);
}
bool isUserAddr(const ScillaExecImpl *SJ, const std::string &Addr) {
ScillaParams::StateQuery SQBalance = {"_balance", 0, {}, false};
ScillaParams::StateQuery SQNonce = {"_nonce", 0, {}, false};
std::any BalanceVal, NonceVal;
std::string BalanceType, NonceType;
bool FoundBalance, FoundNonce;
if (!SJ->SPs.fetchRemoteStateValue(Addr, SQBalance, BalanceVal, FoundBalance,
BalanceType) ||
!SJ->SPs.fetchRemoteStateValue(Addr, SQNonce, NonceVal, FoundNonce,
NonceType)) {
CREATE_ERROR("Error fetching balance / nonce for " + Addr);
}
if (!FoundBalance || !FoundNonce)
return false;
if (BalanceType != "Uint128" || NonceType != "Uint64") {
CREATE_ERROR("Invalid types " + BalanceType + " / " + NonceType +
" for balance / nonce at address " + Addr);
}
// The values are quoted numbers.
auto BalanceS = std::any_cast<std::string>(BalanceVal);
auto NonceS = std::any_cast<std::string>(NonceVal);
unquoteString(BalanceS);
unquoteString(NonceS);
// Parse into numbers
SafeUint128 Balance(BalanceS);
SafeUint64 Nonce(NonceS);
// Balance > 0 || Nonce > 0
return Balance > SafeUint128::Zero || Nonce > SafeUint64::Zero;
}
bool dynamicTypecheck(const ScillaExecImpl *SJ, const ScillaTypes::Typ *TargetT,
const ScillaTypes::Typ *ParsedT, const void *Val,
bool ChargeGas) {
if (!ScillaTypes::Typ::valueCompatible(ParsedT, TargetT)) {
CREATE_ERROR("Value of type " + ScillaTypes::Typ::toString(ParsedT) +
" cannot be assigned to a value of type " +
ScillaTypes::Typ::toString(TargetT));
}
using namespace ScillaTypes;
auto DynTypCheckGasCharge = [&ChargeGas,
SJ](const ScillaTypes::Typ *T) -> void {
if (!ChargeGas)
return;
// Implements `address_typecheck_cost` in Gas.ml.
uint64_t Size = 0;
switch (T->m_t) {
case Typ::Address_typ: {
const AddressTyp *AT = T->m_sub.m_addrt;
ASSERT(AT->m_numFields >= -3);
if (AT->m_numFields >= 0) {
// look up _this_address and every listed field.
Size = 1 + AT->m_numFields;
}
break;
}
case Typ::Prim_typ:
case Typ::ADT_typ:
case Typ::Map_typ:
break;
}
// _balance and _nonce must also be looked up
SJ->consumeGas(Size + 2);
};
std::function<bool(const ScillaTypes::Typ *, const void *)> recurser =
[SJ, &DynTypCheckGasCharge, &recurser](const ScillaTypes::Typ *ExpectedT,
const void *Val) {
switch (ExpectedT->m_t) {
case Typ::Prim_typ:
return true;
case Typ::ADT_typ: {
bool Res = true;
ScillaValues::iterScillaADTConstrArgs(
ExpectedT, Val,
[&recurser, &Res](const ScillaTypes::Typ *T, const void *V) {
if (!Res) {
return;
}
Res = recurser(T, V);
});
return Res;
}
case Typ::Map_typ: {
const ScillaTypes::MapTyp *MT = ExpectedT->m_sub.m_mapt;
auto M = reinterpret_cast<const ScillaParams::MapValueT *>(Val);
ObjManager OM;
return std::all_of(
M->begin(), M->end(), [&OM, MT, recurser](auto Itr) {
auto KeyT = MT->m_keyTyp;
auto ValT = MT->m_valTyp;
Json::Value KeyJ = parseJSONString(Itr.first);
auto *KeyV = ScillaValues::fromJSON(OM, KeyT, KeyJ);
if (!recurser(KeyT, KeyV)) {
return false;
}
switch (ValT->m_t) {
case ScillaTypes::Typ::Map_typ: {
auto &ValJS = std::any_cast<const ScillaParams::MapValueT &>(
Itr.second);
return recurser(ValT, &ValJS);
}
default: {
auto &ValJS = std::any_cast<const std::string &>(Itr.second);
Json::Value ValJ = parseJSONString(ValJS);
auto *ValV = ScillaValues::fromJSON(OM, ValT, ValJ);
return recurser(ValT, ValV);
}
}
});
}
case Typ::Address_typ: {
DynTypCheckGasCharge(ExpectedT);
std::string Addr =
ScillaValues::rawToHex(reinterpret_cast<const uint8_t *>(Val),
ScillaTypes::AddrByStr_Len);
const AddressTyp *AT = ExpectedT->m_sub.m_addrt;
switch (AT->m_numFields) {
case -3:
return isUserAddr(SJ, Addr) || isContrAddr(SJ, Addr) ||
isLibAddr(SJ, Addr);
case -2:
return isLibOrContrAddr(SJ, Addr);
case -1:
return isLibAddr(SJ, Addr);
default:
ASSERT(AT->m_numFields >= 0);
// Check that this is a contract and all fields exist
// and are assignable to the type specified here.
if (!isContrAddr(SJ, Addr))
return false;
return std::all_of(
AT->m_fields, AT->m_fields + AT->m_numFields,
[SJ, &Addr](const ScillaTypes::AddressTyp::Field &F) {
auto RTS = remoteFieldType(SJ, Addr, std::string(F.m_Name));
if (!RTS)
return false;
// Parse RTS into a (possibly incomplete) type.
auto TyDescrs = SJ->getTypeDescrTable();
ObjManager OM;
auto RT = ScillaTypes::Typ::constructTyp(
SJ->TPPC.get(), TyDescrs.first, TyDescrs.second, *RTS,
&OM);
return ScillaTypes::Typ::assignable(F.m_FTyp, RT);
});
}
}
}
CREATE_ERROR("Unreachable executed");
};
return recurser(TargetT, Val);
}
} // namespace ScillaRTL