diff options
author | Torne (Richard Coles) <torne@google.com> | 2014-02-06 14:34:21 +0000 |
---|---|---|
committer | Torne (Richard Coles) <torne@google.com> | 2014-04-17 14:22:17 +0100 |
commit | 12bbb9164578b6512b8b07a3fb093858244b7c7b (patch) | |
tree | 2a32dc3fe2902444aed43647f0ab1d2612507296 /tests/dlext_test.cpp | |
parent | 012cb4583a5f8564059142bb1900ea3a31e7cfa9 (diff) | |
download | bionic-12bbb9164578b6512b8b07a3fb093858244b7c7b.zip bionic-12bbb9164578b6512b8b07a3fb093858244b7c7b.tar.gz bionic-12bbb9164578b6512b8b07a3fb093858244b7c7b.tar.bz2 |
Support loading libraries to a reserved address.
Add flags and parameters to android_dlopen_ext() to allow loading a
library at an already-reserved fixed address. If the library to be
loaded will not fit within the space reserved, then the linker will
either fail, or allocate its own address space as usual, according to
which flag has been specified. This behaviour only applies to the
specific library requested; any other libraries loaded as dependencies
will be loaded in the normal fashion.
There is a new gtest included to cover the functionality added.
Bug: 13005501
Change-Id: I5d1810375b20fc51ba6a9b3191a25f9792c687f1
Diffstat (limited to 'tests/dlext_test.cpp')
-rw-r--r-- | tests/dlext_test.cpp | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/tests/dlext_test.cpp b/tests/dlext_test.cpp new file mode 100644 index 0000000..299b408 --- /dev/null +++ b/tests/dlext_test.cpp @@ -0,0 +1,136 @@ +/* + * Copyright (C) 2014 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 <gtest/gtest.h> + +#include <dlfcn.h> +#include <android/dlext.h> +#include <sys/mman.h> + + +#define ASSERT_DL_NOTNULL(ptr) \ + ASSERT_TRUE(ptr != NULL) << "dlerror: " << dlerror() + +#define ASSERT_DL_ZERO(i) \ + ASSERT_EQ(0, i) << "dlerror: " << dlerror() + + +typedef int (*fn)(void); +#define LIBNAME "libdlext_test.so" +#define LIBSIZE 1024*1024 // how much address space to reserve for it + + +class DlExtTest : public ::testing::Test { +protected: + virtual void SetUp() { + handle_ = NULL; + // verify that we don't have the library loaded already + ASSERT_EQ(NULL, dlsym(RTLD_DEFAULT, "getRandomNumber")); + // call dlerror() to swallow the error, and check it was the one we wanted + ASSERT_STREQ("undefined symbol: getRandomNumber", dlerror()); + } + + virtual void TearDown() { + if (handle_ != NULL) { + ASSERT_DL_ZERO(dlclose(handle_)); + } + } + + void* handle_; +}; + +TEST_F(DlExtTest, ExtInfoNull) { + handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, NULL); + ASSERT_DL_NOTNULL(handle_); + fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); + ASSERT_DL_NOTNULL(f); + EXPECT_EQ(4, f()); +} + +TEST_F(DlExtTest, ExtInfoNoFlags) { + android_dlextinfo extinfo; + extinfo.flags = 0; + handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); + ASSERT_DL_NOTNULL(handle_); + fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); + ASSERT_DL_NOTNULL(f); + EXPECT_EQ(4, f()); +} + +TEST_F(DlExtTest, Reserved) { + void* start = mmap(NULL, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, + -1, 0); + ASSERT_TRUE(start != MAP_FAILED); + android_dlextinfo extinfo; + extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS; + extinfo.reserved_addr = start; + extinfo.reserved_size = LIBSIZE; + handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); + ASSERT_DL_NOTNULL(handle_); + fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); + ASSERT_DL_NOTNULL(f); + EXPECT_GE(f, start); + EXPECT_LT(reinterpret_cast<void*>(f), + reinterpret_cast<char*>(start) + LIBSIZE); + EXPECT_EQ(4, f()); +} + +TEST_F(DlExtTest, ReservedTooSmall) { + void* start = mmap(NULL, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, + -1, 0); + ASSERT_TRUE(start != MAP_FAILED); + android_dlextinfo extinfo; + extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS; + extinfo.reserved_addr = start; + extinfo.reserved_size = PAGE_SIZE; + handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); + EXPECT_EQ(NULL, handle_); +} + +TEST_F(DlExtTest, ReservedHint) { + void* start = mmap(NULL, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, + -1, 0); + ASSERT_TRUE(start != MAP_FAILED); + android_dlextinfo extinfo; + extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT; + extinfo.reserved_addr = start; + extinfo.reserved_size = LIBSIZE; + handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); + ASSERT_DL_NOTNULL(handle_); + fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); + ASSERT_DL_NOTNULL(f); + EXPECT_GE(f, start); + EXPECT_LT(reinterpret_cast<void*>(f), + reinterpret_cast<char*>(start) + LIBSIZE); + EXPECT_EQ(4, f()); +} + +TEST_F(DlExtTest, ReservedHintTooSmall) { + void* start = mmap(NULL, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, + -1, 0); + ASSERT_TRUE(start != MAP_FAILED); + android_dlextinfo extinfo; + extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT; + extinfo.reserved_addr = start; + extinfo.reserved_size = PAGE_SIZE; + handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); + ASSERT_DL_NOTNULL(handle_); + fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); + ASSERT_DL_NOTNULL(f); + EXPECT_TRUE(f < start || (reinterpret_cast<void*>(f) >= + reinterpret_cast<char*>(start) + PAGE_SIZE)); + EXPECT_EQ(4, f()); +} |