Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GWP-ASan] Integration with Scudo [5].
Summary: See D60593 for further information. This patch adds GWP-ASan support to the Scudo hardened allocator. It also implements end-to-end integration tests using Scudo as the backing allocator. The tests include crash handling for buffer over/underflow as well as use-after-free detection. Reviewers: vlad.tsyrklevich, cryptoad Reviewed By: vlad.tsyrklevich, cryptoad Subscribers: kubamracek, mgorny, #sanitizers, llvm-commits, morehouse Tags: #sanitizers, #llvm Differential Revision: https://reviews.llvm.org/D62929 llvm-svn: 363584
- Loading branch information
Showing
21 changed files
with
335 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// REQUIRES: gwp_asan | ||
// RUN: %clangxx_gwp_asan %s -o %t | ||
// RUN: not %run %t 2>&1 | FileCheck %s | ||
|
||
// CHECK: GWP-ASan detected a memory error | ||
// CHECK: Double free occurred when trying to free memory at: | ||
|
||
#include <cstdlib> | ||
|
||
int main() { | ||
char *Ptr = new char; | ||
delete Ptr; | ||
delete Ptr; | ||
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,15 @@ | ||
// REQUIRES: gwp_asan | ||
// RUN: %clangxx_gwp_asan %s -o %t | ||
// RUN: not %run %t 2>&1 | FileCheck %s | ||
|
||
// CHECK: GWP-ASan detected a memory error | ||
// CHECK: Double free occurred when trying to free memory at: | ||
|
||
#include <cstdlib> | ||
|
||
int main() { | ||
char *Ptr = new char[50]; | ||
delete[] Ptr; | ||
delete[] Ptr; | ||
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,15 @@ | ||
// REQUIRES: gwp_asan | ||
// RUN: %clangxx_gwp_asan %s -o %t | ||
// RUN: not %run %t 2>&1 | FileCheck %s | ||
|
||
// CHECK: GWP-ASan detected a memory error | ||
// CHECK: Double free occurred when trying to free memory at: | ||
|
||
#include <cstdlib> | ||
|
||
int main() { | ||
void *Ptr = malloc(10); | ||
free(Ptr); | ||
free(Ptr); | ||
return 0; | ||
} |
This file was deleted.
Oops, something went wrong.
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 @@ | ||
// REQUIRES: gwp_asan | ||
// RUN: %clangxx_gwp_asan %s -o %t | ||
// RUN: %expect_crash %run %t 2>&1 | FileCheck %s | ||
|
||
// CHECK: GWP-ASan detected a memory error | ||
// CHECK: Buffer overflow occurred when accessing memory at: | ||
// CHECK: is located {{[0-9]+}} bytes to the right | ||
|
||
#include <cstdlib> | ||
|
||
#include "page_size.h" | ||
|
||
int main() { | ||
char *Ptr = | ||
reinterpret_cast<char *>(malloc(pageSize())); | ||
volatile char x = *(Ptr + pageSize()); | ||
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,18 @@ | ||
// REQUIRES: gwp_asan | ||
// RUN: %clangxx_gwp_asan %s -o %t | ||
// RUN: %expect_crash %run %t 2>&1 | FileCheck %s | ||
|
||
// CHECK: GWP-ASan detected a memory error | ||
// CHECK: Buffer underflow occurred when accessing memory at: | ||
// CHECK: is located 1 bytes to the left | ||
|
||
#include <cstdlib> | ||
|
||
#include "page_size.h" | ||
|
||
int main() { | ||
char *Ptr = | ||
reinterpret_cast<char *>(malloc(pageSize())); | ||
volatile char x = *(Ptr - 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,16 @@ | ||
// REQUIRES: gwp_asan | ||
// RUN: %clangxx_gwp_asan %s -o %t | ||
// RUN: not %run %t 2>&1 | FileCheck %s | ||
|
||
// CHECK: GWP-ASan detected a memory error | ||
// CHECK: Invalid (wild) free occurred when trying to free memory at: | ||
// CHECK: is located 1 bytes to the left of | ||
|
||
#include <cstdlib> | ||
|
||
int main() { | ||
char *Ptr = | ||
reinterpret_cast<char *>(malloc(1)); | ||
free(Ptr - 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,16 @@ | ||
// REQUIRES: gwp_asan | ||
// RUN: %clangxx_gwp_asan %s -o %t | ||
// RUN: not %run %t 2>&1 | FileCheck %s | ||
|
||
// CHECK: GWP-ASan detected a memory error | ||
// CHECK: Invalid (wild) free occurred when trying to free memory at: | ||
// CHECK: is located 1 bytes to the right | ||
|
||
#include <cstdlib> | ||
|
||
int main() { | ||
char *Ptr = | ||
reinterpret_cast<char *>(malloc(1)); | ||
free(Ptr + 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
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,13 @@ | ||
#ifndef PAGE_SIZE_ | ||
#define PAGE_SIZE_ | ||
|
||
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) | ||
# include <unistd.h> | ||
unsigned pageSize() { | ||
return sysconf(_SC_PAGESIZE); | ||
} | ||
#else | ||
# error "GWP-ASan is not supported on this platform." | ||
#endif | ||
|
||
#endif // PAGE_SIZE_ |
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,44 @@ | ||
// REQUIRES: gwp_asan | ||
// RUN: %clangxx_gwp_asan %s -o %t -DTEST_MALLOC | ||
// RUN: not %run %t 2>&1 | FileCheck %s --check-prefix CHECK-MALLOC | ||
|
||
// Check both C++98 and C. | ||
// RUN: %clangxx_gwp_asan -std=c++98 %s -o %t -DTEST_FREE | ||
// RUN: %expect_crash %run %t 2>&1 | FileCheck %s --check-prefix CHECK-FREE | ||
// RUN: cp %s %t.c && %clang_gwp_asan %t.c -o %t -DTEST_FREE | ||
// RUN: %expect_crash %run %t 2>&1 | FileCheck %s --check-prefix CHECK-FREE | ||
|
||
// Ensure GWP-ASan stub implementation of realloc() in Scudo works to-spec. In | ||
// particular, the behaviour regarding realloc of size zero is interesting, as | ||
// it's defined as free(). | ||
|
||
#include <stdlib.h> | ||
|
||
int main() { | ||
#if defined(TEST_MALLOC) | ||
// realloc(nullptr, size) is equivalent to malloc(size). | ||
char *Ptr = reinterpret_cast<char *>(realloc(nullptr, 1)); | ||
*Ptr = 0; | ||
// Trigger an INVALID_FREE to the right. | ||
free(Ptr + 1); | ||
|
||
// CHECK-MALLOC: GWP-ASan detected a memory error | ||
// CHECK-MALLOC: Invalid (wild) free occurred when trying to free memory at: | ||
// CHECK-MALLOC: is located 1 bytes to the right of a 1-byte allocation | ||
#elif defined(TEST_FREE) | ||
char *Ptr = (char *) malloc(1); | ||
// realloc(ptr, 0) is equivalent to free(ptr) and must return nullptr. Note | ||
// that this is only the specification in C++98 and C. | ||
if (realloc(Ptr, 0) != NULL) { | ||
|
||
} | ||
// Trigger a USE_AFTER_FREE. | ||
*Ptr = 0; | ||
|
||
// CHECK-FREE: GWP-ASan detected a memory error | ||
// CHECK-FREE: Use after free occurred when accessing memory at: | ||
// CHECK-FREE: is a 1-byte allocation | ||
#endif | ||
|
||
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,28 @@ | ||
// REQUIRES: gwp_asan | ||
// This test ensures that normal allocation/memory access/deallocation works | ||
// as expected and we didn't accidentally break the supporting allocator. | ||
|
||
// RUN: %clangxx_gwp_asan %s -o %t | ||
// RUN: %env_gwp_asan_options=MaxSimultaneousAllocations=1 %run %t | ||
// RUN: %env_gwp_asan_options=MaxSimultaneousAllocations=2 %run %t | ||
// RUN: %env_gwp_asan_options=MaxSimultaneousAllocations=11 %run %t | ||
// RUN: %env_gwp_asan_options=MaxSimultaneousAllocations=12 %run %t | ||
// RUN: %env_gwp_asan_options=MaxSimultaneousAllocations=13 %run %t | ||
|
||
#include <cstdlib> | ||
|
||
int main() { | ||
void* Pointers[16]; | ||
for (unsigned i = 0; i < 16; ++i) { | ||
char *Ptr = reinterpret_cast<char*>(malloc(1 << i)); | ||
Pointers[i] = Ptr; | ||
*Ptr = 0; | ||
Ptr[(1 << i) - 1] = 0; | ||
} | ||
|
||
for (unsigned i = 0; i < 16; ++i) { | ||
free(Pointers[i]); | ||
} | ||
|
||
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,18 @@ | ||
// REQUIRES: gwp_asan | ||
// RUN: %clangxx_gwp_asan %s -o %t | ||
// RUN: %expect_crash %run %t 2>&1 | FileCheck %s | ||
|
||
// CHECK: GWP-ASan detected a memory error | ||
// CHECK: Use after free occurred when accessing memory at: | ||
|
||
#include <cstdlib> | ||
|
||
int main() { | ||
char *Ptr = new char; | ||
|
||
*Ptr = 0x0; | ||
|
||
delete Ptr; | ||
volatile char x = *Ptr; | ||
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,20 @@ | ||
// REQUIRES: gwp_asan | ||
// RUN: %clangxx_gwp_asan %s -o %t | ||
// RUN: %expect_crash %run %t 2>&1 | FileCheck %s | ||
|
||
// CHECK: GWP-ASan detected a memory error | ||
// CHECK: Use after free occurred when accessing memory at: | ||
|
||
#include <cstdlib> | ||
|
||
int main() { | ||
char *Ptr = new char[10]; | ||
|
||
for (unsigned i = 0; i < 10; ++i) { | ||
*(Ptr + i) = 0x0; | ||
} | ||
|
||
delete[] Ptr; | ||
volatile char x = *Ptr; | ||
return 0; | ||
} |
Oops, something went wrong.