File: param_tls_limit.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (95 lines) | stat: -rw-r--r-- 2,594 bytes parent folder | download | duplicates (17)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// ParamTLS has limited size. Everything that does not fit is considered fully
// initialized.

// RUN: %clangxx_msan -fno-sanitize-memory-param-retval -O0 %s -o %t && %run %t
// RUN: %clangxx_msan -fno-sanitize-memory-param-retval -fsanitize-memory-track-origins -O0 %s -o %t && %run %t
// RUN: %clangxx_msan -fno-sanitize-memory-param-retval -fsanitize-memory-track-origins=2 -O0 %s -o %t && %run %t
//
// AArch64 and LoongArch64 fail with:
// void f801(S<801>): Assertion `__msan_test_shadow(&s, sizeof(s)) == -1' failed
// XFAIL: target={{(aarch64|loongarch64).*}}
// When passing huge structs by value, SystemZ uses pointers, therefore this
// test in its present form is unfortunately not applicable.
// ABI says: "A struct or union of any other size <snip>. Replace such an
// argument by a pointer to the object, or to a copy where necessary to enforce
// call-by-value semantics."
// XFAIL: target=s390x{{.*}}

#include <sanitizer/msan_interface.h>
#include <assert.h>

// This test assumes that ParamTLS size is 800 bytes.

// This test passes poisoned values through function argument list.
// In case of overflow, argument is unpoisoned.
#define OVERFLOW(x) assert(__msan_test_shadow(&x, sizeof(x)) == -1)
// In case of no overflow, it is still poisoned.
#define NO_OVERFLOW(x) assert(__msan_test_shadow(&x, sizeof(x)) == 0)

#if defined(__x86_64__)
// In x86_64, if argument is partially outside tls, it is considered completely
// unpoisoned
#define PARTIAL_OVERFLOW(x) OVERFLOW(x)
#else
// In other archs, bigger arguments are splitted in multiple IR arguments, so
// they are considered poisoned till tls limit. Checking last byte of such arg:
#define PARTIAL_OVERFLOW(x) assert(__msan_test_shadow((char *)(&(x) + 1) - 1, 1) == -1)
#endif


template<int N>
struct S {
  char x[N];
};

void f100(S<100> s) {
  NO_OVERFLOW(s);
}

void f800(S<800> s) {
  NO_OVERFLOW(s);
}

void f801(S<801> s) {
  PARTIAL_OVERFLOW(s);
}

void f1000(S<1000> s) {
  PARTIAL_OVERFLOW(s);
}

void f_many(int a, double b, S<800> s, int c, double d) {
  NO_OVERFLOW(a);
  NO_OVERFLOW(b);
  PARTIAL_OVERFLOW(s);
  OVERFLOW(c);
  OVERFLOW(d);
}

// -8 bytes for "int a", aligned by 8
// -2 to make "int c" a partial fit
void f_many2(int a, S<800 - 8 - 2> s, int c, double d) {
  NO_OVERFLOW(a);
  NO_OVERFLOW(s);
  PARTIAL_OVERFLOW(c);
  OVERFLOW(d);
}

int main(void) {
  S<100> s100;
  S<800> s800;
  S<801> s801;
  S<1000> s1000;
  f100(s100);
  f800(s800);
  f801(s801);
  f1000(s1000);

  int i;
  double d;
  f_many(i, d, s800, i, d);

  S<800 - 8 - 2> s788;
  f_many2(i, s788, i, d);
  return 0;
}