Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 7e07975e6438f6b7bdeb3699efbc786e2b7dfbff4c1838b1eeafcc9069de8e7e
Ticket: 70944d8f141bfe849c6bd37ded7ce04ce8e27052
invalid refcount for classes in itcl?
User & Date: ivk on 2020-09-28 13:28:16
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/plain"
  4. comment changed to:
    We have multiple TCL interpreters running in different threads. We are suffering from a crash that seems to be caused by the access to the already deleted ItclClass. Long story short, itcl tries to delete the same class twice - when another thread interferes, it can allocate a new class at the same address overriding the ITCL_CLASS_IS_FREED flag which leads to a crash when itcl access this object for the second time.
    Consider the following snippet (crash.tcl):
    
    package require Iwidgets
    iwidgets::Labeledwidget  .l
    pack .l
    after 1000
    exit
    
    I ran it like this (using the self-built interpreter to remove our multi-threaded environment from investigation): 
    TCL_FINALIZE_ON_EXIT=1 tclsh8.6 ~/scripts/crash.tcl
    
    I've also added some logging to ItclReleaseClass():
    
    if (iclsPtr->refCount-- <= 1) {
        pthread_t id = pthread_self();
        fprintf(stderr, "    ItclReleaseClass: %p, refCount: %d, thread: %lu\n", iclsPtr, iclsPtr->refCount, id);
        ItclFreeClass((char *) clientData);
    }
    
    Now, in the logs I see:
        ItclReleaseClass: 0x6290004dba10, refCount: 0, thread: 140564963183552
        ItclReleaseClass: 0x6290004dba10, refCount: -1, thread: 140564963183552
    
    This refcount -1 looks very suspicious meaning that class has already been freed. In a single-threaded environment there are no (re)allocations at this point of time => flags are not overwritten => the code works. However, it doesn't seem right to access the freed memory (without the custom allocator, that would be a sigsegv, but TCL allocator helps here preventing segmentation violation).
    
  5. is_private changed to: "0"
  6. login: "ivk"
  7. priority changed to: "5 Medium"
  8. resolution changed to: "None"
  9. severity changed to: "Minor"
  10. status changed to: "Open"
  11. submitter changed to: "ivk"
  12. subsystem changed to: "69. Other"
  13. title changed to: "invalid refcount for classes in itcl?"
  14. type changed to: "Bug"