Path: blob/main/contrib/llvm-project/compiler-rt/lib/hwasan/hwasan_malloc_bisect.h
35235 views
//===-- hwasan_malloc_bisect.h ----------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file is a part of HWAddressSanitizer.9//10//===----------------------------------------------------------------------===//1112#include "sanitizer_common/sanitizer_hash.h"13#include "hwasan.h"1415namespace __hwasan {1617static u32 malloc_hash(StackTrace *stack, uptr orig_size) {18uptr len = Min(stack->size, (unsigned)7);19MurMur2HashBuilder H(len);20H.add(orig_size);21// Start with frame #1 to skip __sanitizer_malloc frame, which is22// (a) almost always the same (well, could be operator new or new[])23// (b) can change hashes when compiler-rt is rebuilt, invalidating previous24// bisection results.25// Because of ASLR, use only offset inside the page.26for (uptr i = 1; i < len; ++i) H.add(((u32)stack->trace[i]) & 0xFFF);27return H.get();28}2930static inline bool malloc_bisect(StackTrace *stack, uptr orig_size) {31uptr left = flags()->malloc_bisect_left;32uptr right = flags()->malloc_bisect_right;33if (LIKELY(left == 0 && right == 0))34return true;35if (!stack)36return true;37// Allow malloc_bisect_right > (u32)(-1) to avoid spelling the latter in38// decimal.39uptr h = (uptr)malloc_hash(stack, orig_size);40if (h < left || h > right)41return false;42if (flags()->malloc_bisect_dump) {43Printf("[alloc] %u %zu\n", h, orig_size);44stack->Print();45}46return true;47}4849} // namespace __hwasan505152