Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Clang] Compiler Bugs (Emit Incorrect Intel Binaries) #122269

Open
witbring opened this issue Jan 9, 2025 · 1 comment
Open

[Clang] Compiler Bugs (Emit Incorrect Intel Binaries) #122269

witbring opened this issue Jan 9, 2025 · 1 comment
Labels
clang Clang issues not falling into any other category

Comments

@witbring
Copy link

witbring commented Jan 9, 2025

I am reporting a Clang compiler bug discovered during my research.
The test was conducted using the latest version (Clang 19.1.0)
This issue occurs when using the -masm=intel --save-temps flag.

  1. Summary
  • Bug1: The bus occurs when a variable name matches register name, the compiled code accesses an incorrect memory address even in the PIE binaries.
  • Bug2: The bug occurs when a function name matches register name, the compiled code converts the call instruction into an indirect call.
  1. Example Code

Below is an example that reproduces the issues.
In the code, the variable RAX is assigned a value 4, and the function RBX is called.

#include <stdio.h>

int RAX;
void RBX() { printf("hello world\n"); }

int main()
{
    RAX = 4;
    RBX();
    return 0;
}
  1. Compilation Command
clang -masm=intel --save-temps example.c -pie -fPIE
  1. Compiled Binary Output
0000000000001140 <RBX>:
    ...

0000000000001160 <main>:
    1160:	55                   	push   rbp
    1161:	48 89 e5             	mov    rbp,rsp
    1164:	c7 04 05 00 00 00 00 	mov    DWORD PTR [rax*1+0x0],0x4
    116b:	04 00 00 00
    116f:	ff d3                	call   rbx
    1171:	31 c0                	xor    eax,eax
    1173:	5d                   	pop    rbp
    1174:	c3                   	ret
  1. Reproduction

You can reproduce the result through Godbolt Compiler Explorer:
https://godbolt.org/z/7EefT3zW8

@llvmbot llvmbot added the clang Clang issues not falling into any other category label Jan 9, 2025
@lhmouse
Copy link
Contributor

lhmouse commented Jan 10, 2025

You should quote symbols that match MASM keywords:
(https://gcc.godbolt.org/z/1dTfsqsKf)

.intel_syntax noprefix
.data
RAX:
RbX:
Rcx:
    .long 0
.text
test:
	mov	edx, DWORD PTR ["RAX"]
	mov	edx, DWORD PTR ["RbX"]
	mov	edx, DWORD PTR ["Rcx"]
    call "rsP"

rsP:
    ret
test:
 mov    edx,DWORD PTR ds:0x0
 mov    edx,DWORD PTR ds:0x0
 mov    edx,DWORD PTR ds:0x0
 call   1a <rsP>
rsP:
 ret    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category
Projects
None yet
Development

No branches or pull requests

3 participants