Skip to content

Commit 978c0d4

Browse files
committed
Fix consensus diff function to incldue cus
1 parent 615780a commit 978c0d4

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

src/test_suite/fuzz_context.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
context_human_encode_fn=instr_codec.encode_input,
2525
context_human_decode_fn=instr_codec.decode_input,
2626
effects_human_encode_fn=instr_codec.encode_output,
27-
ignore_fields_for_consensus=["custom_err", "cu_avail"],
2827
consensus_diff_effect_fn=instr_diff.consensus_instr_diff_effects,
2928
)
3029

@@ -33,18 +32,23 @@
3332
fixture_desc=vm_pb.SyscallFixture.DESCRIPTOR,
3433
context_human_encode_fn=syscall_codec.encode_input,
3534
effects_human_encode_fn=syscall_codec.encode_output,
35+
context_human_decode_fn=syscall_codec.decode_input,
3636
)
3737

3838
CpiHarness = HarnessCtx(
3939
fuzz_fn_name="sol_compat_vm_cpi_syscall_v1",
4040
fixture_desc=vm_pb.SyscallFixture.DESCRIPTOR,
41+
context_human_encode_fn=syscall_codec.encode_input,
4142
effects_human_encode_fn=syscall_codec.encode_output,
43+
context_human_decode_fn=syscall_codec.decode_input,
4244
)
4345

4446
VmInterpHarness = HarnessCtx(
4547
fuzz_fn_name="sol_compat_vm_interp_v1",
4648
fixture_desc=vm_pb.SyscallFixture.DESCRIPTOR,
49+
context_human_encode_fn=syscall_codec.encode_input,
4750
effects_human_encode_fn=syscall_codec.encode_output,
51+
context_human_decode_fn=syscall_codec.decode_input,
4852
)
4953

5054
VmValidateHarness = HarnessCtx(

src/test_suite/fuzz_interface.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Callable, Type, TypeVar
22
from google.protobuf import message, descriptor, message_factory
33
from dataclasses import dataclass, InitVar, field
4+
import re
45

56
msg_factory = message_factory.MessageFactory()
67

@@ -13,7 +14,6 @@
1314
1415
The following defines the interface:
1516
- fuzz_fn_name: The name of the harness function to call in the fuzz target
16-
- ignore_fields_for_consensus: A list of fields to ignore when comparing effects during --consensus mode
1717
- fixture_desc: The protobuf descriptor for the fixture message.
1818
- A fixture message is a message that contains an input and output message.
1919
- input: The fuzz target Context
@@ -27,17 +27,32 @@
2727
"""
2828

2929

30-
def encode_hex_compact(buf):
30+
def decode_hex_compact(encoded):
31+
res = bytearray()
32+
parts = re.split(r"\.\.\.(\d+) zeros\.\.\.", encoded.decode("ascii"))
33+
34+
for i, part in enumerate(parts):
35+
if i % 2 == 0:
36+
# Regular hex part
37+
res.extend(bytes.fromhex(part))
38+
else:
39+
# Skipped zeros part
40+
res.extend(b"\x00" * int(part))
41+
42+
return bytes(res)
43+
44+
45+
def encode_hex_compact(buf, gap=16):
3146
res = ""
3247
skipped = 0
33-
for i in range(0, len(buf), 16):
34-
row = buf[i : i + 16]
48+
for i in range(0, len(buf), gap):
49+
row = buf[i : i + gap]
3550
if row == bytes([0] * len(row)):
3651
skipped += len(row)
3752
else:
3853
if skipped > 0:
3954
res += f"...{skipped} zeros..."
40-
res += "".join([f"{b:0>2x}" for b in buf[i : i + 16]])
55+
res += "".join([f"{b:0>2x}" for b in buf[i : i + gap]])
4156
skipped = 0
4257
if skipped > 0:
4358
res += f"...{skipped} zeros..."
@@ -69,7 +84,6 @@ class HarnessCtx:
6984
fuzz_fn_name: str
7085
fixture_desc: InitVar[descriptor.Descriptor]
7186
result_field_name: str | None = "result"
72-
ignore_fields_for_consensus: list[str] = field(default_factory=list)
7387
diff_effect_fn: Callable[[EffectsType, EffectsType], bool] = generic_effects_diff
7488
consensus_diff_effect_fn: Callable[[EffectsType, EffectsType], bool] = (
7589
generic_effects_diff

src/test_suite/instr/diff_utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ def consensus_instr_diff_effects(a: invoke_pb.InstrEffects, b: invoke_pb.InstrEf
77
b_san = invoke_pb.InstrEffects()
88
b_san.CopyFrom(b)
99

10-
# Normalize error codes
10+
# Normalize error codes and cus
1111
a_san.result = 0
1212
a_san.custom_err = 0
13+
a_san.cu_avail = 0
1314

1415
b_san.result = 0
1516
b_san.custom_err = 0
17+
b_san.cu_avail = 0
1618

1719
return a_san == b_san

src/test_suite/syscall/codec_utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import test_suite.vm_pb2 as vm_pb
44
from test_suite.fuzz_interface import encode_hex_compact
55
from test_suite.instr.codec_utils import encode_input as instr_encode_input
6+
from test_suite.instr.codec_utils import decode_input as instr_decode_input
67

78

89
def encode_input(input: vm_pb.SyscallContext):
@@ -23,3 +24,10 @@ def encode_output(effects: vm_pb.SyscallEffects):
2324
effects.heap = encode_hex_compact(effects.heap)
2425
effects.stack = encode_hex_compact(effects.stack)
2526
effects.rodata = encode_hex_compact(effects.rodata)
27+
28+
29+
def decode_input(input: vm_pb.SyscallContext):
30+
instr_ctx = invoke_pb.InstrContext()
31+
instr_ctx.CopyFrom(input.instr_ctx)
32+
instr_decode_input(instr_ctx)
33+
input.instr_ctx.CopyFrom(instr_ctx)

0 commit comments

Comments
 (0)