Artifacts Associated With Ticket 8ab8a138c9156e5a
Ticket change [cb73bae87f] (rid 219163) by chrstphrchvz on 2023-10-07 12:30:45:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/x-fossil-wiki"
- 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.
- foundin initialized to: "8.6.13"
- is_private initialized to: "0"
- login: "chrstphrchvz"
- priority initialized to: "5 Medium"
- private_contact initialized to: "6133eb3026f9b10aa9b7b48da92ae2834fc225b0"
- resolution initialized to: "None"
- severity initialized to: "Minor"
- status initialized to: "Open"
- submitter initialized to: "chrstphrchvz"
- subsystem initialized to: "42. Memory Preservation"
- title initialized to:
Do not pass incompatible function pointers to Tcl_EventuallyFree()
- type initialized to: "Patch"
Add attachment "8ab8a138c915.diff" [4dfdb7098a] (rid 219165) by chrstphrchvz on 2023-10-07 12:33:36.
Ticket change [beec9be887] (rid 219166) by chrstphrchvz on 2023-10-07 13:06:18:
- 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?
- login: "chrstphrchvz"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [473fc1ecbe] (rid 219167) by chrstphrchvz on 2023-10-07 13:13:17:
- 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.
- login: "chrstphrchvz"
- mimetype: "text/plain"
- comment changed to:
Ticket change [c7b80349df] (rid 219226) by chrstphrchvz on 2023-10-10 10:07:33:
- icomment:
Proposed patch for Tk 8.6: [https://core.tcl-lang.org/tk/info/d96974d99d72]
- login: "chrstphrchvz"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [42f31524a5] (rid 219292) by chrstphrchvz on 2023-10-11 07:23:35:
- 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>.
- login: "chrstphrchvz"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [6860e38024] (rid 219293) by chrstphrchvz on 2023-10-11 07:32:44:
- icomment: "Ticket title revised."
- login: "chrstphrchvz"
- mimetype: "text/x-fossil-wiki"
- title changed to:
Functions passed to Tcl_EventuallyFree() must be declared/defined as Tcl_FreeProc
Ticket change [0231766c80] (rid 219294) by jan.nijtmans on 2023-10-11 07:50:12:
- assignee changed to: "jan.nijtmans"
- 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
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
Ticket change [339b90b07c] (rid 219311) by jan.nijtmans on 2023-10-11 11:18:49:
- login: "jan.nijtmans"
- mimetype: "text/plain"
- resolution changed to: "Fixed"
- status changed to: "Pending"
Ticket change [92940d9e5f] (rid 219312) by chrstphrchvz on 2023-10-11 12:20:45:
- 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).
- login: "chrstphrchvz"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [1d76a072fe] (rid 219313) by chrstphrchvz on 2023-10-11 12:27:36:
- 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?
- login: "chrstphrchvz"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [133a3ce12e] (rid 219363) by chrstphrchvz on 2023-10-13 06:56:24:
- 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.
- login: "chrstphrchvz"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [30a8da4632] (rid 219386) by chrstphrchvz on 2023-10-13 13:58:28:
- 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>
- login: "chrstphrchvz"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [7cd7441c2f] (rid 219390) by jan.nijtmans on 2023-10-13 14:23:23:
- 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!
- login: "jan.nijtmans"
- mimetype: "text/plain"
- icomment:
Ticket change [f4c18b2b5a] (rid 219748) by chrstphrchvz on 2023-10-22 23:18:44:
- 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.
- login: "chrstphrchvz"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [65cf874dc4] (rid 222267) by chrstphrchvz on 2024-02-16 11:06:31:
- 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]
- login: "chrstphrchvz"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [3b0513364e] (rid 222268) by jan.nijtmans on 2024-02-16 11:28:09:
- 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).
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [108bf18709] (rid 258135) by serhiy.storchaka on 2026-10-15 11:29:07:
- icomment: "Closing: fixed, see above."
- login: "serhiy.storchaka"
- mimetype: "text/x-fossil-wiki"
- status changed to: "Closed"
Ticket change [1871df908d] (rid 259936) by serhiy.storchaka on 2026-10-22 17:19:26:
- closedate changed to: "2461328.9785532523"
- closer changed to: "serhiy.storchaka"
- login: "serhiy.storchaka"