blob: 3118a91053ba0178ef43d1ae5f3e56a7591c4425 [file]
/*
* Copyright (c) 2021, Alliance for Open Media. All rights reserved.
*
* This source code is subject to the terms of the BSD 2 Clause License and
* the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
* was not distributed with this source code in the LICENSE file, you can
* obtain it at www.aomedia.org/license/software. If the Alliance for Open
* Media Patent License 1.0 was not distributed with this source code in the
* PATENTS file, you can obtain it at www.aomedia.org/license/patent.
*/
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <memory>
#include "aom_mem/aom_mem.h"
#include "gtest/gtest.h"
namespace {
struct AomFreeDeleter {
void operator()(void *ptr) const { aom_free(ptr); }
};
using AomMemPtr = std::unique_ptr<void, AomFreeDeleter>;
constexpr size_t kMinAlignment = 2 * sizeof(void *);
void ExpectCallocZeroedAndAligned(size_t num, size_t size) {
SCOPED_TRACE(testing::Message() << "num=" << num << " size=" << size);
const size_t total_size = num * size;
// Poison a matching allocation so recycled heap memory is non-zero.
{
AomMemPtr poisoned(aom_malloc(total_size));
ASSERT_NE(poisoned.get(), nullptr);
if (total_size > 0) {
std::memset(poisoned.get(), 0xA5, total_size);
}
}
AomMemPtr ptr(aom_calloc(num, size));
ASSERT_NE(ptr.get(), nullptr);
EXPECT_EQ(reinterpret_cast<std::uintptr_t>(ptr.get()) % kMinAlignment,
static_cast<size_t>(0));
const auto *bytes = static_cast<const unsigned char *>(ptr.get());
for (size_t i = 0; i < total_size; ++i) {
ASSERT_EQ(bytes[i], 0) << "at byte " << i;
}
}
} // namespace
TEST(AomMemTest, Overflow) {
// Allocations are aligned > 1 so SIZE_MAX should always fail.
ASSERT_EQ(aom_malloc(SIZE_MAX), nullptr);
ASSERT_EQ(aom_calloc(1, SIZE_MAX), nullptr);
ASSERT_EQ(aom_calloc(32, SIZE_MAX / 32), nullptr);
ASSERT_EQ(aom_calloc(SIZE_MAX, SIZE_MAX), nullptr);
ASSERT_EQ(aom_memalign(1, SIZE_MAX), nullptr);
ASSERT_EQ(aom_memalign(64, SIZE_MAX), nullptr);
ASSERT_EQ(aom_memalign(64, SIZE_MAX - 64), nullptr);
ASSERT_EQ(aom_memalign(64, SIZE_MAX - 64 - sizeof(size_t) + 2), nullptr);
AomMemPtr max_ptr(
aom_calloc(1, static_cast<size_t>(AOM_MAX_ALLOCABLE_MEMORY)));
EXPECT_EQ(max_ptr.get(), nullptr);
}
TEST(AomMemTest, NullParams) {
ASSERT_EQ(aom_memset16(nullptr, 0, 0), nullptr);
aom_free(nullptr);
}
TEST(AomMemTest, Calloc) {
const size_t max_pad = kMinAlignment - 1 + sizeof(size_t);
// Small allocations (aligned_size <= 4096, malloc + memset path).
ASSERT_NO_FATAL_FAILURE(ExpectCallocZeroedAndAligned(1, 256));
ASSERT_NO_FATAL_FAILURE(ExpectCallocZeroedAndAligned(64, 4));
ASSERT_NO_FATAL_FAILURE(ExpectCallocZeroedAndAligned(1, 4096 - max_pad));
// Large allocations (aligned_size > 4096, calloc path).
ASSERT_NO_FATAL_FAILURE(ExpectCallocZeroedAndAligned(1, 4096 - max_pad + 1));
ASSERT_NO_FATAL_FAILURE(ExpectCallocZeroedAndAligned(64, 64));
ASSERT_NO_FATAL_FAILURE(ExpectCallocZeroedAndAligned(1, 8192));
ASSERT_NO_FATAL_FAILURE(ExpectCallocZeroedAndAligned(1024, 256));
ASSERT_NO_FATAL_FAILURE(ExpectCallocZeroedAndAligned(1, 256 * 1024));
// Zero-size allocations should succeed and return aligned pointers.
const struct {
size_t num;
size_t size;
} kZeroSizeCases[] = {
{ 0, 0 }, { 0, 16 }, { 16, 0 }, { 0, SIZE_MAX }, { SIZE_MAX, 0 },
};
for (const auto &test_case : kZeroSizeCases) {
AomMemPtr ptr(aom_calloc(test_case.num, test_case.size));
ASSERT_NE(ptr.get(), nullptr);
EXPECT_EQ(reinterpret_cast<std::uintptr_t>(ptr.get()) % kMinAlignment,
static_cast<size_t>(0));
}
}