linker_allocator_test.cpp revision bc23e530c4db5175a065eeef36553c9c2c78fcf4
1/* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include <stdlib.h> 18#include <string.h> 19#include <sys/mman.h> 20 21#include <gtest/gtest.h> 22 23#include "../linker_allocator.h" 24 25#include <unistd.h> 26 27namespace { 28 29struct test_struct_nominal { 30 void* pointer; 31 ssize_t value; 32}; 33 34/* 35 * this one has size below allocator cap which is 2*sizeof(void*) 36 */ 37struct test_struct_small { 38 char dummy_str[5]; 39}; 40 41/* 42 * 1009 byte struct (1009 is prime) 43 */ 44struct test_struct_larger { 45 char dummy_str[1009]; 46}; 47 48static size_t kPageSize = sysconf(_SC_PAGE_SIZE); 49}; 50 51TEST(linker_allocator, test_nominal) { 52 LinkerAllocator<test_struct_nominal> allocator; 53 allocator.init(); 54 55 test_struct_nominal* ptr1 = allocator.alloc(); 56 ASSERT_TRUE(ptr1 != nullptr); 57 test_struct_nominal* ptr2 = allocator.alloc(); 58 ASSERT_TRUE(ptr2 != nullptr); 59 // they should be next to each other. 60 ASSERT_EQ(ptr1+1, ptr2); 61 62 ptr1->value = 42; 63 64 allocator.free(ptr1); 65 allocator.free(ptr2); 66} 67 68TEST(linker_allocator, test_small) { 69 LinkerAllocator<test_struct_small> allocator; 70 allocator.init(); 71 72 char* ptr1 = reinterpret_cast<char*>(allocator.alloc()); 73 char* ptr2 = reinterpret_cast<char*>(allocator.alloc()); 74 75 ASSERT_TRUE(ptr1 != nullptr); 76 ASSERT_TRUE(ptr2 != nullptr); 77 ASSERT_EQ(ptr1+2*sizeof(void*), ptr2); 78} 79 80TEST(linker_allocator, test_larger) { 81 LinkerAllocator<test_struct_larger> allocator; 82 allocator.init(); 83 84 test_struct_larger* ptr1 = allocator.alloc(); 85 test_struct_larger* ptr2 = allocator.alloc(); 86 87 ASSERT_TRUE(ptr1 != nullptr); 88 ASSERT_TRUE(ptr2 != nullptr); 89 90 ASSERT_EQ(ptr1+1, ptr2); 91 92 // lets allocate until we reach next page. 93 size_t n = kPageSize/sizeof(test_struct_larger) + 1 - 2; 94 95 for (size_t i=0; i<n; ++i) { 96 ASSERT_TRUE(allocator.alloc() != nullptr); 97 } 98 99} 100 101static void protect_all() { 102 LinkerAllocator<test_struct_larger> allocator; 103 allocator.init(); 104 105 // number of allocs to reach the end of first page 106 size_t n = kPageSize/sizeof(test_struct_larger) - 1; 107 test_struct_larger* page1_ptr = allocator.alloc(); 108 109 for (size_t i=0; i<n; ++i) { 110 allocator.alloc(); 111 } 112 113 test_struct_larger* page2_ptr = allocator.alloc(); 114 allocator.protect_all(PROT_READ); 115 allocator.protect_all(PROT_READ | PROT_WRITE); 116 // check access 117 page2_ptr->dummy_str[23] = 27; 118 page1_ptr->dummy_str[13] = 11; 119 120 allocator.protect_all(PROT_READ); 121 fprintf(stderr, "trying to access protected page"); 122 123 // this should result in segmentation fault 124 page1_ptr->dummy_str[11] = 7; 125} 126 127TEST(linker_allocator, test_protect) { 128 testing::FLAGS_gtest_death_test_style = "threadsafe"; 129 ASSERT_EXIT(protect_all(), testing::KilledBySignal(SIGSEGV), "trying to access protected page"); 130} 131 132