hwasan_malloc_bisect.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is a part of HWAddressSanitizer.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "sanitizer_common/sanitizer_hash.h"
  13. #include "hwasan.h"
  14. namespace __hwasan {
  15. static u32 malloc_hash(StackTrace *stack, uptr orig_size) {
  16. uptr len = Min(stack->size, (unsigned)7);
  17. MurMur2HashBuilder H(len);
  18. H.add(orig_size);
  19. // Start with frame #1 to skip __sanitizer_malloc frame, which is
  20. // (a) almost always the same (well, could be operator new or new[])
  21. // (b) can change hashes when compiler-rt is rebuilt, invalidating previous
  22. // bisection results.
  23. // Because of ASLR, use only offset inside the page.
  24. for (uptr i = 1; i < len; ++i) H.add(((u32)stack->trace[i]) & 0xFFF);
  25. return H.get();
  26. }
  27. static inline bool malloc_bisect(StackTrace *stack, uptr orig_size) {
  28. uptr left = flags()->malloc_bisect_left;
  29. uptr right = flags()->malloc_bisect_right;
  30. if (LIKELY(left == 0 && right == 0))
  31. return true;
  32. if (!stack)
  33. return true;
  34. // Allow malloc_bisect_right > (u32)(-1) to avoid spelling the latter in
  35. // decimal.
  36. uptr h = (uptr)malloc_hash(stack, orig_size);
  37. if (h < left || h > right)
  38. return false;
  39. if (flags()->malloc_bisect_dump) {
  40. Printf("[alloc] %u %zu\n", h, orig_size);
  41. stack->Print();
  42. }
  43. return true;
  44. }
  45. } // namespace __hwasan