2017-07-19 03:11:04 +08:00
|
|
|
//===-- sanitizer_allocator_checks.h ----------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-07-19 03:11:04 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Various checks shared between ThreadSanitizer, MemorySanitizer, etc. memory
|
|
|
|
// allocators.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef SANITIZER_ALLOCATOR_CHECKS_H
|
|
|
|
#define SANITIZER_ALLOCATOR_CHECKS_H
|
|
|
|
|
|
|
|
#include "sanitizer_internal_defs.h"
|
|
|
|
#include "sanitizer_common.h"
|
|
|
|
#include "sanitizer_platform.h"
|
|
|
|
|
|
|
|
namespace __sanitizer {
|
|
|
|
|
2017-10-12 05:20:04 +08:00
|
|
|
// The following is defined in a separate compilation unit to avoid pulling in
|
|
|
|
// sanitizer_errno.h in this header, which leads to conflicts when other system
|
|
|
|
// headers include errno.h. This is usually the result of an unlikely event,
|
|
|
|
// and as such we do not care as much about having it inlined.
|
|
|
|
void SetErrnoToENOMEM();
|
|
|
|
|
2017-07-19 03:11:04 +08:00
|
|
|
// A common errno setting logic shared by almost all sanitizer allocator APIs.
|
2020-09-17 22:04:50 +08:00
|
|
|
inline void *SetErrnoOnNull(void *ptr) {
|
2017-07-19 03:11:04 +08:00
|
|
|
if (UNLIKELY(!ptr))
|
2017-10-12 05:20:04 +08:00
|
|
|
SetErrnoToENOMEM();
|
2017-07-19 03:11:04 +08:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In case of the check failure, the caller of the following Check... functions
|
|
|
|
// should "return POLICY::OnBadRequest();" where POLICY is the current allocator
|
|
|
|
// failure handling policy.
|
|
|
|
|
|
|
|
// Checks aligned_alloc() parameters, verifies that the alignment is a power of
|
|
|
|
// two and that the size is a multiple of alignment for POSIX implementation,
|
|
|
|
// and a bit relaxed requirement for non-POSIX ones, that the size is a multiple
|
|
|
|
// of alignment.
|
2020-09-17 22:04:50 +08:00
|
|
|
inline bool CheckAlignedAllocAlignmentAndSize(uptr alignment, uptr size) {
|
2017-07-19 03:11:04 +08:00
|
|
|
#if SANITIZER_POSIX
|
2018-06-09 04:40:35 +08:00
|
|
|
return alignment != 0 && IsPowerOfTwo(alignment) &&
|
|
|
|
(size & (alignment - 1)) == 0;
|
2017-07-19 03:11:04 +08:00
|
|
|
#else
|
2018-06-09 04:40:35 +08:00
|
|
|
return alignment != 0 && size % alignment == 0;
|
2017-07-19 03:11:04 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks posix_memalign() parameters, verifies that alignment is a power of two
|
|
|
|
// and a multiple of sizeof(void *).
|
2020-09-17 22:04:50 +08:00
|
|
|
inline bool CheckPosixMemalignAlignment(uptr alignment) {
|
2018-06-09 04:40:35 +08:00
|
|
|
return alignment != 0 && IsPowerOfTwo(alignment) &&
|
2019-09-12 07:19:48 +08:00
|
|
|
(alignment % sizeof(void *)) == 0;
|
2017-07-19 03:11:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if calloc(size, n) call overflows on size*n calculation.
|
2020-09-17 22:04:50 +08:00
|
|
|
inline bool CheckForCallocOverflow(uptr size, uptr n) {
|
2017-07-19 03:11:04 +08:00
|
|
|
if (!size)
|
|
|
|
return false;
|
|
|
|
uptr max = (uptr)-1L;
|
|
|
|
return (max / size) < n;
|
|
|
|
}
|
|
|
|
|
2017-07-26 05:18:02 +08:00
|
|
|
// Returns true if the size passed to pvalloc overflows when rounded to the next
|
|
|
|
// multiple of page_size.
|
2020-09-17 22:04:50 +08:00
|
|
|
inline bool CheckForPvallocOverflow(uptr size, uptr page_size) {
|
2017-07-26 05:18:02 +08:00
|
|
|
return RoundUpTo(size, page_size) < size;
|
|
|
|
}
|
|
|
|
|
2017-07-19 03:11:04 +08:00
|
|
|
} // namespace __sanitizer
|
|
|
|
|
|
|
|
#endif // SANITIZER_ALLOCATOR_CHECKS_H
|