]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_allocator_checks.h
dts: Update our device tree sources file fomr Linux 4.13
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / sanitizer_common / sanitizer_allocator_checks.h
1 //===-- sanitizer_allocator_checks.h ----------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Various checks shared between ThreadSanitizer, MemorySanitizer, etc. memory
11 // allocators.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef SANITIZER_ALLOCATOR_CHECKS_H
16 #define SANITIZER_ALLOCATOR_CHECKS_H
17
18 #include "sanitizer_errno.h"
19 #include "sanitizer_internal_defs.h"
20 #include "sanitizer_common.h"
21 #include "sanitizer_platform.h"
22
23 namespace __sanitizer {
24
25 // A common errno setting logic shared by almost all sanitizer allocator APIs.
26 INLINE void *SetErrnoOnNull(void *ptr) {
27   if (UNLIKELY(!ptr))
28     errno = errno_ENOMEM;
29   return ptr;
30 }
31
32 // In case of the check failure, the caller of the following Check... functions
33 // should "return POLICY::OnBadRequest();" where POLICY is the current allocator
34 // failure handling policy.
35
36 // Checks aligned_alloc() parameters, verifies that the alignment is a power of
37 // two and that the size is a multiple of alignment for POSIX implementation,
38 // and a bit relaxed requirement for non-POSIX ones, that the size is a multiple
39 // of alignment.
40 INLINE bool CheckAlignedAllocAlignmentAndSize(uptr alignment, uptr size) {
41 #if SANITIZER_POSIX
42   return IsPowerOfTwo(alignment) && (size & (alignment - 1)) == 0;
43 #else
44   return size % alignment == 0;
45 #endif
46 }
47
48 // Checks posix_memalign() parameters, verifies that alignment is a power of two
49 // and a multiple of sizeof(void *).
50 INLINE bool CheckPosixMemalignAlignment(uptr alignment) {
51   return IsPowerOfTwo(alignment) && (alignment % sizeof(void *)) == 0; // NOLINT
52 }
53
54 // Returns true if calloc(size, n) call overflows on size*n calculation.
55 INLINE bool CheckForCallocOverflow(uptr size, uptr n) {
56   if (!size)
57     return false;
58   uptr max = (uptr)-1L;
59   return (max / size) < n;
60 }
61
62 } // namespace __sanitizer
63
64 #endif  // SANITIZER_ALLOCATOR_CHECKS_H