blob: 56fbee8b7bcebfd783e25ccbbe21dbf467b7001d [file] [log] [blame]
Dmitriy Ivanovd597d262014-05-05 16:49:04 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -08003 * All rights reserved.
Dmitriy Ivanovd597d262014-05-05 16:49:04 -07004 *
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -08005 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070014 *
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -080015 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070027 */
28
29#include <stdlib.h>
30#include <string.h>
31#include <sys/mman.h>
Eric Miao08cf9492021-11-18 18:48:47 +000032#include <sys/param.h>
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070033
34#include <gtest/gtest.h>
35
Elliott Hughes15a2b7b2019-02-15 13:48:38 -080036#include "linker_block_allocator.h"
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070037
38#include <unistd.h>
39
40namespace {
41
42struct test_struct_nominal {
43 void* pointer;
44 ssize_t value;
45};
46
47/*
Eric Miao08cf9492021-11-18 18:48:47 +000048 * this one has size below kBlockSizeAlign
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070049 */
50struct test_struct_small {
Eric Miao08cf9492021-11-18 18:48:47 +000051 char str[3];
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070052};
53
Eric Miao08cf9492021-11-18 18:48:47 +000054struct test_struct_max_align {
55 char str[16];
56} __attribute__((aligned(16)));
57
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070058/*
59 * 1009 byte struct (1009 is prime)
60 */
61struct test_struct_larger {
Elliott Hughes68ae6ad2020-07-21 16:11:30 -070062 char str[1009];
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070063};
64
65static size_t kPageSize = sysconf(_SC_PAGE_SIZE);
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070066
Eric Miao08cf9492021-11-18 18:48:47 +000067template <typename Element>
68void linker_allocator_test_helper() {
69 LinkerTypeAllocator<Element> allocator;
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070070
Eric Miao08cf9492021-11-18 18:48:47 +000071 Element* ptr1 = allocator.alloc();
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070072 ASSERT_TRUE(ptr1 != nullptr);
Eric Miao08cf9492021-11-18 18:48:47 +000073 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr1) % kBlockSizeAlign);
74 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr1) % alignof(Element));
75 Element* ptr2 = allocator.alloc();
76 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr2) % kBlockSizeAlign);
77 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr2) % alignof(Element));
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070078 ASSERT_TRUE(ptr2 != nullptr);
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070079
Eric Miao08cf9492021-11-18 18:48:47 +000080 // they should be next to each other.
81 size_t dist = __BIONIC_ALIGN(MAX(sizeof(Element), kBlockSizeMin), kBlockSizeAlign);
82 ASSERT_EQ(reinterpret_cast<uint8_t*>(ptr1) + dist, reinterpret_cast<uint8_t*>(ptr2));
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070083
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070084 allocator.free(ptr1);
85 allocator.free(ptr2);
86}
87
Eric Miao08cf9492021-11-18 18:48:47 +000088}; // anonymous namespace
89
90TEST(linker_allocator, test_nominal) {
91 linker_allocator_test_helper<test_struct_nominal>();
92}
93
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070094TEST(linker_allocator, test_small) {
Eric Miao08cf9492021-11-18 18:48:47 +000095 linker_allocator_test_helper<test_struct_small>();
96}
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070097
Eric Miao08cf9492021-11-18 18:48:47 +000098TEST(linker_allocator, test_max_align) {
99 linker_allocator_test_helper<test_struct_max_align>();
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700100}
101
102TEST(linker_allocator, test_larger) {
Eric Miao08cf9492021-11-18 18:48:47 +0000103 linker_allocator_test_helper<test_struct_larger>();
104
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -0700105 LinkerTypeAllocator<test_struct_larger> allocator;
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700106
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700107 // lets allocate until we reach next page.
Eric Miao08cf9492021-11-18 18:48:47 +0000108 size_t n = kPageSize / sizeof(test_struct_larger) + 1;
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700109
110 for (size_t i=0; i<n; ++i) {
111 ASSERT_TRUE(allocator.alloc() != nullptr);
112 }
113
Dmitriy Ivanov10794062014-05-14 12:52:57 -0700114 test_struct_larger* ptr_to_free = allocator.alloc();
115 ASSERT_TRUE(ptr_to_free != nullptr);
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700116}
117
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700118static void protect_all() {
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -0700119 LinkerTypeAllocator<test_struct_larger> allocator;
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700120
121 // number of allocs to reach the end of first page
122 size_t n = kPageSize/sizeof(test_struct_larger) - 1;
123 test_struct_larger* page1_ptr = allocator.alloc();
124
125 for (size_t i=0; i<n; ++i) {
126 allocator.alloc();
127 }
128
129 test_struct_larger* page2_ptr = allocator.alloc();
130 allocator.protect_all(PROT_READ);
131 allocator.protect_all(PROT_READ | PROT_WRITE);
132 // check access
Elliott Hughes68ae6ad2020-07-21 16:11:30 -0700133 page2_ptr->str[23] = 27;
134 page1_ptr->str[13] = 11;
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700135
136 allocator.protect_all(PROT_READ);
137 fprintf(stderr, "trying to access protected page");
138
139 // this should result in segmentation fault
Elliott Hughes68ae6ad2020-07-21 16:11:30 -0700140 page1_ptr->str[11] = 7;
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700141}
142
143TEST(linker_allocator, test_protect) {
144 testing::FLAGS_gtest_death_test_style = "threadsafe";
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700145 ASSERT_EXIT(protect_all(), testing::KilledBySignal(SIGSEGV), "trying to access protected page");
146}