Skip to content
Tcl Source Code History Of Ticket 8ab8a138c9156e5a
Login

History Of Ticket 8ab8a138c9156e5a

Artifacts Associated With Ticket 8ab8a138c9156e5a

  1. Ticket change [cb73bae87f] (rid 219163) by chrstphrchvz on 2023-10-07 12:30:45:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/x-fossil-wiki"
    4. comment initialized to:
      LLVM Clang 17 makes <tt>-fsanitize=function</tt> available for C (previously it was just for C++). It is implied by <tt>-fsanitize=undefined</tt>. It complains about calls to <tt>freeProc(clientData)</tt> from <tt>Tcl_EventuallyFree()</tt> and <tt>Tcl_Release()</tt>. Unlike warnings from e.g. <tt>-Wincompatible-function-pointer-types</tt>, this UBSan error is not suppressed by casts to <tt>(Tcl_FreeProc *)</tt>. Example when exiting tclsh:
      <pre>
      <span style="font-weight:bold">tcl/generic/tclPreserve.c:229:3: <span style="color:firebrick">runtime error:</span> call to function DeleteInterpProc through pointer to incorrect function type 'void (*)(char *)'
      tclBasic.c:1422: <span style="color:gray">note:</span></span> DeleteInterpProc defined here
      SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior tcl/generic/tclPreserve.c:229:3 in 
      </pre>
      There are only a few of instances of this in core Tcl; to patch these, I remove casts to <tt>(Tcl_FreeProc *)</tt>, then use the <tt>Tcl_FreeProc</tt> typedef for function declarations, and then fix function definitions. There are several more instances in core Tk which I am looking into patching. Any extensions passing incompatible function pointers to <tt>Tcl_EventuallyFree()</tt> will also trigger this UBSan error.
      
    5. foundin initialized to: "8.6.13"
    6. is_private initialized to: "0"
    7. login: "chrstphrchvz"
    8. priority initialized to: "5 Medium"
    9. private_contact initialized to: "6133eb3026f9b10aa9b7b48da92ae2834fc225b0"
    10. resolution initialized to: "None"
    11. severity initialized to: "Minor"
    12. status initialized to: "Open"
    13. submitter initialized to: "chrstphrchvz"
    14. subsystem initialized to: "42. Memory Preservation"
    15. title initialized to:
      Do not pass incompatible function pointers to Tcl_EventuallyFree()
      
    16. type initialized to: "Patch"
  2. Add attachment "8ab8a138c915.diff" [4dfdb7098a] (rid 219165) by chrstphrchvz on 2023-10-07 12:33:36.

  3. Ticket change [beec9be887] (rid 219166) by chrstphrchvz on 2023-10-07 13:06:18:

    1. icomment:
      I now notice that in Tcl 9, <tt>Tcl_FreeProc</tt> expects a <tt>void *</tt>. If reverting to <tt>char *</tt> is undesirable, then should a macro or typedef be introduced for use by Tk 8.7 and anything else intending to build against both Tcl 8 and 9?
      
    2. login: "chrstphrchvz"
    3. mimetype: "text/x-fossil-wiki"
  4. Ticket change [473fc1ecbe] (rid 219167) by chrstphrchvz on 2023-10-07 13:13:17:

    1. comment changed to:
      LLVM Clang 17 makes <tt>-fsanitize=function</tt> available for C (previously it was just for C++). It is implied by <tt>-fsanitize=undefined</tt>. It complains about calls to <tt>freeProc(clientData)</tt> from <tt>Tcl_EventuallyFree()</tt> and <tt>Tcl_Release()</tt>. Unlike warnings from e.g. <tt>-Wincompatible-function-pointer-types</tt>, this UBSan error is not suppressed by casts to <tt>(Tcl_FreeProc *)</tt>. Example when exiting tclsh:
      <pre>
      <span style="font-weight:bold">tcl/generic/tclPreserve.c:229:3: <span style="color:firebrick">runtime error:</span> call to function DeleteInterpProc through pointer to incorrect function type 'void (*)(char *)'
      tclBasic.c:1422: <span style="color:gray">note:</span></span> DeleteInterpProc defined here
      SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior tcl/generic/tclPreserve.c:229:3 in 
      </pre>
      There are only a few instances of this in core Tcl; to patch these, I remove casts to <tt>(Tcl_FreeProc *)</tt>, then use the <tt>Tcl_FreeProc</tt> typedef for function declarations, and then fix function definitions. There are several more instances in core Tk which I am looking into patching. Any extensions passing incompatible function pointers to <tt>Tcl_EventuallyFree()</tt> will also trigger this UBSan error.
      
    2. login: "chrstphrchvz"
    3. mimetype: "text/plain"
  5. Ticket change [c7b80349df] (rid 219226) by chrstphrchvz on 2023-10-10 10:07:33:

    1. icomment:
      Proposed patch for Tk 8.6: [https://core.tcl-lang.org/tk/info/d96974d99d72]
      
    2. login: "chrstphrchvz"
    3. mimetype: "text/x-fossil-wiki"
  6. Ticket change [42f31524a5] (rid 219292) by chrstphrchvz on 2023-10-11 07:23:35:

    1. icomment:
      Jan, I am sorry if the title I gave this ticket was misleading, but unfortunately the work so far on the bug-8ab8a138c9 branch ultimately does not solve the problem.
      
      What matters is the type of actual function declarations/definitions, and not the type of function pointers passed to <tt>Tcl_EventuallyFree()</tt>.
      
      At runtime, when <tt>freeProc()</tt> is called from <tt>Tcl_EventuallyFree()</tt> or <tt>Tcl_Release()</tt>, UBSan <tt>-fsanitize=function</tt> checks whether the function pointed to by <tt>freeProc</tt> was actually declared/defined with a signature matching the type of <tt>freeProc</tt>, which is <tt>Tcl_FreeProc</tt>. It does not matter to this UBSan check whether the function pointer in <tt>freeProc</tt> was originally passed to <tt>Tcl_EventuallyFree()</tt> through any casts, so I do not see what is accomplished by additional intermediate casting through <tt>void *</tt>.
      
      I still believe the correct fix for this problem is to declare/define functions correctly, which is what I meant in the title: only pointers to functions whose actual declarations/definitions match <tt>Tcl_FreeProc</tt> should be passed to <tt>Tcl_EventuallyFree()</tt> (which can be done without casts), so that UBSan does not complain when the function is later called from <tt>Tcl_EventuallyFree()</tt> or <tt>Tcl_Release()</tt>.
      
    2. login: "chrstphrchvz"
    3. mimetype: "text/x-fossil-wiki"
  7. Ticket change [6860e38024] (rid 219293) by chrstphrchvz on 2023-10-11 07:32:44:

    1. icomment: "Ticket title revised."
    2. login: "chrstphrchvz"
    3. mimetype: "text/x-fossil-wiki"
    4. title changed to:
      Functions passed to Tcl_EventuallyFree() must be declared/defined as Tcl_FreeProc
      
  8. Ticket change [0231766c80] (rid 219294) by jan.nijtmans on 2023-10-11 07:50:12:

    1. assignee changed to: "jan.nijtmans"
    2. icomment:
      Thanks for your feedback!
      
      I was aware that the extra (void *) cast would not foll ubsan, but I thought it was worth to give it a try. I'll fix it as you suggest
      
    3. login: "jan.nijtmans"
    4. mimetype: "text/x-fossil-wiki"
  9. Ticket change [339b90b07c] (rid 219311) by jan.nijtmans on 2023-10-11 11:18:49:

    1. login: "jan.nijtmans"
    2. mimetype: "text/plain"
    3. resolution changed to: "Fixed"
    4. status changed to: "Pending"
  10. Ticket change [92940d9e5f] (rid 219312) by chrstphrchvz on 2023-10-11 12:20:45:

    1. icomment:
      How are the changes to tclDecls.h beneficial? They ensure the suppression of compiler warnings (or errors as of LLVM Clang 16, by default) regarding passing of incompatible function pointers. Currently, I believe it would be better to not suppress those compile-time complaints, particularly since they help identify and fix would-be <tt>-fsanitize=function</tt> errors, at least for the iterative workflow I used (remove any existing casts to <tt>(Tcl_FreeProc *)</tt> by <tt>Tcl_EventuallyFree()</tt> users, then fix declarations in response to compiler complaints of passing incompatible function pointers, then fix definitions in response to compiler complaints of declaration/definition mismatch).
      
    2. login: "chrstphrchvz"
    3. mimetype: "text/x-fossil-wiki"
  11. Ticket change [1d76a072fe] (rid 219313) by chrstphrchvz on 2023-10-11 12:27:36:

    1. icomment:
      <blockquote>How are the changes to tclDecls.h beneficial?</blockquote>
      What I mean is, aside from the effect of suppressing compiler warnings/errors, is there some benefit I am not aware of?
      
    2. login: "chrstphrchvz"
    3. mimetype: "text/x-fossil-wiki"
  12. Ticket change [133a3ce12e] (rid 219363) by chrstphrchvz on 2023-10-13 06:56:24:

    1. icomment:
      <blockquote>…Currently, I believe it would be better to not suppress those compile-time complaints, particularly since they help identify and fix would-be <tt>-fsanitize=function</tt> errors…</blockquote>
      
      <a href="https://clang.llvm.org/docs/DiagnosticsReference.html#wcast-function-type"><tt>-Wcast-function-type</tt></a> also works.
      
    2. login: "chrstphrchvz"
    3. mimetype: "text/x-fossil-wiki"
  13. Ticket change [30a8da4632] (rid 219386) by chrstphrchvz on 2023-10-13 13:58:28:

    1. icomment:
      <blockquote><tt>-Wcast-function-type</tt> also works.</blockquote>
      …but not when there is an intermediate <tt>(void *)</tt> cast.
      
      The casts in tclDecls.h cause <tt>Tcl_EventuallyFree()</tt> and <tt>Tcl_SetResult()</tt> to be called from function pointers of incorrect type, and so now UBSan complains even more. A couple of examples:
      
      <pre>
      <span style="font-weight:bold">tk/generic/tkMenuDraw.c:796:2: <span style="color:firebrick">runtime error:</span> call to function Tcl_EventuallyFree through pointer to incorrect function type 'void (*)(void *, void *)'
      tclPreserve.c:265: <span style="color:gray">note:</span></span> Tcl_EventuallyFree defined here
      SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior tk/generic/tkMenuDraw.c:796:2 in 
      </pre>
      
      <pre>
      <span style="font-weight:bold">tcl/generic/tclTest.c:1117:2: <span style="color:firebrick">runtime error:</span> call to function Tcl_SetResult through pointer to incorrect function type 'void (*)(struct Tcl_Interp *, char *, void *)'
      tclResult.c:419: <span style="color:gray">note:</span></span> Tcl_SetResult defined here
      SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior tcl/generic/tclTest.c:1117:2 in 
      </pre>
      
    2. login: "chrstphrchvz"
    3. mimetype: "text/x-fossil-wiki"
  14. Ticket change [7cd7441c2f] (rid 219390) by jan.nijtmans on 2023-10-13 14:23:23:

    1. icomment:
      > …Currently, I believe it would be better to not suppress those compile-time complaints, particular
      
      Would it be possible to disable those when doing your UBSan tests? There's a lot of code out there (e.g. Tk, Itcl) which uses this feature, UBSan just reports false-positives for them. As it is now, they can use whatever pointer-type without warning. In Tcl 9.0, the pointer type changes to "void *", there we want the full UBSan warnings, but for 8.x it really doesn't matter much to chase this.
      
      Thanks for all your efforts! I see the point!
      
    2. login: "jan.nijtmans"
    3. mimetype: "text/plain"
  15. Ticket change [f4c18b2b5a] (rid 219748) by chrstphrchvz on 2023-10-22 23:18:44:

    1. icomment:
      <blockquote>There's a lot of code out there (e.g. Tk, Itcl) which uses this
      feature, UBSan just reports false-positives for them.</blockquote>
      
      One can use <tt>-fsanitize=undefined -fno-sanitize=function</tt>, but I doubt UBSan’s reports of undefined behavior are false positives*, despite things behaving as expected under typical calling conventions. Or have I misunderstood what you meant by “false positives”?
      
      *(The only potential false positives I am aware of are those due to this feature’s reliance on name mangling, which is more strict than standard C type compatibility; see [https://reviews.llvm.org/D148827].)
      
      That being said, I do not know how immediate any danger posed by optimizing compilers is for exploiting this type of undefined behavior. Instances within a single translation unit, or with LTO enabled, speculatively seem more concerning.
      
    2. login: "chrstphrchvz"
    3. mimetype: "text/x-fossil-wiki"
  16. Ticket change [65cf874dc4] (rid 222267) by chrstphrchvz on 2024-02-16 11:06:31:

    1. icomment:
      I have since learned that eliminating <tt>-fsanitize=function</tt> complaints is also useful for security hardening, because some control flow integrity schemes require calling functions using the correct pointer type (although some schemes also rely on LTO which I am not aware how well Tcl supports). See [https://maskray.me/blog/2022-12-18-control-flow-integrity#fsanitizefunction]
      
    2. login: "chrstphrchvz"
    3. mimetype: "text/x-fossil-wiki"
  17. Ticket change [3b0513364e] (rid 222268) by jan.nijtmans on 2024-02-16 11:28:09:

    1. icomment:
      > ... LTO which I am not aware how well Tcl supports
      
      Tcl and Tk support LTO, except for the stub libraries (for which LTO actually doesn't make much sense).
      
    2. login: "jan.nijtmans"
    3. mimetype: "text/x-fossil-wiki"
  18. Ticket change [108bf18709] (rid 258135) by serhiy.storchaka on 2026-10-15 11:29:07:

    1. icomment: "Closing: fixed, see above."
    2. login: "serhiy.storchaka"
    3. mimetype: "text/x-fossil-wiki"
    4. status changed to: "Closed"
  19. Ticket change [1871df908d] (rid 259936) by serhiy.storchaka on 2026-10-22 17:19:26:

    1. closedate changed to: "2461328.9785532523"
    2. closer changed to: "serhiy.storchaka"
    3. login: "serhiy.storchaka"