2023-08-15 16:20:26 +02:00
|
|
|
/* SPDX-FileCopyrightText: 2018-2022 Blender Authors
|
2023-06-15 05:09:04 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0 */
|
|
|
|
|
2018-01-14 21:53:32 +01:00
|
|
|
#include "testing/testing.h"
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
2020-11-17 11:38:42 +01:00
|
|
|
#include "guardedalloc_test_base.h"
|
|
|
|
|
2018-01-14 21:53:32 +01:00
|
|
|
/* We expect to abort on integer overflow, to prevent possible exploits. */
|
|
|
|
|
2020-08-12 12:15:01 +02:00
|
|
|
#if defined(__GNUC__) && !defined(__clang__)
|
2020-08-11 05:22:59 +02:00
|
|
|
/* Disable since it's the purpose of this test. */
|
|
|
|
# pragma GCC diagnostic ignored "-Walloc-size-larger-than="
|
|
|
|
#endif
|
|
|
|
|
2018-01-14 21:53:32 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
void MallocArray(size_t len, size_t size)
|
|
|
|
{
|
|
|
|
void *mem = MEM_malloc_arrayN(len, size, "MallocArray");
|
|
|
|
if (mem) {
|
|
|
|
MEM_freeN(mem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallocArray(size_t len, size_t size)
|
|
|
|
{
|
|
|
|
void *mem = MEM_calloc_arrayN(len, size, "CallocArray");
|
|
|
|
if (mem) {
|
|
|
|
MEM_freeN(mem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-11-17 11:38:42 +01:00
|
|
|
TEST_F(LockFreeAllocatorTest, LockfreeIntegerOverflow)
|
2018-01-14 21:53:32 +01:00
|
|
|
{
|
|
|
|
MallocArray(1, SIZE_MAX);
|
|
|
|
CallocArray(SIZE_MAX, 1);
|
|
|
|
MallocArray(SIZE_MAX / 2, 2);
|
|
|
|
CallocArray(SIZE_MAX / 1234567, 1234567);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-01-14 21:53:32 +01:00
|
|
|
EXPECT_EXIT(MallocArray(SIZE_MAX, 2), ABORT_PREDICATE, "");
|
|
|
|
EXPECT_EXIT(CallocArray(7, SIZE_MAX), ABORT_PREDICATE, "");
|
|
|
|
EXPECT_EXIT(MallocArray(SIZE_MAX, 12345567), ABORT_PREDICATE, "");
|
|
|
|
EXPECT_EXIT(CallocArray(SIZE_MAX, SIZE_MAX), ABORT_PREDICATE, "");
|
|
|
|
}
|
|
|
|
|
2020-11-17 11:38:42 +01:00
|
|
|
TEST_F(GuardedAllocatorTest, GuardedIntegerOverflow)
|
2018-01-14 21:53:32 +01:00
|
|
|
{
|
|
|
|
MallocArray(1, SIZE_MAX);
|
|
|
|
CallocArray(SIZE_MAX, 1);
|
|
|
|
MallocArray(SIZE_MAX / 2, 2);
|
|
|
|
CallocArray(SIZE_MAX / 1234567, 1234567);
|
|
|
|
|
|
|
|
EXPECT_EXIT(MallocArray(SIZE_MAX, 2), ABORT_PREDICATE, "");
|
|
|
|
EXPECT_EXIT(CallocArray(7, SIZE_MAX), ABORT_PREDICATE, "");
|
|
|
|
EXPECT_EXIT(MallocArray(SIZE_MAX, 12345567), ABORT_PREDICATE, "");
|
|
|
|
EXPECT_EXIT(CallocArray(SIZE_MAX, SIZE_MAX), ABORT_PREDICATE, "");
|
|
|
|
}
|