xref: /aosp_15_r20/bionic/linker/linker_block_allocator_test.cpp (revision 8d67ca893c1523eb926b9080dbe4e2ffd2a27ba1)
1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker  * Copyright (C) 2013 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker  * All rights reserved.
4*8d67ca89SAndroid Build Coastguard Worker  *
5*8d67ca89SAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
6*8d67ca89SAndroid Build Coastguard Worker  * modification, are permitted provided that the following conditions
7*8d67ca89SAndroid Build Coastguard Worker  * are met:
8*8d67ca89SAndroid Build Coastguard Worker  *  * Redistributions of source code must retain the above copyright
9*8d67ca89SAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer.
10*8d67ca89SAndroid Build Coastguard Worker  *  * Redistributions in binary form must reproduce the above copyright
11*8d67ca89SAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer in
12*8d67ca89SAndroid Build Coastguard Worker  *    the documentation and/or other materials provided with the
13*8d67ca89SAndroid Build Coastguard Worker  *    distribution.
14*8d67ca89SAndroid Build Coastguard Worker  *
15*8d67ca89SAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16*8d67ca89SAndroid Build Coastguard Worker  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17*8d67ca89SAndroid Build Coastguard Worker  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18*8d67ca89SAndroid Build Coastguard Worker  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19*8d67ca89SAndroid Build Coastguard Worker  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20*8d67ca89SAndroid Build Coastguard Worker  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21*8d67ca89SAndroid Build Coastguard Worker  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22*8d67ca89SAndroid Build Coastguard Worker  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23*8d67ca89SAndroid Build Coastguard Worker  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24*8d67ca89SAndroid Build Coastguard Worker  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25*8d67ca89SAndroid Build Coastguard Worker  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8d67ca89SAndroid Build Coastguard Worker  * SUCH DAMAGE.
27*8d67ca89SAndroid Build Coastguard Worker  */
28*8d67ca89SAndroid Build Coastguard Worker 
29*8d67ca89SAndroid Build Coastguard Worker #include <stdlib.h>
30*8d67ca89SAndroid Build Coastguard Worker #include <string.h>
31*8d67ca89SAndroid Build Coastguard Worker #include <sys/mman.h>
32*8d67ca89SAndroid Build Coastguard Worker #include <sys/param.h>
33*8d67ca89SAndroid Build Coastguard Worker 
34*8d67ca89SAndroid Build Coastguard Worker #include <gtest/gtest.h>
35*8d67ca89SAndroid Build Coastguard Worker 
36*8d67ca89SAndroid Build Coastguard Worker #include "linker_block_allocator.h"
37*8d67ca89SAndroid Build Coastguard Worker 
38*8d67ca89SAndroid Build Coastguard Worker #include <unistd.h>
39*8d67ca89SAndroid Build Coastguard Worker 
40*8d67ca89SAndroid Build Coastguard Worker namespace {
41*8d67ca89SAndroid Build Coastguard Worker 
42*8d67ca89SAndroid Build Coastguard Worker struct test_struct_nominal {
43*8d67ca89SAndroid Build Coastguard Worker   void* pointer;
44*8d67ca89SAndroid Build Coastguard Worker   ssize_t value;
45*8d67ca89SAndroid Build Coastguard Worker };
46*8d67ca89SAndroid Build Coastguard Worker 
47*8d67ca89SAndroid Build Coastguard Worker /*
48*8d67ca89SAndroid Build Coastguard Worker  * this one has size below kBlockSizeAlign
49*8d67ca89SAndroid Build Coastguard Worker  */
50*8d67ca89SAndroid Build Coastguard Worker struct test_struct_small {
51*8d67ca89SAndroid Build Coastguard Worker   char str[3];
52*8d67ca89SAndroid Build Coastguard Worker };
53*8d67ca89SAndroid Build Coastguard Worker 
54*8d67ca89SAndroid Build Coastguard Worker struct test_struct_max_align {
55*8d67ca89SAndroid Build Coastguard Worker   char str[16];
56*8d67ca89SAndroid Build Coastguard Worker } __attribute__((aligned(16)));
57*8d67ca89SAndroid Build Coastguard Worker 
58*8d67ca89SAndroid Build Coastguard Worker /*
59*8d67ca89SAndroid Build Coastguard Worker  * 1009 byte struct (1009 is prime)
60*8d67ca89SAndroid Build Coastguard Worker  */
61*8d67ca89SAndroid Build Coastguard Worker struct test_struct_larger {
62*8d67ca89SAndroid Build Coastguard Worker   char str[1009];
63*8d67ca89SAndroid Build Coastguard Worker };
64*8d67ca89SAndroid Build Coastguard Worker 
65*8d67ca89SAndroid Build Coastguard Worker static size_t kPageSize = sysconf(_SC_PAGE_SIZE);
66*8d67ca89SAndroid Build Coastguard Worker 
67*8d67ca89SAndroid Build Coastguard Worker template <typename Element>
linker_allocator_test_helper()68*8d67ca89SAndroid Build Coastguard Worker void linker_allocator_test_helper() {
69*8d67ca89SAndroid Build Coastguard Worker   LinkerTypeAllocator<Element> allocator;
70*8d67ca89SAndroid Build Coastguard Worker 
71*8d67ca89SAndroid Build Coastguard Worker   Element* ptr1 = allocator.alloc();
72*8d67ca89SAndroid Build Coastguard Worker   ASSERT_TRUE(ptr1 != nullptr);
73*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr1) % kBlockSizeAlign);
74*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr1) % alignof(Element));
75*8d67ca89SAndroid Build Coastguard Worker   Element* ptr2 = allocator.alloc();
76*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr2) % kBlockSizeAlign);
77*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr2) % alignof(Element));
78*8d67ca89SAndroid Build Coastguard Worker   ASSERT_TRUE(ptr2 != nullptr);
79*8d67ca89SAndroid Build Coastguard Worker 
80*8d67ca89SAndroid Build Coastguard Worker   // they should be next to each other.
81*8d67ca89SAndroid Build Coastguard Worker   size_t dist = __BIONIC_ALIGN(MAX(sizeof(Element), kBlockSizeMin), kBlockSizeAlign);
82*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(reinterpret_cast<uint8_t*>(ptr1) + dist, reinterpret_cast<uint8_t*>(ptr2));
83*8d67ca89SAndroid Build Coastguard Worker 
84*8d67ca89SAndroid Build Coastguard Worker   allocator.free(ptr1);
85*8d67ca89SAndroid Build Coastguard Worker   allocator.free(ptr2);
86*8d67ca89SAndroid Build Coastguard Worker }
87*8d67ca89SAndroid Build Coastguard Worker 
88*8d67ca89SAndroid Build Coastguard Worker };  // anonymous namespace
89*8d67ca89SAndroid Build Coastguard Worker 
TEST(linker_allocator,test_nominal)90*8d67ca89SAndroid Build Coastguard Worker TEST(linker_allocator, test_nominal) {
91*8d67ca89SAndroid Build Coastguard Worker   linker_allocator_test_helper<test_struct_nominal>();
92*8d67ca89SAndroid Build Coastguard Worker }
93*8d67ca89SAndroid Build Coastguard Worker 
TEST(linker_allocator,test_small)94*8d67ca89SAndroid Build Coastguard Worker TEST(linker_allocator, test_small) {
95*8d67ca89SAndroid Build Coastguard Worker   linker_allocator_test_helper<test_struct_small>();
96*8d67ca89SAndroid Build Coastguard Worker }
97*8d67ca89SAndroid Build Coastguard Worker 
TEST(linker_allocator,test_max_align)98*8d67ca89SAndroid Build Coastguard Worker TEST(linker_allocator, test_max_align) {
99*8d67ca89SAndroid Build Coastguard Worker   linker_allocator_test_helper<test_struct_max_align>();
100*8d67ca89SAndroid Build Coastguard Worker }
101*8d67ca89SAndroid Build Coastguard Worker 
TEST(linker_allocator,test_larger)102*8d67ca89SAndroid Build Coastguard Worker TEST(linker_allocator, test_larger) {
103*8d67ca89SAndroid Build Coastguard Worker   linker_allocator_test_helper<test_struct_larger>();
104*8d67ca89SAndroid Build Coastguard Worker 
105*8d67ca89SAndroid Build Coastguard Worker   LinkerTypeAllocator<test_struct_larger> allocator;
106*8d67ca89SAndroid Build Coastguard Worker 
107*8d67ca89SAndroid Build Coastguard Worker   // lets allocate until we reach next page.
108*8d67ca89SAndroid Build Coastguard Worker   size_t n = kPageSize / sizeof(test_struct_larger) + 1;
109*8d67ca89SAndroid Build Coastguard Worker 
110*8d67ca89SAndroid Build Coastguard Worker   for (size_t i=0; i<n; ++i) {
111*8d67ca89SAndroid Build Coastguard Worker     ASSERT_TRUE(allocator.alloc() != nullptr);
112*8d67ca89SAndroid Build Coastguard Worker   }
113*8d67ca89SAndroid Build Coastguard Worker 
114*8d67ca89SAndroid Build Coastguard Worker   test_struct_larger* ptr_to_free = allocator.alloc();
115*8d67ca89SAndroid Build Coastguard Worker   ASSERT_TRUE(ptr_to_free != nullptr);
116*8d67ca89SAndroid Build Coastguard Worker }
117*8d67ca89SAndroid Build Coastguard Worker 
protect_all()118*8d67ca89SAndroid Build Coastguard Worker static void protect_all() {
119*8d67ca89SAndroid Build Coastguard Worker   LinkerTypeAllocator<test_struct_larger> allocator;
120*8d67ca89SAndroid Build Coastguard Worker 
121*8d67ca89SAndroid Build Coastguard Worker   // number of allocs to reach the end of first page
122*8d67ca89SAndroid Build Coastguard Worker   size_t n = kPageSize/sizeof(test_struct_larger) - 1;
123*8d67ca89SAndroid Build Coastguard Worker   test_struct_larger* page1_ptr = allocator.alloc();
124*8d67ca89SAndroid Build Coastguard Worker 
125*8d67ca89SAndroid Build Coastguard Worker   for (size_t i=0; i<n; ++i) {
126*8d67ca89SAndroid Build Coastguard Worker     allocator.alloc();
127*8d67ca89SAndroid Build Coastguard Worker   }
128*8d67ca89SAndroid Build Coastguard Worker 
129*8d67ca89SAndroid Build Coastguard Worker   test_struct_larger* page2_ptr = allocator.alloc();
130*8d67ca89SAndroid Build Coastguard Worker   allocator.protect_all(PROT_READ);
131*8d67ca89SAndroid Build Coastguard Worker   allocator.protect_all(PROT_READ | PROT_WRITE);
132*8d67ca89SAndroid Build Coastguard Worker   // check access
133*8d67ca89SAndroid Build Coastguard Worker   page2_ptr->str[23] = 27;
134*8d67ca89SAndroid Build Coastguard Worker   page1_ptr->str[13] = 11;
135*8d67ca89SAndroid Build Coastguard Worker 
136*8d67ca89SAndroid Build Coastguard Worker   allocator.protect_all(PROT_READ);
137*8d67ca89SAndroid Build Coastguard Worker   fprintf(stderr, "trying to access protected page");
138*8d67ca89SAndroid Build Coastguard Worker 
139*8d67ca89SAndroid Build Coastguard Worker   // this should result in segmentation fault
140*8d67ca89SAndroid Build Coastguard Worker   page1_ptr->str[11] = 7;
141*8d67ca89SAndroid Build Coastguard Worker }
142*8d67ca89SAndroid Build Coastguard Worker 
TEST(linker_allocator,test_protect)143*8d67ca89SAndroid Build Coastguard Worker TEST(linker_allocator, test_protect) {
144*8d67ca89SAndroid Build Coastguard Worker   testing::FLAGS_gtest_death_test_style = "threadsafe";
145*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EXIT(protect_all(), testing::KilledBySignal(SIGSEGV), "trying to access protected page");
146*8d67ca89SAndroid Build Coastguard Worker }
147