Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2014-10-09
16:29 • Closed ticket [2184717fff]: unaligned malloc() -> Tcl panic plus 8 other ... artifact: 19c1d1f44e user: dgp
2008-10-25
00:10 • Ticket [2184717fff]: 4 changes artifact: 114096f6e2 user: cc_benny
2008-10-24
23:48 • Ticket [2184717fff]: 4 changes artifact: 2d154227e7 user: dkf
22:11 • Ticket [2184717fff]: 4 changes artifact: 5f004db29c user: dgp
22:02 • Ticket [2184717fff]: 4 changes artifact: 76a068a898 user: cc_benny
2008-10-21
15:28 • New ticket [2184717fff]. artifact: 3f9364b0dc user: dgp

unaligned malloc() -> Tcl panic

Closed / Invalid2184717fffffffffffffffffffffffffffffffffCreated 18 years agoUpdated 12 years ago
Descriptionplain2008-10-21 15:28:14
Testing memory hygiene in a program that
embeds Tcl on an x86_64 system with
ElectricFence, we encountered panics
in the Tcl library.

The problem boils down to this.  Tcl's
Tcl_Alloc() routine (and the ckalloc()
form) is documented to return a pointer
aligned for any purpose.  Some of the
Tcl source code is written assuming this
to be true.

Unfortunately, when malloc() returns a
pointer that is misaligned for some
purposes (notably for holding a pointer
value), the TclpAlloc() implementation
in tclThreadAlloc.c propagates that
misalignment up to the pointer returned
by ckalloc() leading to panics in the
code that assumes this cannot happen.

Misaligned malloc() returns is one of
the hazards introduced by ElectricFence
testing.

Since Tcl_Alloc() is documented to return
an aligned pointer, and it cannot always
be assumed that malloc() will cooperate,
it would make Tcl more robust, and more
useful for testing in partnership with
ElectricFence, if the allocator in
tclThreadAlloc.c included safeguards to
prevent misalignment from bubbling up
from below.
cc_bennyplain2008-10-24 22:02:19
Are you sure you have the right culprit here?  What you
describe sounds like a bug in ElectricFence or maybe
somewhere else.

> Tcl's Tcl_Alloc() routine (and the ckalloc() form) is
> documented to return a pointer aligned for any purpose.

This is the same guarantee that malloc gives.  I checked the
C standard as well as my local man pages (on Mac OS X) about
this.  See also
<http://www.opengroup.org/onlinepubs/009695399/functions/malloc.html>.

> Unfortunately, when malloc() returns a pointer that is
> misaligned for some purposes (notably for holding a
> pointer value)

That implementation of malloc would be broken.

Note that there are no simple functions in standard C that
can be used to ensure alignment and that a portable
implementation to do this is non-trivial (from the POV of
the rather generic C standard at least).  A malloc that does
not ensure alignment would be unreasonable or even useless.
dgpplain2008-10-24 22:11:47
By that standard, yes, 
ElectricFence is "broken".

However, EF is a useful tool,
and this aspect of its
brokenness is essential to
how it functions.

If Tcl can be revised with
reasonable effort and without
significant negative impact
so that it tolerates this kind
of brokenness, that would be
desirable.
dkfplain2008-10-24 23:48:43
Sounds to me like EF is broken x86_64. A shame. Report it to the EF people and move on. (If their response is "we don't support EF on that architecture" then we'll just have to live without EF on x86_64.)
cc_bennyplain2008-10-25 00:10:34
> However, EF is a useful tool,

I'm sure it is.  I haven't used it before, but I have heard
about it.

> and this aspect of its brokenness is essential to how it
> functions.

I just downloaded ElectricFence to understand what you mean.
From the manual page it seems that this exact issue is
addressed by its configuration parameter EF_ALIGNMENT and
the section in the manual "WORD-ALIGNMENT AND OVERRUN
DETECTION".

To counteract the problem posed in that section, Tcl_Alloc
could round up every allocation to a multiple of
sizeof(void*) before calling malloc.  OTOH setting
EF_ALIGNMENT=8 seems to have the exact same effect (on a 64
bit system) without requiring changes to Tcl.  That seems to
me to be the better option.

Or did you have some other change in mind that is not just
equivalent to EF_ALIGNMENT=8?
dgp Closed ticket [2184717fff]. [History diff]2014-10-09 16:29:13
Log in (anonymous is fine) to add a comment.