|
1 | 1 | """ |
2 | 2 | EOF JUMPF tests covering simple cases. |
3 | 3 | """ |
| 4 | + |
4 | 5 | import pytest |
5 | 6 |
|
6 | 7 | from ethereum_test_tools import Account, EOFException, EOFStateTestFiller |
7 | 8 | from ethereum_test_tools.eof.v1 import Container, Section |
| 9 | +from ethereum_test_tools.eof.v1.constants import NON_RETURNING_SECTION |
8 | 10 | from ethereum_test_tools.vm.opcode import Opcodes as Op |
9 | 11 |
|
10 | 12 | from .. import EOF_FORK_NAME |
@@ -149,3 +151,193 @@ def test_callf_to_non_returning_section( |
149 | 151 | validity_error=EOFException.MISSING_STOP_OPCODE, |
150 | 152 | ), |
151 | 153 | ) |
| 154 | + |
| 155 | + |
| 156 | +def test_jumpf_stack_size_1024( |
| 157 | + eof_state_test: EOFStateTestFiller, |
| 158 | +): |
| 159 | + """Test stack reaching 1024 items in target function of JUMPF""" |
| 160 | + eof_state_test( |
| 161 | + data=Container( |
| 162 | + sections=[ |
| 163 | + Section.Code( |
| 164 | + code=Op.PUSH0 * 1022 + Op.JUMPF[1], |
| 165 | + max_stack_height=1022, |
| 166 | + ), |
| 167 | + Section.Code( |
| 168 | + Op.SSTORE(slot_code_worked, value_code_worked) + Op.STOP, |
| 169 | + code_inputs=0, |
| 170 | + code_outputs=NON_RETURNING_SECTION, |
| 171 | + max_stack_height=2, |
| 172 | + ), |
| 173 | + ], |
| 174 | + ), |
| 175 | + container_post=Account(storage={slot_code_worked: value_code_worked}), |
| 176 | + ) |
| 177 | + |
| 178 | + |
| 179 | +def test_jumpf_with_inputs_stack_size_1024( |
| 180 | + eof_state_test: EOFStateTestFiller, |
| 181 | +): |
| 182 | + """Test stack reaching 1024 items in target function of JUMPF with inputs""" |
| 183 | + eof_state_test( |
| 184 | + data=Container( |
| 185 | + sections=[ |
| 186 | + Section.Code( |
| 187 | + code=Op.PUSH0 * 1022 + Op.JUMPF[1], |
| 188 | + max_stack_height=1022, |
| 189 | + ), |
| 190 | + Section.Code( |
| 191 | + Op.SSTORE(slot_code_worked, value_code_worked) + Op.STOP, |
| 192 | + code_inputs=3, |
| 193 | + code_outputs=NON_RETURNING_SECTION, |
| 194 | + max_stack_height=5, |
| 195 | + ), |
| 196 | + ], |
| 197 | + ), |
| 198 | + container_post=Account(storage={slot_code_worked: value_code_worked}), |
| 199 | + ) |
| 200 | + |
| 201 | + |
| 202 | +def test_jumpf_stack_size_1024_at_push( |
| 203 | + eof_state_test: EOFStateTestFiller, |
| 204 | +): |
| 205 | + """Test stack reaching 1024 items in JUMPF target function at PUSH0 instruction""" |
| 206 | + eof_state_test( |
| 207 | + data=Container( |
| 208 | + sections=[ |
| 209 | + Section.Code( |
| 210 | + code=Op.PUSH0 * 1023 |
| 211 | + + Op.CALLF[1] |
| 212 | + + Op.POP * 1023 |
| 213 | + + Op.SSTORE(slot_code_worked, value_code_worked) |
| 214 | + + Op.RETURN(0, 0), |
| 215 | + max_stack_height=1023, |
| 216 | + ), |
| 217 | + Section.Code( |
| 218 | + # stack has 1023 items |
| 219 | + Op.JUMPF[2], |
| 220 | + code_inputs=0, |
| 221 | + code_outputs=0, |
| 222 | + max_stack_height=0, |
| 223 | + ), |
| 224 | + Section.Code( |
| 225 | + Op.PUSH0 + |
| 226 | + # stack has 1024 items |
| 227 | + Op.POP + Op.RETF, |
| 228 | + code_inputs=0, |
| 229 | + code_outputs=0, |
| 230 | + max_stack_height=1, |
| 231 | + ), |
| 232 | + ], |
| 233 | + ), |
| 234 | + container_post=Account(storage={slot_code_worked: value_code_worked}), |
| 235 | + ) |
| 236 | + |
| 237 | + |
| 238 | +def test_jumpf_stack_overflow( |
| 239 | + eof_state_test: EOFStateTestFiller, |
| 240 | +): |
| 241 | + """Test stack overflowing 1024 items in JUMPF target function""" |
| 242 | + eof_state_test( |
| 243 | + data=Container( |
| 244 | + sections=[ |
| 245 | + Section.Code( |
| 246 | + code=Op.PUSH0 * 1023 |
| 247 | + + Op.CALLF[1] |
| 248 | + + Op.POP * 1023 |
| 249 | + + Op.SSTORE(slot_code_worked, value_code_worked) |
| 250 | + + Op.RETURN(0, 0), |
| 251 | + max_stack_height=1023, |
| 252 | + ), |
| 253 | + Section.Code( |
| 254 | + # Stack has 1023 items |
| 255 | + Op.JUMPF[2], |
| 256 | + code_inputs=0, |
| 257 | + code_outputs=0, |
| 258 | + max_stack_height=0, |
| 259 | + ), |
| 260 | + Section.Code( |
| 261 | + Op.PUSH0 + Op.PUSH0 + |
| 262 | + # Runtime stack overflow |
| 263 | + Op.POP + Op.POP + Op.RETF, |
| 264 | + code_inputs=0, |
| 265 | + code_outputs=0, |
| 266 | + max_stack_height=2, |
| 267 | + ), |
| 268 | + ], |
| 269 | + ), |
| 270 | + container_post=Account(storage={slot_code_worked: 0}), |
| 271 | + ) |
| 272 | + |
| 273 | + |
| 274 | +def test_jumpf_with_inputs_stack_size_1024_at_push( |
| 275 | + eof_state_test: EOFStateTestFiller, |
| 276 | +): |
| 277 | + """Test stack reaching 1024 items in JUMPF target function with inputs at PUSH0 instruction""" |
| 278 | + eof_state_test( |
| 279 | + data=Container( |
| 280 | + sections=[ |
| 281 | + Section.Code( |
| 282 | + code=Op.PUSH0 * 1023 |
| 283 | + + Op.CALLF[1] |
| 284 | + + Op.POP * 1023 |
| 285 | + + Op.SSTORE(slot_code_worked, value_code_worked) |
| 286 | + + Op.RETURN(0, 0), |
| 287 | + max_stack_height=1023, |
| 288 | + ), |
| 289 | + Section.Code( |
| 290 | + # Stack has 1023 items |
| 291 | + Op.JUMPF[2], |
| 292 | + code_inputs=3, |
| 293 | + code_outputs=3, |
| 294 | + max_stack_height=3, |
| 295 | + ), |
| 296 | + Section.Code( |
| 297 | + Op.PUSH0 + |
| 298 | + # Stack has 1024 items |
| 299 | + Op.POP + Op.RETF, |
| 300 | + code_inputs=3, |
| 301 | + code_outputs=3, |
| 302 | + max_stack_height=4, |
| 303 | + ), |
| 304 | + ], |
| 305 | + ), |
| 306 | + container_post=Account(storage={slot_code_worked: value_code_worked}), |
| 307 | + ) |
| 308 | + |
| 309 | + |
| 310 | +def test_jumpf_with_inputs_stack_overflow( |
| 311 | + eof_state_test: EOFStateTestFiller, |
| 312 | +): |
| 313 | + """Test stack overflowing 1024 items in JUMPF target function with inputs""" |
| 314 | + eof_state_test( |
| 315 | + data=Container( |
| 316 | + sections=[ |
| 317 | + Section.Code( |
| 318 | + code=Op.PUSH0 * 1023 |
| 319 | + + Op.CALLF[1] |
| 320 | + + Op.POP * 1023 |
| 321 | + + Op.SSTORE(slot_code_worked, value_code_worked) |
| 322 | + + Op.RETURN(0, 0), |
| 323 | + max_stack_height=1023, |
| 324 | + ), |
| 325 | + Section.Code( |
| 326 | + # Stack has 1023 items |
| 327 | + Op.JUMPF[2], |
| 328 | + code_inputs=3, |
| 329 | + code_outputs=3, |
| 330 | + max_stack_height=3, |
| 331 | + ), |
| 332 | + Section.Code( |
| 333 | + Op.PUSH0 + Op.PUSH0 + |
| 334 | + # Runtime stackoverflow |
| 335 | + Op.POP + Op.POP + Op.RETF, |
| 336 | + code_inputs=3, |
| 337 | + code_outputs=3, |
| 338 | + max_stack_height=5, |
| 339 | + ), |
| 340 | + ], |
| 341 | + ), |
| 342 | + container_post=Account(storage={slot_code_worked: 0}), |
| 343 | + ) |
0 commit comments