public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Alexander Monakov <amonakov@ispras.ru>
To: Xi Ruoyao <xry111@mengyan1223.wang>
Cc: gcc-help@gcc.gnu.org
Subject: Re: About the "compiler bomb"
Date: Fri, 31 Jul 2020 20:10:28 +0300 (MSK)	[thread overview]
Message-ID: <alpine.LNX.2.20.13.2007311957001.2454@monopod.intra.ispras.ru> (raw)
In-Reply-To: <9a195b2c5534156b2154a6c1f4e2e3a73ec979b4.camel@mengyan1223.wang>

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

      reply	other threads:[~2020-07-31 17:10 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-31 16:50 Xi Ruoyao
2020-07-31 17:10 ` Alexander Monakov [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.LNX.2.20.13.2007311957001.2454@monopod.intra.ispras.ru \
    --to=amonakov@ispras.ru \
    --cc=gcc-help@gcc.gnu.org \
    --cc=xry111@mengyan1223.wang \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).