Tcl Source Code

View Ticket
Login
Ticket UUID: 7842f33a5cc5eed080a57f8af9ac1e7e3cee444a
Title: TCL crash (core)
Type: Bug Created on: 2022-08-27 21:39:42
Submitter: anonymous Assigned to: dkf
Subsystem: 35. TclOO Package Severity: Critical
Priority: 9 Immediate Last modified: 2024-05-23 10:33:40
Status: Closed Closed by: dkf
Resolution: Fixed Closed on: 2024-05-23 10:33:40
Version: 8.6
Description:
The following script will CRASH tcl

TWO instances are linked together, the second instance uses in the destructor the first instance. but the second instance is created in the namespace of the first instance. if the first instance is deleted the destructor of the second instance is called and this is calling the first instance again etc…


oo::class create RpcClient {
}

oo::define RpcClient method write {name } {
  puts "RpcClient → $name"
}

oo::class create LcConfigRpcClient {
  superclass RpcClient
}

oo::define LcConfigRpcClient method LcConfigRpc_create_bug {name } {
  LcConfigRpc create $name [self] 111
}
oo::define LcConfigRpcClient export LcConfigRpc_create_bug


oo::class create MkObjectRpc {
  variable hdl
}

oo::define MkObjectRpc constructor {rpcHdl mqHdl} {
  set hdl $mqHdl
  oo::objdefine [self] forward rpc $rpcHdl
}

oo::define MkObjectRpc destructor {
  my rpc write otto-$hdl
}

oo::class create LcConfigRpc {
  superclass MkObjectRpc
  variable hdl
}

set FH [LcConfigRpcClient new]

$FH LcConfigRpc_create_bug cfg

$FH destroy
User Comments:
chrstphrchvz added on 2023-07-08 12:23:33:

Changing subsystem to TclOO; the “Objects” subsystem is meant for Tcl values in general.


chrstphrchvz added on 2023-10-22 16:41:58:

The reporter’s example still crashes for core-8-6-branch. UBSan (-fsanitize=null) and ASan (-fsanitize=address) errors:

tcl/generic/tclOOCall.c:1024:25: runtime error: member access within null pointer of type 'struct Class'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior tcl/generic/tclOOCall.c:1024:25 in 
AddressSanitizer:DEADLYSIGNAL
=================================================================
==65360==ERROR: AddressSanitizer: SEGV on unknown address 0x0000000000f0 (pc 0x0001109f36a5 bp 0x7ff7b1f52830 sp 0x7ff7b1f51c80 T0)
==65360==The signal is caused by a READ memory access.
==65360==Hint: address points to the zero page.
    #0 0x1109f36a5 in TclOOGetCallContext tclOOCall.c:1024
    #1 0x1109be8b2 in TclOOObjectCmdCore tclOO.c:2599
    #2 0x1109cd36d in PublicNRObjectCmd tclOO.c:2461
    #3 0x10fa83837 in Dispatch tclBasic.c:4520
    #4 0x10fa63bbf in TclNRRunCallbacks tclBasic.c:4556
    #5 0x10fa52ad3 in Tcl_NRCallObjProc tclBasic.c:8269
    #6 0x1109c8db5 in ObjectNamespaceDeleted tclOO.c:1141
    #7 0x1106b22dd in Tcl_DeleteNamespace tclNamesp.c:917
    #8 0x1109cd534 in ObjectRenamedTrace tclOO.c:844
    #9 0x10fa57e58 in CallCommandTraces tclBasic.c:3426
    #10 0x10fa4d7e7 in Tcl_DeleteCommandFromToken tclBasic.c:3226
    #11 0x1106ba16e in TclTeardownNamespace tclNamesp.c:1124
    #12 0x1106b3857 in Tcl_DeleteNamespace tclNamesp.c:1011
    #13 0x1109cd534 in ObjectRenamedTrace tclOO.c:844
    #14 0x10fa57e58 in CallCommandTraces tclBasic.c:3426
    #15 0x10fa4d7e7 in Tcl_DeleteCommandFromToken tclBasic.c:3226
    #16 0x1109d4b98 in TclOO_Object_Destroy tclOOBasic.c:361
    #17 0x1109eb8c0 in TclOOInvokeContext tclOOCall.c:314
    #18 0x1109c0400 in TclOOObjectCmdCore tclOO.c:2645
    #19 0x1109cd36d in PublicNRObjectCmd tclOO.c:2461
    #20 0x10fa83837 in Dispatch tclBasic.c:4520
    #21 0x10fa63bbf in TclNRRunCallbacks tclBasic.c:4556
    #22 0x10fa76fdf in TclEvalObjEx tclBasic.c:6138
    #23 0x10fa5f951 in Tcl_EvalObjEx tclBasic.c:6119
    #24 0x110510fa2 in Tcl_RecordAndEvalObj tclHistory.c:191
    #25 0x1106ab031 in Tcl_MainEx tclMain.c:520
    #26 0x10dfab8d6 in main tclAppInit.c:91
    #27 0x11a14552d in start+0x1cd (dyld:x86_64+0x552d)

==65360==Register values: rax = 0x00000000000000f0 rbx = 0x00007ff7b1f51d80 rcx = 0x0000100000000000 rdx = 0x0000000000000000 rdi = 0x00007ff7b1f50f70 rsi = 0x0000000000000000 rbp = 0x00007ff7b1f52830 rsp = 0x00007ff7b1f51c80 r8 = 0x0000000000000057 r9 = 0xbfffff00000fffff r10 = 0x00007ff809a2059e r11 = 0x0000000000000202 r12 = 0x000000011a1c03a0 r13 = 0x00007ff7b1f57a58 r14 = 0x000000010dfab898 r15 = 0x000000011a1ac010 AddressSanitizer can not provide additional info. SUMMARY: AddressSanitizer: SEGV tclOOCall.c:1024 in TclOOGetCallContext ==65360==ABORTING

A NULL+0xf0 dereference at tclOOCall.c:1024 would mean that oPtr->selfCls is NULL.


dkf added on 2024-05-20 13:36:40:

Stripped down further (from a test case I'm developing):

oo::class create RpcClient {
   method write name {
      lappend ::result "RpcClient -> $name"
   }
   method create_bug {} {
      MkObjectRpc create cfg [self] 111
   }
}
oo::class create MkObjectRpc {
   variable hdl
   constructor {rpcHdl mqHdl} {
      set hdl $mqHdl
      oo::objdefine [self] forward rpc $rpcHdl
   }
   destructor {
      my rpc write otto-$hdl
   }
}

set FH [RpcClient new]
$FH create_bug
$FH destroy

Have confirmed that this is a current issue in both 8.6 and trunk.


dkf added on 2024-05-20 13:51:11:

Debug-annotated stack trace

#0  TclOOGetCallContext (oPtr=0x1d1190, methodNameObj=0x15f510, flags=1, cacheInThisObj=0x15f510)
    at C:/Users/ZZALSDKF/code/tcl/generic/tclOOCall.c:1024
#1  0x00007ffae26a8812 in TclOOObjectCmdCore (oPtr=0x1d1190, interp=0x11d6d0, objc=3,
    objv=0x12afb0, flags=1, startCls=0x0) at C:/Users/ZZALSDKF/code/tcl/generic/tclOO.c:2599
#2  0x00007ffae26a83d7 in PublicNRObjectCmd (clientData=0x1d1190, interp=0x11d6d0, objc=3,
    objv=0x12afb0) at C:/Users/ZZALSDKF/code/tcl/generic/tclOO.c:2461
#3  0x00007ffae256f4b1 in Dispatch (data=0x163f58, interp=0x11d6d0, result=0)
    at C:/Users/ZZALSDKF/code/tcl/generic/tclBasic.c:4503
#4  0x00007ffae256f546 in TclNRRunCallbacks (interp=0x11d6d0, result=0, rootPtr=0x15fc00)
    at C:/Users/ZZALSDKF/code/tcl/generic/tclBasic.c:4539
#5  0x00007ffae2575294 in Tcl_NRCallObjProc (interp=0x11d6d0,
    objProc=0x7ffae26ac3bd <TclOOInvokeContext>, clientData=0x12ae20, objc=0, objv=0x0)
    at C:/Users/ZZALSDKF/code/tcl/generic/tclBasic.c:8235
#6  0x00007ffae26a562a in ObjectNamespaceDeleted (clientData=0x1d1790)
    at C:/Users/ZZALSDKF/code/tcl/generic/tclOO.c:1141
#7  0x00007ffae269c70b in Tcl_DeleteNamespace (namespacePtr=0x1ddd20)
    at C:/Users/ZZALSDKF/code/tcl/generic/tclNamesp.c:919
#8  0x00007ffae26a4ba8 in ObjectRenamedTrace (clientData=0x1d1790, interp=0x11d6d0,
    oldName=0x1cebe0 "::oo::Obj14::cfg", newName=0x0, flags=16512)
    at C:/Users/ZZALSDKF/code/tcl/generic/tclOO.c:844
#9  0x00007ffae256d9c7 in CallCommandTraces (iPtr=0x11d6d0, cmdPtr=0x1d1890,
    oldName=0x1cebe0 "::oo::Obj14::cfg", newName=0x0, flags=16512)
    at C:/Users/ZZALSDKF/code/tcl/generic/tclBasic.c:3409
#10 0x00007ffae256d600 in Tcl_DeleteCommandFromToken (interp=0x11d6d0, cmd=0x1d1890)
    at C:/Users/ZZALSDKF/code/tcl/generic/tclBasic.c:3209
#11 0x00007ffae269cb46 in TclTeardownNamespace (nsPtr=0x1dd920)
    at C:/Users/ZZALSDKF/code/tcl/generic/tclNamesp.c:1127
#12 0x00007ffae269c927 in Tcl_DeleteNamespace (namespacePtr=0x1dd920)
    at C:/Users/ZZALSDKF/code/tcl/generic/tclNamesp.c:1014
#13 0x00007ffae26a4ba8 in ObjectRenamedTrace (clientData=0x1d1190, interp=0x11d6d0,
    oldName=0x17e6c0 "::oo::Obj14", newName=0x0, flags=16512)
    at C:/Users/ZZALSDKF/code/tcl/generic/tclOO.c:844
#14 0x00007ffae256d9c7 in CallCommandTraces (iPtr=0x11d6d0, cmdPtr=0x1d1290,
    oldName=0x17e6c0 "::oo::Obj14", newName=0x0, flags=16512)
    at C:/Users/ZZALSDKF/code/tcl/generic/tclBasic.c:3409
#15 0x00007ffae256d600 in Tcl_DeleteCommandFromToken (interp=0x11d6d0, cmd=0x1d1290)
    at C:/Users/ZZALSDKF/code/tcl/generic/tclBasic.c:3209
#16 0x00007ffae26aa08f in TclOO_Object_Destroy (clientData=0x0, interp=0x11d6d0, context=0x12adf0,
    objc=2, objv=0x12add0) at C:/Users/ZZALSDKF/code/tcl/generic/tclOOBasic.c:361

dkf added on 2024-05-20 15:16:02:

The problem was in the computation of stereotypical call chains when the object in question had already gone rather a long way through the destruction process. Nasty!

Things no longer crash, but if you want to get the MkObjectRpc destructor to run and successfully call into the RpcClient write method, you'd better explicitly destroy the object created by LcConfigRpc_create_bug in the LcConfigRpcClient destructor. Without that, there's just slightly too much state ripped out of the way of things by the point when destruction happens and TclOO instead builds empty call chains (resulting in calls doing nothing; normally calls to non-existent methods result in call chains that go to the unknown method handler, in oo::object if not overridden).


jan.nijtmans added on 2024-05-20 20:53:00:
---- oo-35.7.1 start
Test file error: child killed: segmentation violation


Something is still wrong

jan.nijtmans added on 2024-05-21 08:37:38:

The 8.6 build succeeded, the 8.7 build failed on all platforms.

@dkf please have a look.


oehhar added on 2024-05-21 12:35:06:

Magic Sergey has fixed the crash reported below by commit [1d16344c8c]. Thank you all, great team, Harald


apnadkarni added on 2024-05-22 03:40:33:
While the crash may be fixed, it is not clear to me how things get into that state and if there is an underlying fault in the call chain computation that is hidden. Still worth Donal also taking a look imo.

sebres added on 2024-05-22 10:53:03:

Well, the case where oPtr->selfCls may be NULL is normal case, related to the code (even fix of dkf for other pieces [8b4a3295ed9e537c] shows such conditions check).

The fact that it fails in 8.7+ but not in 8.6 is easy to explain: in 8.6 since [b0e486eaa693a6a6] AddSimpleClassChainToCallContext checks whether classPtr == NULL (direct after tailRecurse label), where in 8.7+ this check was lost (probably due to some conflicts or because it was backported to AddPrivatesFromClassChainToCallContext only, which doesn't exist in 8.6)... Therefore the new tests oo-35.7.* didn't fail in 8.6, but in 8.7+ (I guess Donal simply merged it to 8.7 in [de08cf03235f3ce9], but didn't test it there).

Now this is fixed everywhere (I also restored the mentioned check in [22966032c2912825]), additionally the invocation of AddSimpleClassChainToCallContext and AddPrivatesFromClassChainToCallContext is avoided completely if oPtr->selfCls is NULL (totally unneeded and wouldn't crash if later both would be rewritten again).


apnadkarni added on 2024-05-22 13:42:06:
Thanks for the explanation Sergey.

dkf added on 2024-05-23 10:33:40:

Thanks for fixing that.