Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: base/native_library_unittest.cc

Issue 2277863002: Enable loading native libraries with RTLD_DEEPBIND (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/files/file_path.h" 5 #include "base/files/file_path.h"
6 #include "base/native_library.h" 6 #include "base/native_library.h"
7 #include "base/path_service.h"
8 #include "build/build_config.h"
7 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
8 10
11 #if defined(OS_WIN)
12 #define EXPORT __declspec(dllexport)
13 #else
14 #define EXPORT __attribute__((visibility("default")))
15 #endif
16
17 // This symbol name must match the exported symbol in
18 // native_library_test_library.cc.
19 int EXPORT g_native_library_test_symbol = 0;
20
9 namespace base { 21 namespace base {
10 22
11 const FilePath::CharType kDummyLibraryPath[] = 23 const FilePath::CharType kDummyLibraryPath[] =
12 FILE_PATH_LITERAL("dummy_library"); 24 FILE_PATH_LITERAL("dummy_library");
13 25
14 TEST(NativeLibraryTest, LoadFailure) { 26 TEST(NativeLibraryTest, LoadFailure) {
15 NativeLibraryLoadError error; 27 NativeLibraryLoadError error;
16 EXPECT_FALSE(LoadNativeLibrary(FilePath(kDummyLibraryPath), &error)); 28 EXPECT_FALSE(LoadNativeLibrary(FilePath(kDummyLibraryPath), &error));
17 EXPECT_FALSE(error.ToString().empty()); 29 EXPECT_FALSE(error.ToString().empty());
18 } 30 }
(...skipping 10 matching lines...) Expand all
29 #elif defined(OS_MACOSX) 41 #elif defined(OS_MACOSX)
30 "libmylib.dylib"; 42 "libmylib.dylib";
31 #elif defined(OS_POSIX) 43 #elif defined(OS_POSIX)
32 "libmylib.so"; 44 "libmylib.so";
33 #elif defined(OS_WIN) 45 #elif defined(OS_WIN)
34 "mylib.dll"; 46 "mylib.dll";
35 #endif 47 #endif
36 EXPECT_EQ(kExpectedName, GetNativeLibraryName("mylib")); 48 EXPECT_EQ(kExpectedName, GetNativeLibraryName("mylib"));
37 } 49 }
38 50
51 #if !defined(OS_IOS)
52
53 const char kTestLibraryName[] =
54 #if defined(OS_MACOSX)
55 "libnative_library_test_library.dylib";
56 #elif defined(OS_ANDROID)
57 "libnative_library_test_library.cr.so";
Primiano Tucci (use gerrit) 2016/08/24 22:14:09 .cr.so should be only for component builds. Also l
Ken Rockot(use gerrit already) 2016/08/24 23:23:57 I suppose we don't need to support it now, but I w
58 #elif defined(OS_POSIX)
59 "libnative_library_test_library.so";
60 #elif defined(OS_WIN)
61 "native_library_test_library.dll";
62 #endif
63
64 class TestLibrary {
65 public:
66 explicit TestLibrary(
67 const NativeLibraryOptions& options = NativeLibraryOptions())
68 : library_(nullptr) {
69 base::FilePath exe_path;
70 CHECK(base::PathService::Get(base::DIR_EXE, &exe_path));
71
72 library_ = LoadNativeLibraryWithOptions(
73 exe_path.AppendASCII(kTestLibraryName), options, nullptr);
74 CHECK(library_);
75 }
76
77 ~TestLibrary() {
78 UnloadNativeLibrary(library_);
79 }
80
81 int GetTestValue() {
82 return GetFunction<int(*)()>("GetTestValue")();
83 }
84
85 bool IsSameTestSymbolAddress(int* address) {
86 return GetFunction<bool(*)(int*)>("IsSameTestSymbolAddress")(address);
87 }
88
89 private:
90 template <typename FuncType>
91 FuncType GetFunction(const char* name) {
92 FuncType func = reinterpret_cast<FuncType>(
93 GetFunctionPointerFromNativeLibrary(library_, name));
94 CHECK(func);
95 return func;
96 }
97
98 NativeLibrary library_;
99 };
100
101 // Verifies that we can load a native library and resolve its exported symbols.
102 TEST(NativeLibraryTest, LoadLibrary) {
103 TestLibrary library;
104 EXPECT_EQ(42, library.GetTestValue());
105 }
106
107 // Verifies that if we load a native library which exports some symbol also
108 // exported by the main binary, the native library resolves the symbol to its
109 // own local definition. Note that on some POSIX builds this may not be the
110 // default behavior, so the |prefer_own_symbols| option must be used to opt in.
111 TEST(NativeLibraryTest, LoadLibraryPreferOwnSymbols) {
Primiano Tucci (use gerrit) 2016/08/24 22:14:09 I think it's just clear if this is #ifdef OS_LINUX
Ken Rockot(use gerrit already) 2016/08/24 23:23:57 see comment in BUILD.gn
112 #if defined(EXPECT_GLOBAL_RTLD_BEHAVIOR)
113 // NOTE: This is only defined on non-Android POSIX builds which use gold for
114 // linking. Otherwise we don't expect this behavior.
115 {
116 TestLibrary library;
117 EXPECT_TRUE(library.IsSameTestSymbolAddress(&g_native_library_test_symbol));
118 }
119 #endif // defined(EXPECT_GLOBAL_RTLD_BEHAVIOR)
120
121 NativeLibraryOptions options;
122 options.prefer_own_symbols = true;
123 TestLibrary library(options);
124 EXPECT_FALSE(library.IsSameTestSymbolAddress(&g_native_library_test_symbol));
125 }
126
127 #endif // !defined(OS_IOS)
128
39 } // namespace base 129 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698