llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[AArch64] Miscompilation when stack tagging is enabled in AArch64

Open kartcq opened this issue 3 years ago • 8 comments

Here is the reduced test : https://godbolt.org/ There is a br instruction (line 16) at end of the entry block which is suppose to read the NZCV flag edited by previous cmp instruction (line 14) and branch accordingly. But the stg loop gets in the way and edits the nzcv flag which is not handled properly.

This results in mis compiled assembly

  cmp     w0, #10  
LBB0_1:                                
  st2g    x9, [x9], #32  
  subs    x8, x8, #32   
  b.ne    .LBB0_1
  b.ge    .LBB0_4

Here b.ge jumps based on NZCV edited by subs instruction rather than from cmp instruction.

Run Command

llc -mtriple=aarch64 -mattr=+mte -aarch64-order-frame-objects=0 settag.ll

settag.ll

declare void @llvm.aarch64.settag(ptr %p, i64 %a)
; Function Attrs: nounwind
declare i32 @printf(ptr, ...) #0

@.str = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1

define i32 @stg_func(i32 %in) {
entry:

  %a = alloca i8, i32 16, align 16
  %b = alloca i8, i32 512, align 16
  %c = alloca i8, i32 16, align 16
  call void @llvm.aarch64.settag(ptr %a, i64 16)
  call void @llvm.aarch64.settag(ptr %b, i64 512)
  %cmp = icmp slt i32 %in, 10
  call void @llvm.aarch64.settag(ptr %c, i64 16)
  br i1 %cmp, label %return0, label %return1

return0:                                           ; preds = %entry
  %call = call i32 (ptr, ...) @printf(ptr @.str, i32 10) #1
  ret i32 0

return1:
  ret i32 1
}

This is observed after the patch : 5e612bc As this patch expands stg loops to b.ne instead of Bcc used earlier

kartcq avatar Sep 14 '23 08:09 kartcq

@llvm/issue-subscribers-backend-aarch64

Here is the reduced test : [https://godbolt.org/](https://godbolt.org/) There is a **br** instruction (line 16) at end of the **entry** block which is suppose to **read the NZCV flag** edited by previous **cmp** instruction (line 14) and branch accordingly. But the **stg loop** gets in the way and edits the **nzcv flag** which is not handled properly.

This results in mis compiled assembly

  cmp     w0, #<!-- -->10  
LBB0_1:                                
  st2g    x9, [x9], #<!-- -->32  
  subs    x8, x8, #<!-- -->32   
  b.ne    .LBB0_1
  b.ge    .LBB0_4

Here b.ge jumps based on NZCV edited by subs instruction rather than from cmp instruction.

Run Command

llc -mtriple=aarch64 -mattr=+mte -aarch64-order-frame-objects=0 settag.ll

settag.ll

declare void @<!-- -->llvm.aarch64.settag(ptr %p, i64 %a)
; Function Attrs: nounwind
declare i32 @<!-- -->printf(ptr, ...) #<!-- -->0

@.str = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1

define i32 @<!-- -->stg_func(i32 %in) {
entry:

  %a = alloca i8, i32 16, align 16
  %b = alloca i8, i32 512, align 16
  %c = alloca i8, i32 16, align 16
  call void @<!-- -->llvm.aarch64.settag(ptr %a, i64 16)
  call void @<!-- -->llvm.aarch64.settag(ptr %b, i64 512)
  %cmp = icmp slt i32 %in, 10
  call void @<!-- -->llvm.aarch64.settag(ptr %c, i64 16)
  br i1 %cmp, label %return0, label %return1

return0:                                           ; preds = %entry
  %call = call i32 (ptr, ...) @<!-- -->printf(ptr @.str, i32 10) #<!-- -->1
  ret i32 0

return1:
  ret i32 1
}

This is observed after the patch : 5e612bc As this patch expands stg loops to b.ne instead of Bcc used earlier

llvmbot avatar Sep 15 '23 15:09 llvmbot

@kbeyls for the mentioned commit (though it's not clear yet if that's the commit that causes it).

DavidSpickett avatar Sep 15 '23 15:09 DavidSpickett

I think this issue is a duplicate of #64309, and was already fixed in commit b09c575975b691e988a0f2e31d632c5f1038ab1d

kbeyls avatar Sep 15 '23 16:09 kbeyls

From our internal testing, there were actually two issues: one, the scheduling issue fixed by b09c575975b691e988a0f2e31d632c5f1038ab1d, and two, an issue where the tryMergeAdjacentSTG transform clobbers the flags register.

@kartcq has a patch for tryMergeAdjacentSTG; we'll try to get it posted soon.

efriedma-quic avatar Sep 15 '23 17:09 efriedma-quic

Created a Pull Request to fix the issue : https://github.com/llvm/llvm-project/pull/68873 @DavidSpickett @kbeyls @efriedma-quic @eugenis

kartcq avatar Oct 12 '23 10:10 kartcq

can we close this issue now that the patch is in main branch?

hiraditya avatar Nov 20 '23 17:11 hiraditya

Yes we can, as the patch is merged.

kartcq avatar Nov 21 '23 04:11 kartcq