Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 1 | // Test strict_string_checks option in strtoll function |
| 2 | // RUN: %clang_asan -DTEST1 %s -o %t |
| 3 | // RUN: %run %t test1 2>&1 |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 4 | // RUN: env ASAN_OPTIONS=$ASAN_OPTIONS:strict_string_checks=false %run %t test1 2>&1 |
| 5 | // RUN: env ASAN_OPTIONS=$ASAN_OPTIONS:strict_string_checks=true not %run %t test1 2>&1 | FileCheck %s --check-prefix=CHECK1 |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 6 | // RUN: %run %t test2 2>&1 |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 7 | // RUN: env ASAN_OPTIONS=$ASAN_OPTIONS:strict_string_checks=false %run %t test2 2>&1 |
| 8 | // RUN: env ASAN_OPTIONS=$ASAN_OPTIONS:strict_string_checks=true not %run %t test2 2>&1 | FileCheck %s --check-prefix=CHECK2 |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 9 | // RUN: %run %t test3 2>&1 |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 10 | // RUN: env ASAN_OPTIONS=$ASAN_OPTIONS:strict_string_checks=false %run %t test3 2>&1 |
| 11 | // RUN: env ASAN_OPTIONS=$ASAN_OPTIONS:strict_string_checks=true not %run %t test3 2>&1 | FileCheck %s --check-prefix=CHECK3 |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 12 | // RUN: %run %t test4 2>&1 |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 13 | // RUN: env ASAN_OPTIONS=$ASAN_OPTIONS:strict_string_checks=false %run %t test4 2>&1 |
| 14 | // RUN: env ASAN_OPTIONS=$ASAN_OPTIONS:strict_string_checks=true not %run %t test4 2>&1 | FileCheck %s --check-prefix=CHECK4 |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 15 | // RUN: %run %t test5 2>&1 |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 16 | // RUN: env ASAN_OPTIONS=$ASAN_OPTIONS:strict_string_checks=false %run %t test5 2>&1 |
| 17 | // RUN: env ASAN_OPTIONS=$ASAN_OPTIONS:strict_string_checks=true not %run %t test5 2>&1 | FileCheck %s --check-prefix=CHECK5 |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 18 | // RUN: %run %t test6 2>&1 |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 19 | // RUN: env ASAN_OPTIONS=$ASAN_OPTIONS:strict_string_checks=false %run %t test6 2>&1 |
| 20 | // RUN: env ASAN_OPTIONS=$ASAN_OPTIONS:strict_string_checks=true not %run %t test6 2>&1 | FileCheck %s --check-prefix=CHECK6 |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 21 | // RUN: %run %t test7 2>&1 |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 22 | // RUN: env ASAN_OPTIONS=$ASAN_OPTIONS:strict_string_checks=false %run %t test7 2>&1 |
| 23 | // RUN: env ASAN_OPTIONS=$ASAN_OPTIONS:strict_string_checks=true not %run %t test7 2>&1 | FileCheck %s --check-prefix=CHECK7 |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 24 | |
| 25 | #include <assert.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 28 | #include <sanitizer/asan_interface.h> |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 29 | |
| 30 | void test1(char *array, char *endptr) { |
| 31 | // Buffer overflow if there is no terminating null (depends on base) |
| 32 | long long r = strtoll(array, &endptr, 3); |
| 33 | assert(array + 2 == endptr); |
| 34 | assert(r == 5); |
| 35 | } |
| 36 | |
| 37 | void test2(char *array, char *endptr) { |
| 38 | // Buffer overflow if there is no terminating null (depends on base) |
| 39 | array[2] = 'z'; |
| 40 | long long r = strtoll(array, &endptr, 35); |
| 41 | assert(array + 2 == endptr); |
| 42 | assert(r == 37); |
| 43 | } |
| 44 | |
| 45 | void test3(char *array, char *endptr) { |
| 46 | // Buffer overflow if base is invalid. |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 47 | memset(array, 0, 8); |
| 48 | ASAN_POISON_MEMORY_REGION(array, 8); |
| 49 | long long r = strtoll(array + 1, NULL, -1); |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 50 | assert(r == 0); |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 51 | ASAN_UNPOISON_MEMORY_REGION(array, 8); |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void test4(char *array, char *endptr) { |
| 55 | // Buffer overflow if base is invalid. |
| 56 | long long r = strtoll(array + 3, NULL, 1); |
| 57 | assert(r == 0); |
| 58 | } |
| 59 | |
| 60 | void test5(char *array, char *endptr) { |
| 61 | // Overflow if no digits are found. |
| 62 | array[0] = ' '; |
| 63 | array[1] = '+'; |
| 64 | array[2] = '-'; |
| 65 | long long r = strtoll(array, NULL, 0); |
| 66 | assert(r == 0); |
| 67 | } |
| 68 | |
| 69 | void test6(char *array, char *endptr) { |
| 70 | // Overflow if no digits are found. |
| 71 | array[0] = ' '; |
| 72 | array[1] = array[2] = 'z'; |
| 73 | long long r = strtoll(array, &endptr, 0); |
| 74 | assert(array == endptr); |
| 75 | assert(r == 0); |
| 76 | } |
| 77 | |
| 78 | void test7(char *array, char *endptr) { |
| 79 | // Overflow if no digits are found. |
| 80 | array[2] = 'z'; |
| 81 | long long r = strtoll(array + 2, NULL, 0); |
| 82 | assert(r == 0); |
| 83 | } |
| 84 | |
| 85 | int main(int argc, char **argv) { |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 86 | char *array0 = (char*)malloc(11); |
| 87 | char* array = array0 + 8; |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 88 | char *endptr = NULL; |
| 89 | array[0] = '1'; |
| 90 | array[1] = '2'; |
| 91 | array[2] = '3'; |
| 92 | if (argc != 2) return 1; |
| 93 | if (!strcmp(argv[1], "test1")) test1(array, endptr); |
| 94 | // CHECK1: {{.*ERROR: AddressSanitizer: heap-buffer-overflow on address}} |
| 95 | // CHECK1: READ of size 4 |
| 96 | if (!strcmp(argv[1], "test2")) test2(array, endptr); |
| 97 | // CHECK2: {{.*ERROR: AddressSanitizer: heap-buffer-overflow on address}} |
| 98 | // CHECK2: READ of size 4 |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 99 | if (!strcmp(argv[1], "test3")) test3(array0, endptr); |
| 100 | // CHECK3: {{.*ERROR: AddressSanitizer: use-after-poison on address}} |
| 101 | // CHECK3: READ of size 1 |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 102 | if (!strcmp(argv[1], "test4")) test4(array, endptr); |
| 103 | // CHECK4: {{.*ERROR: AddressSanitizer: heap-buffer-overflow on address}} |
| 104 | // CHECK4: READ of size 1 |
| 105 | if (!strcmp(argv[1], "test5")) test5(array, endptr); |
| 106 | // CHECK5: {{.*ERROR: AddressSanitizer: heap-buffer-overflow on address}} |
| 107 | // CHECK5: READ of size 4 |
| 108 | if (!strcmp(argv[1], "test6")) test6(array, endptr); |
| 109 | // CHECK6: {{.*ERROR: AddressSanitizer: heap-buffer-overflow on address}} |
| 110 | // CHECK6: READ of size 4 |
| 111 | if (!strcmp(argv[1], "test7")) test7(array, endptr); |
| 112 | // CHECK7: {{.*ERROR: AddressSanitizer: heap-buffer-overflow on address}} |
| 113 | // CHECK7: READ of size 2 |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 114 | free(array0); |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 115 | return 0; |
| 116 | } |