File: overflow-in-qsort.cpp

package info (click to toggle)
llvm-toolchain-11 1%3A11.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 995,808 kB
  • sloc: cpp: 4,767,656; ansic: 760,916; asm: 477,436; python: 170,940; objc: 69,804; lisp: 29,914; sh: 23,855; f90: 18,173; pascal: 7,551; perl: 7,471; ml: 5,603; awk: 3,489; makefile: 2,573; xml: 915; cs: 573; fortran: 503; javascript: 452
file content (49 lines) | stat: -rw-r--r-- 1,567 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
// RUN: %clangxx_asan -O2 %s -o %t
// RUN: %env_asan_opts=fast_unwind_on_fatal=1 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-FAST
// RUN: %env_asan_opts=fast_unwind_on_fatal=0 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-SLOW

// Test how well we unwind in presence of qsort in the stack
// (i.e. if we can unwind through a function compiled w/o frame pointers).
// https://code.google.com/p/address-sanitizer/issues/detail?id=137

// Fast unwinder is only available on x86_64 and i386.
// REQUIRES: x86-target-arch

#include <stdlib.h>
#include <stdio.h>

int global_array[10];
volatile int one = 1;

extern "C" {
int QsortCallback(const void *a, const void *b) {
  char *x = (char*)a;
  char *y = (char*)b;
  printf("Calling QsortCallback\n");
  global_array[one * 10] = 0;  // BOOM
  return (int)*x - (int)*y;
}

__attribute__((noinline))
void MyQsort(char *a, size_t size) {
  printf("Calling qsort\n");
  qsort(a, size, sizeof(char), QsortCallback);
  printf("Done\n");  // Avoid tail call.
}
}  // extern "C"

int main() {
  char a[2] = {1, 2};
  MyQsort(a, 2);
}

// Fast unwind may not unwind through qsort.
// CHECK-FAST: ERROR: AddressSanitizer: global-buffer-overflow
// CHECK-FAST: #0{{.*}} in QsortCallback
// CHECK-FAST: is located 0 bytes to the right of global variable 'global_array

// CHECK-SLOW: ERROR: AddressSanitizer: global-buffer-overflow
// CHECK-SLOW: #0{{.*}} in QsortCallback
// CHECK-SLOW: #{{.*}} in MyQsort
// CHECK-SLOW: #{{.*}} in main
// CHECK-SLOW: is located 0 bytes to the right of global variable 'global_array