Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2002-09-25
15:52 • Closed ticket [613117ffff]: Memory corruption (in expr?) plus 6 other change... artifact: ca1ce1cbac user: dkf
15:39 • Open ticket [613117ffff]. artifact: 073796a5ff user: jkbonfield
05:20 • Ticket [613117ffff]: 4 changes artifact: 9517c49afb user: techentin
2002-09-24
21:29 • Pending ticket [613117ffff]. artifact: 17ea72fa38 user: dkf
20:34 • Ticket [613117ffff]: 6 changes artifact: 7eafa16cdd user: jkbonfield
16:31 • Closed ticket [613117ffff]. artifact: a12cc13ce2 user: dkf
16:12 • Ticket [613117ffff]: 4 changes artifact: d370d69361 user: dkf
16:04 • Ticket [613117ffff]: 4 changes artifact: 121e9b5213 user: dkf
15:59 • Ticket [613117ffff]: 4 changes artifact: 489b70a958 user: dkf
15:47 • Ticket [613117ffff]: 6 changes artifact: ceaf15b5a7 user: dkf
04:33 • Ticket [613117ffff]: 4 changes artifact: 427b781217 user: pspjuth
03:50 • Ticket [613117ffff]: 4 changes artifact: 1604d8617c user: techentin
2002-09-23
23:53 • Ticket [613117ffff]: 4 changes artifact: 191ad05479 user: jkbonfield
23:43 • Ticket [613117ffff]: 6 changes artifact: 7556d29e2e user: hobbs
23:24 • Ticket [613117ffff]: 4 changes artifact: 640d3947f0 user: jkbonfield
17:42 • Ticket [613117ffff]: 4 changes artifact: c7b5ca70a1 user: jkbonfield
16:36 • Ticket [613117ffff]: 4 changes artifact: 7fc0a6593f user: jkbonfield
08:50 • New ticket [613117ffff]. artifact: f4a39f00b9 user: jkbonfield

Memory corruption (in expr?)

Closed / Fixed613117ffffffffffffffffffffffffffffffffffCreated 24 years agoUpdated 24 years ago
Descriptionplain2002-09-23 08:50:13
The following script produces an incorrect output
("ld" instead of "150" for the last line) when run on a
Compaq personal workstation 433au (Alpha processor) 
running Digital Unix 4.0E. My window manager is afterstep.

set tk_strictMotif 1
tk_getOpenFile; #then hit cancel
puts [expr {double(150)/100}]
puts [expr 150]

I'm currently trying to analyse the bug with purify on Solaris 
and memdebug on the alpha. I'll post any suitable debug 
info here.
jkbonfieldplain2002-09-23 16:36:54
Logged In: YES 
user_id=154581

I've now tried this with --enable-memdebug on the Alpha 
system. It triggers no response (assuming I'm using it 
correctly):

% bash$ /tmp/ins/bin/tclsh8.4 
% package require Tk
8.4
% memory validate on
% set tk_strictMotif 1
1
% tk_getOpenFile
% puts [expr {double(150)/100}] 
1.5
% puts [expr 150] 
ld
% 

I've also now built it on Solaris 8 using -DPURIFY and tested 
under purify. This greatly reduces the number of warnings to 
just a few dlopen issues (which are probably false).
jkbonfieldplain2002-09-23 17:42:02
Logged In: YES 
user_id=154581

Now I'm getting somewhere. Still no useful debug output from 
the memory checkers (which may imply an algorithmic bug?), 
but I can replace the tk_getOpenFile with a one line procedure 
calling grid. It has to be a procedure. The -minsize parameter 
in the grid is the number which becomes cached incorrectly. Eg:

package require Tk 
proc bug {} { 
    grid columnconfig . 0 -minsize 10 
} 
bug 
puts [expr {double(10)}] 
puts [expr 10] 
exit 

Fortunately this is now sufficiently short to start low level 
debugging...
jkbonfieldplain2002-09-23 23:24:32
Logged In: YES 
user_id=154581

It's not a memory corruption, but a sprintf format bug. The
TCL_LL_MODIFIER gets set to ll, which leads to a sprintf of 
%lld and this in turn leads to printing up of "ld" (as Digital Unix 
does not support this,  and neither does it support lltostr).

Digital Unix, running on an Alpha, has long as 64bits. %d is for 
32-bit integers and %ld for l64-bit longs. This is the nice and 
easy case, which other OSes have broken away from with 
their long long hacks. I need to figure out why the automatic 
type detection in tcl.h is chosing the wrong macro, but once 
I've done that I'll submit a patch for testing (I cannot test it on 
linux/alpha).

What I cannot figure out from all this is quite how come this 
bug required the black magic invocations I listed at the start to 
trigger it. Clearly calling double(150) causes a 64-bit version 
of the number and a removal of the string-format copy, but I 
cannot see why this is dependant on 150 being used in an 
argument to grid! Anyway, I'm pleased to finally get 
somewhere with this rather bizarre bug!

James
hobbsplain2002-09-23 23:43:31
Logged In: YES 
user_id=72656

Good analysis.  Forwarding to dkf to fix (his code changes 
IIRC).
jkbonfieldplain2002-09-23 23:53:55
Logged In: YES 
user_id=154581

Ok I said I'd produce a patch, but I think I'm going to break 
things royally if I try!

I see that the correct Makefile defines to get things working 
for me are -DTCL_WIDE_INT_TYPE_IS_LONG 
-DTCL_WIDE_INT=long. In addition to this we need to add a 
definition of TCL_LL_MODIFIER to be "l" for this case. The 
code looks incomplete and when TCL_WIDE_INT_TYPE is 
indeed long this never gets defined. Eg as an (incomplete) 
patch:

*** tcl.h~      Wed Sep 18 15:54:35 2002
--- tcl.h       Mon Sep 23 17:45:20 2002
***************
*** 401,406 ****
--- 401,407 ----
  #   define Tcl_LongAsWide(val)                ((long)(val))
  #   define Tcl_WideAsDouble(val)      ((double)((long)(val)))
  #   define Tcl_DoubleAsWide(val)      ((long)((double)(val)))
+ #   define TCL_LL_MODIFIER            "l"
  #else /* TCL_WIDE_INT_IS_LONG */
  #   ifndef __WIN32__
  #      ifdef HAVE_STRUCT_STAT64

How to define TCL_WIDE_INT_IS_LONG? Looks like tcl.m4 
and the SC_TCL_64BIT_FLAGS macro. This is where I start 
failing, not having enough knowledge of autoconf. I think an 
AC_TRY run of "int main() {exit(sizeof(long)==8);}" may 
suffice, but I'm not certain.
techentinplain2002-09-24 03:50:23
Logged In: YES 
user_id=43494

For what it is worth, I can reproduce this bug on our alpha 
running OSF1 v4.0.  But I also get a number of test suite 
failures, where some numeric value is expected, but the 
string "ld" is returned.  Like this:

==== expr-15.12 ExprCallMathFunc: call registered math 
function FAILED
==== Contents of test case:

    expr T3(21, wide(37))

---- Result was:
ld
---- Result should have been (exact matching):
37
==== expr-15.12 FAILED


Apparently related tests failing (for me) are for binary scan, 
expr, format, scan, interp and slave recursion limits, tell, file 
size, and string is integer/double.  I can send a list, if that is 
helpful.
pspjuthplain2002-09-24 04:33:47
Logged In: YES 
user_id=98900

Looks like a case of object shimmering in
shared literals.

I 
extended the script with some debug info to
see what's going on. 
([info objtype] is something
in my debug version that prints the 
internal object
type):

proc bug {} { 
    grid columnconfig . 0 -
minsize 10 
} 

proc main {} {
    puts "Before bug: [info objtype 
10]"
    bug 
    puts "After bug: [info objtype 10]"
    puts [expr 
{double(10)}] 
    puts "After double: [info objtype 10]"
    puts [expr 10] 

    puts "After expr: [info objtype 10]"
}

proc main2 {} {
    puts 
"\nWithout bug: [info objtype 11]"
    puts [expr {double(11)}] 
    puts 
"After double: [info objtype 11]"
    puts [expr 11] 
    puts "After expr: 
[info objtype 11]"
}

main
main2


Before bug: 
int
After bug: pixel
10.0
After double: wideInt
10
After 
expr: wideInt

Without bug: int
11.0
After double: 
int
11
After expr: int


Grid turns the literal into a "pixel" 
object and
then it seems double() has a preference for 
"wideInt"
when faced with something that isn't an "int",
which 
may or may not be considered a bug.
It sure is a bug though that wide 
integers don't
work properly on your platform, but this shows 
why
it triggers the bug in that strange case.

Does this also 
trigger the bug?
puts [expr wide(10)]
dkfplain2002-09-24 15:47:14
Logged In: YES 
user_id=79902

Definitely sounds like something has come unstuck, probably
in configure.
dkfplain2002-09-24 15:59:19
Logged In: YES 
user_id=79902

Does the following produce the wrong output?
puts [expr wide(5+5)]
dkfplain2002-09-24 16:04:34
Logged In: YES 
user_id=79902

The correct thing to do is to define TCL_WIDE_INT_IS_LONG on
all AXP-based platforms. Why is this not happening here?
dkfplain2002-09-24 16:12:30
Logged In: YES 
user_id=79902

D'oh!  Stupid error on my part that just happened to work on
some platforms.  TCL_WIDE_INT_IS_LONG was never being
defined in tcl.m4, contrary to documentation and
expectation...
dkfplain2002-09-24 16:31:09
Logged In: YES 
user_id=79902

I believe this is fixed in CVS now; please reopen if that's
not the case.

Fix was to add AC_DEFINE(TCL_WIDE_INT_IS_LONG) to tcl.m4 a
couple of lines before the place where TCL_WIDE_INT_TYPE is
defined (it should be fairly easy to spot) and regenerate
configure (for those people who don't just want to synch
with the HEAD.)
jkbonfieldplain2002-09-24 20:34:28
Logged In: YES 
user_id=154581

The CVS change fixes this for gcc, but not for the native cc. 
This appears to pick __int64 as the data type and hence 
starts using %lld again.

Would it be sensible to do the long == 64bits check as the first 
test as this then enables lots of code to be ifdefed out (so 
should take priority over using an alternative type).
dkfplain2002-09-24 21:29:42
Logged In: YES 
user_id=79902

OK, I think I've got it cracked now.  (You've got one mighty
strange compiler, there!)
techentinplain2002-09-25 05:20:42
Logged In: YES 
user_id=43494

The latest CVS head now works fine on my system.  Only 
bug left seems to be bug 574573
jkbonfieldplain2002-09-25 15:39:40
Logged In: YES 
user_id=154581

The configure script seems to work for me now with both DEC cc 
and GNU gcc. Thanks Donal.
dkfplain2002-09-25 15:52:48
Logged In: YES 
user_id=79902

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