Blender V4.3
mallocn_inline.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2002-2017 `Jason Evans <jasone@canonware.com>`. All rights reserved.
2 * SPDX-FileCopyrightText: 2007-2012 Mozilla Foundation. All rights reserved.
3 * SPDX-FileCopyrightText: 2009-2017 Facebook, Inc. All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-2-Clause */
6#pragma once
7
12#include <cstdlib>
13
14/* BEGIN copied from BLI_asan.h */
15
16/* Clang defines this. */
17#ifndef __has_feature
18# define __has_feature(x) 0
19#endif
20
21#if (defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer)) && \
22 (!defined(_MSC_VER) || _MSC_VER > 1929) /* MSVC 2019 and below doesn't ship ASAN headers. */
23# include "sanitizer/asan_interface.h"
24# define WITH_ASAN
25#else
26/* Ensure return value is used. Just using UNUSED_VARS results in a warning. */
27# define ASAN_POISON_MEMORY_REGION(addr, size) (void)(0 && ((size) != 0 && (addr) != NULL))
28# define ASAN_UNPOISON_MEMORY_REGION(addr, size) (void)(0 && ((size) != 0 && (addr) != NULL))
29#endif
30
31/* END copied from BLI_asan.h */
32
33MEM_INLINE bool MEM_size_safe_multiply(size_t a, size_t b, size_t *result)
34{
35 /* A size_t with its high-half bits all set to 1. */
36 const size_t high_bits = SIZE_MAX << (sizeof(size_t) * 8 / 2);
37 *result = a * b;
38
39 if (UNLIKELY(*result == 0)) {
40 return (a == 0 || b == 0);
41 }
42
43 /*
44 * We got a non-zero size, but we don't know if we overflowed to get
45 * there. To avoid having to do a divide, we'll be clever and note that
46 * if both A and B can be represented in N/2 bits, then their product
47 * can be represented in N bits (without the possibility of overflow).
48 */
49 return ((high_bits & (a | b)) == 0 || (*result / b == a));
50}
51
58#ifdef WITH_ASAN
59MEM_INLINE void MEM_trigger_error_on_memory_block(const void *address, const size_t size)
60{
61 if (address == nullptr) {
62# ifdef WITH_ASSERT_ABORT
63 abort();
64# endif
65 return;
66 }
67
68 /* Trigger ASAN error by poisoning the memory and accessing it. */
69 ASAN_POISON_MEMORY_REGION(address, size);
70 char *buffer = const_cast<char *>(static_cast<const char *>(address));
71 const char c = *buffer;
72 *buffer &= 255;
73 *buffer = c;
74
75 /* In case ASAN is set to not terminate on error, but abort on assert is requested. */
76# ifdef WITH_ASSERT_ABORT
77 abort();
78# endif
79 ASAN_UNPOISON_MEMORY_REGION(address, size);
80}
81#else
82MEM_INLINE void MEM_trigger_error_on_memory_block(const void * /*address*/, const size_t /*size*/)
83{
84# ifdef WITH_ASSERT_ABORT
85 abort();
86# endif
87}
88#endif
#define UNLIKELY(x)
local_group_size(16, 16) .push_constant(Type b
#define ASAN_POISON_MEMORY_REGION(addr, size)
MEM_INLINE bool MEM_size_safe_multiply(size_t a, size_t b, size_t *result)
#define ASAN_UNPOISON_MEMORY_REGION(addr, size)
MEM_INLINE void MEM_trigger_error_on_memory_block(const void *, const size_t)
#define MEM_INLINE
#define SIZE_MAX
Definition stdint.h:206