Tcl Source Code

View Ticket
Login
Ticket UUID: 3007374
Title: Segfault in bytecode compiled try with chained handler
Type: Bug Created on: 2010-05-26 14:00:05
Submitter: cyan Assigned to: dkf
Subsystem: 47. Bytecode Compiler Severity:
Priority: 9 Immediate Last modified: 2010-05-28 16:13:05
Status: Closed Closed by: dkf
Resolution: Fixed Closed on: 2010-05-28 09:13:05
Version: obsolete: 8.6b1.1
Description:
In current CVS (2010-05-26 15:30) on Ubuntu 9.10 amd64, bytecode compiled try commands that use chained handlers (with "-") segfault when the handler given as "-" is hit _after_ the handler script executes, but before the finally clause.

The following code reproduces the problem for me:

try {
return [list foo1 bar1]
} on return {res} - on ok {res} {
puts "1 Result ok: ($res)"
} finally {
puts "1 In finally"
}

apply {
{} {
try {
return [list foo2 bar2]
} on return {res} - on ok {res} {
puts "2 Result ok: ($res)"
} finally {
puts "2 In finally"
}
}
}


Expected output:

1 Result ok: (foo1 bar1)
1 In finally
2 Result ok: (foo2 bar2)
2 In finally

Got output:

1 Result ok: (foo1 bar1)
1 In finally
2 Result ok: (foo2 bar2)
zsh: segmentation fault (core dumped)  ./ok_result.tcl
User Comments:
dkf added on 2010-05-26 22:23:51:
Bytecode for the offending script (as sole content of 'proc foo' for ease of my instrumentation):
ByteCode 0x0x7ee10, refCt 1, epoch 4, interp 0x0x2dc10 (epoch 4)
  Source "\n\t\ttry {\n\t\t\treturn [list foo2 bar2]\n\t\t} on return {res}"
  Cmds 5, src 144, inst 160, litObjs 9, aux 0, stkDepth 5, code/src 0.00
  Proc 0x0x905d0, refCt 1, args 0, compiled locals 3
      slot 0, scalar, "res"
      slot 1, scalar, temp
      slot 2, scalar, temp
  Exception ranges 3, depth 1:
      0: level 0, catch, pc 5-33, catch 43
      1: level 0, catch, pc 67-71, catch 77
      2: level 0, catch, pc 105-128, catch 137
  Commands 5:
      1: pc 0-158, src 3-141        2: pc 5-33, src 12-34
      3: pc 14-22, src 20-33        4: pc 117-128, src 75-100
      5: pc 147-152, src 119-137
  Command 1: "try {\n\t\t\treturn [list foo2 bar2]\n\t\t} on return {res} - "
    (0) beginCatch4 0 
  Command 2: "return [list foo2 bar2]"
    (5) startCommand +29 2 # next cmd at pc 34, 2 cmds start here
  Command 3: "list foo2 bar2"
    (14) push1 0 # "foo2"
    (16) push1 1 # "bar2"
    (18) list 2 
    (23) push1 2 # ""
    (25) returnImm +0 1 
    (34) push1 3 # "0"
    (36) reverse 2 
    (41) jump1 +4 # pc 45
    (43) pushReturnCode 
    (44) pushResult 
    (45) pushReturnOpts 
    (46) endCatch 
    (47) storeScalar1 %v2 # temp var 2
    (49) pop 
    (50) storeScalar1 %v1 # temp var 1
    (52) pop 
    (53) dup 
    (54) push1 4 # "2"
    (56) eq 
    (57) jumpFalse4 +34 # pc 91
    (62) beginCatch4 1 
    (67) loadScalar1 %v1 # temp var 1
    (69) storeScalar1 %v0 # var "res"
    (71) pop 
    (72) jump4 +40 # pc 112
    (77) pushReturnOpts 
    (78) pushResult 
    (79) endCatch 
    (80) storeScalar1 %v1 # temp var 1
    (82) pop 
    (83) storeScalar1 %v2 # temp var 2
    (85) pop 
    (86) jump4 +60 # pc 146
    (91) dup 
    (92) push1 3 # "0"
    (94) eq 
    (95) jumpFalse4 +51 # pc 146
    (100) beginCatch4 2 
    (105) loadScalar1 %v1 # temp var 1
    (107) storeScalar1 %v0 # var "res"
    (109) pop 
    (110) jump1 +7 # pc 117
    (112) beginCatch4 2 
  Command 4: "puts \"2 Result ok: ($res)\""
    (117) push1 5 # "puts"
    (119) push1 6 # "2 Result ok: ("
    (121) loadScalar1 %v0 # var "res"
    (123) push1 7 # ")"
    (125) concat1 3 
    (127) invokeStk1 2 
    (129) pushReturnOpts 
    (130) reverse 2 
    (135) jump1 +4 # pc 139
    (137) pushReturnOpts 
    (138) pushResult 
    (139) endCatch 
    (140) storeScalar1 %v1 # temp var 1
    (142) pop 
    (143) storeScalar1 %v2 # temp var 2
    (145) pop 
    (146) pop 
  Command 5: "puts \"2 In finally\""
    (147) push1 5 # "puts"
    (149) push1 8 # "2 In finally"
    (151) invokeStk1 2 
    (153) pop 
    (154) loadScalar1 %v2 # temp var 2
    (156) loadScalar1 %v1 # temp var 1
    (158) returnStk 
    (159) done

dkf added on 2010-05-28 14:45:00:
Crash is at addr 146, an INST_POP. That means that the stack balance is wrong. Which is tricky to hunt. :-(

dkf added on 2010-05-28 15:32:18:
Executing ByteCode 0x0x161d90, refCt 2, epoch 4, interp 0x0x807620 (epoch 4)
  Source: "\n\t\ttry {\n\t\t\treturn [list foo2 bar2]\n\t\t} on return {res} - on"
  Cmds 5, src 144, inst 160, litObjs 9, aux 0, stkDepth 5, code/src 2.92
  Code 420 = header 104+inst 160+litObj 36+exc 84+aux 0+cmdMap 28
  Proc 0x0x1667d0, refCt 2, args 0, compiled locals 3
  Starting stack top=0
 1:  0 (0) beginCatch4 0 
 1:  0 (0) beginCatch4 0 => catchTop=0, stackTop=0
 1:  0 (5) startCommand +29 2 # next cmd at pc 34, 2 cmds start here
 1:  0 (14) push1 0 # "foo2"
 1:  1 (14) push1 0 => "foo2"
 1:  1 (16) push1 1 # "bar2"
 1:  2 (16) push1 1 => "bar2"
 1:  2 (18) list 2 
 1:  2 (18) list 2 => "foo2 bar2"
 1:  1 (23) push1 2 # ""
 1:  2 (23) push1 2 => ""
 1:  2 (25) returnImm +0 1 
 1:  2 (25) returnImm 0 1 =>  1:  2 (25) returnImm => TCL_RETURN, result= "foo2 bar2"
  ... found catch at 5, catchTop=0, unwound to 0, new pc 43
 1:  0 (43) pushReturnCode 
 1:  0 (43) pushReturnCode => 2
 1:  1 (44) pushResult 
 1:  1 (44) pushResult => "foo2 bar2"
 1:  2 (45) pushReturnOpts 
 1:  2 (45) pushReturnOpts => "-code 0 -level 1"
 1:  3 (46) endCatch 
 1:  3 (46) endCatch => catchTop=-1
 1:  3 (47) storeScalar1 %v2 # temp var 2
 1:  3 (47) storeScalar1 2 <- "-code 0 -level 1" => -code 0 -level 1
 1:  3 (49) pop 
 1:  3 (49) pop => discarding "-code 0 -level 1"
 1:  2 (50) storeScalar1 %v1 # temp var 1
 1:  2 (50) storeScalar1 1 <- "foo2 bar2" => foo2 bar2
 1:  2 (52) pop 
 1:  2 (52) pop => discarding "foo2 bar2"
 1:  1 (53) dup 
 1:  1 (53) dup => "2"
 1:  2 (54) push1 4 # "2"
 1:  3 (54) push1 4 => "2"
 1:  3 (56) eq 
 1:  2 (57) jumpFalse4 +34 # pc 91
 1:  2 (57) jumpFalse4 34 => 1 true
 1:  1 (62) beginCatch4 1 
 1:  1 (62) beginCatch4 1 => catchTop=0, stackTop=1
 1:  1 (67) loadScalar1 %v1 # temp var 1
 1:  1 (67) loadScalar1 1 => foo2 bar2
 1:  2 (69) storeScalar1 %v0 # var "res"
 1:  2 (69) storeScalar1 0 <- "foo2 bar2" => foo2 bar2
 1:  2 (71) pop 
 1:  2 (71) pop => discarding "foo2 bar2"
 1:  1 (72) jump4 +40 # pc 112
 1:  1 (72) jump4 40 => new pc 112
 1:  1 (112) beginCatch4 2 
 1:  1 (112) beginCatch4 2 => catchTop=1, stackTop=1
 1:  1 (117) push1 5 # "puts"
 1:  2 (117) push1 5 => "puts"
 1:  2 (119) push1 6 # "2 Result ok: ("
 1:  3 (119) push1 6 => "2 Result ok: ("
 1:  3 (121) loadScalar1 %v0 # var "res"
 1:  3 (121) loadScalar1 0 => foo2 bar2
 1:  4 (123) push1 7 # ")"
 1:  5 (123) push1 7 => ")"
 1:  5 (125) concat1 3 
 1:  5 (125) concat1 3 => "2 Result ok: (foo2 bar2)"
 1:  3 (127) invokeStk1 2 
 1:  3 (127) invokeStk1 2 => call "puts" "2 Result ok: (f" 
2 Result ok: (foo2 bar2)
 1:  3 (129) pushReturnOpts 2 => ... after "puts": TCL_OK, result=""
 1:  2 (129) pushReturnOpts 
 1:  2 (129) pushReturnOpts => "-code 0 -level 0"
 1:  3 (130) reverse 2 
 1:  3 (135) jump1 +4 # pc 139
 1:  3 (135) jump1 4 => new pc 139
 1:  3 (139) endCatch 
 1:  3 (139) endCatch => catchTop=0
 1:  3 (140) storeScalar1 %v1 # temp var 1
 1:  3 (140) storeScalar1 1 <- "" => 
 1:  3 (142) pop 
 1:  3 (142) pop => discarding ""
 1:  2 (143) storeScalar1 %v2 # temp var 2
 1:  2 (143) storeScalar1 2 <- "-code 0 -level 0" => -code 0 -level 0
 1:  2 (145) pop 
 1:  2 (145) pop => discarding "-code 0 -level 0"
 1:  1 (146) pop 

Program received signal EXC_BAD_ACCESS, Could not access memory.

dkf added on 2010-05-28 16:01:28:
Problem is a failure to manage the exception stack correctly. Bleah.

dkf added on 2010-05-28 16:13:04:
That was it; issuing one extra instruction in the right place made it all work.

dkf added on 2010-05-28 16:13:05:

allow_comments - 1