|
| 1 | +""" |
| 2 | +EOF V1 Code Validation tests |
| 3 | +""" |
| 4 | + |
| 5 | +from typing import List |
| 6 | + |
| 7 | +from ethereum_test_tools import EOFException |
| 8 | +from ethereum_test_tools.eof.v1 import Container, Section |
| 9 | +from ethereum_test_tools.eof.v1.constants import NON_RETURNING_SECTION |
| 10 | +from ethereum_test_tools.vm.opcode import Opcodes as Op |
| 11 | + |
| 12 | + |
| 13 | +def quick_code(code, inputs=0, outputs=NON_RETURNING_SECTION, height=0): |
| 14 | + """A sorter way to write code with section 0 defaults""" |
| 15 | + return Section.Code( |
| 16 | + code=code, code_inputs=inputs, code_outputs=outputs, max_stack_height=height |
| 17 | + ) |
| 18 | + |
| 19 | + |
| 20 | +def container_name(c: Container): |
| 21 | + """ |
| 22 | + Return the name of the container for use in pytest ids. |
| 23 | + """ |
| 24 | + if hasattr(c, "name"): |
| 25 | + return c.name |
| 26 | + else: |
| 27 | + return c.__class__.__name__ |
| 28 | + |
| 29 | + |
| 30 | +def generate_jumpf_target_rules(): |
| 31 | + """ |
| 32 | + Generate tests for JUMPF where we are testing the validity of the JUNMPF target. |
| 33 | + We are not testing stack so a lot of the logic is to get correct stack values. |
| 34 | + """ |
| 35 | + valid = [] |
| 36 | + invalid = [] |
| 37 | + for current_outputs in [NON_RETURNING_SECTION, 0, 2, 4]: |
| 38 | + current_non_returning = current_outputs == NON_RETURNING_SECTION |
| 39 | + current_height = 0 if current_non_returning else current_outputs |
| 40 | + for target_outputs in [NON_RETURNING_SECTION, 0, 2, 4]: |
| 41 | + target_non_returning = target_outputs == NON_RETURNING_SECTION |
| 42 | + target_height = 0 if target_non_returning else target_outputs |
| 43 | + delta = ( |
| 44 | + 0 |
| 45 | + if target_non_returning or current_non_returning |
| 46 | + else target_outputs - current_height |
| 47 | + ) |
| 48 | + current_extra_push = max(0, current_height - target_height) |
| 49 | + current_section = Section.Code( |
| 50 | + code=Op.PUSH0 * (current_height) |
| 51 | + + Op.CALLDATALOAD(0) |
| 52 | + + Op.RJUMPI[1] |
| 53 | + + (Op.STOP if current_non_returning else Op.RETF) |
| 54 | + + Op.PUSH0 * current_extra_push |
| 55 | + + Op.JUMPF[2], |
| 56 | + code_inputs=0, |
| 57 | + code_outputs=current_outputs, |
| 58 | + max_stack_height=current_height + max(1, current_extra_push), |
| 59 | + ) |
| 60 | + target_section = Section.Code( |
| 61 | + code=((Op.PUSH0 * delta) if delta >= 0 else (Op.POP * -delta)) |
| 62 | + + Op.CALLF[3] |
| 63 | + + (Op.STOP if target_non_returning else Op.RETF), |
| 64 | + code_inputs=current_height, |
| 65 | + code_outputs=target_outputs, |
| 66 | + max_stack_height=max(current_height, current_height + delta), |
| 67 | + ) |
| 68 | + |
| 69 | + container = Container( |
| 70 | + name="target_co-%s_to-%s" |
| 71 | + % ( |
| 72 | + "N" if current_non_returning else current_outputs, |
| 73 | + "N" if target_non_returning else target_outputs, |
| 74 | + ), |
| 75 | + sections=[ |
| 76 | + quick_code(Op.JUMPF[1], height=0 if current_non_returning else current_height) |
| 77 | + if current_non_returning |
| 78 | + else quick_code( |
| 79 | + Op.CALLF[1](0, 0) + Op.STOP, |
| 80 | + height=0 if current_non_returning else 2 + current_outputs, |
| 81 | + ), |
| 82 | + current_section, |
| 83 | + target_section, |
| 84 | + quick_code(Op.SSTORE(0, 1) + Op.RETF, outputs=0, height=2), |
| 85 | + ], |
| 86 | + ) |
| 87 | + |
| 88 | + # now sort validity... |
| 89 | + if target_non_returning: |
| 90 | + valid.append(container) |
| 91 | + elif current_non_returning or current_outputs < target_outputs: |
| 92 | + # both as non-returning handled above |
| 93 | + container.validity_error = EOFException.UNDEFINED_EXCEPTION |
| 94 | + invalid.append(container) |
| 95 | + else: |
| 96 | + # both are returning, and current >= target |
| 97 | + valid.append(container) |
| 98 | + return (valid, invalid) |
| 99 | + |
| 100 | + |
| 101 | +def generate_jumpf_stack_returning_rules(): |
| 102 | + """ |
| 103 | + Generate tests for JUMPF where we are testing the stack rules. Returning section cases |
| 104 | + """ |
| 105 | + valid = [] |
| 106 | + invalid = [] |
| 107 | + for current_outputs in [0, 2, 4]: |
| 108 | + for target_outputs in [x for x in [0, 2, 4] if x <= current_outputs]: |
| 109 | + for target_inputs in [0, 2, 4]: |
| 110 | + for stack_diff in [-1, 0, 1] if target_inputs > 0 else [0, 1]: |
| 111 | + target_delta = target_outputs - target_inputs |
| 112 | + container = Container( |
| 113 | + name="stack-retuning_co-%d_to-%d_ti-%d_diff-%d" |
| 114 | + % (current_outputs, target_outputs, target_inputs, stack_diff), |
| 115 | + sections=[ |
| 116 | + quick_code( |
| 117 | + Op.CALLF[1] + Op.SSTORE(0, 1) + Op.STOP, height=2 + current_outputs |
| 118 | + ), |
| 119 | + quick_code( |
| 120 | + Op.PUSH0 * max(0, target_inputs + stack_diff) + Op.JUMPF[2], |
| 121 | + outputs=current_outputs, |
| 122 | + height=target_inputs, |
| 123 | + ), |
| 124 | + quick_code( |
| 125 | + ( |
| 126 | + Op.POP * -target_delta |
| 127 | + if target_delta < 0 |
| 128 | + else Op.PUSH0 * target_delta |
| 129 | + ) |
| 130 | + + Op.RETF, |
| 131 | + inputs=target_inputs, |
| 132 | + outputs=target_outputs, |
| 133 | + height=max(target_inputs, target_outputs), |
| 134 | + ), |
| 135 | + ], |
| 136 | + ) |
| 137 | + |
| 138 | + if stack_diff == current_outputs - target_outputs: |
| 139 | + valid.append(container) |
| 140 | + else: |
| 141 | + container.validity_error = EOFException.UNDEFINED_EXCEPTION |
| 142 | + invalid.append(container) |
| 143 | + |
| 144 | + return (valid, invalid) |
| 145 | + |
| 146 | + |
| 147 | +def generate_jumpf_stack_non_returning_rules(): |
| 148 | + """ |
| 149 | + Generate tests for JUMPF where we are testing the stack rules. Non-returning section cases. |
| 150 | + """ |
| 151 | + valid = [] |
| 152 | + invalid = [] |
| 153 | + for stack_height in [0, 2, 4]: |
| 154 | + for target_inputs in [0, 2, 4]: |
| 155 | + container = Container( |
| 156 | + name="stack-non-retuning_h-%d_ti-%d" % (stack_height, target_inputs), |
| 157 | + sections=[ |
| 158 | + quick_code(Op.JUMPF[1]), |
| 159 | + quick_code( |
| 160 | + Op.PUSH0 * stack_height + Op.JUMPF[2], |
| 161 | + height=stack_height, |
| 162 | + ), |
| 163 | + quick_code( |
| 164 | + Op.POP * target_inputs + Op.SSTORE(0, 1) + Op.STOP, |
| 165 | + inputs=target_inputs, |
| 166 | + height=max(2, target_inputs), |
| 167 | + ), |
| 168 | + ], |
| 169 | + ) |
| 170 | + |
| 171 | + if stack_height >= target_inputs: |
| 172 | + valid.append(container) |
| 173 | + else: |
| 174 | + container.validity_error = EOFException.UNDEFINED_EXCEPTION |
| 175 | + invalid.append(container) |
| 176 | + |
| 177 | + return (valid, invalid) |
| 178 | + |
| 179 | + |
| 180 | +jump_forward = Container( |
| 181 | + name="jump_forward", |
| 182 | + sections=[quick_code(Op.JUMPF[1]), quick_code(Op.SSTORE(0, 1) + Op.STOP, height=2)], |
| 183 | +) |
| 184 | +jump_backward = Container( |
| 185 | + name="jump_backward", |
| 186 | + sections=[ |
| 187 | + quick_code(Op.CALLF[2] + Op.SSTORE(0, 1) + Op.STOP, height=2), |
| 188 | + quick_code(Op.RETF, outputs=0), |
| 189 | + quick_code(Op.JUMPF[1], outputs=0), |
| 190 | + ], |
| 191 | +) |
| 192 | +jump_to_self = Container( |
| 193 | + name="jump_to_self", |
| 194 | + sections=[ |
| 195 | + quick_code( |
| 196 | + Op.SLOAD(0) + Op.ISZERO + Op.RJUMPI[1] + Op.STOP + Op.SSTORE(0, 1) + Op.JUMPF[0], |
| 197 | + height=2, |
| 198 | + ) |
| 199 | + ], |
| 200 | +) |
| 201 | +jump_too_large = Container( |
| 202 | + name="jump_too_large", |
| 203 | + sections=[quick_code(Op.JUMPF[1025])], |
| 204 | + validity_error=EOFException.UNDEFINED_EXCEPTION, |
| 205 | +) |
| 206 | +jump_way_too_large = Container( |
| 207 | + name="jump_way_too_large", |
| 208 | + sections=[quick_code(Op.JUMPF[0xFFFF])], |
| 209 | + validity_error=EOFException.UNDEFINED_EXCEPTION, |
| 210 | +) |
| 211 | +jump_non_existent_section = Container( |
| 212 | + name="jump_non_existent_section", |
| 213 | + sections=[quick_code(Op.JUMPF[5])], |
| 214 | + validity_error=EOFException.UNDEFINED_EXCEPTION, |
| 215 | +) |
| 216 | +callf_non_returning = Container( |
| 217 | + name="callf_non_returning", |
| 218 | + sections=[quick_code(Op.CALLF[1]), quick_code(Op.STOP, outputs=NON_RETURNING_SECTION)], |
| 219 | + validity_error=EOFException.UNDEFINED_EXCEPTION, |
| 220 | +) |
| 221 | + |
| 222 | + |
| 223 | +jumpf_targets = generate_jumpf_target_rules() |
| 224 | +jumpf_stack_returning = generate_jumpf_stack_returning_rules() |
| 225 | +jumpf_stack_non_returning = generate_jumpf_stack_non_returning_rules() |
| 226 | + |
| 227 | +VALID: List[Container] = [ |
| 228 | + jump_forward, |
| 229 | + jump_backward, |
| 230 | + jump_to_self, |
| 231 | + *jumpf_targets[0], |
| 232 | + *jumpf_stack_returning[0], |
| 233 | + *jumpf_stack_non_returning[0], |
| 234 | +] |
| 235 | + |
| 236 | +INVALID: List[Container] = [ |
| 237 | + jump_too_large, |
| 238 | + jump_too_large, |
| 239 | + jump_non_existent_section, |
| 240 | + callf_non_returning, |
| 241 | + *jumpf_targets[1], |
| 242 | + *jumpf_stack_returning[1], |
| 243 | + *jumpf_stack_non_returning[1], |
| 244 | +] |
0 commit comments