xref: /freebsd/contrib/llvm-project/compiler-rt/lib/scudo/standalone/wrappers_c_checks.h (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1 //===-- wrappers_c_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 #ifndef SCUDO_CHECKS_H_
10 #define SCUDO_CHECKS_H_
11 
12 #include "common.h"
13 
14 #include <errno.h>
15 
16 #ifndef __has_builtin
17 #define __has_builtin(X) 0
18 #endif
19 
20 namespace scudo {
21 
22 // A common errno setting logic shared by almost all Scudo C wrappers.
23 inline void *setErrnoOnNull(void *Ptr) {
24   if (UNLIKELY(!Ptr))
25     errno = ENOMEM;
26   return Ptr;
27 }
28 
29 // Checks return true on failure.
30 
31 // Checks aligned_alloc() parameters, verifies that the alignment is a power of
32 // two and that the size is a multiple of alignment.
33 inline bool checkAlignedAllocAlignmentAndSize(uptr Alignment, uptr Size) {
34   return Alignment == 0 || !isPowerOfTwo(Alignment) ||
35          !isAligned(Size, Alignment);
36 }
37 
38 // Checks posix_memalign() parameters, verifies that alignment is a power of two
39 // and a multiple of sizeof(void *).
40 inline bool checkPosixMemalignAlignment(uptr Alignment) {
41   return Alignment == 0 || !isPowerOfTwo(Alignment) ||
42          !isAligned(Alignment, sizeof(void *));
43 }
44 
45 // Returns true if calloc(Size, N) overflows on Size*N calculation. Use a
46 // builtin supported by recent clang & GCC if it exists, otherwise fallback to a
47 // costly division.
48 inline bool checkForCallocOverflow(uptr Size, uptr N, uptr *Product) {
49 #if __has_builtin(__builtin_umull_overflow) && (SCUDO_WORDSIZE == 64U)
50   return __builtin_umull_overflow(Size, N, Product);
51 #elif __has_builtin(__builtin_umul_overflow) && (SCUDO_WORDSIZE == 32U)
52   return __builtin_umul_overflow(Size, N, Product);
53 #else
54   *Product = Size * N;
55   if (!Size)
56     return false;
57   return (*Product / Size) != N;
58 #endif
59 }
60 
61 // Returns true if the size passed to pvalloc overflows when rounded to the next
62 // multiple of PageSize.
63 inline bool checkForPvallocOverflow(uptr Size, uptr PageSize) {
64   return roundUpTo(Size, PageSize) < Size;
65 }
66 
67 } // namespace scudo
68 
69 #endif // SCUDO_CHECKS_H_
70