[asan] speed up small memcpy (> 32 but <= 64 bytes)

llvm-svn: 301837
This commit is contained in:
Kostya Serebryany 2017-05-01 21:05:29 +00:00
parent 8b8c5a0e5a
commit e5ca68cfcd
2 changed files with 35 additions and 0 deletions

View File

@ -37,12 +37,19 @@
namespace __asan {
// Return true if we can quickly decide that the region is unpoisoned.
// We assume that a redzone is at least 16 bytes.
static inline bool QuickCheckForUnpoisonedRegion(uptr beg, uptr size) {
if (size == 0) return true;
if (size <= 32)
return !AddressIsPoisoned(beg) &&
!AddressIsPoisoned(beg + size - 1) &&
!AddressIsPoisoned(beg + size / 2);
if (size <= 64)
return !AddressIsPoisoned(beg) &&
!AddressIsPoisoned(beg + size / 4) &&
!AddressIsPoisoned(beg + size - 1) &&
!AddressIsPoisoned(beg + 3 * size / 4) &&
!AddressIsPoisoned(beg + size / 2);
return false;
}

View File

@ -0,0 +1,28 @@
// Test that small memcpy works correctly.
// RUN: %clangxx_asan %s -o %t
// RUN: not %run %t 8 24 2>&1 | FileCheck %s --check-prefix=CHECK
// RUN: not %run %t 16 32 2>&1 | FileCheck %s --check-prefix=CHECK
// RUN: not %run %t 24 40 2>&1 | FileCheck %s --check-prefix=CHECK
// RUN: not %run %t 32 48 2>&1 | FileCheck %s --check-prefix=CHECK
// RUN: not %run %t 40 56 2>&1 | FileCheck %s --check-prefix=CHECK
// RUN: not %run %t 48 64 2>&1 | FileCheck %s --check-prefix=CHECK
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sanitizer/asan_interface.h>
int main(int argc, char **argv) {
assert(argc == 3);
size_t poison_from = atoi(argv[1]);
size_t poison_to = atoi(argv[2]);
assert(poison_from <= poison_to);
char A1[64], A2[64];
fprintf(stderr, "%zd %zd\n", poison_from, poison_to - poison_from);
__asan_poison_memory_region(&A1[0] + poison_from, poison_to - poison_from);
memcpy(A1, A2, sizeof(A1));
// CHECK: AddressSanitizer: use-after-poison
return 0;
}