51 lines · c
1//===-- hwasan_malloc_bisect.h ----------------------------------*- C++ -*-===//2//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-exception6//7//===----------------------------------------------------------------------===//8//9// This file is a part of HWAddressSanitizer.10//11//===----------------------------------------------------------------------===//12 13#include "sanitizer_common/sanitizer_hash.h"14#include "hwasan.h"15 16namespace __hwasan {17 18static u32 malloc_hash(StackTrace *stack, uptr orig_size) {19 uptr len = Min(stack->size, (unsigned)7);20 MurMur2HashBuilder H(len);21 H.add(orig_size);22 // Start with frame #1 to skip __sanitizer_malloc frame, which is23 // (a) almost always the same (well, could be operator new or new[])24 // (b) can change hashes when compiler-rt is rebuilt, invalidating previous25 // bisection results.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);28 return H.get();29}30 31static 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))35 return true;36 if (!stack)37 return true;38 // Allow malloc_bisect_right > (u32)(-1) to avoid spelling the latter in39 // decimal.40 uptr h = (uptr)malloc_hash(stack, orig_size);41 if (h < left || h > right)42 return false;43 if (flags()->malloc_bisect_dump) {44 Printf("[alloc] %u %zu\n", (u32)h, orig_size);45 stack->Print();46 }47 return true;48}49 50} // namespace __hwasan51