1*76559068SAndroid Build Coastguard Worker //===-- wrappers_c_checks.h -------------------------------------*- C++ -*-===//
2*76559068SAndroid Build Coastguard Worker //
3*76559068SAndroid Build Coastguard Worker // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*76559068SAndroid Build Coastguard Worker // See https://llvm.org/LICENSE.txt for license information.
5*76559068SAndroid Build Coastguard Worker // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*76559068SAndroid Build Coastguard Worker //
7*76559068SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
8*76559068SAndroid Build Coastguard Worker
9*76559068SAndroid Build Coastguard Worker #ifndef SCUDO_CHECKS_H_
10*76559068SAndroid Build Coastguard Worker #define SCUDO_CHECKS_H_
11*76559068SAndroid Build Coastguard Worker
12*76559068SAndroid Build Coastguard Worker #include "common.h"
13*76559068SAndroid Build Coastguard Worker
14*76559068SAndroid Build Coastguard Worker #include <errno.h>
15*76559068SAndroid Build Coastguard Worker
16*76559068SAndroid Build Coastguard Worker #ifndef __has_builtin
17*76559068SAndroid Build Coastguard Worker #define __has_builtin(X) 0
18*76559068SAndroid Build Coastguard Worker #endif
19*76559068SAndroid Build Coastguard Worker
20*76559068SAndroid Build Coastguard Worker namespace scudo {
21*76559068SAndroid Build Coastguard Worker
22*76559068SAndroid Build Coastguard Worker // A common errno setting logic shared by almost all Scudo C wrappers.
setErrnoOnNull(void * Ptr)23*76559068SAndroid Build Coastguard Worker inline void *setErrnoOnNull(void *Ptr) {
24*76559068SAndroid Build Coastguard Worker if (UNLIKELY(!Ptr))
25*76559068SAndroid Build Coastguard Worker errno = ENOMEM;
26*76559068SAndroid Build Coastguard Worker return Ptr;
27*76559068SAndroid Build Coastguard Worker }
28*76559068SAndroid Build Coastguard Worker
29*76559068SAndroid Build Coastguard Worker // Checks return true on failure.
30*76559068SAndroid Build Coastguard Worker
31*76559068SAndroid Build Coastguard Worker // Checks aligned_alloc() parameters, verifies that the alignment is a power of
32*76559068SAndroid Build Coastguard Worker // two and that the size is a multiple of alignment.
checkAlignedAllocAlignmentAndSize(uptr Alignment,uptr Size)33*76559068SAndroid Build Coastguard Worker inline bool checkAlignedAllocAlignmentAndSize(uptr Alignment, uptr Size) {
34*76559068SAndroid Build Coastguard Worker return !isPowerOfTwo(Alignment) || !isAligned(Size, Alignment);
35*76559068SAndroid Build Coastguard Worker }
36*76559068SAndroid Build Coastguard Worker
37*76559068SAndroid Build Coastguard Worker // Checks posix_memalign() parameters, verifies that alignment is a power of two
38*76559068SAndroid Build Coastguard Worker // and a multiple of sizeof(void *).
checkPosixMemalignAlignment(uptr Alignment)39*76559068SAndroid Build Coastguard Worker inline bool checkPosixMemalignAlignment(uptr Alignment) {
40*76559068SAndroid Build Coastguard Worker return !isPowerOfTwo(Alignment) || !isAligned(Alignment, sizeof(void *));
41*76559068SAndroid Build Coastguard Worker }
42*76559068SAndroid Build Coastguard Worker
43*76559068SAndroid Build Coastguard Worker // Returns true if calloc(Size, N) overflows on Size*N calculation. Use a
44*76559068SAndroid Build Coastguard Worker // builtin supported by recent clang & GCC if it exists, otherwise fallback to a
45*76559068SAndroid Build Coastguard Worker // costly division.
checkForCallocOverflow(uptr Size,uptr N,uptr * Product)46*76559068SAndroid Build Coastguard Worker inline bool checkForCallocOverflow(uptr Size, uptr N, uptr *Product) {
47*76559068SAndroid Build Coastguard Worker #if __has_builtin(__builtin_umull_overflow) && (SCUDO_WORDSIZE == 64U)
48*76559068SAndroid Build Coastguard Worker return __builtin_umull_overflow(Size, N,
49*76559068SAndroid Build Coastguard Worker reinterpret_cast<unsigned long *>(Product));
50*76559068SAndroid Build Coastguard Worker #elif __has_builtin(__builtin_umul_overflow) && (SCUDO_WORDSIZE == 32U)
51*76559068SAndroid Build Coastguard Worker // On, e.g. armv7, uptr/uintptr_t may be defined as unsigned long
52*76559068SAndroid Build Coastguard Worker return __builtin_umul_overflow(Size, N,
53*76559068SAndroid Build Coastguard Worker reinterpret_cast<unsigned int *>(Product));
54*76559068SAndroid Build Coastguard Worker #else
55*76559068SAndroid Build Coastguard Worker *Product = Size * N;
56*76559068SAndroid Build Coastguard Worker if (!Size)
57*76559068SAndroid Build Coastguard Worker return false;
58*76559068SAndroid Build Coastguard Worker return (*Product / Size) != N;
59*76559068SAndroid Build Coastguard Worker #endif
60*76559068SAndroid Build Coastguard Worker }
61*76559068SAndroid Build Coastguard Worker
62*76559068SAndroid Build Coastguard Worker // Returns true if the size passed to pvalloc overflows when rounded to the next
63*76559068SAndroid Build Coastguard Worker // multiple of PageSize.
checkForPvallocOverflow(uptr Size,uptr PageSize)64*76559068SAndroid Build Coastguard Worker inline bool checkForPvallocOverflow(uptr Size, uptr PageSize) {
65*76559068SAndroid Build Coastguard Worker return roundUp(Size, PageSize) < Size;
66*76559068SAndroid Build Coastguard Worker }
67*76559068SAndroid Build Coastguard Worker
68*76559068SAndroid Build Coastguard Worker } // namespace scudo
69*76559068SAndroid Build Coastguard Worker
70*76559068SAndroid Build Coastguard Worker #endif // SCUDO_CHECKS_H_
71