sanitizer_allocator_checks.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //===-- sanitizer_allocator_checks.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. // Various checks shared between ThreadSanitizer, MemorySanitizer, etc. memory
  10. // allocators.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef SANITIZER_ALLOCATOR_CHECKS_H
  14. #define SANITIZER_ALLOCATOR_CHECKS_H
  15. #include "sanitizer_internal_defs.h"
  16. #include "sanitizer_common.h"
  17. #include "sanitizer_platform.h"
  18. namespace __sanitizer {
  19. // The following is defined in a separate compilation unit to avoid pulling in
  20. // sanitizer_errno.h in this header, which leads to conflicts when other system
  21. // headers include errno.h. This is usually the result of an unlikely event,
  22. // and as such we do not care as much about having it inlined.
  23. void SetErrnoToENOMEM();
  24. // A common errno setting logic shared by almost all sanitizer allocator APIs.
  25. inline void *SetErrnoOnNull(void *ptr) {
  26. if (UNLIKELY(!ptr))
  27. SetErrnoToENOMEM();
  28. return ptr;
  29. }
  30. // In case of the check failure, the caller of the following Check... functions
  31. // should "return POLICY::OnBadRequest();" where POLICY is the current allocator
  32. // failure handling policy.
  33. // Checks aligned_alloc() parameters, verifies that the alignment is a power of
  34. // two and that the size is a multiple of alignment for POSIX implementation,
  35. // and a bit relaxed requirement for non-POSIX ones, that the size is a multiple
  36. // of alignment.
  37. inline bool CheckAlignedAllocAlignmentAndSize(uptr alignment, uptr size) {
  38. #if SANITIZER_POSIX
  39. return alignment != 0 && IsPowerOfTwo(alignment) &&
  40. (size & (alignment - 1)) == 0;
  41. #else
  42. return alignment != 0 && size % alignment == 0;
  43. #endif
  44. }
  45. // Checks posix_memalign() parameters, verifies that alignment is a power of two
  46. // and a multiple of sizeof(void *).
  47. inline bool CheckPosixMemalignAlignment(uptr alignment) {
  48. return alignment != 0 && IsPowerOfTwo(alignment) &&
  49. (alignment % sizeof(void *)) == 0;
  50. }
  51. // Returns true if calloc(size, n) call overflows on size*n calculation.
  52. inline bool CheckForCallocOverflow(uptr size, uptr n) {
  53. if (!size)
  54. return false;
  55. uptr max = (uptr)-1L;
  56. return (max / size) < n;
  57. }
  58. // Returns true if the size passed to pvalloc overflows when rounded to the next
  59. // multiple of page_size.
  60. inline bool CheckForPvallocOverflow(uptr size, uptr page_size) {
  61. return RoundUpTo(size, page_size) < size;
  62. }
  63. } // namespace __sanitizer
  64. #endif // SANITIZER_ALLOCATOR_CHECKS_H