Skip to content

Commit ec42c78

Browse files
committed
[sanitizer][msan] VarArgHelper for loongarch64
This patch adds support for variadic argument for loongarch64, which is based on MIPS64. And `check-msan` all pass. Reviewed By: vitalybuka Differential Revision: https://reviews.llvm.org/D158587
1 parent 1d7cec6 commit ec42c78

File tree

3 files changed

+85
-3
lines changed

3 files changed

+85
-3
lines changed

compiler-rt/test/msan/signal_stress_test.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
// Reported deadly signal due to stack-overflow
66
// XFAIL: target={{.*netbsd.*}}
77

8-
// VarArg implementation on LoongArch isn't supported yet.
9-
// UNSUPPORTED: target=loongarch{{.*}}
10-
118
#include <signal.h>
129
#include <stdarg.h>
1310
#include <sanitizer/msan_interface.h>

llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4943,6 +4943,7 @@ struct VarArgAMD64Helper : public VarArgHelper {
49434943
};
49444944

49454945
/// MIPS64-specific implementation of VarArgHelper.
4946+
/// NOTE: This is also used for LoongArch64.
49464947
struct VarArgMIPS64Helper : public VarArgHelper {
49474948
Function &F;
49484949
MemorySanitizer &MS;
@@ -5834,6 +5835,10 @@ struct VarArgSystemZHelper : public VarArgHelper {
58345835
}
58355836
};
58365837

5838+
// Loongarch64 is not a MIPS, but the current vargs calling convention matches
5839+
// the MIPS.
5840+
using VarArgLoongArch64Helper = VarArgMIPS64Helper;
5841+
58375842
/// A no-op implementation of VarArgHelper.
58385843
struct VarArgNoOpHelper : public VarArgHelper {
58395844
VarArgNoOpHelper(Function &F, MemorySanitizer &MS,
@@ -5866,6 +5871,8 @@ static VarArgHelper *CreateVarArgHelper(Function &Func, MemorySanitizer &Msan,
58665871
return new VarArgPowerPC64Helper(Func, Msan, Visitor);
58675872
else if (TargetTriple.getArch() == Triple::systemz)
58685873
return new VarArgSystemZHelper(Func, Msan, Visitor);
5874+
else if (TargetTriple.isLoongArch64())
5875+
return new VarArgLoongArch64Helper(Func, Msan, Visitor);
58695876
else
58705877
return new VarArgNoOpHelper(Func, Msan, Visitor);
58715878
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
; RUN: opt < %s -S -passes=msan 2>&1 | FileCheck %s
2+
3+
target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n64-S128"
4+
target triple = "loongarch64-unknown-linux-gnu"
5+
6+
;; First, check allocation of the save area.
7+
declare void @llvm.lifetime.start.p0(i64, ptr nocapture) #1
8+
declare void @llvm.va_start(ptr) #2
9+
declare void @llvm.va_end(ptr) #2
10+
declare void @llvm.lifetime.end.p0(i64, ptr nocapture) #1
11+
define i32 @foo(i32 %guard, ...) {
12+
; CHECK-LABEL: @foo
13+
; CHECK: [[TMP1:%.*]] = load {{.*}} @__msan_va_arg_overflow_size_tls
14+
; CHECK: [[TMP2:%.*]] = add i64 0, [[TMP1]]
15+
; CHECK: [[TMP3:%.*]] = alloca {{.*}} [[TMP2]]
16+
; CHECK: call void @llvm.memset.p0.i64(ptr align 8 [[TMP3]], i8 0, i64 [[TMP2]], i1 false)
17+
; CHECK: [[TMP4:%.*]] = call i64 @llvm.umin.i64(i64 [[TMP2]], i64 800)
18+
; CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[TMP3]], ptr align 8 @__msan_va_arg_tls, i64 [[TMP4]], i1 false)
19+
;
20+
%vl = alloca ptr, align 8
21+
call void @llvm.lifetime.start.p0(i64 32, ptr %vl)
22+
call void @llvm.va_start(ptr %vl)
23+
call void @llvm.va_end(ptr %vl)
24+
call void @llvm.lifetime.end.p0(i64 32, ptr %vl)
25+
ret i32 0
26+
}
27+
28+
;; Save the incoming shadow value from the arguments in the __msan_va_arg_tls
29+
;; array.
30+
define i32 @bar() {
31+
; CHECK-LABEL: @bar
32+
; CHECK: store i32 0, ptr @__msan_va_arg_tls, align 8
33+
; CHECK: store i64 0, ptr inttoptr (i64 add (i64 ptrtoint (ptr @__msan_va_arg_tls to i64), i64 8) to ptr), align 8
34+
; CHECK: store i64 0, ptr inttoptr (i64 add (i64 ptrtoint (ptr @__msan_va_arg_tls to i64), i64 16) to ptr), align 8
35+
; CHECK: store {{.*}} 24, {{.*}} @__msan_va_arg_overflow_size_tls
36+
;
37+
%1 = call i32 (i32, ...) @foo(i32 0, i32 1, i64 2, double 3.000000e+00)
38+
ret i32 %1
39+
}
40+
41+
;; Check multiple fixed arguments.
42+
declare i32 @foo2(i32 %g1, i32 %g2, ...)
43+
define i32 @bar2() {
44+
; CHECK-LABEL: @bar2
45+
; CHECK: store i64 0, ptr @__msan_va_arg_tls, align 8
46+
; CHECK: store i64 0, ptr inttoptr (i64 add (i64 ptrtoint (ptr @__msan_va_arg_tls to i64), i64 8) to ptr), align 8
47+
; CHECK: store {{.*}} 16, {{.*}} @__msan_va_arg_overflow_size_tls
48+
;
49+
%1 = call i32 (i32, i32, ...) @foo2(i32 0, i32 1, i64 2, double 3.000000e+00)
50+
ret i32 %1
51+
}
52+
53+
;; Test that MSan doesn't generate code overflowing __msan_va_arg_tls when too many arguments are
54+
;; passed to a variadic function.
55+
declare i64 @sum(i64 %n, ...)
56+
define dso_local i64 @many_args() {
57+
;; If the size of __msan_va_arg_tls changes the second argument of `add` must also be changed.
58+
; CHECK-LABEL: @many_args
59+
; CHECK: i64 add (i64 ptrtoint (ptr @__msan_va_arg_tls to i64), i64 792)
60+
; CHECK-NOT: i64 add (i64 ptrtoint (ptr @__msan_va_arg_tls to i64), i64 800)
61+
;
62+
entry:
63+
%ret = call i64 (i64, ...) @sum(i64 120,
64+
i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1,
65+
i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1,
66+
i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1,
67+
i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1,
68+
i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1,
69+
i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1,
70+
i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1,
71+
i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1,
72+
i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1,
73+
i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1,
74+
i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1,
75+
i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1
76+
)
77+
ret i64 %ret
78+
}

0 commit comments

Comments
 (0)