Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/remove-promote-change-addr-space…
Browse files Browse the repository at this point in the history
…' into amd-hcc
  • Loading branch information
scchan committed Apr 3, 2017
2 parents 910d41a + 2589bd7 commit fb18901
Show file tree
Hide file tree
Showing 18 changed files with 172 additions and 22 deletions.
12 changes: 12 additions & 0 deletions lib/asan/asan_allocator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,18 @@ struct Allocator {
AsanThread *t = GetCurrentThread();
m->free_tid = t ? t->tid() : 0;
m->free_context_id = StackDepotPut(*stack);

Flags &fl = *flags();
if (fl.max_free_fill_size > 0) {
// We have to skip the chunk header, it contains free_context_id.
uptr scribble_start = (uptr)m + kChunkHeaderSize + kChunkHeader2Size;
if (m->UsedSize() >= kChunkHeader2Size) { // Skip Header2 in user area.
uptr size_to_fill = m->UsedSize() - kChunkHeader2Size;
size_to_fill = Min(size_to_fill, (uptr)fl.max_free_fill_size);
REAL(memset)((void *)scribble_start, fl.free_fill_byte, size_to_fill);
}
}

// Poison the region.
PoisonShadow(m->Beg(),
RoundUpTo(m->UsedSize(), SHADOW_GRANULARITY),
Expand Down
12 changes: 12 additions & 0 deletions lib/asan/asan_flags.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ void InitializeFlags() {
RegisterCommonFlags(&ubsan_parser);
#endif

if (SANITIZER_MAC) {
// Support macOS MallocScribble and MallocPreScribble:
// <https://developer.apple.com/library/content/documentation/Performance/
// Conceptual/ManagingMemory/Articles/MallocDebug.html>
if (GetEnv("MallocScribble")) {
f->max_free_fill_size = 0x1000;
}
if (GetEnv("MallocPreScribble")) {
f->malloc_fill_byte = 0xaa;
}
}

// Override from ASan compile definition.
const char *asan_compile_def = MaybeUseAsanDefaultOptionsCompileDefinition();
asan_parser.ParseString(asan_compile_def);
Expand Down
9 changes: 9 additions & 0 deletions lib/asan/asan_flags.inc
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,14 @@ ASAN_FLAG(
int, max_malloc_fill_size, 0x1000, // By default, fill only the first 4K.
"ASan allocator flag. max_malloc_fill_size is the maximal amount of "
"bytes that will be filled with malloc_fill_byte on malloc.")
ASAN_FLAG(
int, max_free_fill_size, 0,
"ASan allocator flag. max_free_fill_size is the maximal amount of "
"bytes that will be filled with free_fill_byte during free.")
ASAN_FLAG(int, malloc_fill_byte, 0xbe,
"Value used to fill the newly allocated memory.")
ASAN_FLAG(int, free_fill_byte, 0x55,
"Value used to fill deallocated memory.")
ASAN_FLAG(bool, allow_user_poisoning, true,
"If set, user may manually mark memory regions as poisoned or "
"unpoisoned.")
Expand Down Expand Up @@ -152,3 +158,6 @@ ASAN_FLAG(bool, allocator_frees_and_returns_null_on_realloc_zero, true,
"realloc(p, 0) is equivalent to free(p) by default (Same as the "
"POSIX standard). If set to false, realloc(p, 0) will return a "
"pointer to an allocated space which can not be used.")
ASAN_FLAG(bool, verify_asan_link_order, true,
"Check position of ASan runtime in library list (needs to be disabled"
" when other library has to be preloaded system-wide)")
2 changes: 1 addition & 1 deletion lib/asan/asan_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ static void ReportIncompatibleRT() {
}

void AsanCheckDynamicRTPrereqs() {
if (!ASAN_DYNAMIC)
if (!ASAN_DYNAMIC || !flags()->verify_asan_link_order)
return;

// Ensure that dynamic RT is the first DSO in the list
Expand Down
3 changes: 1 addition & 2 deletions lib/sanitizer_common/sanitizer_flags.inc
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ COMMON_FLAG(
COMMON_FLAG(
int, verbosity, 0,
"Verbosity level (0 - silent, 1 - a bit of output, 2+ - more output).")
COMMON_FLAG(bool, detect_leaks, SANITIZER_WORDSIZE == 64,
"Enable memory leak detection.")
COMMON_FLAG(bool, detect_leaks, true, "Enable memory leak detection.")
COMMON_FLAG(
bool, leak_check_at_exit, true,
"Invoke leak checking in an atexit handler. Has no effect if "
Expand Down
6 changes: 6 additions & 0 deletions lib/tsan/rtl/tsan_interceptors_mac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,12 @@ TSAN_INTERCEPTOR(void, xpc_connection_send_message_with_reply,
(connection, message, replyq, new_handler);
}

TSAN_INTERCEPTOR(void, xpc_connection_cancel, xpc_connection_t connection) {
SCOPED_TSAN_INTERCEPTOR(xpc_connection_cancel, connection);
Release(thr, pc, (uptr)connection);
REAL(xpc_connection_cancel)(connection);
}

// On macOS, libc++ is always linked dynamically, so intercepting works the
// usual way.
#define STDCXX_INTERCEPTOR TSAN_INTERCEPTOR
Expand Down
57 changes: 57 additions & 0 deletions test/asan/TestCases/Darwin/scribble.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// RUN: %clang_asan -O2 %s -o %t
// RUN: %run %t 2>&1 | FileCheck --check-prefix=CHECK-NOSCRIBBLE %s
// RUN: env MallocScribble=1 MallocPreScribble=1 %run %t 2>&1 | FileCheck --check-prefix=CHECK-SCRIBBLE %s
// RUN: %env_asan_opts=max_free_fill_size=4096 %run %t 2>&1 | FileCheck --check-prefix=CHECK-SCRIBBLE %s

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Isa {
const char *class_name;
};

struct MyClass {
long padding;
Isa *isa;
long data;

void print_my_class_name();
};

__attribute__((no_sanitize("address")))
void MyClass::print_my_class_name() {
fprintf(stderr, "this = %p\n", this);
fprintf(stderr, "padding = 0x%lx\n", this->padding);
fprintf(stderr, "isa = %p\n", this->isa);

if ((uint32_t)(uintptr_t)this->isa != 0x55555555) {
fprintf(stderr, "class name: %s\n", this->isa->class_name);
}
}

int main() {
Isa *my_class_isa = (Isa *)malloc(sizeof(Isa));
memset(my_class_isa, 0x77, sizeof(Isa));
my_class_isa->class_name = "MyClass";

MyClass *my_object = (MyClass *)malloc(sizeof(MyClass));
memset(my_object, 0x88, sizeof(MyClass));
my_object->isa = my_class_isa;
my_object->data = 42;

my_object->print_my_class_name();
// CHECK-SCRIBBLE: class name: MyClass
// CHECK-NOSCRIBBLE: class name: MyClass

free(my_object);

my_object->print_my_class_name();
// CHECK-NOSCRIBBLE: class name: MyClass
// CHECK-SCRIBBLE: isa = {{(0x)?}}{{5555555555555555|55555555}}

fprintf(stderr, "okthxbai!\n");
// CHECK-SCRIBBLE: okthxbai!
// CHECK-NOSCRIBBLE: okthxbai!
}
2 changes: 2 additions & 0 deletions test/asan/TestCases/Linux/asan_dlopen_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//
// RUN: %clangxx %s -DRT=\"%shared_libasan\" -o %t -ldl
// RUN: not %run %t 2>&1 | FileCheck %s
// RUN: %env_asan_opts=verify_asan_link_order=true not %run %t 2>&1 | FileCheck %s
// RUN: %env_asan_opts=verify_asan_link_order=false %run %t 2>&1
// REQUIRES: asan-dynamic-runtime
// XFAIL: android

Expand Down
10 changes: 5 additions & 5 deletions test/asan/TestCases/Linux/coverage-missing.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
// RUN: %env_asan_opts=coverage=1:coverage_dir=%T/coverage-missing %run %t
// RUN: %sancov print *.sancov > main.txt
// RUN: rm *.sancov
// RUN: [ $(cat main.txt | wc -l) == 1 ]
// RUN: count 1 < main.txt
// RUN: %env_asan_opts=coverage=1:coverage_dir=%T/coverage-missing %run %t x
// RUN: %sancov print *.sancov > foo.txt
// RUN: rm *.sancov
// RUN: [ $(cat foo.txt | wc -l) == 3 ]
// RUN: count 3 < foo.txt
// RUN: %env_asan_opts=coverage=1:coverage_dir=%T/coverage-missing %run %t x x
// RUN: %sancov print *.sancov > bar.txt
// RUN: rm *.sancov
// RUN: [ $(cat bar.txt | wc -l) == 4 ]
// RUN: count 4 < bar.txt
// RUN: %sancov missing %t < foo.txt > foo-missing.txt
// RUN: sort main.txt foo-missing.txt -o foo-missing-with-main.txt
// The "missing from foo" set may contain a few bogus PCs from the sanitizer
Expand All @@ -35,11 +35,11 @@
// RUN: %env_asan_opts=coverage=1:coverage_dir=%T/coverage-missing %run %t x
// RUN: %sancov print $LIBNAME.*.sancov > foo.txt
// RUN: rm *.sancov
// RUN: [ $(cat foo.txt | wc -l) == 2 ]
// RUN: count 2 < foo.txt
// RUN: %env_asan_opts=coverage=1:coverage_dir=%T/coverage-missing %run %t x x
// RUN: %sancov print $LIBNAME.*.sancov > bar.txt
// RUN: rm *.sancov
// RUN: [ $(cat bar.txt | wc -l) == 3 ]
// RUN: count 3 < bar.txt
// RUN: %sancov missing %dynamiclib < foo.txt > foo-missing.txt
// RUN: ( diff bar.txt foo-missing.txt || true ) | not grep "^<"

Expand Down
3 changes: 2 additions & 1 deletion test/asan/TestCases/Posix/closed-fds.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// symbolizer still works.

// RUN: rm -f %t.log.*
// RUN: %clangxx_asan -O0 %s -o %t 2>&1 && %env_asan_opts=log_path='"%t.log"':verbosity=2 not %run %t 2>&1
// RUN: %clangxx_asan -O0 %s -o %t
// RUN: %env_asan_opts=log_path='"%t.log"':verbosity=2 not %run %t
// RUN: FileCheck %s --check-prefix=CHECK-FILE < %t.log.*

// FIXME: copy %t.log back from the device and re-enable on Android.
Expand Down
3 changes: 2 additions & 1 deletion test/asan/TestCases/Posix/coverage-maybe-open-file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// RUN: mkdir -p %T/coverage-maybe-open-file && cd %T/coverage-maybe-open-file
// RUN: %env_asan_opts=coverage=1 %run %t | FileCheck %s --check-prefix=CHECK-success
// RUN: %env_asan_opts=coverage=0 %run %t | FileCheck %s --check-prefix=CHECK-fail
// RUN: [ "$(cat test.sancov.packed)" == "test" ]
// RUN: FileCheck %s < test.sancov.packed -implicit-check-not={{.}} --check-prefix=CHECK-test
// RUN: cd .. && rm -rf %T/coverage-maybe-open-file

#include <stdio.h>
Expand All @@ -30,3 +30,4 @@ int main(int argc, char **argv) {

// CHECK-success: SUCCESS
// CHECK-fail: FAIL
// CHECK-test: {{^}}test{{$}}
12 changes: 6 additions & 6 deletions test/asan/TestCases/Posix/coverage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
// RUN: %clangxx_asan -fsanitize-coverage=func %s %ld_flags_rpath_exe -o %t
// RUN: rm -rf %T/coverage && mkdir -p %T/coverage && cd %T/coverage
// RUN: %env_asan_opts=coverage=1:verbosity=1 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-main
// RUN: %sancov print `ls coverage.*sancov | grep -v '.so'` 2>&1 | FileCheck %s --check-prefix=CHECK-SANCOV1
// RUN: %sancov print coverage.*sancov 2>&1 | FileCheck %s --check-prefix=CHECK-SANCOV1
// RUN: %env_asan_opts=coverage=1:verbosity=1 %run %t foo 2>&1 | FileCheck %s --check-prefix=CHECK-foo
// RUN: %sancov print `ls coverage.*sancov | grep -v '.so'` 2>&1 | FileCheck %s --check-prefix=CHECK-SANCOV2
// RUN: %sancov print coverage.*sancov 2>&1 | FileCheck %s --check-prefix=CHECK-SANCOV2
// RUN: %env_asan_opts=coverage=1:verbosity=1 %run %t bar 2>&1 | FileCheck %s --check-prefix=CHECK-bar
// RUN: %sancov print `ls *coverage.*sancov | grep -v '.so'` 2>&1 | FileCheck %s --check-prefix=CHECK-SANCOV2
// RUN: %sancov print coverage.*sancov 2>&1 | FileCheck %s --check-prefix=CHECK-SANCOV2
// RUN: %env_asan_opts=coverage=1:verbosity=1 %run %t foo bar 2>&1 | FileCheck %s --check-prefix=CHECK-foo-bar
// RUN: %sancov print `ls *coverage.*sancov | grep -v '.so'` 2>&1 | FileCheck %s --check-prefix=CHECK-SANCOV2
// RUN: %sancov print `ls *coverage.*sancov | grep '.so'` 2>&1 | FileCheck %s --check-prefix=CHECK-SANCOV1
// RUN: %sancov merge `ls *coverage.*sancov | grep -v '.so'` > merged-cov
// RUN: %sancov print coverage.*sancov 2>&1 | FileCheck %s --check-prefix=CHECK-SANCOV2
// RUN: %sancov print libcoverage.*sancov 2>&1 | FileCheck %s --check-prefix=CHECK-SANCOV1
// RUN: %sancov merge coverage.*sancov > merged-cov
// RUN: %sancov print merged-cov 2>&1 | FileCheck %s --check-prefix=CHECK-SANCOV2
// RUN: %env_asan_opts=coverage=1:verbosity=1 not %run %t foo bar 4 2>&1 | FileCheck %s --check-prefix=CHECK-report
// RUN: %env_asan_opts=coverage=1:verbosity=1 not %run %t foo bar 4 5 2>&1 | FileCheck %s --check-prefix=CHECK-segv
Expand Down
2 changes: 1 addition & 1 deletion test/asan/TestCases/Posix/halt_on_error-torture.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// RUN: rm -f 1.txt
// RUN: %env_asan_opts=halt_on_error=false:suppress_equal_pcs=false %run %t 1 10 >>1.txt 2>&1
// RUN: FileCheck %s < 1.txt
// RUN: [ $(grep -c 'ERROR: AddressSanitizer: use-after-poison' 1.txt) -eq 10 ]
// RUN: grep 'ERROR: AddressSanitizer: use-after-poison' 1.txt | count 10
// RUN: FileCheck --check-prefix=CHECK-NO-COLLISION %s < 1.txt
//
// Collisions are unlikely but still possible so we need the ||.
Expand Down
4 changes: 2 additions & 2 deletions test/asan/TestCases/Posix/halt_on_error_suppress_equal_pcs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
// Check that we die after reaching different reports number threshold.
// RUN: rm -f %t1.log
// RUN: %env_asan_opts=halt_on_error=false not %run %t 1 >> %t1.log 2>&1
// RUN: [ $(grep -c 'ERROR: AddressSanitizer: stack-buffer-overflow' %t1.log) -eq 25 ]
// RUN: grep 'ERROR: AddressSanitizer: stack-buffer-overflow' %t1.log | count 25
//
// Check suppress_equal_pcs=true behavior is equal to default one.
// RUN: %env_asan_opts=halt_on_error=false:suppress_equal_pcs=true %run %t 2>&1 | FileCheck %s
//
// Check suppress_equal_pcs=false behavior isn't equal to default one.
// RUN: rm -f %t2.log
// RUN: %env_asan_opts=halt_on_error=false:suppress_equal_pcs=false %run %t >> %t2.log 2>&1
// RUN: [ $(grep -c 'ERROR: AddressSanitizer: stack-buffer-overflow' %t2.log) -eq 30 ]
// RUN: grep 'ERROR: AddressSanitizer: stack-buffer-overflow' %t2.log | count 30

#define ACCESS_ARRAY_FIVE_ELEMENTS(array, i) \
array[i] = i; \
Expand Down
4 changes: 4 additions & 0 deletions test/asan/TestCases/use-after-scope.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// RUN: %clangxx_asan -O1 -fsanitize-address-use-after-scope %s -o %t && \
// RUN: not %run %t 2>&1 | FileCheck %s

// -fsanitize-address-use-after-scope is now on by default:
// RUN: %clangxx_asan -O1 %s -o %t && \
// RUN: not %run %t 2>&1 | FileCheck %s

volatile int *p = 0;

int main() {
Expand Down
1 change: 0 additions & 1 deletion test/esan/TestCases/workingset-samples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <sys/mman.h>

const int size = 0x1 << 25; // 523288 cache lines
const int iters = 6;

int main(int argc, char **argv) {
char *buf = (char *)mmap(0, size, PROT_READ | PROT_WRITE,
Expand Down
15 changes: 13 additions & 2 deletions test/lit.common.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,19 @@ import subprocess
import lit.formats
import lit.util

# Setup test format. Use bash on Unix and the lit shell on Windows.
execute_external = (not sys.platform in ['win32'])

# Choose between lit's internal shell pipeline runner and a real shell. If
# LIT_USE_INTERNAL_SHELL is in the environment, we use that as an override.
use_lit_shell = os.environ.get("LIT_USE_INTERNAL_SHELL")
if use_lit_shell:
# 0 is external, "" is default, and everything else is internal.
execute_external = (use_lit_shell == "0")
else:
# Otherwise we default to internal on Windows and external elsewhere, as
# bash on Windows is usually very slow.
execute_external = (not sys.platform in ['win32'])

# Setup test format.
config.test_format = lit.formats.ShTest(execute_external)
if execute_external:
config.available_features.add('shell')
Expand Down
37 changes: 37 additions & 0 deletions test/tsan/Darwin/xpc-cancel.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// RUN: %clang_tsan %s -o %t -framework Foundation
// RUN: %run %t 2>&1 | FileCheck %s

#import <Foundation/Foundation.h>
#import <xpc/xpc.h>

long global;

int main(int argc, const char *argv[]) {
fprintf(stderr, "Hello world.\n");

dispatch_queue_t server_q = dispatch_queue_create("server.queue", DISPATCH_QUEUE_CONCURRENT);
xpc_connection_t server_conn = xpc_connection_create(NULL, server_q);

xpc_connection_set_event_handler(server_conn, ^(xpc_object_t client) {
if (client == XPC_ERROR_CONNECTION_INTERRUPTED || client == XPC_ERROR_CONNECTION_INVALID) {
global = 43;

dispatch_async(dispatch_get_main_queue(), ^{
CFRunLoopStop(CFRunLoopGetCurrent());
});
}
});
xpc_connection_resume(server_conn);

global = 42;

xpc_connection_cancel(server_conn);

CFRunLoopRun();

fprintf(stderr, "Done.\n");
}

// CHECK: Hello world.
// CHECK-NOT: WARNING: ThreadSanitizer
// CHECK: Done.

0 comments on commit fb18901

Please sign in to comment.