Tcl Source Code

View Ticket
Login
Ticket UUID: db4f2843cdb28c27fddf511567bc9287484852ae
Title: Sporadic segfault (BO) using tclIO
Type: Bug Created on: 2023-12-05 22:21:04
Submitter: sebres Assigned to: nobody
Subsystem: 10. Objects Severity: Critical
Priority: 5 Medium Last modified: 2024-08-28 13:42:40
Status: Closed Closed by: pooryorick
Resolution: Fixed Closed on: 2024-08-28 13:42:40
Version: any
Description:

I saw this segfault very sporadic previously (as an after-effect of a buffer overrun somewhere by IO operations with objects), just it was like a heisenbug and I never caught it. Unless I implemented some module using Tcl_ReadChars with appendFlag = 1, and with that command I could reproduce this BO relative simply. Since the segfault is almost always a result of buffer overrun, I'll not provide the callstack here (which is mostly caused by corrupt object and thus broken objects heap in thread-alloc).

I already fixed it on my side, so will backport it to new 8.6-based branch now.

User Comments:
sebres added on 2023-12-05 23:11:11:

Fixed in [cc90065303e11f36].
The reason for this tricky bug is that Tcl_AppendToObj always prefers unicode representation to utf-8 if object has unicode (in opposite to Tcl_SetObjLength which always prefer utf-8 if it is there), and so Tcl_AppendToObj would rather enlarge the unicode representation of object (basically reserve place for unicode data) if it shimmers to real unicode somewhere by processing outside, however the ReadChars (that itself always works with utf-8 due to external to utf-8 conversions by encoding etc) expects that the buffer of utf-8 gets enlarged instead, what doesn't happen by backwards utf-8 regeneration in followed TclGetString.
It would only work with Tcl_AppendToObj if we working on completely new or empty object (e. g. appendFlag = 0 which would clear it and so tries to reuse the buffer) or it got duplicated (if shared) previously, and duplication prefers utf-8 representation, etc.
It was really rare, because really tricky, since it expects certain constellation (like appendFlag = 1 and "shimmering" to real unicode) to get reproduced. Anyway fixed now, waiting for CI.


jan.nijtmans added on 2023-12-06 13:39:58:

Looks good to me. Many thanks!


sebres added on 2023-12-06 17:27:51:

Thus merged to 8.6 in [c5a68de09a4b3fc4]


oehhar added on 2024-01-22 15:10:12:

This fix has a big performance impact on large files, see [da16d15574]. *Tcl_SetObjLength* does not allocate in advance like *Tcl_AppendToObj* and thus makes one realloc per 4096 bytes read.

The issue fixed in this ticket may only be triggered, if an extension calls *Tcl_ReadChars* with an object having a string representation in unicode. This never happens in the core.

Thus, the fix is reverted for now by commit [7a188dd1c9].

Nevertheless, the issue persists for users of the C interface.


sebres added on 2024-01-22 15:13:08:

The fix [cc90065303e11f36] is reverted in [7a188dd1c9b28f99] because of performance regression (due to slower buffer growth resulting to too many realocks).


jan.nijtmans added on 2024-01-22 22:34:37:

Would [100933523c937604|this] help?

Explanation: If we know that Tcl_AppendToObj() doesn't behave well with tclStringType, and we know also that it has a string representation (because Tcl_GetStringFromObj() is called just before), we can simply remove the IntRep, can we?

Just an idea ....., quite harmless.

@sebres, would this help in your situation?


sebres added on 2024-01-23 00:35:43:

No, it wouldn't help. Moreover it'd completely regress (would probably have still worse performance than my initial fix) because would "remove" previously enlarged buffer on every iteration.

Even if the object is utf-8 append makes a string, that doesn't have unicode, but contains info in stringPtr about the bytes buffer. The removal of internal representation would simply cause that info like stringPtr->allocated get lost.

Basically, the simplest fix could be to declare AppendUtfToUtfRep as internal TclAppendUtfToUtfRep and call it instead of Tcl_AppendToObj.


kevin_walzer added on 2024-01-23 03:04:02:
This discussion has been very helpful because it helped me track down a crashing bug in tcltls. Thank you!

oehhar added on 2024-01-23 08:41:15:

Great Kevin. So, this is a real issue confirmed by two Wizards. Could you imagine a test case?

I suppose, we need a piece of C-code which crafts the object passed to Tcl_ReadChars in a certain way.

Take care, Harald


sebres added on 2024-01-26 22:48:43:

I added performance regressions tests for read in [492aaec52129ccbc], that illustrates pretty well the quadratic regression in absence of append (doubling of bytes in internal object used for read on every iteration).

The result of execution (original 8.6 vs. previous fix without append) can be found as diff in gist.github.com/sebres/0875a4a75c489b13b6cba8fc9adcc99a.

As one indeed can see the regression is pretty obvious (because of too many reallocs), but...

It looks strange if one try to use larger buffersize:

1. the regression is still quadratic even if large buffer size is used (1MB instead of 4KB), however it becomes a bit faster with larger buffersize.

2. still worse is that even by original version with append, the usage of large buffersize degrades (whether I'd expect vice versa result), for example reading 10M blocks it takes 37ms/# by 4KB vs. 104ms/# by 1MB.

So it looks like command read or rather Tcl_ReadChars internally use small chunks (a bit unexpected for my taste) or it has another issue yet (not directly related to the "append" or "realloc" predicament).


sebres added on 2024-01-29 11:12:17:

Found the discrepancy by 4K vs. 1M buffer size - it was interim flush on output side (fixed in [2dce470bdfdd9f25]) - now it works as expected: there is almost no bottleneck with large buffer anymore.
Also updated gist with new result of performance test.


sebres added on 2024-01-29 14:05:10:

BO ultimately fixed in [37e659d589622e28], and also like before function ReadChars follows TCL STRING GROWTH ALGORITHM.
As expected the tests chan.perf.tcl doesn't show any regression too (it's even faster now).


oehhar added on 2024-01-29 14:21:06:

Looks like a clean solution with all pitfall corner cases fixed.

Will you merge to core-8-branch and main branches ?

Thank you and take care, Harald


sebres added on 2024-01-29 14:40:15:
Well, I wanted wait for a CI firstly (since at some point only few branches are covered in CI, other branches would not trigger GHA at all)... 
If all tests passed, I'd merge it.

oehhar added on 2024-01-29 14:52:56:
Great. Please consider, that others may be in troubble, as they can not merge other stuff without mergin your change. Or they have to back it out.
If you want to run a CI test, I think, there are possibilities to test a branch, while modifying a certain CI instruction file.

Thank you !
Harald

sebres added on 2024-01-29 15:43:56:

they can not merge other stuff without mergin your change.

One doesn't necessarily need to merge 8.6 to 8/main right now (can be made later).

Or they have to back it out.

Why should they? If needed to merge 8.6 to 8/main, then it can surely be done with my change.

The changes are simple and obvious, and even if it'd conflict somewhere, they can be solved very simply (somehow it belongs to the work with distributed SCM repos, so everyone should be able to do that).

But OK, it is done now.

If you want to run a CI test, I think, there are possibilities to test a branch, while modifying a certain CI instruction file.

Too inconvenient... Also guess must do that twice (depends on main flow config) and in worse case wait for fossil to git two times. And after all revert config backwards. Previously I had my own fossil2git, so could test in GHA on forked repo, but they are too far apart now.


oehhar added on 2024-01-29 16:14:51:
THank you for your comprehension, I appreciate !
Harald

pooryorick added on 2024-08-25 08:23:18:

sebres: Could you refrain from using non-standard acronyms like "BO" in your communications, and particularly in tickets? You use them frequently enough that they become a barrier to readability.


jan.nijtmans added on 2024-08-25 10:58:12:

@Nahan, don't BO (Blame Others), not even BO (Barack Obama) if you don't know what BO (buffer overflow) means


pooryorick added on 2024-08-28 13:42:40:

Here are some others to noodle on: CP ([8f89e2e059aafa1c]), ,ISC ([fa6bf38d0728fbc7]), JD [16e4fc30961de5dc], NTS ([8c4082563d6a7ebd]), RC ([f583715154c6f33d]), SF ([f583715154c6f33d]), SO ([fec0c17d39db02fa]).

Of course it's possible to figure all these out, but it takes time and impacts readability, and even for the ones we all know it takes time to verify that it means what we think it means in that context. BO approves of this message.