-
Notifications
You must be signed in to change notification settings - Fork 514
/
Copy pathEvmStack.cs
418 lines (356 loc) · 13.6 KB
/
EvmStack.cs
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System;
using System.Buffers.Binary;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Nethermind.Core;
using Nethermind.Int256;
using Nethermind.Evm.Tracing;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Intrinsics;
using System.Diagnostics;
using System.Runtime.Intrinsics.X86;
using Nethermind.Core.Extensions;
using Nethermind.State;
namespace Nethermind.Evm;
using static VirtualMachine;
using Word = Vector256<byte>;
[StructLayout(LayoutKind.Auto)]
public ref struct EvmStack<TTracing>
where TTracing : struct, IIsTracing
{
public const int MaxStackSize = EvmStack.MaxStackSize;
public const int WordSize = EvmStack.WordSize;
public const int AddressSize = EvmStack.AddressSize;
public EvmStack(scoped in int head, ITxTracer txTracer, scoped in Span<byte> bytes)
{
Head = head;
_tracer = txTracer;
_bytes = bytes;
}
private readonly ITxTracer _tracer;
private readonly Span<byte> _bytes;
public int Head;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref byte PushBytesRef()
{
// Workhorse method
int head = Head;
if ((Head = head + 1) >= MaxStackSize)
{
EvmStack.ThrowEvmStackOverflowException();
}
return ref Unsafe.Add(ref MemoryMarshal.GetReference(_bytes), head * WordSize);
}
/// <summary>
/// An optimized version of the push, accepting directly <see cref="StorageValue"/>
/// and using it's size alignment with the stack.
/// </summary>
public void PushBytes(in StorageValue value)
{
if (typeof(TTracing) == typeof(IsTracing)) _tracer.ReportStackPush(value.BytesWithNoLeadingZeroes);
ref byte bytes = ref PushBytesRef();
Unsafe.As<byte, Word>(ref bytes) = value.UnsafeRef;
}
public void PushBytes(scoped ReadOnlySpan<byte> value)
{
if (typeof(TTracing) == typeof(IsTracing)) _tracer.ReportStackPush(value);
ref byte bytes = ref PushBytesRef();
if (value.Length != WordSize)
{
// Not full entry, clear first
Unsafe.As<byte, Word>(ref bytes) = default;
value.CopyTo(MemoryMarshal.CreateSpan(ref Unsafe.Add(ref bytes, WordSize - value.Length), value.Length));
}
else
{
Unsafe.As<byte, Word>(ref bytes) = Unsafe.As<byte, Word>(ref MemoryMarshal.GetReference(value));
}
}
public void PushBytes(scoped in ZeroPaddedSpan value)
{
if (typeof(TTracing) == typeof(IsTracing)) _tracer.ReportStackPush(value);
ref byte bytes = ref PushBytesRef();
ReadOnlySpan<byte> valueSpan = value.Span;
if (valueSpan.Length != WordSize)
{
// Not full entry, clear first
Unsafe.As<byte, Word>(ref bytes) = default;
valueSpan.CopyTo(MemoryMarshal.CreateSpan(ref bytes, value.Length));
}
else
{
Unsafe.As<byte, Word>(ref bytes) = Unsafe.As<byte, Word>(ref MemoryMarshal.GetReference(valueSpan));
}
}
public void PushLeftPaddedBytes(ReadOnlySpan<byte> value, int paddingLength)
{
if (typeof(TTracing) == typeof(IsTracing)) _tracer.ReportStackPush(value);
ref byte bytes = ref PushBytesRef();
if (value.Length != WordSize)
{
// Not full entry, clear first
Unsafe.As<byte, Word>(ref bytes) = default;
value.CopyTo(MemoryMarshal.CreateSpan(ref Unsafe.Add(ref bytes, WordSize - paddingLength), value.Length));
}
else
{
Unsafe.As<byte, Word>(ref bytes) = Unsafe.As<byte, Word>(ref MemoryMarshal.GetReference(value));
}
}
public void PushByte(byte value)
{
if (typeof(TTracing) == typeof(IsTracing)) _tracer.ReportStackPush(value);
ref byte bytes = ref PushBytesRef();
// Not full entry, clear first
Unsafe.As<byte, Word>(ref bytes) = default;
Unsafe.Add(ref bytes, WordSize - sizeof(byte)) = value;
}
public void PushOne()
{
if (typeof(TTracing) == typeof(IsTracing)) _tracer.ReportStackPush(Bytes.OneByteSpan);
ref byte bytes = ref PushBytesRef();
// Not full entry, clear first
Unsafe.As<byte, Word>(ref bytes) = default;
Unsafe.Add(ref bytes, WordSize - sizeof(byte)) = 1;
}
public void PushZero()
{
if (typeof(TTracing) == typeof(IsTracing)) _tracer.ReportStackPush(Bytes.ZeroByteSpan);
ref byte bytes = ref PushBytesRef();
Unsafe.As<byte, Word>(ref bytes) = default;
}
public void PushUInt32(in int value)
{
ref byte bytes = ref PushBytesRef();
// Not full entry, clear first
Unsafe.As<byte, Word>(ref bytes) = default;
Span<byte> intPlace = MemoryMarshal.CreateSpan(ref Unsafe.Add(ref bytes, WordSize - sizeof(uint)), sizeof(uint));
BinaryPrimitives.WriteInt32BigEndian(intPlace, value);
if (typeof(TTracing) == typeof(IsTracing)) _tracer.ReportStackPush(intPlace);
}
/// <summary>
/// Pushes an Uint256 written in big endian.
/// </summary>
/// <remarks>
/// This method is a counterpart to <see cref="PopUInt256"/> and uses the same, raw data approach to write data back.
/// </remarks>
public void PushUInt256(in UInt256 value)
{
ref byte bytes = ref PushBytesRef();
if (Avx2.IsSupported)
{
Word shuffle = Vector256.Create(
0x18191a1b1c1d1e1ful,
0x1011121314151617ul,
0x08090a0b0c0d0e0ful,
0x0001020304050607ul).AsByte();
if (Avx512Vbmi.VL.IsSupported)
{
Word data = Unsafe.As<UInt256, Word>(ref Unsafe.AsRef(in value));
Unsafe.WriteUnaligned(ref bytes, Avx512Vbmi.VL.PermuteVar32x8(data, shuffle));
}
else if (Avx2.IsSupported)
{
Vector256<ulong> permute = Unsafe.As<UInt256, Vector256<ulong>>(ref Unsafe.AsRef(in value));
Vector256<ulong> convert = Avx2.Permute4x64(permute, 0b_01_00_11_10);
Unsafe.WriteUnaligned(ref bytes, Avx2.Shuffle(Unsafe.As<Vector256<ulong>, Word>(ref convert), shuffle));
}
}
else
{
ulong u3, u2, u1, u0;
if (BitConverter.IsLittleEndian)
{
u3 = BinaryPrimitives.ReverseEndianness(value.u3);
u2 = BinaryPrimitives.ReverseEndianness(value.u2);
u1 = BinaryPrimitives.ReverseEndianness(value.u1);
u0 = BinaryPrimitives.ReverseEndianness(value.u0);
}
else
{
u3 = value.u3;
u2 = value.u2;
u1 = value.u1;
u0 = value.u0;
}
Unsafe.WriteUnaligned(ref bytes, Vector256.Create(u3, u2, u1, u0));
}
if (typeof(TTracing) == typeof(IsTracing)) _tracer.ReportStackPush(MemoryMarshal.CreateReadOnlySpan(ref bytes, WordSize));
}
public void PushSignedInt256(in Int256.Int256 value)
{
// tail call into UInt256
PushUInt256(in Unsafe.As<Int256.Int256, UInt256>(ref Unsafe.AsRef(in value)));
}
public void PopLimbo()
{
if (Head-- == 0)
{
EvmStack.ThrowEvmStackUnderflowException();
}
}
/// <summary>
/// Pops an Uint256 written in big endian.
/// </summary>
/// <remarks>
/// This method does its own calculations to create the <paramref name="result"/>. It knows that 32 bytes were popped with <see cref="PopBytesByRef"/>. It doesn't have to check the size of span or slice it.
/// All it does is <see cref="Unsafe.ReadUnaligned{T}(ref byte)"/> and then reverse endianness if needed. Then it creates <paramref name="result"/>.
/// </remarks>
/// <param name="result">The returned value.</param>
public bool PopUInt256(out UInt256 result)
{
Unsafe.SkipInit(out result);
ref byte bytes = ref PopBytesByRef();
if (Unsafe.IsNullRef(ref bytes)) return false;
if (Avx2.IsSupported)
{
Word data = Unsafe.ReadUnaligned<Word>(ref bytes);
Word shuffle = Vector256.Create(
0x18191a1b1c1d1e1ful,
0x1011121314151617ul,
0x08090a0b0c0d0e0ful,
0x0001020304050607ul).AsByte();
if (Avx512Vbmi.VL.IsSupported)
{
Word convert = Avx512Vbmi.VL.PermuteVar32x8(data, shuffle);
result = Unsafe.As<Word, UInt256>(ref convert);
}
else
{
Word convert = Avx2.Shuffle(data, shuffle);
Vector256<ulong> permute = Avx2.Permute4x64(Unsafe.As<Word, Vector256<ulong>>(ref convert), 0b_01_00_11_10);
result = Unsafe.As<Vector256<ulong>, UInt256>(ref permute);
}
}
else
{
ulong u3, u2, u1, u0;
if (BitConverter.IsLittleEndian)
{
// Combine read and switch endianness to movbe reg, mem
u3 = BinaryPrimitives.ReverseEndianness(Unsafe.ReadUnaligned<ulong>(ref bytes));
u2 = BinaryPrimitives.ReverseEndianness(Unsafe.ReadUnaligned<ulong>(ref Unsafe.Add(ref bytes, sizeof(ulong))));
u1 = BinaryPrimitives.ReverseEndianness(Unsafe.ReadUnaligned<ulong>(ref Unsafe.Add(ref bytes, 2 * sizeof(ulong))));
u0 = BinaryPrimitives.ReverseEndianness(Unsafe.ReadUnaligned<ulong>(ref Unsafe.Add(ref bytes, 3 * sizeof(ulong))));
}
else
{
u3 = Unsafe.ReadUnaligned<ulong>(ref bytes);
u2 = Unsafe.ReadUnaligned<ulong>(ref Unsafe.Add(ref bytes, sizeof(ulong)));
u1 = Unsafe.ReadUnaligned<ulong>(ref Unsafe.Add(ref bytes, 2 * sizeof(ulong)));
u0 = Unsafe.ReadUnaligned<ulong>(ref Unsafe.Add(ref bytes, 3 * sizeof(ulong)));
}
result = new UInt256(u0, u1, u2, u3);
}
return true;
}
public readonly bool PeekUInt256IsZero()
{
int head = Head;
if (head-- == 0)
{
return false;
}
ref byte bytes = ref _bytes[head * WordSize];
return Unsafe.ReadUnaligned<UInt256>(ref bytes).IsZero;
}
public readonly Span<byte> PeekWord256()
{
int head = Head;
if (head-- == 0)
{
EvmStack.ThrowEvmStackUnderflowException();
}
return _bytes.Slice(head * WordSize, WordSize);
}
public Address? PopAddress() => Head-- == 0 ? null : new Address(_bytes.Slice(Head * WordSize + WordSize - AddressSize, AddressSize).ToArray());
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref byte PopBytesByRef()
{
int head = Head;
if (head == 0)
{
return ref Unsafe.NullRef<byte>();
}
head--;
Head = head;
return ref Unsafe.Add(ref MemoryMarshal.GetReference(_bytes), head * WordSize);
}
public Span<byte> PopWord256()
{
ref byte bytes = ref PopBytesByRef();
if (Unsafe.IsNullRef(ref bytes)) EvmStack.ThrowEvmStackUnderflowException();
return MemoryMarshal.CreateSpan(ref bytes, WordSize);
}
public byte PopByte()
{
ref byte bytes = ref PopBytesByRef();
if (Unsafe.IsNullRef(ref bytes)) EvmStack.ThrowEvmStackUnderflowException();
return Unsafe.Add(ref bytes, WordSize - sizeof(byte));
}
public bool Dup(in int depth)
{
if (!EnsureDepth(depth)) return false;
ref byte bytes = ref MemoryMarshal.GetReference(_bytes);
ref byte from = ref Unsafe.Add(ref bytes, (Head - depth) * WordSize);
ref byte to = ref Unsafe.Add(ref bytes, Head * WordSize);
Unsafe.WriteUnaligned(ref to, Unsafe.ReadUnaligned<Word>(ref from));
if (typeof(TTracing) == typeof(IsTracing))
{
Trace(depth);
}
if (++Head >= MaxStackSize)
{
EvmStack.ThrowEvmStackOverflowException();
}
return true;
}
public readonly bool EnsureDepth(int depth)
=> Head >= depth;
public readonly bool Swap(int depth)
{
if (!EnsureDepth(depth)) return false;
ref byte bytes = ref MemoryMarshal.GetReference(_bytes);
ref byte bottom = ref Unsafe.Add(ref bytes, (Head - depth) * WordSize);
ref byte top = ref Unsafe.Add(ref bytes, (Head - 1) * WordSize);
Word buffer = Unsafe.ReadUnaligned<Word>(ref bottom);
Unsafe.WriteUnaligned(ref bottom, Unsafe.ReadUnaligned<Word>(ref top));
Unsafe.WriteUnaligned(ref top, buffer);
if (typeof(TTracing) == typeof(IsTracing))
{
Trace(depth);
}
return true;
}
private readonly void Trace(int depth)
{
for (int i = depth; i > 0; i--)
{
_tracer.ReportStackPush(_bytes.Slice(Head * WordSize - i * WordSize, WordSize));
}
}
}
public static class EvmStack
{
public const int RegisterLength = 1;
public const int MaxStackSize = 1025;
public const int ReturnStackSize = 1023;
public const int WordSize = 32;
public const int AddressSize = 20;
[StackTraceHidden]
[DoesNotReturn]
internal static void ThrowEvmStackUnderflowException()
{
Metrics.EvmExceptions++;
throw new EvmStackUnderflowException();
}
[StackTraceHidden]
[DoesNotReturn]
internal static void ThrowEvmStackOverflowException()
{
Metrics.EvmExceptions++;
throw new EvmStackOverflowException();
}
}