Skip to content
Tcl Source Code View Ticket
Login

View Ticket

`make valgrind` detections

Closed / Fixed167e0635db79f05d23d766376f6eed32b1572451Created 3 years agoUpdated 3 years ago
Descriptionplain2024-04-04 23:16:49
==4654== Conditional jump or move depends on uninitialised value(s)
==4654==    at 0x44F4EB: ClockParseFmtScnArgs (tclClock.c:3443)
==4654==    by 0x44FA4A: ClockFormatObjCmd (tclClock.c:3583)
==4654==    by 0x4373A0: Dispatch (tclBasic.c:4608)
==4654==    by 0x437404: TclNRRunCallbacks (tclBasic.c:4624)
==4654==    by 0x436CE3: Tcl_EvalObjv (tclBasic.c:4367)
==4654==    by 0x439464: TclEvalEx (tclBasic.c:5463)
==4654==    by 0x57A182: Tcl_FSEvalFileEx (tclIOUtil.c:1790)
==4654==    by 0x587D26: Tcl_MainEx (tclMain.c:404)
==4654==    by 0x40F3CB: main (tclAppInit.c:94)


...

==4654== Conditional jump or move depends on uninitialised value(s)
==4654==    at 0x551500: CreateHashEntry (tclHash.c:288)
==4654==    by 0x453146: FindOrCreateFmtScnStorage (tclClockFmt.c:800)
==4654==    by 0x453281: Tcl_GetClockFrmScnFromObj (tclClockFmt.c:864)
==4654==    by 0x45740E: ClockGetOrParseFmtFormat (tclClockFmt.c:3132)
==4654==    by 0x457A7D: ClockFormat (tclClockFmt.c:3276)
==4654==    by 0x44FA84: ClockFormatObjCmd (tclClock.c:3596)
==4654==    by 0x4373A0: Dispatch (tclBasic.c:4608)
==4654==    by 0x437404: TclNRRunCallbacks (tclBasic.c:4624)
==4654==    by 0x436CE3: Tcl_EvalObjv (tclBasic.c:4367)
==4654==    by 0x439464: TclEvalEx (tclBasic.c:5463)
==4654==    by 0x57A182: Tcl_FSEvalFileEx (tclIOUtil.c:1790)
==4654==    by 0x587D26: Tcl_MainEx (tclMain.c:404)
==4654==    by 0x40F3CB: main (tclAppInit.c:94)
==4654== 
==4654== Conditional jump or move depends on uninitialised value(s)
==4654==    at 0x551500: CreateHashEntry (tclHash.c:288)
==4654==    by 0x453146: FindOrCreateFmtScnStorage (tclClockFmt.c:800)
==4654==    by 0x453281: Tcl_GetClockFrmScnFromObj (tclClockFmt.c:864)
==4654==    by 0x454FD9: ClockGetOrParseScanFormat (tclClockFmt.c:2080)
==4654==    by 0x4558CD: ClockScan (tclClockFmt.c:2286)
==4654==    by 0x44FCD9: ClockScanObjCmd (tclClock.c:3686)
==4654==    by 0x4373A0: Dispatch (tclBasic.c:4608)
==4654==    by 0x437404: TclNRRunCallbacks (tclBasic.c:4624)
==4654==    by 0x436CE3: Tcl_EvalObjv (tclBasic.c:4367)
==4654==    by 0x439464: TclEvalEx (tclBasic.c:5463)
==4654==    by 0x57A182: Tcl_FSEvalFileEx (tclIOUtil.c:1790)
==4654==    by 0x587D26: Tcl_MainEx (tclMain.c:404)
==4654==    by 0x40F3CB: main (tclAppInit.c:94)


...

==4654== Conditional jump or move depends on uninitialised value(s)
==4654==    at 0x44F4EB: ClockParseFmtScnArgs (tclClock.c:3443)
==4654==    by 0x45136A: ClockAddObjCmd (tclClock.c:4436)
==4654==    by 0x4373A0: Dispatch (tclBasic.c:4608)
==4654==    by 0x437404: TclNRRunCallbacks (tclBasic.c:4624)
==4654==    by 0x436CE3: Tcl_EvalObjv (tclBasic.c:4367)
==4654==    by 0x439464: TclEvalEx (tclBasic.c:5463)
==4654==    by 0x57A182: Tcl_FSEvalFileEx (tclIOUtil.c:1790)
==4654==    by 0x587D26: Tcl_MainEx (tclMain.c:404)
==4654==    by 0x40F3CB: main (tclAppInit.c:94)
==4654== 
==4654== Conditional jump or move depends on uninitialised value(s)
==4654==    at 0x44F4EB: ClockParseFmtScnArgs (tclClock.c:3443)
==4654==    by 0x44FBC4: ClockScanObjCmd (tclClock.c:3660)
==4654==    by 0x4373A0: Dispatch (tclBasic.c:4608)
==4654==    by 0x437404: TclNRRunCallbacks (tclBasic.c:4624)
==4654==    by 0x436CE3: Tcl_EvalObjv (tclBasic.c:4367)
==4654==    by 0x439464: TclEvalEx (tclBasic.c:5463)
==4654==    by 0x57A182: Tcl_FSEvalFileEx (tclIOUtil.c:1790)
==4654==    by 0x587D26: Tcl_MainEx (tclMain.c:404)
==4654==    by 0x40F3CB: main (tclAppInit.c:94)
Referenced by4
  • 62f9f8b56bcherrypick [b73516f7cfccbc9f] to 8.7 - closes [167e0635db]: solves leaks, valgrind test, etc check-in2024-04-17 15:13:00
  • b73516f7cfcloses [167e0635db]: solves leaks, valgrind test, etc check-in2024-04-17 15:03:46
  • 9d7596c138simple attempt to silence valgrind [167e0635db], improved error message by bad seconds (or -base), repair errorCode (index for badOption), remove unneeded code check-in2024-04-06 13:20:29
  • 561cb0ffb2Proposed (partial) fix for [167e0635db]: `make valgrind` detections. Also fixes two constants in tclDate.h (for 32-bit platforms) check-in2024-04-05 12:57:05
jan.nijtmanswiki2024-04-05 13:00:40

This looks like 2 different issues, one of them should be fixed [here]. @sebres, please review.

sebreswiki2024-04-05 14:46:52

I an unsure it meant really that, what you were trying to fix.

Conditional jump or move depends on uninitialised value(s) (tclClock.c:3443), could mean either baseObj or its content (length/bytes).

However I'd try to check it later (if get valgrind to hand).

sebresmarkdown2024-04-05 19:57:32

Oh... now I followed your approach... however I don't think it is well, because now due to an attempt with Tcl_GetIndexFromObj firstly, it would always try to convert numeric seconds (either regular case) to index (together with conversion numeric to str, etc), what was avoided before.

I could suggest this one:

	if (
-	    (baseObj->length == 4 && baseObj->bytes && *(baseObj->bytes+1) == 'n') ||
+	    (baseObj->bytes && baseObj->length == 4 && *(baseObj->bytes+1) == 'n') ||

But I cannot reproduce it - don't see "Conditional jump or move depends on uninitialised value" at all... Everything I see from valgrind is a lot of "X bytes in 1 blocks are still reachable in loss record".

jan.nijtmanswiki2024-04-05 20:57:11

@sebres, did you time the benefit of your optimization? I don't think it really makes a noticeable difference. The first time Tcl_GetIndexFormObj() is called, it will be converted to 'index' type, yes that takes somewhat more time. But second time and later it's already of 'index' type and that's fast.

So, typical code doing a "clock seconds -now" repeatedly should be very fast anyway.

jan.nijtmanswiki2024-04-05 21:01:42

For the integer case, it could help to check the type: If the type is 'integer' it makes no sense to call Tcl_GetIndexFromObj(). That's a quick check that might make a difference

sebresmarkdown2024-04-05 21:37:56

@Jan, of course I meant the regular (integer) case.
One could surely do it with type-check, as you suggest, however in clockmod (8.6 based) there were several int-types (int32, wide), but with that thing I wanted just fast avoid additional shimmer from whatever type (unicode, string with utf-8, future objects like clock-seconds, etc with theoretic fast conversion to integer), since unneeded shimmer is always bad... Thus it was made how it made.
And it is not "optimization", this is very quick and simple trick.

But back to valgrind...
I don't follow the first claim:

To have uninitialized value in that condition, baseObj->length or baseObj->bytes needs to be uninitialized, because baseObj is always initialized either within TclNewObj & co or to NULL by memset in ClockInitFmtScnArgs.

So I don't follow the issue why valgrind may complain.

Also I don't follow the second claim:

To have uninitialized value in CreateHashEntry (tclHash.c:288) from FindOrCreateFmtScnStorage (tclClockFmt.c:800) the last must use strFmt (and related key) uninitialized, but it is obtained by TclGetString(objPtr) in (tclClockFmt.c:777), so this is basically impossible (unless something makes hash-table corrupt and it is rather hPtr->key.oneWordValue in the condition where valgrind complains).

// tclHash.c:

240 static Tcl_HashEntry *
241 CreateHashEntry(
242     Tcl_HashTable *tablePtr,	/* Table in which to lookup entry. */
243     const char *key,		/* Key to use to find or create matching
244
245  				 * entry. */
...
288 	    if ((key == hPtr->key.oneWordValue)

// tclClockFmt.c:

772 static ClockFmtScnStorage *
773 FindOrCreateFmtScnStorage(
774     Tcl_Interp *interp,
775     Tcl_Obj    *objPtr)
776 {
777     const char *strFmt = TclGetString(objPtr);
...
800     hPtr = Tcl_CreateHashEntry(&FmtScnHashTable, strFmt, &isNew);

Bug in valgrind? Bug in 9.0 somewhere? Anyway I don't reproduce it, neither for 8.7 nor 9.0 with my valgrind / gcc:

$ valgrind --version
valgrind-3.19.0
$ gcc --version
gcc (Debian 12.2.0-14) 12.2.0
sebreswiki2024-04-06 14:04:28

@dgp, can you please explain how you get it, Don?
For instance exact build args (configure/make), CFLAGS, etc... And which versions (valgrind, gcc or clang, platform) did you use?
I'm trying desperately to reproduce it and don't see "Conditional jump or move depends on uninitialised value" messages. One can surely rewrite it to avoid that check, but I would like to understand the issue as precise as possible (what exactly causes it, since normally that tcl-objects shall not produce uninitialized values at all, neither literals nor dynamically created objects, so would be nice to know the origin of them).

dkfmarkdown2024-04-09 11:28:52

I suppose the first question is: Which test triggers this behaviour? Being able to trace things from a single test is a lot easier than a whole test file. (If it is many tests, pick one.) If no single test triggers it, it's going to be a lot trickier to hunt down (speaking with experience from TclOO).

dkfmarkdown2024-04-09 15:51:20

Why are you collecting the baseObj in the ClockFmtScnCmdArgs when it's only ever used in the same function that sets it (ClockParseFmtScnArgs())? It's not even being used to pass between multiple calls of the same function. I'm not sure that it's the problem, but such fields definitely can't be the problem if they don't exist...

sebresmarkdown2024-04-09 18:47:11

Why are you collecting the baseObj in the ClockFmtScnCmdArgs when it's only ever used in the same function ...

In my own engine (where I back-ported it from), baseVal (date->seconds) is converted to seconds (and others parts) on demand, e. g. to bypass unneeded conversions through the TZ etc, for instance if the whole date is specified in input string. Also it is used additionally on few other functions, since, on my side, this object is not necessarily an integer but can be clock-seconds (special type), previously got from another clock-scan calls (e. g. to format it or use as another base).
Because it was too complex and also too different (since core-tcl's object can shimmer, cannot be converted from some type to integer without interim string-representation etc), I decided not to provide that logic firstly.

However this mentioned "on demand conversion" can be surely done later (e. g. it could speed-up scans like clock scan "2020-01-01 00:00:00" with totally unneeded base).

Anyway, I don't think it bothers somehow (moreover may help by some deep debug on a conditional BP etc)... And it's definitely not the problem that valgrind complains, because initially opts.baseObj pointer is 0 (by memset via ClockInitFmtScnArgs) and the supposed place where valgrind means it'd be uninitialized is a condition over content of it (bytes & length), which shall be always initialized in properly created tcl-object (no matter it'd be local or belong to some structure).

As already said it is not explainable to me, and I hoped to reproduce it to go deeper, just unsuccessful.
Could you reproduce it with your valgrind, Donal?

dkfplain2024-04-10 08:23:37
> Could you reproduce it with your valgrind, Donal?

I don't have valgrind on this machine at all.
dkfmarkdown2024-04-10 10:43:56

In any case, the hallmarks of this are of a Tcl_Obj* with an uninitialized but non-NULL bytes field; that would be sufficient to explain this. That is a serious bug... but not (necessarily) in the clock code; those appear to all be values associated with an argument supplied to the command.

sebresmarkdown2024-04-10 12:02:51

Yes, that was my assumption too (therefore I wanted to debug that deeper)...
However, before [9d7596c1381ec788] (where Don's valgrind produces the issue) the checks for bytes and length were inverted (length firstly), so if it was not some UB-similar thing by compiler optimization, and was indeed uninitialized length, I tried to rewrite it more similar to macro TclGetString* which firstly checks bytes and seems not bother in valgrind. Although 8.7/9.0 tcl-core initializes bytes in TclNewObj or whatever (where they set to 0 and &tclEmptyString) or by TclInitEmptyStringRep.

sebreswiki2024-04-10 12:07:00

repeated, because of wrong type (Markdown) in previous comment that destroyed it.

Yes, that was my assumption too (therefore I wanted to debug that deeper)...
However, before [9d7596c1381ec788] (where Don's valgrind produces the issue) the checks for bytes and length were inverted (length firstly), so if it was not some UB-similar thing by compiler optimization, and was indeed uninitialized length, I tried to rewrite it more similar to macro `TclGetString*` which firstly checks bytes and seems not bother in valgrind. Although 8.7/9.0 tcl-core initializes bytes in TclNewObj or whatever (where they set to `0` and `&tclEmptyString`) or by `TclInitEmptyStringRep`.

dkfmarkdown2024-04-10 12:56:16

Ah yes, length is only defined at all if bytes is non-NULL, just as internalRep is only defined if typePtr is non-NULL (and then you need to know the implementation of the typePtr to know how to interpret it). Accessing a non-defined field is the sort of thing that valgrind ought to catch.

I think we can assume that that fixes it, in the absence of further info from dgp (specifically what test triggered it and what fossil ID was tested).

sebresmarkdown2024-04-10 14:37:34

Sure, but (as already wrote) it doesn't explain the second case (in CreateHashEntry/FindOrCreateFmtScnStorage) and ...

Ah yes, length is only defined at all if bytes is non-NULL

Well, not really - I did not found any place where length may remain uninitialized (let alone it is always set to 0 by new object).

dgpplain2024-04-10 15:43:06
Much improved results from changes so far.  Here are remaining reports:

$ make valgrind TESTFLAGS="-file clock.test -verbose bpste"
...
---- clock-5.1.vm:0 start
==32290== Conditional jump or move depends on uninitialised value(s)
==32290==    at 0x5514D3: CreateHashEntry (tclHash.c:288)
==32290==    by 0x453119: FindOrCreateFmtScnStorage (tclClockFmt.c:799)
==32290==    by 0x453254: Tcl_GetClockFrmScnFromObj (tclClockFmt.c:863)
==32290==    by 0x4573E1: ClockGetOrParseFmtFormat (tclClockFmt.c:3131)
==32290==    by 0x457A50: ClockFormat (tclClockFmt.c:3275)
==32290==    by 0x44FA4B: ClockFormatObjCmd (tclClock.c:3599)
==32290==    by 0x4373A0: Dispatch (tclBasic.c:4608)
==32290==    by 0x437404: TclNRRunCallbacks (tclBasic.c:4624)
==32290==    by 0x436CE3: Tcl_EvalObjv (tclBasic.c:4367)
==32290==    by 0x439464: TclEvalEx (tclBasic.c:5463)
==32290==    by 0x57A155: Tcl_FSEvalFileEx (tclIOUtil.c:1790)
==32290==    by 0x587CF9: Tcl_MainEx (tclMain.c:404)
==32290==    by 0x40F3CB: main (tclAppInit.c:94)
==32290== 
++++ clock-5.1.vm:0 PASSED
...
---- clock-28.1.vm:0 start
==32290== Conditional jump or move depends on uninitialised value(s)
==32290==    at 0x5514D3: CreateHashEntry (tclHash.c:288)
==32290==    by 0x453119: FindOrCreateFmtScnStorage (tclClockFmt.c:799)
==32290==    by 0x453254: Tcl_GetClockFrmScnFromObj (tclClockFmt.c:863)
==32290==    by 0x454FAC: ClockGetOrParseScanFormat (tclClockFmt.c:2079)
==32290==    by 0x4558A0: ClockScan (tclClockFmt.c:2285)
==32290==    by 0x44FCA0: ClockScanObjCmd (tclClock.c:3689)
==32290==    by 0x4373A0: Dispatch (tclBasic.c:4608)
==32290==    by 0x437404: TclNRRunCallbacks (tclBasic.c:4624)
==32290==    by 0x436CE3: Tcl_EvalObjv (tclBasic.c:4367)
==32290==    by 0x439464: TclEvalEx (tclBasic.c:5463)
==32290==    by 0x57A155: Tcl_FSEvalFileEx (tclIOUtil.c:1790)
==32290==    by 0x587CF9: Tcl_MainEx (tclMain.c:404)
==32290==    by 0x40F3CB: main (tclAppInit.c:94)
==32290== 
++++ clock-28.1.vm:0 PASSED
...


At the end are several warnings about read and write after free
by ClockFmtObj_FreeInternalRep()
sebreswiki2024-04-10 16:01:35

I still don't get it, Don...

How look the configure options? zip-enabled? memdebug?

What is CC (gcc or clang), CFLAGS etc?

Which versions (platform, gcc/clang, valgrind)? I provided my versions below.

dgpplain2024-04-10 16:43:56
Also involved is test clock-1.3.

The common feature is a -format value that is an empty string literal.
sebresmarkdown2024-04-10 22:44:27

I could surely try to fix it blind again and again...

But why can't I reproduce it?

Here is a gist how I made it with an output 1.txt generated by test.

Nothing what you observe.

sebresmarkdown2024-04-10 23:12:54

If I try with symbols=mem, I get this summary (but also no one error you posted before):

$ ../configure --enable-64bit --enable-symbols=mem
$ make valgrind TESTFLAGS="-file clock.test -verbose bpste" >1.txt 2>&1
...

==11046== LEAK SUMMARY:
==11046==    definitely lost: 0 bytes in 0 blocks
==11046==    indirectly lost: 0 bytes in 0 blocks
==11046==      possibly lost: 0 bytes in 0 blocks
==11046==    still reachable: 3,960,225 bytes in 25,518 blocks
==11046==         suppressed: 0 bytes in 0 blocks
==11046== 
==11046== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

And also tried both with and without zipfs, for x64 and x86, for *nix & windows in several boxes, it changes nothing on the result...

I don't get it, simply.

chwplain2024-04-10 23:34:42
Not that I in detail followed your conversation but
am impressed of the puzzle which comes up.

It seems to be a good path to define an exact testing
ground for both (and any) of you in terms of

a) which linux
b) which cpu and of course
d) finally forget c) (that's me).
dgpplain2024-04-12 19:25:46
See branch dgp-bug-167e0635db for a patch to remove the last
round of "Conditional jump" reports from valgrind.

Now looking into the read/write after free troubles....
dgpplain2024-04-12 19:38:51
Applied a crude protection against accessing the fss storages
after a finalization pass has already freed them.
dgpplain2024-04-12 19:55:25
Two memleaks also plugged on that branch.
sebresmarkdown2024-04-12 19:58:13

Related to 0e273ad998f16100

Tcl_HashEntry::key is a union. So the key.string and key.oneWordValue pointing to the same memory.

The next line (with memcpy) overwrites the same memory block (size is always larger than 1):

    hPtr->key.oneWordValue = 0; /* See Ticket [167e0635db] */
    memcpy(&hPtr->key.string, string, size);

Your valgrind seems to be broken, Don.

Or it is some weird constellation (strange alignment or similar thing) on this system, so sizeof and co going confused somewhere. What platform is it actually?

However size is just strlen + 1. Therefore I can only repeat, that valgrind seems to claim nonsense here.

dgpplain2024-04-12 20:19:38
I see the problems arise when the format string is an empty string.
Tests clock-1.3, clock-5.1, clock-28.1

When the format string is empty, the value of size is 1.  It is not
larger than 1.  Does that explain anything?

I would expect the memcpy to copy a single NUL byte into the
hPtr->key.string field.

I don't know whether that should cause valgrind to think that
the multi-byte hPtr->key.oneWordValue field is initialized.

This is a CentOS Linux 7.9.2009 system.  The valgrind program
I am running claims to be version valgrind-3.15.0
sebresmarkdown2024-04-12 20:23:13

Hmm...

CreateHashEntry may have a bug here.
Or at least it is strange behaviour.

And it doesn't complain in AllocStringEntry just because of memset here.

It could be partially mea culpa - blame shows 273d53b2e7567430 - and it is correct for pointers (or Tcl_Obj), but for key.string it doesn't look correct to me. Shame on me (my fork shows completely different algorithm, so I overlooked something by backporting).

jan.nijtmanswiki2024-04-12 20:26:30

What if size < sizeof(hPtr->key.oneWordValue)?

Then, not the whole union is initialized, that's what valgrind is reporting. sizeof(hPtr->key.oneWordValue) will normally be 4 or 8.

dgpplain2024-04-12 20:28:56
The routine ClockFmtScnStorageAllocProc also has a memset() call.

I did not attempt to review the calculations of allocsize.
sebresmarkdown2024-04-12 20:29:30

Oh yes... I had a flags by Tcl_HashTable, which allows the direct key compare, otherwise only compares via compareKeysProc.
Tcl-Core doesn't have it (and it's public interface)...

Looking now how one can fix that bug without that.

sebresplain2024-04-12 20:31:06
It is definitely a bug in tclHash, not in tclClockFmt. So I'll try to fix it (without mentioned flag for hash-table).
sebreswiki2024-04-12 22:24:05

The bug in tclHash is ultimately fixed now across all affected branches:
8.5 - [969a185ea4d19037] to 9.0 - [c3c498a6a853f3b8].

This was really heavy bug - create/find hash entry had mistakenly compare pointer with a content, invalid for all non-pointer hash-types, containing data (strings/arrays) directly in the hash key.

Well, fortunately Dons valgrind was better than my, and finally it came to light... And thanks for your obstinacy, Don.

I'm also sure the complain would disappear now (also without [0e273ad998f16100]).

> The routine ClockFmtScnStorageAllocProc also has a memset() call.

This memset only resets the part responsible for ClockFmtScnStorage... So after all by empty format (1 byte long, NTS) it contains 3 (x86) or 7 (x64) uninitialized bytes in key. (And it was a good thing, otherwise that bug would be never found and caused extremely rare heisenbug on some constellation for containing data key types).

> I did not attempt to review the calculations of allocsize.

Everything must be correct there, but I could rewrite it more clear.

dgpplain2024-04-15 17:08:55
I am still trying to digest the full impact of this change, but I can
report that it seems to have corrected the problem seen in
Ticket [167e0635db] as well.
sebresmarkdown2024-04-17 15:20:08

merged (in 9.0 and backported to 8.7), thus close it for now

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