Skip to content

Commit 430f69e

Browse files
committed
core/vm/runtime: more unshipping
1 parent 7834e4a commit 430f69e

File tree

1 file changed

+2
-223
lines changed

1 file changed

+2
-223
lines changed

core/vm/runtime/runtime_test.go

Lines changed: 2 additions & 223 deletions
Original file line numberDiff line numberDiff line change
@@ -330,242 +330,21 @@ func (s *stepCounter) CaptureStart(from common.Address, to common.Address, creat
330330
return nil
331331
}
332332

333-
func (s *stepCounter) CaptureState(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, memory *vm.Memory, stack *vm.Stack, rStack *vm.ReturnStack, rData []byte, contract *vm.Contract, depth int, err error) error {
333+
func (s *stepCounter) CaptureState(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, memory *vm.Memory, stack *vm.Stack, rData []byte, contract *vm.Contract, depth int, err error) error {
334334
s.steps++
335335
// Enable this for more output
336336
//s.inner.CaptureState(env, pc, op, gas, cost, memory, stack, rStack, contract, depth, err)
337337
return nil
338338
}
339339

340-
func (s *stepCounter) CaptureFault(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, memory *vm.Memory, stack *vm.Stack, rStack *vm.ReturnStack, contract *vm.Contract, depth int, err error) error {
340+
func (s *stepCounter) CaptureFault(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, memory *vm.Memory, stack *vm.Stack, contract *vm.Contract, depth int, err error) error {
341341
return nil
342342
}
343343

344344
func (s *stepCounter) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) error {
345345
return nil
346346
}
347347

348-
func TestJumpSub1024Limit(t *testing.T) {
349-
state, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
350-
address := common.HexToAddress("0x0a")
351-
// Code is
352-
// 0 beginsub
353-
// 1 push 0
354-
// 3 jumpsub
355-
//
356-
// The code recursively calls itself. It should error when the returns-stack
357-
// grows above 1023
358-
state.SetCode(address, []byte{
359-
byte(vm.PUSH1), 3,
360-
byte(vm.JUMPSUB),
361-
byte(vm.BEGINSUB),
362-
byte(vm.PUSH1), 3,
363-
byte(vm.JUMPSUB),
364-
})
365-
tracer := stepCounter{inner: vm.NewJSONLogger(nil, os.Stdout)}
366-
// Enable 2315
367-
_, _, err := Call(address, nil, &Config{State: state,
368-
GasLimit: 20000,
369-
ChainConfig: params.AllEthashProtocolChanges,
370-
EVMConfig: vm.Config{
371-
ExtraEips: []int{2315},
372-
Debug: true,
373-
//Tracer: vm.NewJSONLogger(nil, os.Stdout),
374-
Tracer: &tracer,
375-
}})
376-
exp := "return stack limit reached"
377-
if err.Error() != exp {
378-
t.Fatalf("expected %v, got %v", exp, err)
379-
}
380-
if exp, got := 2048, tracer.steps; exp != got {
381-
t.Fatalf("expected %d steps, got %d", exp, got)
382-
}
383-
}
384-
385-
func TestReturnSubShallow(t *testing.T) {
386-
state, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
387-
address := common.HexToAddress("0x0a")
388-
// The code does returnsub without having anything on the returnstack.
389-
// It should not panic, but just fail after one step
390-
state.SetCode(address, []byte{
391-
byte(vm.PUSH1), 5,
392-
byte(vm.JUMPSUB),
393-
byte(vm.RETURNSUB),
394-
byte(vm.PC),
395-
byte(vm.BEGINSUB),
396-
byte(vm.RETURNSUB),
397-
byte(vm.PC),
398-
})
399-
tracer := stepCounter{}
400-
401-
// Enable 2315
402-
_, _, err := Call(address, nil, &Config{State: state,
403-
GasLimit: 10000,
404-
ChainConfig: params.AllEthashProtocolChanges,
405-
EVMConfig: vm.Config{
406-
ExtraEips: []int{2315},
407-
Debug: true,
408-
Tracer: &tracer,
409-
}})
410-
411-
exp := "invalid retsub"
412-
if err.Error() != exp {
413-
t.Fatalf("expected %v, got %v", exp, err)
414-
}
415-
if exp, got := 4, tracer.steps; exp != got {
416-
t.Fatalf("expected %d steps, got %d", exp, got)
417-
}
418-
}
419-
420-
// disabled -- only used for generating markdown
421-
func DisabledTestReturnCases(t *testing.T) {
422-
cfg := &Config{
423-
EVMConfig: vm.Config{
424-
Debug: true,
425-
Tracer: vm.NewMarkdownLogger(nil, os.Stdout),
426-
ExtraEips: []int{2315},
427-
},
428-
}
429-
// This should fail at first opcode
430-
Execute([]byte{
431-
byte(vm.RETURNSUB),
432-
byte(vm.PC),
433-
byte(vm.PC),
434-
}, nil, cfg)
435-
436-
// Should also fail
437-
Execute([]byte{
438-
byte(vm.PUSH1), 5,
439-
byte(vm.JUMPSUB),
440-
byte(vm.RETURNSUB),
441-
byte(vm.PC),
442-
byte(vm.BEGINSUB),
443-
byte(vm.RETURNSUB),
444-
byte(vm.PC),
445-
}, nil, cfg)
446-
447-
// This should complete
448-
Execute([]byte{
449-
byte(vm.PUSH1), 0x4,
450-
byte(vm.JUMPSUB),
451-
byte(vm.STOP),
452-
byte(vm.BEGINSUB),
453-
byte(vm.PUSH1), 0x9,
454-
byte(vm.JUMPSUB),
455-
byte(vm.RETURNSUB),
456-
byte(vm.BEGINSUB),
457-
byte(vm.RETURNSUB),
458-
}, nil, cfg)
459-
}
460-
461-
// DisabledTestEipExampleCases contains various testcases that are used for the
462-
// EIP examples
463-
// This test is disabled, as it's only used for generating markdown
464-
func DisabledTestEipExampleCases(t *testing.T) {
465-
cfg := &Config{
466-
EVMConfig: vm.Config{
467-
Debug: true,
468-
Tracer: vm.NewMarkdownLogger(nil, os.Stdout),
469-
ExtraEips: []int{2315},
470-
},
471-
}
472-
prettyPrint := func(comment string, code []byte) {
473-
instrs := make([]string, 0)
474-
it := asm.NewInstructionIterator(code)
475-
for it.Next() {
476-
if it.Arg() != nil && 0 < len(it.Arg()) {
477-
instrs = append(instrs, fmt.Sprintf("%v 0x%x", it.Op(), it.Arg()))
478-
} else {
479-
instrs = append(instrs, fmt.Sprintf("%v", it.Op()))
480-
}
481-
}
482-
ops := strings.Join(instrs, ", ")
483-
484-
fmt.Printf("%v\nBytecode: `0x%x` (`%v`)\n",
485-
comment,
486-
code, ops)
487-
Execute(code, nil, cfg)
488-
}
489-
490-
{ // First eip testcase
491-
code := []byte{
492-
byte(vm.PUSH1), 4,
493-
byte(vm.JUMPSUB),
494-
byte(vm.STOP),
495-
byte(vm.BEGINSUB),
496-
byte(vm.RETURNSUB),
497-
}
498-
prettyPrint("This should jump into a subroutine, back out and stop.", code)
499-
}
500-
501-
{
502-
code := []byte{
503-
byte(vm.PUSH9), 0x00, 0x00, 0x00, 0x00, 0x0, 0x00, 0x00, 0x00, 4 + 8,
504-
byte(vm.JUMPSUB),
505-
byte(vm.STOP),
506-
byte(vm.BEGINSUB),
507-
byte(vm.PUSH1), 8 + 9,
508-
byte(vm.JUMPSUB),
509-
byte(vm.RETURNSUB),
510-
byte(vm.BEGINSUB),
511-
byte(vm.RETURNSUB),
512-
}
513-
prettyPrint("This should execute fine, going into one two depths of subroutines", code)
514-
}
515-
// TODO(@holiman) move this test into an actual test, which not only prints
516-
// out the trace.
517-
{
518-
code := []byte{
519-
byte(vm.PUSH9), 0x01, 0x00, 0x00, 0x00, 0x0, 0x00, 0x00, 0x00, 4 + 8,
520-
byte(vm.JUMPSUB),
521-
byte(vm.STOP),
522-
byte(vm.BEGINSUB),
523-
byte(vm.PUSH1), 8 + 9,
524-
byte(vm.JUMPSUB),
525-
byte(vm.RETURNSUB),
526-
byte(vm.BEGINSUB),
527-
byte(vm.RETURNSUB),
528-
}
529-
prettyPrint("This should fail, since the given location is outside of the "+
530-
"code-range. The code is the same as previous example, except that the "+
531-
"pushed location is `0x01000000000000000c` instead of `0x0c`.", code)
532-
}
533-
{
534-
// This should fail at first opcode
535-
code := []byte{
536-
byte(vm.RETURNSUB),
537-
byte(vm.PC),
538-
byte(vm.PC),
539-
}
540-
prettyPrint("This should fail at first opcode, due to shallow `return_stack`", code)
541-
542-
}
543-
{
544-
code := []byte{
545-
byte(vm.PUSH1), 5, // Jump past the subroutine
546-
byte(vm.JUMP),
547-
byte(vm.BEGINSUB),
548-
byte(vm.RETURNSUB),
549-
byte(vm.JUMPDEST),
550-
byte(vm.PUSH1), 3, // Now invoke the subroutine
551-
byte(vm.JUMPSUB),
552-
}
553-
prettyPrint("In this example. the JUMPSUB is on the last byte of code. When the "+
554-
"subroutine returns, it should hit the 'virtual stop' _after_ the bytecode, "+
555-
"and not exit with error", code)
556-
}
557-
558-
{
559-
code := []byte{
560-
byte(vm.BEGINSUB),
561-
byte(vm.RETURNSUB),
562-
byte(vm.STOP),
563-
}
564-
prettyPrint("In this example, the code 'walks' into a subroutine, which is not "+
565-
"allowed, and causes an error", code)
566-
}
567-
}
568-
569348
// benchmarkNonModifyingCode benchmarks code, but if the code modifies the
570349
// state, this should not be used, since it does not reset the state between runs.
571350
func benchmarkNonModifyingCode(gas uint64, code []byte, name string, b *testing.B) {

0 commit comments

Comments
 (0)