Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[sanitizer] Add fgets, fputs and puts into sanitizer_common
Summary: Add fgets, fputs and puts to sanitizer_common. This adds ASAN coverage for these functions, extends MSAN support from fgets to fputs/puts and extends TSAN support from puts to fputs. Fixes: google/sanitizers#952 Reviewed By: vitalybuka Differential Revision: https://reviews.llvm.org/D46545 llvm-svn: 334450
- Loading branch information
1 parent
61db138
commit 1c05c95
Showing
10 changed files
with
210 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// RUN: %clangxx_asan -g %s -o %t | ||
// RUN: not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-FGETS | ||
// RUN: not %run %t 2 2>&1 | FileCheck %s --check-prefix=CHECK-FPUTS | ||
// RUN: not %run %t 3 3 2>&1 | FileCheck %s --check-prefix=CHECK-PUTS | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
int test_fgets() { | ||
FILE *fp = fopen("/etc/passwd", "r"); | ||
char buf[2]; | ||
fgets(buf, sizeof(buf) + 1, fp); // BOOM | ||
fclose(fp); | ||
return 0; | ||
} | ||
|
||
int test_fputs() { | ||
FILE *fp = fopen("/dev/null", "w"); | ||
char buf[1] = {'x'}; // Note: not nul-terminated | ||
fputs(buf, fp); // BOOM | ||
return fclose(fp); | ||
} | ||
|
||
void test_puts() { | ||
char *p = strdup("x"); | ||
free(p); | ||
puts(p); // BOOM | ||
} | ||
|
||
int main(int argc, char *argv[]) { | ||
if (argc == 1) | ||
test_fgets(); | ||
else if (argc == 2) | ||
test_fputs(); | ||
else | ||
test_puts(); | ||
return 0; | ||
} | ||
|
||
// CHECK-FGETS: {{.*ERROR: AddressSanitizer: stack-buffer-overflow}} | ||
// CHECK-FGETS: #{{.*}} in {{(wrap_|__interceptor_)?}}fgets | ||
// CHECK-FPUTS: {{.*ERROR: AddressSanitizer: stack-buffer-overflow}} | ||
// CHECK-FPUTS: #{{.*}} in {{(wrap_|__interceptor_)?}}fputs | ||
// CHECK-PUTS: {{.*ERROR: AddressSanitizer: heap-use-after-free}} | ||
// CHECK-PUTS: #{{.*}} in {{(wrap_|__interceptor_)?}}puts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// RUN: %clangxx_msan -g %s -o %t | ||
// RUN: %run %t | ||
// RUN: not %run %t 2 2>&1 | FileCheck %s --check-prefix=CHECK-FPUTS | ||
// RUN: not %run %t 3 3 2>&1 | FileCheck %s --check-prefix=CHECK-PUTS | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
int test_fgets() { | ||
FILE *fp = fopen("/dev/zero", "r"); | ||
char c; | ||
|
||
if (!fgets(&c, 1, fp)) | ||
return 1; | ||
|
||
if (c == '1') // No error | ||
return 2; | ||
|
||
fclose(fp); | ||
return 0; | ||
} | ||
|
||
int test_fputs() { | ||
FILE *fp = fopen("/dev/null", "w"); | ||
char buf[2]; | ||
fputs(buf, fp); // BOOM | ||
return fclose(fp); | ||
} | ||
|
||
void test_puts() { | ||
char buf[2]; | ||
puts(buf); // BOOM | ||
} | ||
|
||
int main(int argc, char *argv[]) { | ||
if (argc == 1) | ||
test_fgets(); | ||
else if (argc == 2) | ||
test_fputs(); | ||
else | ||
test_puts(); | ||
return 0; | ||
} | ||
|
||
// CHECK-FPUTS: Uninitialized bytes in __interceptor_fputs at offset 0 inside | ||
// CHECK-PUTS: Uninitialized bytes in __interceptor_puts at offset 0 inside |
20 changes: 20 additions & 0 deletions
20
compiler-rt/test/sanitizer_common/TestCases/Posix/fgets.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// RUN: %clangxx -g %s -o %t && %run %t | ||
|
||
#include <stdio.h> | ||
|
||
int main(void) { | ||
FILE *fp; | ||
char buf[2]; | ||
char *s; | ||
|
||
fp = fopen("/etc/passwd", "r"); | ||
if (!fp) | ||
return 1; | ||
|
||
s = fgets(buf, sizeof(buf), fp); | ||
if (!s) | ||
return 2; | ||
|
||
fclose(fp); | ||
return 0; | ||
} |
18 changes: 18 additions & 0 deletions
18
compiler-rt/test/sanitizer_common/TestCases/Posix/fputs_puts.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// RUN: %clangxx -g %s -o %t && %run %t | FileCheck %s | ||
// CHECK: {{^foobar$}} | ||
|
||
#include <stdio.h> | ||
|
||
int main(void) { | ||
int r; | ||
|
||
r = fputs("foo", stdout); | ||
if (r < 0) | ||
return 1; | ||
|
||
r = puts("bar"); | ||
if (r < 0) | ||
return 1; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s | ||
#include "test.h" | ||
|
||
char s[] = "abracadabra"; | ||
|
||
void *Thread0(void *p) { | ||
fputs(s, stdout); | ||
barrier_wait(&barrier); | ||
return 0; | ||
} | ||
|
||
void *Thread1(void *p) { | ||
barrier_wait(&barrier); | ||
s[3] = 'z'; | ||
return 0; | ||
} | ||
|
||
int main() { | ||
barrier_init(&barrier, 2); | ||
pthread_t th[2]; | ||
pthread_create(&th[0], 0, Thread0, 0); | ||
pthread_create(&th[1], 0, Thread1, 0); | ||
pthread_join(th[0], 0); | ||
pthread_join(th[1], 0); | ||
fprintf(stderr, "DONE"); | ||
} | ||
|
||
// CHECK: WARNING: ThreadSanitizer: data race | ||
// CHECK: DONE |