Skip to content

Commit 8152965

Browse files
committed
Issue #27: Refine the template to accept more complicated test input/outputs.
1 parent af4d0ff commit 8152965

File tree

7 files changed

+62
-67
lines changed

7 files changed

+62
-67
lines changed

test/CodeGen/EVM/runtime_tests/simple_test_1.ll

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
; no input, constant output
12
define i256 @simple_test() {
23
entry:
34
ret i256 1

test/CodeGen/EVM/runtime_tests/simple_test_2.ll

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
;; simple input and output
12
define i256 @abcd(i256 %a, i256 %b) {
23
entry:
34
%0 = add nsw i256 %a, %b

test/CodeGen/EVM/runtime_tests/simple_test_3.ll

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
; no input, no output
12
define void @simple_test() {
23
entry:
34
ret void
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
;; one input, no output
2+
define void @simple_test(i256) {
3+
entry:
4+
ret void
5+
}
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
;; simple input and output
2+
define i256 @abc(i256 %a) {
3+
%1 = add i256 %a, 1
4+
ret i256 %1
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
;; two inputs, one input is not used.
2+
define i256 @abcd(i256 %a, i256 %b) {
3+
entry:
4+
ret i256 %a
5+
}
6+

tools/evm-test/evm_test.py

+42-67
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
# to make it work for label relocations and directives, etc.
1111
import pyevmasm as asm
1212

13-
template_0_1 = """
13+
template_x_1 = """
1414
Start:
1515
PUSH1 Return
16+
${pushes}
1617
PUSH2 Function
1718
JUMP
1819
Return:
@@ -25,71 +26,33 @@
2526
Function:
2627
"""
2728

28-
template_0_0 = """
29+
template_x_0 = """
2930
Start:
3031
PUSH1 Return
32+
${pushes}
3133
PUSH2 Function
3234
JUMP
3335
Return:
3436
JUMPDEST
3537
STOP
3638
Function:
3739
"""
38-
39-
template_1_1 = """
40-
Start:
41-
PUSH1 Return
42-
PUSH4 ${input_value}
43-
PUSH2 Function
44-
JUMP
45-
Return:
46-
JUMPDEST
47-
PUSH1 0x00
48-
MSTORE
49-
PUSH1 0x20
50-
PUSH1 0x00
51-
RETURN
52-
Function:
53-
"""
54-
55-
template_2_1 = """
56-
Start:
57-
PUSH1 Return
58-
PUSH4 ${input_value_1}
59-
PUSH4 ${input_value_2}
60-
PUSH2 Function
61-
JUMP
62-
Return:
63-
JUMPDEST
64-
PUSH1 0x00
65-
MSTORE
66-
PUSH1 0x20
67-
PUSH1 0x00
68-
RETURN
69-
Function:
70-
"""
71-
72-
def generate_header_str(inputs: List[int], output: str) -> str:
73-
assert len(inputs) < 3
74-
t = ""
75-
if len(inputs) is 0:
76-
if output == "":
77-
return template_0_0
78-
t = template_0_1
79-
if len(inputs) is 1:
80-
t = Template(template_1_1).substitute(input_value = inputs[0])
81-
elif len(inputs) is 2:
82-
t = Template(template_2_1).substitute(
83-
input_value_1 = inputs[0], input_value_2 = inputs[1])
84-
return t
85-
86-
def generate_function_binary(filename: str, offset: int) -> str:
87-
assert offset > 0
88-
# being lazy
89-
pass
40+
def generate_header_str(inputs: List[str], output: str) -> str:
41+
push_list = []
42+
for input in inputs:
43+
push = Template("PUSH4 ${input}").substitute(input=input)
44+
push_list.append(push)
45+
46+
t = None
47+
if output == "":
48+
# case X 0
49+
t = Template(template_x_0)
50+
else:
51+
# case X 1
52+
t = Template(template_x_1)
53+
return t.substitute(pushes='\n'.join(push_list))
9054

9155
def generate_contract(inputs: List[int], output: str, func: str) -> str:
92-
assert len(inputs) < 3
9356
complete_str = generate_header_str(inputs, output) + func
9457
assembly = asm.assemble_hex(complete_str)
9558
return assembly
@@ -98,11 +61,12 @@ def execute_in_evm(code: str, expected: str) -> str:
9861
# we could use py-evm to do it so everything will be in python.
9962
emv_path = ""
10063
try:
101-
emv_path = os.environ['EVM_PATH']
102-
except KeyError(key):
103-
print("\"" + key + "\" not defined, using pwd instead")
64+
emv_path = os.environ['EVM_PATH'] + "/evm"
65+
except KeyError:
66+
print("EVM_PATH not defined, using pwd instead")
67+
evm_pth = "evm"
10468

105-
command = [emv_path + "/evm", "--code", code, "run"]
69+
command = ["evm", "--code", code, "run"]
10670
result = subprocess.run(command, stdout=subprocess.PIPE)
10771
result.check_returncode()
10872
return result.stdout
@@ -183,6 +147,8 @@ def run_assembly(name: str, inputs: List[str], output: str, filename: str) -> No
183147
result = execute_in_evm(code=contract, expected=output).decode("utf-8")
184148
success = check_result(name, result, output)
185149
if not success:
150+
print("Generated Assembly: ")
151+
print(generate_header_str(inputs, output) + cleaned_content)
186152
print("contract: ")
187153
print(contract)
188154
return success
@@ -203,28 +169,37 @@ def run_string_input(name: str, inputs: List[str], output: str, function: str) -
203169
# these are just demos
204170
"str_test_1_1": {"input": ["0x12345678"],
205171
"output": "0x0000000000000000000000000000000000000000000000000000000012345678",
206-
"func": "JUMPDEST\nJUMP"},
172+
"func": "JUMPDEST\nSWAP1\nJUMP"},
207173
"str_test_2_1": {"input": ["0x12345678", "0x87654321"],
208174
"output": "0x0000000000000000000000000000000000000000000000000000000099999999",
209175
"func": "JUMPDEST\nADD\nSWAP1\nJUMP"},
210176
}
211177

212178
runtime_file_prefix = "../../test/CodeGen/EVM/runtime_tests/"
213179
file_input_fixtures = {
214-
"file_test_1" : {
180+
"simple_test_1.ll" : {
215181
"input": [],
216182
"output": ["0x000000000000000000000000000000000000000000000000000000001"],
217-
"file": "simple_test_1.ll"
218183
},
219-
"file_test_2" : {
184+
"simple_test_2.ll" : {
220185
"input": ["0x12345678", "0x87654321"],
221186
"output": "0x000000000000000000000000000000000000000000000000000000001",
222-
"file": "simple_test_2.ll"
223187
},
224-
"file_test_3" : {
188+
"simple_test_3.ll" : {
225189
"input": [],
226190
"output": "",
227-
"file": "simple_test_3.ll"
191+
},
192+
"simple_test_4.ll" : {
193+
"input": ["0x12345678"],
194+
"output": "",
195+
},
196+
"simple_test_5.ll" : {
197+
"input": ["0x12345678"],
198+
"output": "0x000000000000000000000000000000000000000000000000012345679",
199+
},
200+
"simple_test_6.ll" : {
201+
"input": ["0x12345678", "0x87654321"],
202+
"output": "0x000000000000000000000000000000000000000000000000012345678",
228203
},
229204
}
230205

@@ -238,7 +213,7 @@ def execute_tests() -> None:
238213
for key,val in file_input_fixtures.items():
239214
inputs = val["input"]
240215
output = val["output"]
241-
filename = runtime_file_prefix + val["file"]
216+
filename = runtime_file_prefix + key
242217
run_assembly(name=key, inputs=inputs, output=output, filename=filename)
243218

244219
seed(2019)

0 commit comments

Comments
 (0)