Description
CLANG version:
clang version 17.0.0 (https://github.com/llvm/llvm-project.git e369577cd0585d928cad1edfa7d546f3f6750f39)
Hello.
After llvm/llvm-project#60116 was resolved, the patch for using per-cpu stack protector for X86_64 still couldn't work due to wrong instructions being generated in some files. After diving deep into the issue, I was able to reproduce it.
The compile command is:
clang -S -c -m64 -O0 -mcmodel=kernel -fno-PIE -fstack-protector -mstack-protector-guard-reg=gs -mstack-protector-guard-symbol=__stack_chk_guard test.c -o test.s
I found that if there is a definition of "_stack_chk_guard" in the file:
unsigned long __stack_chk_guard;
int foo(void)
{
char X[200];
return 3;
}
The output assembly is:
.......
movq __stack_chk_guard(%rip), %rax
movq %rax, -8(%rbp)
movq __stack_chk_guard(%rip), %rax
movq -8(%rbp), %rcx
.......
It would generate wrong instruction without %gs.
If there is a declaration of "__stack_chk_guard" in the file:
extern unsigned long __stack_chk_guard;
int foo(void)
{
char X[200];
return 3;
}
The output assembly is:
.......
movq %gs:__stack_chk_guard(%rip), %rax
movq %rax, -8(%rbp)
.......
It would generate right instruction.
However, If there is a reference of "__stack_chk_guard" in the file:
extern unsigned long __stack_chk_guard;
void test(void)
{
__stack_chk_guard = 1;
}
int foo(void)
{
char X[200];
return 3;
}
The output assembly is:
.......
movq __stack_chk_guard(%rip), %rax
movq %rax, -8(%rbp)
.......
It would generate wrong instruction without %gs too.
This is why I found the wrong instruction in some files where "__stack_chk_guard" was set up.