Re: how to avoid a memset() optimization

"Lars Duening" <lars@bearnip.com>
12 Nov 2002 14:05:00 -0500

          From comp.compilers

Related articles
how to avoid a memset() optimization fwai@rsasecurity.com (Francis Wai) (2002-11-07)
Re: how to avoid a memset() optimization lars@bearnip.com (Lars Duening) (2002-11-08)
Re: how to avoid a memset() optimization alexc@world.std.com (Alex Colvin) (2002-11-08)
Re: how to avoid a memset() optimization fjh@cs.mu.OZ.AU (Fergus Henderson) (2002-11-12)
Re: how to avoid a memset() optimization christian.bau@freeserve.co.uk (Christian Bau) (2002-11-12)
Re: how to avoid a memset() optimization lars@bearnip.com (Lars Duening) (2002-11-12)
Re: how to avoid a memset() optimization cgweav@aol.com (Clayton Weaver) (2002-11-12)
Re: how to avoid a memset() optimization n2102139816.ch@chch.demon.co.uk (Charles Bryant) (2002-11-13)
Re: how to avoid a memset() optimization dobes@dobesland.com (Dobes Vandermeer) (2002-11-13)
Re: how to avoid a memset() optimization fjh@cs.mu.OZ.AU (Fergus Henderson) (2002-11-13)
Re: how to avoid a memset() optimization jvorbrueggen@mediasec.de (Jan C. =?iso-8859-1?Q?Vorbr=FCggen?=) (2002-11-13)
Re: how to avoid a memset() optimization usenet-1ugeabe@qeng-ho.org (Arthur Chance) (2002-11-13)
[6 later articles]
| List of all articles for this month |

From: "Lars Duening" <lars@bearnip.com>
Newsgroups: comp.compilers
Date: 12 Nov 2002 14:05:00 -0500
Organization: Compilers Central
References: 02-11-030 02-11-040
Keywords: C, design
Posted-Date: 12 Nov 2002 14:05:00 EST

Alex Colvin <alexc@world.std.com> wrote:


> "Francis Wai" <fwai@rsasecurity.com> writes:
>
> ...the case of a memory scrub optimized away by the compiler...
>
> >Various suggestions have been made, such as declaring the variable
> >volatile and having a scrub memory function in a file of its own. I'm
> >wondering if there are better ways such as telling the compiler not to
> >optimize away a function call.
>
> >[Declaring the array volatile is the right way to do it. The reason
> >volatile exists is to tell the compiler not to do otherwise valid
> >optimizations. -John]
>
> I hesitate to contradict the master, but I vote against 'volatile" for
> key[]. ...
>
> You want the compiler to assume a reference to key[] after memset(),
> which is what you're assuming when you worry about someone seeing
> it. Try declaring key[] static or external instead. That warns the
> compiler that you're assuming a lifetime beyond main().


All these suggestions have in common that they try to use language
features in order to achieve a meta-language effect, while relying on
particular compiler implementations (in theory a compiler with global
lifetime analysis could optimize away the code to clear a static key[]
as well).


I think the lesson to be learned here is that compiler writers would do
well to give programmers some control over the compiler mechanics not
covered by the language. In this case, a construct


#pragma eliminate_dead_code=no
      memset(key, 0, sizeof key);
#pragma eliminate_dead_code=restore


would state the programmer's intentions far clearer than any 'volatile'
construct, and also allow other optimizations still be performed on the
key.
[I don't see what the difference is between "volatile" and "don't
eliminate dead code". Volatile exists precisely to tell the compiler
that code that might appear to be dead isn't. -John]



Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.