blob: 983da9f7ed30e6a193cab1d6af296bb2a9e02af8 [file] [log] [blame]
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -07001// 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 Nainarcdce50b2015-07-01 12:26:56 -07004// 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 Nainar259f7062015-05-06 11:49:53 -07006// RUN: %run %t test2 2>&1
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -07007// 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 Nainar259f7062015-05-06 11:49:53 -07009// RUN: %run %t test3 2>&1
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070010// 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 Nainar259f7062015-05-06 11:49:53 -070012// RUN: %run %t test4 2>&1
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070013// 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 Nainar259f7062015-05-06 11:49:53 -070015// RUN: %run %t test5 2>&1
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070016// 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 Nainar259f7062015-05-06 11:49:53 -070018// RUN: %run %t test6 2>&1
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070019// 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 Nainar259f7062015-05-06 11:49:53 -070021// RUN: %run %t test7 2>&1
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070022// 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 Nainar259f7062015-05-06 11:49:53 -070024
25#include <assert.h>
26#include <stdlib.h>
27#include <string.h>
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070028#include <sanitizer/asan_interface.h>
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070029
30void 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
37void 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
45void test3(char *array, char *endptr) {
46 // Buffer overflow if base is invalid.
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070047 memset(array, 0, 8);
48 ASAN_POISON_MEMORY_REGION(array, 8);
49 long long r = strtoll(array + 1, NULL, -1);
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070050 assert(r == 0);
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070051 ASAN_UNPOISON_MEMORY_REGION(array, 8);
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070052}
53
54void 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
60void 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
69void 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
78void 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
85int main(int argc, char **argv) {
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070086 char *array0 = (char*)malloc(11);
87 char* array = array0 + 8;
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070088 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 Nainarcdce50b2015-07-01 12:26:56 -070099 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 Nainar259f7062015-05-06 11:49:53 -0700102 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 Nainarcdce50b2015-07-01 12:26:56 -0700114 free(array0);
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700115 return 0;
116}