public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* About the "compiler bomb"
@ 2020-07-31 16:50 Xi Ruoyao
  2020-07-31 17:10 ` Alexander Monakov
  0 siblings, 1 reply; 2+ messages in thread
From: Xi Ruoyao @ 2020-07-31 16:50 UTC (permalink / raw)
  To: gcc-help

Hi all,

Today we had a discussion about a "compiler bomb":

    int a[-1u] = {1};

One of my collegues suggests that the compiler should "optimize" it into
something like

    int a[-1u] = {};
    static void __init_a __attribute__((constructor)) {a[0] = 1;}

Is there some reason preventing this kind of translation?
-- 
Xi Ruoyao <xry111@mengyan1223.wang>
School of Aerospace Science and Technology, Xidian University


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: About the "compiler bomb"
  2020-07-31 16:50 About the "compiler bomb" Xi Ruoyao
@ 2020-07-31 17:10 ` Alexander Monakov
  0 siblings, 0 replies; 2+ messages in thread
From: Alexander Monakov @ 2020-07-31 17:10 UTC (permalink / raw)
  To: Xi Ruoyao; +Cc: gcc-help

On Sat, 1 Aug 2020, Xi Ruoyao via Gcc-help wrote:

> Hi all,
> 
> Today we had a discussion about a "compiler bomb":
> 
>     int a[-1u] = {1};
> 
> One of my collegues suggests that the compiler should "optimize" it into
> something like
> 
>     int a[-1u] = {};
>     static void __init_a __attribute__((constructor)) {a[0] = 1;}
> 
> Is there some reason preventing this kind of translation?

This seems to match the intended use of constructors, with the (usual) caveat
that as order of constructors is not completely specified, it's possible for
another constructor to execute earlier than __init_a, in which case it will
see a[0]==0.

Another issue arises if you consider what needs to happen if your example had
'const int' rather than 'int'. The toolchain normally wants to put global const
objects to .rodata section which later becomes part of a not writable segment.
With the constructor, it either needs to give up on runtime memory protection
and emit the array in a writable section, or arrange for special section similar
to .data.rel.ro that can be modified early on by constructors and then changed
to become read-only.

Alexander

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-07-31 17:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-31 16:50 About the "compiler bomb" Xi Ruoyao
2020-07-31 17:10 ` Alexander Monakov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).