1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "partition_alloc/partition_alloc_base/native_library.h"
6 
7 #include <dlfcn.h>
8 
9 #include "build/build_config.h"
10 #include "partition_alloc/partition_alloc_base/check.h"
11 #include "partition_alloc/partition_alloc_base/files/file_path.h"
12 
13 namespace partition_alloc::internal::base {
14 
ToString() const15 std::string NativeLibraryLoadError::ToString() const {
16   return message;
17 }
18 
LoadNativeLibraryWithOptions(const FilePath & library_path,const NativeLibraryOptions & options,NativeLibraryLoadError * error)19 NativeLibrary LoadNativeLibraryWithOptions(const FilePath& library_path,
20                                            const NativeLibraryOptions& options,
21                                            NativeLibraryLoadError* error) {
22   // TODO(1151236): Temporarily disable this ScopedBlockingCall. After making
23   // partition_alloc ScopedBlockingCall() to see the same blocking_observer_
24   // in base's ScopedBlockingCall(), we will copy ScopedBlockingCall code and
25   // will enable this.
26 
27   // dlopen() opens the file off disk.
28   // ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK);
29 
30   // We deliberately do not use RTLD_DEEPBIND by default.  For the history why,
31   // please refer to the bug tracker.  Some useful bug reports to read include:
32   // http://crbug.com/17943, http://crbug.com/17557, http://crbug.com/36892,
33   // and http://crbug.com/40794.
34   int flags = RTLD_LAZY;
35 #if BUILDFLAG(IS_ANDROID) || !defined(RTLD_DEEPBIND)
36   // Certain platforms don't define RTLD_DEEPBIND. Android dlopen() requires
37   // further investigation, as it might vary across versions. Crash here to
38   // warn developers that they're trying to rely on uncertain behavior.
39   PA_BASE_CHECK(!options.prefer_own_symbols);
40 #else
41   if (options.prefer_own_symbols) {
42     flags |= RTLD_DEEPBIND;
43   }
44 #endif
45   void* dl = dlopen(library_path.value().c_str(), flags);
46   if (!dl && error) {
47     error->message = dlerror();
48   }
49 
50   return dl;
51 }
52 
GetFunctionPointerFromNativeLibrary(NativeLibrary library,const std::string & name)53 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
54                                           const std::string& name) {
55   return dlsym(library, name.c_str());
56 }
57 
58 }  // namespace partition_alloc::internal::base
59