File: qsort.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 (85 lines) | stat: -rw-r--r-- 2,061 bytes parent folder | download | duplicates (12)
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
// RUN: %clangxx_msan -fno-sanitize-memory-param-retval -O0 -g %s -o %t && %run %t
// RUN: %clangxx_msan -fno-sanitize-memory-param-retval -DPOISON -O0 -g %s -o %t && not %run %t 2>&1 | FileCheck %s

#include <assert.h>
#include <errno.h>
#include <glob.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sanitizer/msan_interface.h>

constexpr size_t kSize1 = 27;
constexpr size_t kSize2 = 7;

bool seen2;

void dummy(long a, long b, long c, long d, long e) {}

void poison_stack_and_param() {
  char x[10000];
  int y;
  dummy(y, y, y, y, y);
}

__attribute__((always_inline)) int cmp(long a, long b) {
  if (a < b)
    return -1;
  else if (a > b)
    return 1;
  else
    return 0;
}

int compar2(const void *a, const void *b) {
  assert(a);
  assert(b);
  __msan_check_mem_is_initialized(a, sizeof(long));
  __msan_check_mem_is_initialized(b, sizeof(long));
  seen2 = true;
  poison_stack_and_param();
  return cmp(*(long *)a, *(long *)b);
}

int compar1(const void *a, const void *b) {
  assert(a);
  assert(b);
  __msan_check_mem_is_initialized(a, sizeof(long));
  __msan_check_mem_is_initialized(b, sizeof(long));

  long *p = new long[kSize2];
  // kind of random
  for (int i = 0; i < kSize2; ++i)
    p[i] = i * 2 + (i % 3 - 1) * 3;
  qsort(p, kSize1, sizeof(long), compar2);
  __msan_check_mem_is_initialized(p, sizeof(long) * kSize2);
  delete[] p;

  poison_stack_and_param();
  return cmp(*(long *)a, *(long *)b);
}

int main(int argc, char *argv[]) {
  long *p = new long[kSize1];
  // kind of random
  for (int i = 0; i < kSize1; ++i)
    p[i] = i * 2 + (i % 3 - 1) * 3;
  poison_stack_and_param();
#ifdef POISON
  __msan_poison(p + 1, sizeof(long));
  // CHECK: Uninitialized bytes in __msan_check_mem_is_initialized at offset 0 inside [{{.*}}, 8)
#endif
  qsort(p, kSize1, sizeof(long), compar1);
  __msan_check_mem_is_initialized(p, sizeof(long) * kSize1);
  assert(seen2);
  delete[] p;

  p = new long[0];
  qsort(p, 0, sizeof(long), compar1);
  delete[] p;

  qsort(nullptr, 0, sizeof(long), compar1);

  return 0;
}