xref: /openbsd-src/gnu/llvm/compiler-rt/lib/sanitizer_common/sanitizer_allocator_checks.h (revision d89ec533011f513df1010f142a111086a0785f09)
13cab2bb3Spatrick //===-- sanitizer_allocator_checks.h ----------------------------*- C++ -*-===//
23cab2bb3Spatrick //
33cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information.
53cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63cab2bb3Spatrick //
73cab2bb3Spatrick //===----------------------------------------------------------------------===//
83cab2bb3Spatrick //
93cab2bb3Spatrick // Various checks shared between ThreadSanitizer, MemorySanitizer, etc. memory
103cab2bb3Spatrick // allocators.
113cab2bb3Spatrick //
123cab2bb3Spatrick //===----------------------------------------------------------------------===//
133cab2bb3Spatrick 
143cab2bb3Spatrick #ifndef SANITIZER_ALLOCATOR_CHECKS_H
153cab2bb3Spatrick #define SANITIZER_ALLOCATOR_CHECKS_H
163cab2bb3Spatrick 
173cab2bb3Spatrick #include "sanitizer_internal_defs.h"
183cab2bb3Spatrick #include "sanitizer_common.h"
193cab2bb3Spatrick #include "sanitizer_platform.h"
203cab2bb3Spatrick 
213cab2bb3Spatrick namespace __sanitizer {
223cab2bb3Spatrick 
233cab2bb3Spatrick // The following is defined in a separate compilation unit to avoid pulling in
243cab2bb3Spatrick // sanitizer_errno.h in this header, which leads to conflicts when other system
253cab2bb3Spatrick // headers include errno.h. This is usually the result of an unlikely event,
263cab2bb3Spatrick // and as such we do not care as much about having it inlined.
273cab2bb3Spatrick void SetErrnoToENOMEM();
283cab2bb3Spatrick 
293cab2bb3Spatrick // A common errno setting logic shared by almost all sanitizer allocator APIs.
SetErrnoOnNull(void * ptr)30*d89ec533Spatrick inline void *SetErrnoOnNull(void *ptr) {
313cab2bb3Spatrick   if (UNLIKELY(!ptr))
323cab2bb3Spatrick     SetErrnoToENOMEM();
333cab2bb3Spatrick   return ptr;
343cab2bb3Spatrick }
353cab2bb3Spatrick 
363cab2bb3Spatrick // In case of the check failure, the caller of the following Check... functions
373cab2bb3Spatrick // should "return POLICY::OnBadRequest();" where POLICY is the current allocator
383cab2bb3Spatrick // failure handling policy.
393cab2bb3Spatrick 
403cab2bb3Spatrick // Checks aligned_alloc() parameters, verifies that the alignment is a power of
413cab2bb3Spatrick // two and that the size is a multiple of alignment for POSIX implementation,
423cab2bb3Spatrick // and a bit relaxed requirement for non-POSIX ones, that the size is a multiple
433cab2bb3Spatrick // of alignment.
CheckAlignedAllocAlignmentAndSize(uptr alignment,uptr size)44*d89ec533Spatrick inline bool CheckAlignedAllocAlignmentAndSize(uptr alignment, uptr size) {
453cab2bb3Spatrick #if SANITIZER_POSIX
463cab2bb3Spatrick   return alignment != 0 && IsPowerOfTwo(alignment) &&
473cab2bb3Spatrick          (size & (alignment - 1)) == 0;
483cab2bb3Spatrick #else
493cab2bb3Spatrick   return alignment != 0 && size % alignment == 0;
503cab2bb3Spatrick #endif
513cab2bb3Spatrick }
523cab2bb3Spatrick 
533cab2bb3Spatrick // Checks posix_memalign() parameters, verifies that alignment is a power of two
543cab2bb3Spatrick // and a multiple of sizeof(void *).
CheckPosixMemalignAlignment(uptr alignment)55*d89ec533Spatrick inline bool CheckPosixMemalignAlignment(uptr alignment) {
563cab2bb3Spatrick   return alignment != 0 && IsPowerOfTwo(alignment) &&
573cab2bb3Spatrick          (alignment % sizeof(void *)) == 0;
583cab2bb3Spatrick }
593cab2bb3Spatrick 
603cab2bb3Spatrick // Returns true if calloc(size, n) call overflows on size*n calculation.
CheckForCallocOverflow(uptr size,uptr n)61*d89ec533Spatrick inline bool CheckForCallocOverflow(uptr size, uptr n) {
623cab2bb3Spatrick   if (!size)
633cab2bb3Spatrick     return false;
643cab2bb3Spatrick   uptr max = (uptr)-1L;
653cab2bb3Spatrick   return (max / size) < n;
663cab2bb3Spatrick }
673cab2bb3Spatrick 
683cab2bb3Spatrick // Returns true if the size passed to pvalloc overflows when rounded to the next
693cab2bb3Spatrick // multiple of page_size.
CheckForPvallocOverflow(uptr size,uptr page_size)70*d89ec533Spatrick inline bool CheckForPvallocOverflow(uptr size, uptr page_size) {
713cab2bb3Spatrick   return RoundUpTo(size, page_size) < size;
723cab2bb3Spatrick }
733cab2bb3Spatrick 
743cab2bb3Spatrick } // namespace __sanitizer
753cab2bb3Spatrick 
763cab2bb3Spatrick #endif  // SANITIZER_ALLOCATOR_CHECKS_H
77