blob: 6fb2b26806175a7c14cf8f8c4311762bdb0c918f [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>
32
33#include <gtest/gtest.h>
34
Elliott Hughes15a2b7b2019-02-15 13:48:38 -080035#include "linker_block_allocator.h"
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070036
37#include <unistd.h>
38
39namespace {
40
41struct test_struct_nominal {
42 void* pointer;
43 ssize_t value;
44};
45
46/*
47 * this one has size below allocator cap which is 2*sizeof(void*)
48 */
49struct test_struct_small {
Elliott Hughes68ae6ad2020-07-21 16:11:30 -070050 char str[5];
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070051};
52
53/*
54 * 1009 byte struct (1009 is prime)
55 */
56struct test_struct_larger {
Elliott Hughes68ae6ad2020-07-21 16:11:30 -070057 char str[1009];
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070058};
59
60static size_t kPageSize = sysconf(_SC_PAGE_SIZE);
61};
62
63TEST(linker_allocator, test_nominal) {
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -070064 LinkerTypeAllocator<test_struct_nominal> allocator;
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070065
66 test_struct_nominal* ptr1 = allocator.alloc();
67 ASSERT_TRUE(ptr1 != nullptr);
Dimitry Ivanov3edc5c42016-01-21 10:55:40 -080068 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr1) % 16);
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070069 test_struct_nominal* ptr2 = allocator.alloc();
Dimitry Ivanov3edc5c42016-01-21 10:55:40 -080070 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr2) % 16);
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070071 ASSERT_TRUE(ptr2 != nullptr);
72 // they should be next to each other.
Dimitry Ivanov3edc5c42016-01-21 10:55:40 -080073 ASSERT_EQ(reinterpret_cast<uint8_t*>(ptr1)+16, reinterpret_cast<uint8_t*>(ptr2));
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070074
75 ptr1->value = 42;
76
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070077 allocator.free(ptr1);
78 allocator.free(ptr2);
79}
80
81TEST(linker_allocator, test_small) {
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -070082 LinkerTypeAllocator<test_struct_small> allocator;
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070083
84 char* ptr1 = reinterpret_cast<char*>(allocator.alloc());
85 char* ptr2 = reinterpret_cast<char*>(allocator.alloc());
86
87 ASSERT_TRUE(ptr1 != nullptr);
Dimitry Ivanov3edc5c42016-01-21 10:55:40 -080088 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr1) % 16);
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070089 ASSERT_TRUE(ptr2 != nullptr);
Dimitry Ivanov3edc5c42016-01-21 10:55:40 -080090 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr2) % 16);
91 ASSERT_EQ(ptr1+16, ptr2); // aligned to 16
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070092}
93
94TEST(linker_allocator, test_larger) {
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -070095 LinkerTypeAllocator<test_struct_larger> allocator;
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070096
97 test_struct_larger* ptr1 = allocator.alloc();
98 test_struct_larger* ptr2 = allocator.alloc();
99
100 ASSERT_TRUE(ptr1 != nullptr);
Dimitry Ivanov3edc5c42016-01-21 10:55:40 -0800101 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr1) % 16);
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700102 ASSERT_TRUE(ptr2 != nullptr);
Dimitry Ivanov3edc5c42016-01-21 10:55:40 -0800103 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr2) % 16);
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700104
Dimitry Ivanov3edc5c42016-01-21 10:55:40 -0800105 ASSERT_EQ(reinterpret_cast<uint8_t*>(ptr1) + 1024, reinterpret_cast<uint8_t*>(ptr2));
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700106
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700107 // lets allocate until we reach next page.
108 size_t n = kPageSize/sizeof(test_struct_larger) + 1 - 2;
109
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);
116 allocator.free(ptr1);
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700117}
118
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700119static void protect_all() {
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -0700120 LinkerTypeAllocator<test_struct_larger> allocator;
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700121
122 // number of allocs to reach the end of first page
123 size_t n = kPageSize/sizeof(test_struct_larger) - 1;
124 test_struct_larger* page1_ptr = allocator.alloc();
125
126 for (size_t i=0; i<n; ++i) {
127 allocator.alloc();
128 }
129
130 test_struct_larger* page2_ptr = allocator.alloc();
131 allocator.protect_all(PROT_READ);
132 allocator.protect_all(PROT_READ | PROT_WRITE);
133 // check access
Elliott Hughes68ae6ad2020-07-21 16:11:30 -0700134 page2_ptr->str[23] = 27;
135 page1_ptr->str[13] = 11;
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700136
137 allocator.protect_all(PROT_READ);
138 fprintf(stderr, "trying to access protected page");
139
140 // this should result in segmentation fault
Elliott Hughes68ae6ad2020-07-21 16:11:30 -0700141 page1_ptr->str[11] = 7;
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700142}
143
144TEST(linker_allocator, test_protect) {
145 testing::FLAGS_gtest_death_test_style = "threadsafe";
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700146 ASSERT_EXIT(protect_all(), testing::KilledBySignal(SIGSEGV), "trying to access protected page");
147}