Skip to content
Tcl Source Code Artifact [e4b85930b3]
Login

Artifact [e4b85930b3]

Artifact e4b85930b309b38db8bb9a2adfb1b0f62e8b1be327be557c40bf69771e3026a2:

Ticket change [e4b85930b3] - Open ticket [4eb3a155ac6f73f4|4eb3a155ac]: <i>apply causes memory corruption / crash</i> plus 5 other changes by apnadkarni on 2022-07-23 04:24:50.
D 2022-07-23T04:24:50.072
J closer nobody
J comment The\s[apn-apply-bug](https://core.tcl-lang.org/tcl/timeline?r=apn-apply-bug)\sbranch\scontains\sa\sdemonstration\sand\sproposed\sfix\sfor\sthe\sbug.\r\n\r\nThe\sbug\sis\striggered\swhen\sthe\sapply\scommand\sis\spassed\sa\s"lambda"\sargument\swhich\sis\sis\sa\stwo\selement\slist\s(i.e.\snot\syet\sshimmered\sto\sthe\s`lambdaType`\sinternal\srepresentation)\sbut\sthe\ssecond\selement\sof\sthe\slist\s(the\slambda\sbody)\sis\sunshared\s**and**\salready\shas\sa\sinternal\s`tclByteCodeType`\srepresentation.\r\n\r\nThe\stest\scommand\s[testapplylambda](https://core.tcl-lang.org/tcl/artifact?name=f0202c540752a8dc&ln=8127)\sis\san\sexample\sof\sthe\sabove.\r\n\r\nAnalysis:\r\n\r\nWhen\s`TclNRApplyObjCmd`\sis\scalled\swith\san\sargument\sthat\sis\sa\slist\sof\stwo\sstrings,\sit\sfirst\s[converts](https://core.tcl-lang.org/tcl/artifact?name=37dbfa89bf70d3f9&ln=2683)\sthe\sargument\sto\sthe\sinternal\s`lambdaType`\sin\s`procPtr`.\sIt\sthen\scompiles\sthe\ssecond\selement\sof\sthe\sargument,\sthe\sbody,\sby\scalling\s[TclPushProcCallFrame](https://core.tcl-lang.org/tcl/artifact?name=37dbfa89bf70d3f9&ln=2720).\sCrucially,\sthis\scompilation\supdates\sthe\s`procPtr->numCompiledLocals`\sfield\swith\sthe\snumber\sof\slocals\sused\sin\sthe\sbody.\r\n\r\nThe\snext\stime\s`TclNRApplyObjCmd`\sis\scalled\swith\sthe\ssame\sargument,\sthe\sargument\sobject\sis\salready\sshimmered\sto\s`lambdaType`\sso\sno\sconversion\sis\sneeded.\sIt\salso\salready\scontains\sthe\sright\svalue\sof\s`procPtr->numCompiledLocals`\sfrom\sthe\sprevious\sconversion.\sSo\sall\sis\swell.\r\n\r\nThe\sbug\soccurs\swhen\sthe\slambda\sargument\sis\sa\slist\sbut\sthe\sbody\selement\salready\shas\sa\s`tclByteCodeType`\sinternal\stype.\sIn\sthis\scase,\sagain\s`TclNRApplyObjCmd`\scalls\s`TclGetLambdaFromObj`\sto\sshimmer\sthe\sargument\sto\sa\s`lambdaType`\sand\sget\sa\snew\s`procPtr`.\sHowever,\swhen\s`TclPushProcCallFrame`\sis\scalled,\sit\sfinds\sthe\sbody\selement\sis\salready\sof\sthe\s`tclByteCodeType`\stype\sand\s[does\snot\srecompile](https://core.tcl-lang.org/tcl/artifact?name=37dbfa89bf70d3f9&ln=1579)\sthe\sbody\sscript.\s**Consequently\s`procPtr->numCompiledLocals`\sin\sthe\snewly\sallocated\s`procPtr`\sdoes\snot\sget\supdated.**\sSubsequently,\swhen\sthe\sbyte\scode\sis\sexecuted\sit\stries\sto\saccess\sthe\sexpected\slocations\sfor\slocal\svariables\swhich\shas\snot\sactually\sbeen\sallocated\sand\scrashes.\r\n\r\nProposed\sfix:\r\n\r\nThe\sproposed\s[fix](https://core.tcl-lang.org/tcl/artifact?name=37dbfa89bf70d3f9&ln=2461)\sis\sto\salways\sfree\sthe\sinternal\srepresentation\sof\sthe\sbody\selement\s`Tcl_Obj`\sof\sa\slambda\sargument\swhen\sthe\slatter\sis\sshimmered\sto\sa\s`lambdaType`.\sThis\sensures\srecompilation.\r\n\r\nThis\sis\sthe\ssimplest\sand\scleanest\sfix\sI\scan\scome\sup\swith\sgiven\smy\slimited\sknowledge\sof\sthis\sarea.\sAn\salternative\smore\sefficient\salternative\smight\sbe\sto\s"remember"\sthe\svalue\sof\snumCompiledLocals\sand\sstash\sit\saway\sin\sthe\s`tclByteCodeType`\sinternal\srep\sto\sbe\sused\sto\sreinitialize\s`procPtr->numCompiledLocals`\swhen\sthe\sbody\sitself\sdoes\snot\sneed\srecompilation.\sI\sam\sreluctant\sto\sexplore\sthat\sbeing\sunsure\sof\swhat\sother\scompilation\sside-effects\salso\swould\sneed\sto\sbe\saccounted\sfor.\sSomeone\swith\smore\sexpertise\smight\sexplore\sthat\spath.\r\n\r\nWhy\sthis\shas\snot\sshown\sup:\r\n\r\nReproducing\sthis\sat\sthe\sscript\slevel\sis\shard\s(impossible?)\sin\sthe\scurrent\sTcl\simplementation\sbecause\sit\srequires\scertain\sconditions\s-\sthe\slambda\sitself\smust\snot\salready\shave\san\sinternal\slambda\srep\sAND\sthe\sbody\selement\smust\salready\shave\sbyteCode\sinternal\srep\sAND\sthe\sbody\selement\smust\sbe\sunshared\s(shared\sTcl_Obj\sare\salways\srecompiled).\sThe\sshimmering\sand\sreference\scounting\sthat\shappens\sin\sscript\slevel\scommands\smeans\sthese\sconditions\sare\srare\s(never\soccur?).\sNevertheless\sthis\sseems\sto\sbe\sa\sgenuine\sbug\sas\sillustrated\sin\sthe\sC\scode.\r\n\r\nWhy\sthis\sfix\shas\smore\srelevance\snow:\r\n\r\nWith\sTIP\s625\slist\soptimizations,\sthis\scan\sbe\sreproduced\sat\sthe\sscript\slevel\swith\sthe\sfollowing\scode.\r\n\r\n```\r\nset\slambda\s[list\s{}\s{set\sx\s42}]\r\napply\s[lrange\s$lambda\s0\send]\r\napply\s[lrange\s$lambda\s0\send]\r\n```\r\n\r\nWith\sTIP\s625,\sthe\s`lrange`\scommands\sreturn\sa\snew\sTcl_Obj\s(because\sthe\sstring\srepresentation\smust\sbe\scanonical)\sbut\sshare\sthe\ssame\sinternal\slist\srepresentation\ssince\sthe\sentire\slist\sis\sbeing\sreturned.\sConsequently\sthe\ssecond\sapply\sproduces\sthe\sconditions\srequired\sfor\sthe\scrash.\r\n\r\nPre-TIP\s625,\sthe\s`lrange`\scommands\swould\snot\sonly\sreturn\sa\snew\sTcl_Obj\sbut\swould\salso\sreturn\san\sentirely\snew\sinternal\slist\srepresentation\seven\swhen\sthe\sentire\slist\swas\scovered\sby\sthe\srange\s(this\sis\ssub-optimal).\sThe\sconditions\sfor\sthe\sbug\sare\sthus\snot\smet\sand\sthe\sbug\sis\snot\striggered.
J login apnadkarni
J mimetype text/plain
J resolution None
J status Open
K 4eb3a155ac6f73f48b270a19e543841d26bf2e6f
U apnadkarni
Z eb7ee6bed499399986ade0a59ebd5a27