1 //===-- hwasan_malloc_bisect.h ----------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is a part of HWAddressSanitizer.
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_common/sanitizer_hash.h"
18 static u32
malloc_hash(StackTrace
*stack
, uptr orig_size
) {
19 uptr len
= Min(stack
->size
, (unsigned)7);
20 MurMur2HashBuilder
H(len
);
22 // Start with frame #1 to skip __sanitizer_malloc frame, which is
23 // (a) almost always the same (well, could be operator new or new[])
24 // (b) can change hashes when compiler-rt is rebuilt, invalidating previous
26 // Because of ASLR, use only offset inside the page.
27 for (uptr i
= 1; i
< len
; ++i
) H
.add(((u32
)stack
->trace
[i
]) & 0xFFF);
31 static inline bool malloc_bisect(StackTrace
*stack
, uptr orig_size
) {
32 uptr left
= flags()->malloc_bisect_left
;
33 uptr right
= flags()->malloc_bisect_right
;
34 if (LIKELY(left
== 0 && right
== 0))
38 // Allow malloc_bisect_right > (u32)(-1) to avoid spelling the latter in
40 uptr h
= (uptr
)malloc_hash(stack
, orig_size
);
41 if (h
< left
|| h
> right
)
43 if (flags()->malloc_bisect_dump
) {
44 Printf("[alloc] %u %zu\n", h
, orig_size
);
50 } // namespace __hwasan