Blender V4.3
guardedalloc_overflow_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2018-2022 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "testing/testing.h"
6
7#include "MEM_guardedalloc.h"
8
10
11/* We expect to abort on integer overflow, to prevent possible exploits. */
12
13#if defined(__GNUC__) && !defined(__clang__)
14/* Disable since it's the purpose of this test. */
15# pragma GCC diagnostic ignored "-Walloc-size-larger-than="
16#endif
17
18namespace {
19
20void MallocArray(size_t len, size_t size)
21{
22 void *mem = MEM_malloc_arrayN(len, size, "MallocArray");
23 if (mem) {
24 MEM_freeN(mem);
25 }
26}
27
28void CallocArray(size_t len, size_t size)
29{
30 void *mem = MEM_calloc_arrayN(len, size, "CallocArray");
31 if (mem) {
32 MEM_freeN(mem);
33 }
34}
35
36} // namespace
37
38TEST_F(LockFreeAllocatorTest, LockfreeIntegerOverflow)
39{
40 MallocArray(1, SIZE_MAX);
41 CallocArray(SIZE_MAX, 1);
42 MallocArray(SIZE_MAX / 2, 2);
43 CallocArray(SIZE_MAX / 1234567, 1234567);
44
45 EXPECT_EXIT(MallocArray(SIZE_MAX, 2), ABORT_PREDICATE, "");
46 EXPECT_EXIT(CallocArray(7, SIZE_MAX), ABORT_PREDICATE, "");
47 EXPECT_EXIT(MallocArray(SIZE_MAX, 12345567), ABORT_PREDICATE, "");
48 EXPECT_EXIT(CallocArray(SIZE_MAX, SIZE_MAX), ABORT_PREDICATE, "");
49}
50
51TEST_F(GuardedAllocatorTest, GuardedIntegerOverflow)
52{
53 MallocArray(1, SIZE_MAX);
54 CallocArray(SIZE_MAX, 1);
55 MallocArray(SIZE_MAX / 2, 2);
56 CallocArray(SIZE_MAX / 1234567, 1234567);
57
58 EXPECT_EXIT(MallocArray(SIZE_MAX, 2), ABORT_PREDICATE, "");
59 EXPECT_EXIT(CallocArray(7, SIZE_MAX), ABORT_PREDICATE, "");
60 EXPECT_EXIT(MallocArray(SIZE_MAX, 12345567), ABORT_PREDICATE, "");
61 EXPECT_EXIT(CallocArray(SIZE_MAX, SIZE_MAX), ABORT_PREDICATE, "");
62}
Read Guarded memory(de)allocation.
int len
TEST_F(LockFreeAllocatorTest, LockfreeIntegerOverflow)
void *(* MEM_malloc_arrayN)(size_t len, size_t size, const char *str)
Definition mallocn.cc:45
void *(* MEM_calloc_arrayN)(size_t len, size_t size, const char *str)
Definition mallocn.cc:43
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
#define SIZE_MAX
Definition stdint.h:206