public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "krystalgamer at protonmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug target/103882] New: Register corruption in ASM only functions when optization is -O2/-Os/-O3
Date: Fri, 31 Dec 2021 17:11:10 +0000	[thread overview]
Message-ID: <bug-103882-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103882

            Bug ID: 103882
           Summary: Register corruption in ASM only functions when
                    optization is -O2/-Os/-O3
           Product: gcc
           Version: 10.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: krystalgamer at protonmail dot com
  Target Milestone: ---
              Host: x86_64-linux-gnu
            Target: mips-linux-gnu
             Build: x86_64-linux-gnu

When a function is only composed of an `asm` statement if the optimizations are
at least -O2 it will make it assume none of the registers will be tainted.


Example code:

```
int is_first_char_a(const char *f){
        return f[0] == 'a';
}

void fail(){
        asm __volatile__ (
                "li $a0, 0x60606060\n"
                );
}

int example(char *c){
        fail();
        return is_first_char_a(c);
}

void __start(){}
```


Compiled with `-nostdlib -O1`, generates the following code:

```
00400180 <example>:
  400180:       27bdffe0        addiu   sp,sp,-32
  400184:       afbf001c        sw      ra,28(sp)
  400188:       afb00018        sw      s0,24(sp)
  40018c:       00808025        move    s0,a0
  400190:       0c10005d        jal     400174 <fail>
  400194:       00000000        nop
  400198:       82020000        lb      v0,0(s0)
  40019c:       38420061        xori    v0,v0,0x61
  4001a0:       2c420001        sltiu   v0,v0,1
  4001a4:       8fbf001c        lw      ra,28(sp)
  4001a8:       8fb00018        lw      s0,24(sp)
  4001ac:       03e00008        jr      ra
  4001b0:       27bd0020        addiu   sp,sp,32
```


Compiled with `-nostdlib -O2`, generates the following code:

```
00400180 <example>:
  400180:       3c046060        lui     a0,0x6060
  400184:       34846060        ori     a0,a0,0x6060
  400188:       80820000        lb      v0,0(a0)
  40018c:       38420061        xori    v0,v0,0x61
  400190:       03e00008        jr      ra
  400194:       2c420001        sltiu   v0,v0,1
```


As can be seen before -O1 levels of optimization the compiler saves $a0 in $s0
and then restores it. -O2 level and beyond causes the compiler to assume that
a0 is preserved.



>From what I can tell this might be because the optimizer doesn't take into
consideration ASM statements(which is good), but it makes wrong assumptions. A
possible solution would be to save the caller-saved registers whenever a
function with an ASM statement is called.

             reply	other threads:[~2021-12-31 17:11 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-31 17:11 krystalgamer at protonmail dot com [this message]
2021-12-31 18:53 ` [Bug target/103882] " pinskia at gcc dot gnu.org
2021-12-31 20:48 ` krystalgamer at protonmail dot com
2021-12-31 20:51 ` pinskia at gcc dot gnu.org
2021-12-31 20:51 ` pinskia at gcc dot gnu.org
2021-12-31 22:36 ` jakub at gcc dot gnu.org
2022-01-01 12:45 ` krystalgamer at protonmail dot com
2022-01-01 14:05 ` jakub at gcc dot gnu.org
2022-01-01 14:11 ` jakub at gcc dot gnu.org
2022-01-01 15:06 ` krystalgamer at protonmail dot com
2022-01-01 16:37 ` jakub at gcc dot gnu.org
2022-01-01 19:19 ` krystalgamer at protonmail dot com
2022-01-01 19:47 ` schwab@linux-m68k.org
2022-01-01 20:34 ` jakub at gcc dot gnu.org
2022-01-01 23:15 ` krystalgamer at protonmail dot com
2022-01-02 13:27 ` jakub at gcc dot gnu.org
2022-01-02 14:40 ` krystalgamer at protonmail dot com
2022-01-02 15:00 ` schwab@linux-m68k.org

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=bug-103882-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /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).