summaryrefslogtreecommitdiffstats
path: root/linker/tests
diff options
context:
space:
mode:
authorDmitriy Ivanov <dimitry@google.com>2014-05-05 16:49:04 -0700
committerDmitriy Ivanov <dimitry@google.com>2014-05-12 21:40:09 -0700
commitd597d263bc32422402d4810ce4ec070f0227c2f7 (patch)
tree82c874dc2a5e8e1821a23af6c9d9fce7091be185 /linker/tests
parent4b57305afe2b54a6afb733361f6fd93cb92ccfa8 (diff)
downloadbionic-d597d263bc32422402d4810ce4ec070f0227c2f7.zip
bionic-d597d263bc32422402d4810ce4ec070f0227c2f7.tar.gz
bionic-d597d263bc32422402d4810ce4ec070f0227c2f7.tar.bz2
Refactor linker allocator
Makes it reusable for different fixed sized and not very big structures (<PAGE_SIZE). Change-Id: Id5ec13fc6541b1935ef7fe3671c22b98685abbae
Diffstat (limited to 'linker/tests')
-rw-r--r--linker/tests/Android.mk38
-rw-r--r--linker/tests/linker_allocator_test.cpp161
2 files changed, 199 insertions, 0 deletions
diff --git a/linker/tests/Android.mk b/linker/tests/Android.mk
new file mode 100644
index 0000000..600fe69
--- /dev/null
+++ b/linker/tests/Android.mk
@@ -0,0 +1,38 @@
+#
+# Copyright (C) 2012 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+ifneq ($(BUILD_TINY_ANDROID),true)
+
+LOCAL_PATH:= $(call my-dir)
+
+include $(CLEAR_VARS)
+LOCAL_MULTILIB := both
+LOCAL_MODULE := linker-unit-tests
+LOCAL_MODULE_STEM_32 := $(LOCAL_MODULE)32
+LOCAL_MODULE_STEM_64 := $(LOCAL_MODULE)64
+
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
+
+LOCAL_CFLAGS += -g -Wall -Wextra -Werror -std=gnu++11
+LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../libc/
+
+LOCAL_SRC_FILES := \
+ linker_allocator_test.cpp \
+ ../linker_allocator.cpp
+
+include $(BUILD_NATIVE_TEST)
+
+endif # !BUILD_TINY_ANDROID
diff --git a/linker/tests/linker_allocator_test.cpp b/linker/tests/linker_allocator_test.cpp
new file mode 100644
index 0000000..ccbdce6
--- /dev/null
+++ b/linker/tests/linker_allocator_test.cpp
@@ -0,0 +1,161 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+
+#include <gtest/gtest.h>
+
+#include "../linker_allocator.h"
+
+#include <unistd.h>
+
+namespace {
+
+struct test_struct_nominal {
+ void* pointer;
+ ssize_t value;
+};
+
+/*
+ * this one has size below allocator cap which is 2*sizeof(void*)
+ */
+struct test_struct_small {
+ char dummy_str[5];
+};
+
+/*
+ * 1009 byte struct (1009 is prime)
+ */
+struct test_struct_larger {
+ char dummy_str[1009];
+};
+
+static size_t kPageSize = sysconf(_SC_PAGE_SIZE);
+};
+
+TEST(linker_allocator, test_nominal) {
+ LinkerAllocator<test_struct_nominal> allocator;
+ allocator.init();
+
+ test_struct_nominal* ptr1 = allocator.alloc();
+ ASSERT_TRUE(ptr1 != nullptr);
+ test_struct_nominal* ptr2 = allocator.alloc();
+ ASSERT_TRUE(ptr2 != nullptr);
+ // they should be next to each other.
+ ASSERT_EQ(ptr1+1, ptr2);
+
+ ptr1->value = 42;
+
+ allocator.protect_page(ptr1, PROT_READ);
+
+ allocator.free(ptr1);
+ allocator.free(ptr2);
+}
+
+TEST(linker_allocator, test_small) {
+ LinkerAllocator<test_struct_small> allocator;
+ allocator.init();
+
+ char* ptr1 = reinterpret_cast<char*>(allocator.alloc());
+ char* ptr2 = reinterpret_cast<char*>(allocator.alloc());
+
+ ASSERT_TRUE(ptr1 != nullptr);
+ ASSERT_TRUE(ptr2 != nullptr);
+ ASSERT_EQ(ptr1+2*sizeof(void*), ptr2);
+}
+
+TEST(linker_allocator, test_larger) {
+ LinkerAllocator<test_struct_larger> allocator;
+ allocator.init();
+
+ test_struct_larger* ptr1 = allocator.alloc();
+ test_struct_larger* ptr2 = allocator.alloc();
+
+ ASSERT_TRUE(ptr1 != nullptr);
+ ASSERT_TRUE(ptr2 != nullptr);
+
+ ASSERT_EQ(ptr1+1, ptr2);
+
+ allocator.protect_page(ptr2, PROT_READ);
+
+ // lets allocate until we reach next page.
+ size_t n = kPageSize/sizeof(test_struct_larger) + 1 - 2;
+
+ for (size_t i=0; i<n; ++i) {
+ ASSERT_TRUE(allocator.alloc() != nullptr);
+ }
+
+}
+
+static void protect_one_page() {
+ LinkerAllocator<test_struct_larger> allocator;
+ allocator.init();
+
+ // 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_page(page2_ptr, PROT_READ);
+
+ // check that we still have access to page1
+ page1_ptr->dummy_str[17] = 52;
+
+ fprintf(stderr, "trying to access protected page");
+
+ // this should result in segmentation fault
+ page2_ptr->dummy_str[12] = 3;
+}
+
+static void protect_all() {
+ LinkerAllocator<test_struct_larger> allocator;
+ allocator.init();
+
+ // 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
+ page2_ptr->dummy_str[23] = 27;
+ page1_ptr->dummy_str[13] = 11;
+
+ allocator.protect_all(PROT_READ);
+ fprintf(stderr, "trying to access protected page");
+
+ // this should result in segmentation fault
+ page1_ptr->dummy_str[11] = 7;
+}
+
+TEST(linker_allocator, test_protect) {
+ testing::FLAGS_gtest_death_test_style = "threadsafe";
+ ASSERT_EXIT(protect_one_page(), testing::KilledBySignal(SIGSEGV), "trying to access protected page");
+ ASSERT_EXIT(protect_all(), testing::KilledBySignal(SIGSEGV), "trying to access protected page");
+}
+