Skip to content

Commit 3c623fc

Browse files
authored
chore(avm): handle parsing error (#10203)
Resolves #9770
1 parent 0c7c4c9 commit 3c623fc

16 files changed

+389
-151
lines changed

barretenberg/cpp/src/barretenberg/vm/avm/tests/execution.test.cpp

+67-33
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ TEST_F(AvmExecutionTests, basicAddReturn)
152152
"00FF"; // ret size offset 255
153153

154154
auto bytecode = hex_to_bytes(bytecode_hex);
155-
auto instructions = Deserialization::parse_bytecode_statically(bytecode);
155+
auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
156+
ASSERT_TRUE(is_ok(error));
156157

157158
// 2 instructions
158159
ASSERT_THAT(instructions, SizeIs(5));
@@ -214,7 +215,8 @@ TEST_F(AvmExecutionTests, setAndSubOpcodes)
214215
"00FF"; // ret size offset 255
215216

216217
auto bytecode = hex_to_bytes(bytecode_hex);
217-
auto instructions = Deserialization::parse_bytecode_statically(bytecode);
218+
auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
219+
ASSERT_TRUE(is_ok(error));
218220

219221
ASSERT_THAT(instructions, SizeIs(5));
220222

@@ -295,7 +297,8 @@ TEST_F(AvmExecutionTests, powerWithMulOpcodes)
295297
bytecode_hex.append(ret_hex);
296298

297299
auto bytecode = hex_to_bytes(bytecode_hex);
298-
auto instructions = Deserialization::parse_bytecode_statically(bytecode);
300+
auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
301+
ASSERT_TRUE(is_ok(error));
299302

300303
ASSERT_THAT(instructions, SizeIs(16));
301304

@@ -394,7 +397,8 @@ TEST_F(AvmExecutionTests, simpleInternalCall)
394397
;
395398

396399
auto bytecode = hex_to_bytes(bytecode_hex);
397-
auto instructions = Deserialization::parse_bytecode_statically(bytecode);
400+
auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
401+
ASSERT_TRUE(is_ok(error));
398402

399403
EXPECT_THAT(instructions, SizeIs(7));
400404

@@ -475,7 +479,8 @@ TEST_F(AvmExecutionTests, nestedInternalCalls)
475479
bytecode_f2 + bytecode_f1 + bytecode_g;
476480

477481
auto bytecode = hex_to_bytes(bytecode_hex);
478-
auto instructions = Deserialization::parse_bytecode_statically(bytecode);
482+
auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
483+
ASSERT_TRUE(is_ok(error));
479484

480485
ASSERT_THAT(instructions, SizeIs(13));
481486

@@ -555,7 +560,8 @@ TEST_F(AvmExecutionTests, jumpAndCalldatacopy)
555560
;
556561

557562
auto bytecode = hex_to_bytes(bytecode_hex);
558-
auto instructions = Deserialization::parse_bytecode_statically(bytecode);
563+
auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
564+
ASSERT_TRUE(is_ok(error));
559565

560566
ASSERT_THAT(instructions, SizeIs(8));
561567

@@ -657,7 +663,8 @@ TEST_F(AvmExecutionTests, jumpiAndCalldatacopy)
657663
;
658664

659665
auto bytecode = hex_to_bytes(bytecode_hex);
660-
auto instructions = Deserialization::parse_bytecode_statically(bytecode);
666+
auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
667+
ASSERT_TRUE(is_ok(error));
661668

662669
ASSERT_THAT(instructions, SizeIs(9));
663670

@@ -714,7 +721,8 @@ TEST_F(AvmExecutionTests, movOpcode)
714721
"00FF"; // ret size offset 255
715722

716723
auto bytecode = hex_to_bytes(bytecode_hex);
717-
auto instructions = Deserialization::parse_bytecode_statically(bytecode);
724+
auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
725+
ASSERT_TRUE(is_ok(error));
718726

719727
ASSERT_THAT(instructions, SizeIs(4));
720728

@@ -773,7 +781,8 @@ TEST_F(AvmExecutionTests, indMovOpcode)
773781
"00FF"; // ret size offset 255
774782

775783
auto bytecode = hex_to_bytes(bytecode_hex);
776-
auto instructions = Deserialization::parse_bytecode_statically(bytecode);
784+
auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
785+
ASSERT_TRUE(is_ok(error));
777786

778787
ASSERT_THAT(instructions, SizeIs(6));
779788

@@ -815,7 +824,8 @@ TEST_F(AvmExecutionTests, setAndCastOpcodes)
815824
"00FF"; // ret size offset 255
816825

817826
auto bytecode = hex_to_bytes(bytecode_hex);
818-
auto instructions = Deserialization::parse_bytecode_statically(bytecode);
827+
auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
828+
ASSERT_TRUE(is_ok(error));
819829

820830
ASSERT_THAT(instructions, SizeIs(4));
821831

@@ -884,7 +894,8 @@ TEST_F(AvmExecutionTests, toRadixBeOpcodeBytes)
884894
"0200"; // ret size offset 512
885895

886896
auto bytecode = hex_to_bytes(bytecode_hex);
887-
auto instructions = Deserialization::parse_bytecode_statically(bytecode);
897+
auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
898+
ASSERT_TRUE(is_ok(error));
888899

889900
// Assign a vector that we will mutate internally in gen_trace to store the return values;
890901
std::vector<FF> returndata;
@@ -958,7 +969,8 @@ TEST_F(AvmExecutionTests, toRadixBeOpcodeBitsMode)
958969
"0200"; // ret size offset 512
959970

960971
auto bytecode = hex_to_bytes(bytecode_hex);
961-
auto instructions = Deserialization::parse_bytecode_statically(bytecode);
972+
auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
973+
ASSERT_TRUE(is_ok(error));
962974

963975
// Assign a vector that we will mutate internally in gen_trace to store the return values;
964976
std::vector<FF> returndata;
@@ -1031,7 +1043,8 @@ TEST_F(AvmExecutionTests, sha256CompressionOpcode)
10311043
"0200"; // ret size offset 512
10321044

10331045
auto bytecode = hex_to_bytes(bytecode_hex);
1034-
auto instructions = Deserialization::parse_bytecode_statically(bytecode);
1046+
auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
1047+
ASSERT_TRUE(is_ok(error));
10351048

10361049
// Assign a vector that we will mutate internally in gen_trace to store the return values;
10371050
std::vector<FF> calldata = std::vector<FF>();
@@ -1093,7 +1106,8 @@ TEST_F(AvmExecutionTests, poseidon2PermutationOpCode)
10931106
"0200"; // ret size offset 512
10941107

10951108
auto bytecode = hex_to_bytes(bytecode_hex);
1096-
auto instructions = Deserialization::parse_bytecode_statically(bytecode);
1109+
auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
1110+
ASSERT_TRUE(is_ok(error));
10971111

10981112
// Assign a vector that we will mutate internally in gen_trace to store the return values;
10991113
std::vector<FF> returndata = std::vector<FF>();
@@ -1168,7 +1182,8 @@ TEST_F(AvmExecutionTests, keccakf1600OpCode)
11681182
"0200"; // ret size offset 512
11691183

11701184
auto bytecode = hex_to_bytes(bytecode_hex);
1171-
auto instructions = Deserialization::parse_bytecode_statically(bytecode);
1185+
auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
1186+
ASSERT_TRUE(is_ok(error));
11721187

11731188
// Assign a vector that we will mutate internally in gen_trace to store the return values;
11741189
std::vector<FF> calldata = std::vector<FF>();
@@ -1241,7 +1256,8 @@ TEST_F(AvmExecutionTests, embeddedCurveAddOpCode)
12411256
"0200"; // ret size offset 512
12421257

12431258
auto bytecode = hex_to_bytes(bytecode_hex);
1244-
auto instructions = Deserialization::parse_bytecode_statically(bytecode);
1259+
auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
1260+
ASSERT_TRUE(is_ok(error));
12451261

12461262
// Assign a vector that we will mutate internally in gen_trace to store the return values;
12471263
std::vector<FF> returndata;
@@ -1336,7 +1352,8 @@ TEST_F(AvmExecutionTests, msmOpCode)
13361352
"0200"; // ret size offset 512
13371353

13381354
auto bytecode = hex_to_bytes(bytecode_hex);
1339-
auto instructions = Deserialization::parse_bytecode_statically(bytecode);
1355+
auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
1356+
ASSERT_TRUE(is_ok(error));
13401357

13411358
// Assign a vector that we will mutate internally in gen_trace to store the return values;
13421359
std::vector<FF> returndata;
@@ -1407,7 +1424,8 @@ TEST_F(AvmExecutionTests, getEnvOpcode)
14071424
"0200"; // ret size offset 512
14081425

14091426
auto bytecode = hex_to_bytes(bytecode_hex);
1410-
auto instructions = Deserialization::parse_bytecode_statically(bytecode);
1427+
auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
1428+
ASSERT_TRUE(is_ok(error));
14111429

14121430
ASSERT_THAT(instructions, SizeIs(13));
14131431

@@ -1623,7 +1641,8 @@ TEST_F(AvmExecutionTests, getEnvOpcode)
16231641
// "0001"; // dst_offset
16241642
//
16251643
// auto bytecode = hex_to_bytes(bytecode_hex);
1626-
// auto instructions = Deserialization::parse_bytecode_statically(bytecode);
1644+
// auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
1645+
// ASSERT_TRUE(is_ok(error));
16271646
//
16281647
// // Public inputs for the circuit
16291648
// std::vector<FF> calldata;
@@ -1660,7 +1679,8 @@ TEST_F(AvmExecutionTests, l2GasLeft)
16601679
"00FF"; // ret size offset 255
16611680

16621681
auto bytecode = hex_to_bytes(bytecode_hex);
1663-
auto instructions = Deserialization::parse_bytecode_statically(bytecode);
1682+
auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
1683+
ASSERT_TRUE(is_ok(error));
16641684

16651685
ASSERT_THAT(instructions, SizeIs(4));
16661686

@@ -1708,7 +1728,8 @@ TEST_F(AvmExecutionTests, daGasLeft)
17081728
"00FF"; // ret size offset 255
17091729

17101730
auto bytecode = hex_to_bytes(bytecode_hex);
1711-
auto instructions = Deserialization::parse_bytecode_statically(bytecode);
1731+
auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
1732+
ASSERT_TRUE(is_ok(error));
17121733

17131734
ASSERT_THAT(instructions, SizeIs(4));
17141735

@@ -1747,7 +1768,8 @@ TEST_F(AvmExecutionTests, ExecutorThrowsWithTooMuchGasAllocated)
17471768
public_inputs.gas_settings.gas_limits.l2_gas = MAX_L2_GAS_PER_ENQUEUED_CALL;
17481769

17491770
auto bytecode = hex_to_bytes(bytecode_hex);
1750-
auto instructions = Deserialization::parse_bytecode_statically(bytecode);
1771+
auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
1772+
ASSERT_TRUE(is_ok(error));
17511773

17521774
ExecutionHints execution_hints;
17531775
EXPECT_THROW_WITH_MESSAGE(gen_trace(bytecode, calldata, public_inputs, returndata, execution_hints),
@@ -1767,7 +1789,8 @@ TEST_F(AvmExecutionTests, ExecutorThrowsWithTooMuchGasAllocated)
17671789
// std::vector<FF> public_inputs = { 1 };
17681790
//
17691791
// auto bytecode = hex_to_bytes(bytecode_hex);
1770-
// auto instructions = Deserialization::parse_bytecode_statically(bytecode);
1792+
// auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
1793+
// ASSERT_TRUE(is_ok(error));
17711794
//
17721795
// ExecutionHints execution_hints;
17731796
// EXPECT_THROW_WITH_MESSAGE(gen_trace(bytecode, calldata, public_inputs, returndata, execution_hints),
@@ -1814,7 +1837,8 @@ TEST_F(AvmExecutionTests, kernelOutputEmitOpcodes)
18141837
"0000"; // ret size 0
18151838

18161839
auto bytecode = hex_to_bytes(bytecode_hex);
1817-
auto instructions = Deserialization::parse_bytecode_statically(bytecode);
1840+
auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
1841+
ASSERT_TRUE(is_ok(error));
18181842

18191843
ASSERT_THAT(instructions, SizeIs(8));
18201844

@@ -1943,7 +1967,8 @@ TEST_F(AvmExecutionTests, kernelOutputStorageLoadOpcodeSimple)
19431967
"00FF"; // ret size offset 255
19441968

19451969
auto bytecode = hex_to_bytes(bytecode_hex);
1946-
auto instructions = Deserialization::parse_bytecode_statically(bytecode);
1970+
auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
1971+
ASSERT_TRUE(is_ok(error));
19471972

19481973
ASSERT_THAT(instructions, SizeIs(5));
19491974

@@ -2010,7 +2035,8 @@ TEST_F(AvmExecutionTests, kernelOutputStorageStoreOpcodeSimple)
20102035
"0200"; // ret size offset 512
20112036

20122037
auto bytecode = hex_to_bytes(bytecode_hex);
2013-
auto instructions = Deserialization::parse_bytecode_statically(bytecode);
2038+
auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
2039+
ASSERT_TRUE(is_ok(error));
20142040

20152041
std::vector<FF> returndata;
20162042

@@ -2072,7 +2098,8 @@ TEST_F(AvmExecutionTests, kernelOutputStorageOpcodes)
20722098
"00FF"; // ret size offset 255
20732099

20742100
auto bytecode = hex_to_bytes(bytecode_hex);
2075-
auto instructions = Deserialization::parse_bytecode_statically(bytecode);
2101+
auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
2102+
ASSERT_TRUE(is_ok(error));
20762103

20772104
ASSERT_THAT(instructions, SizeIs(6));
20782105

@@ -2159,7 +2186,8 @@ TEST_F(AvmExecutionTests, kernelOutputHashExistsOpcodes)
21592186
"0200"; // ret size offset 512
21602187

21612188
auto bytecode = hex_to_bytes(bytecode_hex);
2162-
auto instructions = Deserialization::parse_bytecode_statically(bytecode);
2189+
auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
2190+
ASSERT_TRUE(is_ok(error));
21632191

21642192
ASSERT_THAT(instructions, SizeIs(7));
21652193

@@ -2313,7 +2341,8 @@ TEST_F(AvmExecutionTests, opCallOpcodes)
23132341
"0200"; // ret size offset 512
23142342

23152343
auto bytecode = hex_to_bytes(bytecode_hex);
2316-
auto instructions = Deserialization::parse_bytecode_statically(bytecode);
2344+
auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
2345+
ASSERT_TRUE(is_ok(error));
23172346

23182347
std::vector<FF> returndata;
23192348

@@ -2392,7 +2421,8 @@ TEST_F(AvmExecutionTests, opGetContractInstanceOpcode)
23922421
"0200"; // ret size offset 512
23932422

23942423
auto bytecode = hex_to_bytes(bytecode_hex);
2395-
auto instructions = Deserialization::parse_bytecode_statically(bytecode);
2424+
auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
2425+
ASSERT_TRUE(is_ok(error));
23962426

23972427
ASSERT_THAT(instructions, SizeIs(6));
23982428

@@ -2427,7 +2457,9 @@ TEST_F(AvmExecutionTests, opGetContractInstanceOpcodeBadEnum)
24272457
+ to_hex(static_cast<uint8_t>(ContractInstanceMember::MAX_MEMBER)); // member enum
24282458

24292459
auto bytecode = hex_to_bytes(bytecode_hex);
2430-
auto instructions = Deserialization::parse_bytecode_statically(bytecode);
2460+
auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
2461+
ASSERT_TRUE(is_ok(error));
2462+
24312463
ASSERT_THAT(instructions, SizeIs(2));
24322464

24332465
std::vector<FF> calldata;
@@ -2457,7 +2489,8 @@ TEST_F(AvmExecutionTests, invalidOpcode)
24572489
"0000"; // ret size 0
24582490

24592491
auto bytecode = hex_to_bytes(bytecode_hex);
2460-
EXPECT_THROW_WITH_MESSAGE(Deserialization::parse_bytecode_statically(bytecode), "Invalid opcode");
2492+
const auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
2493+
ASSERT_EQ(error, AvmError::INVALID_OPCODE);
24612494
}
24622495

24632496
// Negative test detecting an incomplete instruction: instruction tag present but an operand is missing
@@ -2474,7 +2507,8 @@ TEST_F(AvmExecutionTests, truncatedInstructionNoOperand)
24742507
"FF"; // addr b and missing address for c = a-b
24752508

24762509
auto bytecode = hex_to_bytes(bytecode_hex);
2477-
EXPECT_THROW_WITH_MESSAGE(Deserialization::parse_bytecode_statically(bytecode), "Operand is missing");
2510+
const auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
2511+
ASSERT_EQ(error, AvmError::PARSING_ERROR);
24782512
}
24792513

24802514
} // namespace tests_avm

0 commit comments

Comments
 (0)