2014-05-06 01:49:04 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 The Android Open Source Project
|
2017-02-16 00:31:13 +01:00
|
|
|
* All rights reserved.
|
2014-05-06 01:49:04 +02:00
|
|
|
*
|
2017-02-16 00:31:13 +01:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
2014-05-06 01:49:04 +02:00
|
|
|
*
|
2017-02-16 00:31:13 +01:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
2014-05-06 01:49:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/mman.h>
|
2021-11-18 19:48:47 +01:00
|
|
|
#include <sys/param.h>
|
2014-05-06 01:49:04 +02:00
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2019-02-15 22:48:38 +01:00
|
|
|
#include "linker_block_allocator.h"
|
2014-05-06 01:49:04 +02:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct test_struct_nominal {
|
|
|
|
void* pointer;
|
|
|
|
ssize_t value;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2021-11-18 19:48:47 +01:00
|
|
|
* this one has size below kBlockSizeAlign
|
2014-05-06 01:49:04 +02:00
|
|
|
*/
|
|
|
|
struct test_struct_small {
|
2021-11-18 19:48:47 +01:00
|
|
|
char str[3];
|
2014-05-06 01:49:04 +02:00
|
|
|
};
|
|
|
|
|
2021-11-18 19:48:47 +01:00
|
|
|
struct test_struct_max_align {
|
|
|
|
char str[16];
|
|
|
|
} __attribute__((aligned(16)));
|
|
|
|
|
2014-05-06 01:49:04 +02:00
|
|
|
/*
|
|
|
|
* 1009 byte struct (1009 is prime)
|
|
|
|
*/
|
|
|
|
struct test_struct_larger {
|
2020-07-22 01:11:30 +02:00
|
|
|
char str[1009];
|
2014-05-06 01:49:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static size_t kPageSize = sysconf(_SC_PAGE_SIZE);
|
|
|
|
|
2021-11-18 19:48:47 +01:00
|
|
|
template <typename Element>
|
|
|
|
void linker_allocator_test_helper() {
|
|
|
|
LinkerTypeAllocator<Element> allocator;
|
2014-05-06 01:49:04 +02:00
|
|
|
|
2021-11-18 19:48:47 +01:00
|
|
|
Element* ptr1 = allocator.alloc();
|
2014-05-06 01:49:04 +02:00
|
|
|
ASSERT_TRUE(ptr1 != nullptr);
|
2021-11-18 19:48:47 +01:00
|
|
|
ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr1) % kBlockSizeAlign);
|
|
|
|
ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr1) % alignof(Element));
|
|
|
|
Element* ptr2 = allocator.alloc();
|
|
|
|
ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr2) % kBlockSizeAlign);
|
|
|
|
ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr2) % alignof(Element));
|
2014-05-06 01:49:04 +02:00
|
|
|
ASSERT_TRUE(ptr2 != nullptr);
|
|
|
|
|
2021-11-18 19:48:47 +01:00
|
|
|
// they should be next to each other.
|
|
|
|
size_t dist = __BIONIC_ALIGN(MAX(sizeof(Element), kBlockSizeMin), kBlockSizeAlign);
|
|
|
|
ASSERT_EQ(reinterpret_cast<uint8_t*>(ptr1) + dist, reinterpret_cast<uint8_t*>(ptr2));
|
2014-05-06 01:49:04 +02:00
|
|
|
|
|
|
|
allocator.free(ptr1);
|
|
|
|
allocator.free(ptr2);
|
|
|
|
}
|
|
|
|
|
2021-11-18 19:48:47 +01:00
|
|
|
}; // anonymous namespace
|
2014-05-06 01:49:04 +02:00
|
|
|
|
2021-11-18 19:48:47 +01:00
|
|
|
TEST(linker_allocator, test_nominal) {
|
|
|
|
linker_allocator_test_helper<test_struct_nominal>();
|
2014-05-06 01:49:04 +02:00
|
|
|
}
|
|
|
|
|
2021-11-18 19:48:47 +01:00
|
|
|
TEST(linker_allocator, test_small) {
|
|
|
|
linker_allocator_test_helper<test_struct_small>();
|
|
|
|
}
|
2014-05-06 01:49:04 +02:00
|
|
|
|
2021-11-18 19:48:47 +01:00
|
|
|
TEST(linker_allocator, test_max_align) {
|
|
|
|
linker_allocator_test_helper<test_struct_max_align>();
|
|
|
|
}
|
2014-05-06 01:49:04 +02:00
|
|
|
|
2021-11-18 19:48:47 +01:00
|
|
|
TEST(linker_allocator, test_larger) {
|
|
|
|
linker_allocator_test_helper<test_struct_larger>();
|
2014-05-06 01:49:04 +02:00
|
|
|
|
2021-11-18 19:48:47 +01:00
|
|
|
LinkerTypeAllocator<test_struct_larger> allocator;
|
2014-05-06 01:49:04 +02:00
|
|
|
|
|
|
|
// lets allocate until we reach next page.
|
2021-11-18 19:48:47 +01:00
|
|
|
size_t n = kPageSize / sizeof(test_struct_larger) + 1;
|
2014-05-06 01:49:04 +02:00
|
|
|
|
|
|
|
for (size_t i=0; i<n; ++i) {
|
|
|
|
ASSERT_TRUE(allocator.alloc() != nullptr);
|
|
|
|
}
|
|
|
|
|
2014-05-14 21:52:57 +02:00
|
|
|
test_struct_larger* ptr_to_free = allocator.alloc();
|
|
|
|
ASSERT_TRUE(ptr_to_free != nullptr);
|
2014-05-06 01:49:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void protect_all() {
|
2015-03-10 23:43:50 +01:00
|
|
|
LinkerTypeAllocator<test_struct_larger> allocator;
|
2014-05-06 01:49:04 +02:00
|
|
|
|
|
|
|
// number of allocs to reach the end of first page
|
|
|
|
size_t n = kPageSize/sizeof(test_struct_larger) - 1;
|
|
|
|
test_struct_larger* page1_ptr = allocator.alloc();
|
|
|
|
|
|
|
|
for (size_t i=0; i<n; ++i) {
|
|
|
|
allocator.alloc();
|
|
|
|
}
|
|
|
|
|
|
|
|
test_struct_larger* page2_ptr = allocator.alloc();
|
|
|
|
allocator.protect_all(PROT_READ);
|
|
|
|
allocator.protect_all(PROT_READ | PROT_WRITE);
|
|
|
|
// check access
|
2020-07-22 01:11:30 +02:00
|
|
|
page2_ptr->str[23] = 27;
|
|
|
|
page1_ptr->str[13] = 11;
|
2014-05-06 01:49:04 +02:00
|
|
|
|
|
|
|
allocator.protect_all(PROT_READ);
|
|
|
|
fprintf(stderr, "trying to access protected page");
|
|
|
|
|
|
|
|
// this should result in segmentation fault
|
2020-07-22 01:11:30 +02:00
|
|
|
page1_ptr->str[11] = 7;
|
2014-05-06 01:49:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(linker_allocator, test_protect) {
|
|
|
|
testing::FLAGS_gtest_death_test_style = "threadsafe";
|
|
|
|
ASSERT_EXIT(protect_all(), testing::KilledBySignal(SIGSEGV), "trying to access protected page");
|
|
|
|
}
|