Overview
| Artifact ID: | e4b85930b309b38db8bb9a2adfb1b0f62e8b1be327be557c40bf69771e3026a2 |
|---|---|
| Ticket: | 4eb3a155ac6f73f48b270a19e543841d26bf2e6f
apply causes memory corruption / crash |
| User & Date: | apnadkarni on 2022-07-23 04:24:50 |
Changes
- closer changed to: "nobody"
- comment changed to:
The [apn-apply-bug](https://core.tcl-lang.org/tcl/timeline?r=apn-apply-bug) branch contains a demonstration and proposed fix for the bug. The bug is triggered when the apply command is passed a "lambda" argument which is is a two element list (i.e. not yet shimmered to the `lambdaType` internal representation) but the second element of the list (the lambda body) is unshared **and** already has a internal `tclByteCodeType` representation. The test command [testapplylambda](https://core.tcl-lang.org/tcl/artifact?name=f0202c540752a8dc&ln=8127) is an example of the above. Analysis: When `TclNRApplyObjCmd` is called with an argument that is a list of two strings, it first [converts](https://core.tcl-lang.org/tcl/artifact?name=37dbfa89bf70d3f9&ln=2683) the argument to the internal `lambdaType` in `procPtr`. It then compiles the second element of the argument, the body, by calling [TclPushProcCallFrame](https://core.tcl-lang.org/tcl/artifact?name=37dbfa89bf70d3f9&ln=2720). Crucially, this compilation updates the `procPtr->numCompiledLocals` field with the number of locals used in the body. The next time `TclNRApplyObjCmd` is called with the same argument, the argument object is already shimmered to `lambdaType` so no conversion is needed. It also already contains the right value of `procPtr->numCompiledLocals` from the previous conversion. So all is well. The bug occurs when the lambda argument is a list but the body element already has a `tclByteCodeType` internal type. In this case, again `TclNRApplyObjCmd` calls `TclGetLambdaFromObj` to shimmer the argument to a `lambdaType` and get a new `procPtr`. However, when `TclPushProcCallFrame` is called, it finds the body element is already of the `tclByteCodeType` type and [does not recompile](https://core.tcl-lang.org/tcl/artifact?name=37dbfa89bf70d3f9&ln=1579) the body script. **Consequently `procPtr->numCompiledLocals` in the newly allocated `procPtr` does not get updated.** Subsequently, when the byte code is executed it tries to access the expected locations for local variables which has not actually been allocated and crashes. Proposed fix: The proposed [fix](https://core.tcl-lang.org/tcl/artifact?name=37dbfa89bf70d3f9&ln=2461) is to always free the internal representation of the body element `Tcl_Obj` of a lambda argument when the latter is shimmered to a `lambdaType`. This ensures recompilation. This is the simplest and cleanest fix I can come up with given my limited knowledge of this area. An alternative more efficient alternative might be to "remember" the value of numCompiledLocals and stash it away in the `tclByteCodeType` internal rep to be used to reinitialize `procPtr->numCompiledLocals` when the body itself does not need recompilation. I am reluctant to explore that being unsure of what other compilation side-effects also would need to be accounted for. Someone with more expertise might explore that path. Why this has not shown up: Reproducing this at the script level is hard (impossible?) in the current Tcl implementation because it requires certain conditions - the lambda itself must not already have an internal lambda rep AND the body element must already have byteCode internal rep AND the body element must be unshared (shared Tcl_Obj are always recompiled). The shimmering and reference counting that happens in script level commands means these conditions are rare (never occur?). Nevertheless this seems to be a genuine bug as illustrated in the C code. Why this fix has more relevance now: With TIP 625 list optimizations, this can be reproduced at the script level with the following code. ``` set lambda [list {} {set x 42}] apply [lrange $lambda 0 end] apply [lrange $lambda 0 end] ``` With TIP 625, the `lrange` commands return a new Tcl_Obj (because the string representation must be canonical) but share the same internal list representation since the entire list is being returned. Consequently the second apply produces the conditions required for the crash. Pre-TIP 625, the `lrange` commands would not only return a new Tcl_Obj but would also return an entirely new internal list representation even when the entire list was covered by the range (this is sub-optimal). The conditions for the bug are thus not met and the bug is not triggered. - login: "apnadkarni"
- mimetype: "text/plain"
- resolution changed to: "None"
- status changed to: "Open"