Files
blender/intern/guardedalloc/tests/guardedalloc_overflow_test.cc
Sergey Sharybin a44bb8603e Guarded allocator: Fix lock-free allocator tests
Previously the lock-free tests were actually testing guarded allocator
because the main entry point of tests was switching allocator to the
guarded one.

There seems to be no allocations happening between the initialization
sequence and the fixture's SetUp(), so easiest seems to be just to
switch to lockfree implementation in the fixture's SetUp().

The test are passing locally, so the "should work" has high chance
of actually being truth :)

Differential Revision: https://developer.blender.org/D9584
2020-11-19 16:17:48 +01:00

61 lines
1.6 KiB
C++

/* Apache License, Version 2.0 */
#include "testing/testing.h"
#include "MEM_guardedalloc.h"
#include "guardedalloc_test_base.h"
/* We expect to abort on integer overflow, to prevent possible exploits. */
#if defined(__GNUC__) && !defined(__clang__)
/* Disable since it's the purpose of this test. */
# pragma GCC diagnostic ignored "-Walloc-size-larger-than="
#endif
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
TEST_F(LockFreeAllocatorTest, LockfreeIntegerOverflow)
{
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, "");
}
TEST_F(GuardedAllocatorTest, GuardedIntegerOverflow)
{
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, "");
}