From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 340CC3858D39; Fri, 31 Dec 2021 17:11:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 340CC3858D39 From: "krystalgamer at protonmail dot com" 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: target X-Bugzilla-Version: 10.3.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: krystalgamer at protonmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone cf_gcchost cf_gcctarget cf_gccbuild Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 31 Dec 2021 17:11:10 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D103882 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] =3D=3D '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 : 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 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 : 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 th= at 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.=