Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reviewers: kcc, pcc Subscribers: kubamracek, mgorny, jdoerfert, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D58162 llvm-svn: 354156
- Loading branch information
Showing
8 changed files
with
141 additions
and
19 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,50 @@ | ||
//===-- hwasan_malloc_bisect.h ----------------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file is a part of HWAddressSanitizer. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "sanitizer_common/sanitizer_hash.h" | ||
#include "hwasan.h" | ||
|
||
namespace __hwasan { | ||
|
||
static u32 malloc_hash(StackTrace *stack, uptr orig_size) { | ||
uptr len = Min(stack->size, (unsigned)7); | ||
MurMur2HashBuilder H(len); | ||
H.add(orig_size); | ||
// Start with frame #1 to skip __sanitizer_malloc frame, which is | ||
// (a) almost always the same (well, could be operator new or new[]) | ||
// (b) can change hashes when compiler-rt is rebuilt, invalidating previous | ||
// bisection results. | ||
// Because of ASLR, use only offset inside the page. | ||
for (uptr i = 1; i < len; ++i) H.add(((u32)stack->trace[i]) & 0xFFF); | ||
return H.get(); | ||
} | ||
|
||
static INLINE bool malloc_bisect(StackTrace *stack, uptr orig_size) { | ||
uptr left = flags()->malloc_bisect_left; | ||
uptr right = flags()->malloc_bisect_right; | ||
if (LIKELY(left == 0 && right == 0)) | ||
return true; | ||
if (!stack) | ||
return true; | ||
// Allow malloc_bisect_right > (u32)(-1) to avoid spelling the latter in | ||
// decimal. | ||
uptr h = (uptr)malloc_hash(stack, orig_size); | ||
if (h < left || h > right) | ||
return false; | ||
if (flags()->malloc_bisect_dump) { | ||
Printf("[alloc] %u %zu\n", h, orig_size); | ||
stack->Print(); | ||
} | ||
return true; | ||
} | ||
|
||
} // namespace __hwasan |
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,43 @@ | ||
//===-- sanitizer_common.h --------------------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file implements a simple hash function. | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef SANITIZER_HASH_H | ||
#define SANITIZER_HASH_H | ||
|
||
#include "sanitizer_internal_defs.h" | ||
|
||
namespace __sanitizer { | ||
class MurMur2HashBuilder { | ||
static const u32 m = 0x5bd1e995; | ||
static const u32 seed = 0x9747b28c; | ||
static const u32 r = 24; | ||
u32 h; | ||
|
||
public: | ||
explicit MurMur2HashBuilder(u32 init = 0) { h = seed ^ init; } | ||
void add(u32 k) { | ||
k *= m; | ||
k ^= k >> r; | ||
k *= m; | ||
h *= m; | ||
h ^= k; | ||
} | ||
u32 get() { | ||
u32 x = h; | ||
x ^= x >> 13; | ||
x *= m; | ||
x ^= x >> 15; | ||
return x; | ||
} | ||
}; | ||
} //namespace __sanitizer | ||
|
||
#endif // SANITIZER_HASH_H |
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,26 @@ | ||
// RUN: %clang_hwasan -O0 %s -o %t | ||
// RUN: %env_hwasan_opts=malloc_bisect_left=0,malloc_bisect_right=0 not %run %t 2>&1 | \ | ||
// RUN: FileCheck %s --check-prefix=CRASH | ||
// RUN: %env_hwasan_opts=malloc_bisect_left=1000,malloc_bisect_right=999 %run %t 2>&1 | ||
// RUN: %env_hwasan_opts=malloc_bisect_left=0,malloc_bisect_right=4294967295 not %run %t 2>&1 | \ | ||
// RUN: FileCheck %s --check-prefix=CRASH | ||
// RUN: %env_hwasan_opts=malloc_bisect_left=0,malloc_bisect_right=4294967295,malloc_bisect_dump=1 not %run %t 2>&1 | \ | ||
// RUN: FileCheck %s --check-prefixes=CRASH,DUMP | ||
|
||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <sanitizer/hwasan_interface.h> | ||
|
||
int main() { | ||
__hwasan_enable_allocator_tagging(); | ||
// DUMP: [alloc] {{.*}} 10{{$}} | ||
// DUMP: in main{{.*}}malloc_bisect.c | ||
char * volatile p = (char*)malloc(10); | ||
// CRASH: HWAddressSanitizer: tag-mismatch on address | ||
// CRASH: in main{{.*}}malloc_bisect.c | ||
char volatile x = p[16]; | ||
free(p); | ||
__hwasan_disable_allocator_tagging(); | ||
|
||
return 0; | ||
} |