| Ticket UUID: | 1f40aa83c552f597c7aa2e01c7a3a5a2a3fe7c1c | ||
| Title: | crash with CFLAGS=-ftrapv | ||
| Type: | Bug | Created on: | 2024-03-15 12:36:30 |
| Submitter: | jan.nijtmans | Assigned to: | sebres |
| Subsystem: | 16. Commands A-H | Severity: | Severe |
| Priority: | 5 Medium | Last modified: | 2024-03-26 08:44:01 |
| Status: | Closed | Closed by: | jan.nijtmans |
| Resolution: | Fixed | Closed on: | 2024-03-26 08:44:01 |
| Version: | sebres-9-0-clock-speedup-cr2 | ||
| Description: | ||||
|
When running the test-suite with CFLAGS=-ftrapv, 4 "clock" testcases are crashing. Those are: 1) clock-6.9 2) clock-6.10 3) clock-34.16.1a 4) clock-34.16.1b See: https://github.com/tcltk/tcl/actions/runs/8292646669/job/22694304229 For now, I marked those 4 testcases as "knownBug" | ||||
| User Comments: | ||||
sebres added on 2024-03-15 14:20:08:
Fixed in [176cb4ca94597dcc] by suppressing of integer-overflow trapping (at the moment, GCC "trapv" only) for intended pieces; test cases (knownBug) reverted. By the way, imho usage of overflow trapping is bad (too expensive in gcc)... it's better use overflow detecting intrinsics where it's needed (and -fsanitize=signed-integer-overflow produces more efficient code, anyway). jan.nijtmans added on 2024-03-22 09:12:50:
Re-opening, because there really is a bug, discovered by the -ftrapv option. First: $ tclsh9.0 % clock scan 27670116110564327424 -format %s integer value too large to represent % clock scan 27670116110564327424 AbortedThe first answer is correct: 27670116110564327424 in hex is 0x18000000000000000, which - indeed - is too large to represent in 64-bit. Now: $ tclsh9.0 % clock scan 27670116110564327423 -format %s 9223372036854775807 % clock scan 27670116110564327423 Aborted Now, the first command gives an - incorrect - answer: The hex value of 27670116110564327423 is 0x17fffffffffffffff, which is also too large to represent in 64-bit. The second crash - most likely - happened because my build was with CFLAGS=-ftrapv, and an overflow occurred somewhere else. Compare this with Tcl 8.6: $ tclsh8.6 % clock scan 27670116110564327423 -format %s integer value too large to represent % clock scan 27670116110564327423 89170590265200 Explanation: Let's parse the string "27670116110564327423" with function _str2wideInt(). After 19 iterations, the string is parsed almost completely, only the last digit is missing. "prev" has the value 2767011611056432742, which is 0x2666666666666666 in hex, everything fine. Then "val" is multiplied by 10 and the last digit is added. Result is 0x17fffffffffffffff, which overflows and thus is truncated to 0x7fffffffffffffff. This means val > prev, and the function returns TCL_OK, without detecting that an overflow happened. I agree with you that usage of overflow trapping is bad .... in production code. But it's useful for finding bugs otherwise difficult to detect, as proven in this example: Whenever an overflow occurs during multiplication and this overflow is not detected, it might lead to a wrong answer without any indication that it happened. That's why a -ftrapv build is included in our build automation. It should never be used in production builds. My recommendation would be to use the function strtoll(). It's not ideal, because you need to check the overflow situation using errno (or - mayby - just by using *endptr), but it's better than using this (buggy) _str2wideInt(). But ... I'll leave the solution to you. A "knownBug" testcase is [a601eda829827c4c|here], now committed to the tip-688 branch. We don't need to stop the Voting: I'm sure this bug can be fixed (if you don't do it, I will) Thanks! sebres added on 2024-03-22 19:31:46:
Fixed now in [4fa26c98f225a44f]: the overflow check could indeed mistakenly pass in some cases (so basically needs div 10 to do it properly); Additionally I optimized both str2int now, because we don't need to check it for most cases at all (up to 10 char and 18 chars accordingly), thus definitely faster now (bounds P.S. I moved trap-pragma back, because although we may don't have tests right now, but someone can indeed compile it with trapping, and it can definitely overflow in format-scan, so would crash in that (surely obscure) cases. So if it is known, intentional code-piece, made on purpose, why not disable the trap? jan.nijtmans added on 2024-03-23 09:59:58:
> Fixed now in [4fa26c98f225a44f] Sorry, but this fix has one remaining problem and one new problem. The new problem is for --enable-symbols=mem builds, seen on all platforms: ==== var-22.1 leak in localVarName internalrep: Bug 80304238ac FAILED ==== Contents of test case:set end [getbytes] for {set i 0} {$i < 5} {incr i} { doit set tmp $end set end [getbytes] } set leakedBytes [expr {$end - $tmp}] The remaining problem is with CFLAGS=-ftrapv builds. Still some overflow somewhere https://github.com/tcltk/tcl/actions/runs/8400230213/job/23008061558: clock-ivm.test Validity default mode: on Test file error: child killed: SIGABRT clock.test Validity default mode: off Test file error: child killed: SIGABRT Yes, there's progress, but we'r not there yet. jan.nijtmans added on 2024-03-24 14:40:37:
It turns out that the "new" test failures were not actually new: The "sebres-9-0-clock-speedup-cr2" branch had them as well, this branch was never executed with --enable-symbols=mem, therefore it was never noticed. I suspect that the failure is related with the literals in CLOCK_LITERAL_ARRAY, which is not cleaned-up when the interpreter is deleted. That's what the failing testcases are testing for: leaked memory. If I'm right, this failure is a different problem than this ticket, if you prefer it can be handled in a separate ticket. Anyway, it's important to fix this before merging the tip-688 branch to trunk. sebres added on 2024-03-24 15:25:03:
Regarding the mem tests, it's not a leak at all. It goes to internal global cache or storage-GC, which contains few (default 32) last used formats (as well as for scan and format), that allows, reuse them again across all threads and interpreters. I guess to pass such tests we'd need either to disable this cache for that kind of build (e. g. define CLOCK_FMT_SCN_STORAGE_GC_SIZE 0), or to provide some command (unsupported? configure?) forcing GC to clean everything. As for the overflow, how it is possible, Jan? If you'd take a closer look at [4fa26c98f225a44f], every change, I made here, is in the area with disabled trapping. So it is something else (some of your previous commits)? jan.nijtmans added on 2024-03-24 15:55:37:
disabling this cache for --enable-symbols=mem builds is OK for me sebres added on 2024-03-24 16:41:22:
Yes, it was yours [bef135d7a5f32c78] ... Anyway number conversion in free-scan rewritten now in [5696b3f72c6d9d98] with common str2wide primitive. Fix for mem-build follows. jan.nijtmans added on 2024-03-24 17:58:45:
> Fix for mem-build follows. I don't know if more is going to follow, but [c0566e020d546de2|this commit] doesn't help :-( Review: please don't use the "register" keyword. It's going to be removed from the C/C++ standard, since modern optimizers can figure it out by themselves. sebres added on 2024-03-24 22:32:07:
> please don't use the "register" keyword It was a cherry-pick and I simply missed to remove it (overlooked). As for mem-debug build, I'll try to find out what is going on. sebres added on 2024-03-25 20:24:25:
@Jan, the mem-leak was introduced by your move of configure to unsupported [06b7ba18bdd9c2c3] (reason: missing clean-up and therefore decreased refCount). jan.nijtmans added on 2024-03-26 08:44:01:
Indeed, mea culpa. Confirming, it's fixed now. Many thanks! | ||||
