Skip to content

Commit 1d1915f

Browse files
committed
Merge remote-tracking branch 'remotes/origin/fierce_fermion_6.0RC' into testnet
2 parents 3134a27 + 066319f commit 1d1915f

File tree

6 files changed

+64
-5
lines changed

6 files changed

+64
-5
lines changed

bvm/Shaders/pipe/contract.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Pipe
55
{
66
#pragma pack (push, 1) // the following structures will be stored in the node in binary form
77

8-
static const ShaderID s_SID = { 0xf8,0xca,0x04,0x65,0x73,0x78,0x82,0xa0,0x0a,0xdd,0x9b,0x48,0xe8,0xa8,0xae,0x12,0x86,0x0a,0xcb,0xd3,0x4c,0xa0,0x3a,0x38,0x4a,0x4d,0xd9,0x3a,0xbc,0x2b,0x61,0x9e };
8+
static const ShaderID s_SID = { 0x0e,0x62,0x05,0x20,0xda,0x78,0xe9,0x87,0xe0,0x05,0x9b,0x8d,0x30,0xe4,0x75,0x9e,0x36,0x67,0x7d,0xdd,0x3d,0x2d,0xad,0xfb,0xa4,0xff,0x5c,0x0c,0x85,0xdc,0x26,0xff };
99

1010
struct Cfg
1111
{

bvm/Shaders/sidechain/contract.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Sidechain
55
{
6-
static const ShaderID s_SID = { 0xea,0x2f,0xa3,0x54,0x9f,0x7a,0xcd,0x72,0x79,0xe1,0x36,0x81,0x2d,0xc7,0xc2,0x30,0x6a,0x70,0xa0,0xd1,0x47,0xef,0x39,0xbe,0xaf,0x5b,0xdd,0x87,0x2c,0x7c,0xc1,0x40 };
6+
static const ShaderID s_SID = { 0x2e,0x9e,0xf4,0xfd,0x41,0xb6,0x52,0x57,0xc4,0xad,0xf4,0xdf,0x68,0x93,0x79,0xba,0x22,0xb1,0x23,0x50,0x1a,0x88,0xe8,0xbe,0xb1,0x22,0x21,0xae,0x45,0x39,0x68,0xb2 };
77

88
#pragma pack (push, 1)
99

bvm/bvm2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ namespace bvm2 {
160160
ZeroObject(m_Code);
161161
ZeroObject(m_Data);
162162
ZeroObject(m_LinearMem);
163-
ZeroObject(m_Instruction);
163+
m_Instruction.m_p0 = m_Instruction.m_p1 = nullptr;
164164

165165
m_vStack.resize((nStackBytes + sizeof(Wasm::Word) - 1) / sizeof(Wasm::Word), 0);
166166

bvm/wasm_interpreter.cpp

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ namespace Wasm {
101101
static_assert(!std::numeric_limits<T>::is_signed); // the sign flag must be specified separately
102102

103103
T ret = 0;
104+
constexpr unsigned int nBitsMax = sizeof(ret) * 8;
105+
104106
for (unsigned int nShift = 0; ; )
105107
{
106108
uint8_t n = Read1();
@@ -116,12 +118,49 @@ namespace Wasm {
116118
if constexpr (bSigned)
117119
{
118120
if (0x40 & n)
119-
ret |= (~static_cast<T>(0) << nShift);
121+
{
122+
// Attention: Bug workaround.
123+
// According to the standard we must pad the remaining bits of the result with 1's.
124+
// However this should only be done if there are bits left. That is, only if nShift is lesser than the number of result bits.
125+
// Original code didn't take care of this.
126+
127+
if (nShift >= nBitsMax)
128+
{
129+
constexpr uint32_t nBitsJustFed = ((nBitsMax - 1) % 7) + 1; // how many bits were just fed into result
130+
static_assert(nBitsJustFed < 6); // the 0x40 bit was not fed. It's unnecessary
131+
132+
switch (m_Mode)
133+
{
134+
case Mode::AutoWorkAround:
135+
n &= ~0x40; // safe to remove this flag, it's redundant and didn't have to appear at all
136+
Cast::NotConst(m_p0[-1]) = n; // replace it back!
137+
break;
138+
139+
case Mode::Emulate_x86:
140+
// emulate the unfixed behavior. On x86 bitshift is effective modulo size of operand
141+
ret |= (~static_cast<T>(0) << (nShift % nBitsMax));
142+
break;
143+
144+
case Mode::Standard:
145+
// Standard behavior, ignore padding
146+
break;
147+
148+
default:
149+
assert(false);
150+
// no break;
151+
case Mode::Restrict:
152+
Fail("Conflicting flags");
153+
}
154+
}
155+
else
156+
// standard padding
157+
ret |= (~static_cast<T>(0) << nShift);
158+
}
120159
}
121160
break;
122161
}
123162

124-
Test(nShift < sizeof(ret) * 8);
163+
Test(nShift < nBitsMax);
125164
}
126165

127166
return ret;

bvm/wasm_interpreter.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ namespace Wasm {
9696
const uint8_t* m_p0;
9797
const uint8_t* m_p1;
9898

99+
enum struct Mode {
100+
AutoWorkAround, // automatically remove conflicting flags. Default during compilation
101+
Restrict, // fail if conflicting flags are detected. Default for tx verification
102+
Emulate_x86, // emulate unfixed x86/amd64 behavior
103+
Standard, // Comply to wasm standard. Will be activated on the HF4
104+
105+
} m_Mode;
106+
107+
Reader(Mode eMode = Mode::AutoWorkAround) :m_Mode(eMode) {}
108+
99109
void Ensure(uint32_t n);
100110
const uint8_t* Consume(uint32_t n);
101111

@@ -327,6 +337,10 @@ namespace Wasm {
327337
bool m_ExtCall = false;
328338
} m_Dbg;
329339

340+
Processor()
341+
:m_Instruction(Reader::Mode::Emulate_x86)
342+
{
343+
}
330344

331345
Word get_Ip() const;
332346
void Jmp(uint32_t ip);

node/processor.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2329,6 +2329,7 @@ struct NodeProcessor::BlockInterpretCtx
23292329
bool m_Temporary = false; // Interpretation will be followed by 'undo', try to avoid heavy state changes (use mem vars whenever applicable)
23302330
bool m_SkipDefinition = false; // no need to calculate the full definition (i.e. not generating/interpreting a block), MMR updates and etc. can be omitted
23312331
bool m_LimitExceeded = false;
2332+
bool m_TxValidation = false; // tx or block
23322333
uint8_t m_TxStatus = proto::TxStatus::Unspecified;
23332334
std::ostream* m_pTxErrorInfo = nullptr;
23342335

@@ -4445,6 +4446,10 @@ bool NodeProcessor::BlockInterpretCtx::BvmProcessor::Invoke(const bvm2::Contract
44454446
InitStackPlus(m_Stack.AlignUp(static_cast<uint32_t>(krn.m_Args.size())));
44464447
m_Stack.PushAlias(krn.m_Args);
44474448

4449+
m_Instruction.m_Mode = m_Bic.m_TxValidation ?
4450+
Wasm::Reader::Mode::Restrict :
4451+
Wasm::Reader::Mode::Emulate_x86;
4452+
44484453
CallFar(cid, iMethod, m_Stack.get_AlasSp());
44494454

44504455
ECC::Hash::Processor hp;
@@ -5553,6 +5558,7 @@ uint8_t NodeProcessor::ValidateTxContextEx(const Transaction& tx, const HeightRa
55535558
bic.SetAssetHi(*this);
55545559

55555560
bic.m_Temporary = true;
5561+
bic.m_TxValidation = true;
55565562
bic.m_SkipDefinition = true;
55575563
bic.m_pTxErrorInfo = pExtraInfo;
55585564

0 commit comments

Comments
 (0)