Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2024-08-19
16:59
Merge [7fce749488fbd896], "Fix [e58d7e19e9]: Upwards compatibility of TclObjTypeHasProc()", and reso... check-in: 7e47b21429 user: pooryorick tags: unchained, INCOMPATIBLE_LICENSE
16:03
Merge [354b22cef810c880], " Add (hopefully confincing) testcase for bug [e58d7e19e9]" and resolve co... check-in: be88ae27ad user: pooryorick tags: unchained, INCOMPATIBLE_LICENSE
2024-08-18
11:51 • Ticket [e58d7e19e9] Upwards compatibility of TclObjTypeHasProc() status stil... artifact: cf64a4efef user: pooryorick
2023-08-01
06:55 • Closed ticket [e58d7e19e9]. artifact: a890fc809a user: jan.nijtmans
2023-07-31
15:58
Fix [e58d7e19e9]: Upwards compatibility of TclObjTypeHasProc() check-in: 7fce749488 user: jan.nijtmans tags: trunk, main
15:48 • Ticket [e58d7e19e9] Upwards compatibility of TclObjTypeHasProc() status stil... artifact: e9ba8fcd55 user: jan.nijtmans
15:45
Add (hopefully confincing) testcase for bug [e58d7e19e9] check-in: 354b22cef8 user: jan.nijtmans tags: trunk, main
09:47 • Ticket [e58d7e19e9] Upwards compatibility of TclObjTypeHasProc() status stil... artifact: 3cec29a30a user: griffin
2023-07-29
20:57 • Ticket [e58d7e19e9]: 3 changes artifact: fca85637fe user: jan.nijtmans
18:11
Demo bug [e58d7e19e9] closed check-in: cdcadcb379 user: jan.nijtmans tags: bug-e58d7e19e9-demo
2023-07-28
09:38 • Ticket [e58d7e19e9] Upwards compatibility of TclObjTypeHasProc() status stil... artifact: 176aa23486 user: apnadkarni
2023-07-27
20:00 • Ticket [e58d7e19e9]: 3 changes artifact: b0907fde3e user: griffin
19:08 • Ticket [e58d7e19e9]: 3 changes artifact: 7bb14abf13 user: jan.nijtmans
17:53 • Ticket [e58d7e19e9]: 3 changes artifact: 4b076511cc user: griffin
15:24 • Ticket [e58d7e19e9]: 3 changes artifact: 141c59ae74 user: apnadkarni
13:22 • Ticket [e58d7e19e9]: 3 changes artifact: 115e0fa1d0 user: jan.nijtmans
13:10
Proposed fix for [e58d7e19e9]: Upwards compatibility of TclObjTypeHasProc() check-in: 07e8db69b8 user: jan.nijtmans tags: bug-e58d7e19e9
12:57
Proposed fix for [e58d7e19e9]: Upwards compatibility of TclObjTypeHasProc() check-in: eea8cdef80 user: jan.nijtmans tags: bug-e58d7e19e9
12:45 • New ticket [e58d7e19e9] Upwards compatibility of TclObjTypeHasProc(). artifact: e3ec43ba68 user: jan.nijtmans

Upwards compatibility of TclObjTypeHasProc()

Closed / Fixede58d7e19e937496f179fe711345b415f3f3d31b2Created 3 years agoUpdated 2 years ago
Descriptionwiki2023-07-27 12:45:52
The function TclObjTypeHasProc() currently only supports version "1" and version "2". That means that as soon as a version "3" is introduced, already-released Tcl versions won't recognise it any more, even if the Tcl_ObjType struct has the same functions at the start. This is a fundamental problem, easily solvable. It's the problem behind this remark

The problem was introduced with [this] commit (despite the comment "Cleanup abstract list definitions"); everything was fine before. If we assume that Tcl_ObjType is a structure, increasing in size with newer versions, we can continue to update the structure with adding new functions at the end without losing future upwards compatibility. This is already proven in the Tcl\_Channel structure, which follows the same concept.

Referenced by7
  • 7e47b21429Merge [7fce749488fbd896], "Fix [e58d7e19e9]: Upwards compatibility of TclObjTypeHasProc()", and resolve conflicts, making this a no-op. check-in2024-08-19 16:59:34
  • be88ae27adMerge [354b22cef810c880], " Add (hopefully confincing) testcase for bug [e58d7e19e9]" and resolve conflicts. Add string.isEmpty interface for Tcl_Obj values and modify TclCheckEmptyString to use it. Also modify TclCheckEmptyString to accept a Tcl_Interp arg and return a standard Tcl result. check-in2024-08-19 16:03:12
  • 7fce749488Fix [e58d7e19e9]: Upwards compatibility of TclObjTypeHasProc() check-in2023-07-31 15:58:14
  • 354b22cef8Add (hopefully confincing) testcase for bug [e58d7e19e9] check-in2023-07-31 15:45:13
  • cdcadcb379Demo bug [e58d7e19e9] check-in2023-07-29 18:11:26
  • 07e8db69b8Proposed fix for [e58d7e19e9]: Upwards compatibility of TclObjTypeHasProc() check-in2023-07-27 13:10:40
  • eea8cdef80Proposed fix for [e58d7e19e9]: Upwards compatibility of TclObjTypeHasProc() check-in2023-07-27 12:57:27
jan.nijtmanswiki2023-07-27 13:22:37

Proposed solution [here]

apnadkarniplain2023-07-27 15:24:33
+1 for Jan's proposal. This was on my mind as well to simplify the check for whether a method is supported so we don't need to check multiple versions on every call.

Although not necessary, the version field could be sizeof(Tcl_ObjType) as Jan had originally suggested (which I was initially against but came around to favour). That's not strictly necessary though as long as the structure keeps monotonically increasing in size with old fields maintained.

/Ashok
griffinmarkdown2023-07-27 17:53:18

When the Tcl_ObjType is changed, a recompile will be necessary. This means the structure and access functions/macros will always be in sync. So I don’t understand the “won’t be recognized” statement.

“If we assume that Tcl_ObjType is a structure, increasing in size with newer versions…”

I think this would be a big mistake. A new version should be free to change the shape of the structure. Don’t constrain the future.

Bottom line is that my changes were intentional.

jan.nijtmanswiki2023-07-27 19:08:32

>Bottom line is that my changes were intentional.

Intentional or not, your changes were a mistake in my view, and one of the reasons I didn't vote a YES for this TIP. But I figured, implementation mistakes can be corrected, so it wasn't enough for me to vote NO either.

>A new version should be free to change the shape of the structure Disagreed! Any change other than adding new fields at the end will cause source incompatibility with already existing extensions.

griffinmarkdown2023-07-27 20:00:42

Tcl_ObjType is a publicly visible structure. Any change to it will require a recompile, even if some changes would allow existing compiled code to work, it is still inappropriate to assume this is ok.

A change to Tcl_ObjType will require a TIP, and be limited to version updates, either minor or major. Any such update requires a recompile. So I don't see how there's an incompatibility here.

Also: "Any change other than adding new fields at the end..."

There's no guarantee that the address after the end in the older compiled code is a valid address.

apnadkarnimarkdown2023-07-28 09:38:28

The rationale of TIP 644 was to permit the Tcl_ObjType to be extended within minor versions of Tcl and allow Tcl extensions to work without needing recompilation. This is how the Tcl_Channel works as Jan described previously.

Now both models of versioning being debated need to support the above requirement.

Suppose in 9.1 a new method Tcl_DictObjLookupProc *dictLookup were added to Tcl_ObjType at the end of the struct with version bumped. What would be needed for the following call to work

TclObjTypeHasProc(objPtr, dictLookup);

Jan's version needs no modification at all that I can see.

On the other hand, the macro in the trunk will fail in its current form as it does not check for V3 and has to be modified. (I will ignore the small bit of work having to update all the TCL_OBJTYPE_VX macros.) A redefinition like

#define TclObjTypeHasProc(objPtr, proc)		\
    (((objPtr)->typePtr				\
      && (   (objPtr)->typePtr->version == 1	\
	  || (objPtr)->typePtr->version == 2))	\
	  || (objPtr)->typePtr->version == 3))	\
     ?	((objPtr)->typePtr)->proc		\
     : NULL)

will not only begin to look ugly as version 4, 5, etc are added and has performance implications on every call, but the bigger problem is that it is not correct.

Consider an extension built for 9.0 that has a V2 Tcl_ObjType loaded into Tcl 9.1. When the macro TclObjTypeHasProc(objPtr, dictLookup) (within Tcl 9.1 core, not the extension) is called, it is going to see version as 2, and thus check objPtr->typePtr->proc which is a garbage location (remembering that the size of structure in the extension is the 9.0 V2 size, NOT the 9.1 size and the 9.0 TCL_OBJTYPE_V2 macro, not the 9.1, would have been used to initialize it).

The problem is that proc values that are defined only in V3 should only be checked if the version is V3 but the preprocessor will not let you do that (remember neither objPtr nor proc are constants) in any way that I can see.

The only way out then is to have a separate macro for each version of Tcl_ObjType - TclObjTypeHasProcV1, TclObjTypeHasProcV2 etc. that do checks specific to the methods defined for that version. This is more work and more complicated for the programmer who now has to track which macro needs to be used for which method call. And for no functional benefit.

Note this problem already exists between V0 V1 V2 except that there are no extensions out there built with older versioned structures.

In terms of limiting changes to Tcl_ObjType to only adding functions at the end, that is a given for exactly the same reasons as above. Unless one is willing to write separate macros for each version. I don't see adding at the end limiting in any significant fashion anyways for many reasons.

Thus I don't currently see the current trunk definition as being workable.

One final note about Brian's comment "There's no guarantee that the address after the end in the older compiled code is a valid address." - I do not see how that macro accesses a potentially invalid address at all. It is only calculating structure offsets which is perfectly legitimate.

/Ashok

jan.nijtmanswiki2023-07-29 20:57:02

Let's actually demo the problem. Suppose there was a Tcl version before 9.0 which only had TCL_OBJTYPE_V1; indexProc and everything after that didn't exist yet. In 9.0, we have:

#   define TCL_OBJTYPE_V1(a) 1, \
	    a,0,0,0,0,0,0 /* Tcl 9 Version 1 */
In this pre-9.0 header it would have looked like this:
#   define TCL_OBJTYPE_V1(a) 1, \
	    a /* pre-Tcl 9 Version 1 */
that means - indexProc and everything after has random content, should never be accessed when running in 9.0.

We can simulate such pre-9.0 V1 extension in 9.0, by changing it to:

#   define TCL_OBJTYPE_V1(a) 1, \
	    a,INT2PTR(1),INT2PTR(1),INT2PTR(1),INT2PTR(1),INT2PTR(1),INT2PTR(1) /* Tcl 9 Version 1 */
If any after indexProc is not accessed it shouldn't matter ..... Well, it does! See: https://core.tcl-lang.org/tcl/vinfo/cdcadcb3792b117e?diff=2. Running this branch, tclsh immediately crashes.

Brian, what more proof do you need? Does Ashok's explanation make sense to you? (to me it does!)

griffinmarkdown2023-07-31 09:47:56

Sorry for the delay in responding. While traveling I've had time but no internet, or internet but no time. :)

I did not remember, or was not aware, that minor versions are required to be binary compatible.
That one is on me.

I still think it will be a mistake to limit the ability to change the shape of the ObjType in the future. Such a change would only happen on a major version change anyway. But I'm willing to accept it.

As for checking multiple versions, I assumed the check would become a switch statement in the future. I probably should have coded it that way to begin with.

The comment about invalid memory reads was a mistake on my part, I was thinking about the version test incorrectly. Please ignore that.

The current trunk is fine as it is since there really is only one version of the ObjType. To reiterate, when Version 3 is created, the test will have to be changed to a switch statement. If the shape changes, using sizeof can no longer be assumed to be unique between versions.

The failure demo is unimpressive. Rewriting the test as a switch can solve that one. However, if using size & offset tests is they way others want to proceed, I'll go along with it.

As an aside, can we change this macro to an inline function? These can be debugged, whereas macros cannot.

jan.nijtmanswiki2023-07-31 15:48:37

> However, if using size & offset tests is they way others want to proceed, I'll go along with it.

Thanks!

It's the simplest solution which adheres to the TIP 644 requirement.

>As an aside, can we change this macro to an inline function? These can be debugged, whereas macros cannot. The second argument of TclObjTypeHasProc() is the name of a struct member. Such kind of thing cannot be done by a C inline function.

I'm really sorry, but I'm fullyaware that you don't realize there is a real problem. I added a (hopefully more convincing, but I'm not fully confident) testcase now.

jan.nijtmans Closed ticket [e58d7e19e9]. [History diff]2023-08-01 06:55:20
pooryorickwiki2024-08-18 11:51:35

This issue only exists because the structure that provides the function lookup table is public. This lookup table should be hidden, and functions should instead be provided as the public-facing interface. This is how it is done in the unchained branch. TIP 644 was unnecessary, and TIP 636 took the wrong path.

Log in (anonymous is fine) to add a comment.