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

Unified 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: Enable loading native libraries with RTLD_DEEPBIND Created 4 years, 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/native_library_test_library.cc ('k') | base/native_library_win.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/native_library_unittest.cc
diff --git a/base/native_library_unittest.cc b/base/native_library_unittest.cc
index c189f56e995c04598df5bf2d85c1a882dcafd629..64270b8e57c576e9bab6e9fbee643e8b08995102 100644
--- a/base/native_library_unittest.cc
+++ b/base/native_library_unittest.cc
@@ -4,8 +4,25 @@
#include "base/files/file_path.h"
#include "base/native_library.h"
+#include "base/path_service.h"
+#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
+#if defined(OS_WIN)
+#define EXPORT __declspec(dllexport)
+#else
+#define EXPORT __attribute__((visibility("default")))
+#endif
+
+// This variable name must match the exported variable in
+// native_library_test_library.cc.
+int EXPORT g_native_library_exported_value = 0;
+
+// This function name must match the exported function with the same signature
+// in native_library_test_library.cc. This function must return a different
+// value from the one defined there.
+int EXPORT GetNativeLibraryTestMagicValue() { return 41; }
+
namespace base {
const FilePath::CharType kDummyLibraryPath[] =
@@ -36,4 +53,84 @@ TEST(NativeLibraryTest, GetNativeLibraryName) {
EXPECT_EQ(kExpectedName, GetNativeLibraryName("mylib"));
}
+// We don't support dynamic loading on iOS, and ASAN will complain about our
+// intentional ODR violation here.
+#if !defined(OS_IOS) && !defined(ADDRESS_SANITIZER)
+
+const char kTestLibraryName[] =
+#if defined(OS_MACOSX)
+ "libnative_library_test_library.dylib";
+#elif defined(OS_ANDROID) && defined(COMPONENT_BUILD)
+ "libnative_library_test_library.cr.so";
+#elif defined(OS_POSIX)
+ "libnative_library_test_library.so";
+#elif defined(OS_WIN)
+ "native_library_test_library.dll";
+#endif
+
+class TestLibrary {
+ public:
+ explicit TestLibrary(
+ const NativeLibraryOptions& options = NativeLibraryOptions())
+ : library_(nullptr) {
+ base::FilePath exe_path;
+ CHECK(base::PathService::Get(base::DIR_EXE, &exe_path));
+
+ library_ = LoadNativeLibraryWithOptions(
+ exe_path.AppendASCII(kTestLibraryName), options, nullptr);
+ CHECK(library_);
+ }
+
+ ~TestLibrary() {
+ UnloadNativeLibrary(library_);
+ }
+
+ template <typename ReturnType, typename... Args>
+ ReturnType Call(const char* function_name, Args... args) {
+ return reinterpret_cast<ReturnType(*)(Args...)>(
+ GetFunctionPointerFromNativeLibrary(library_, function_name))(args...);
+ }
+
+ private:
+ NativeLibrary library_;
+};
+
+// Verifies that we can load a native library and resolve its exported symbols.
+TEST(NativeLibraryTest, LoadLibrary) {
+ TestLibrary library;
+ EXPECT_EQ(5, library.Call<int>("GetSimpleTestValue"));
+}
+
+// Android dlopen() behavior is not well specified and may vary across versions.
+#if !defined(OS_ANDROID)
+
+// Verifies that the |prefer_own_symbols| option satisfies its guarantee that
+// a loaded library will always prefer local symbol resolution before
+// considering global symbols.
+TEST(NativeLibraryTest, LoadLibraryPreferOwnSymbols) {
+ NativeLibraryOptions options;
+ options.prefer_own_symbols = true;
+ TestLibrary library(options);
+
+ // Verify that this binary and the DSO use different storage for
+ // |g_native_library_exported_value|.
+ g_native_library_exported_value = 1;
+ library.Call<void>("SetExportedValue", 2);
+ EXPECT_EQ(1, g_native_library_exported_value);
+ g_native_library_exported_value = 3;
+ EXPECT_EQ(2, library.Call<int>("GetExportedValue"));
+
+ // Both this binary and the library export GetNativeLibraryTestMagicValue(),
+ // which is called internally by the DSO's GetMagicValue(). We verify here
+ // that the DSO always calls its own local definition rather than ours.
+ EXPECT_EQ(42, library.Call<int>("GetMagicValue"));
Ken Rockot(use gerrit already) 2016/08/25 17:43:21 Only problem now: If prefer_own_symbols is false,
+
+ // Likewise, we should always call our own local definition.
+ EXPECT_EQ(41, GetNativeLibraryTestMagicValue());
+}
+
+#endif // !defined(OS_ANDROID)
+
+#endif // !defined(OS_IOS) && !defined(ADDRESS_SANITIZER)
+
} // namespace base
« no previous file with comments | « base/native_library_test_library.cc ('k') | base/native_library_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698