File: bundle_utils.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (118 lines) | stat: -rw-r--r-- 4,615 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/android/bundle_utils.h"

#include <android/dlext.h>
#include <dlfcn.h>

#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/bundle_utils_jni/BundleUtils_jni.h"
#include "base/check.h"
#include "base/compiler_specific.h"
#include "base/containers/span.h"
#include "base/files/file_path.h"
#include "base/notreached.h"

// These symbols are added by the lld linker when creating a partitioned shared
// library. The symbols live in the base library, and are used to properly load
// the other partitions (feature libraries) when needed.
struct PartitionIndexEntry {
  int32_t name_relptr;
  int32_t addr_relptr;
  uint32_t size;
};
static_assert(sizeof(PartitionIndexEntry) == 12U,
              "Unexpected PartitionIndexEntry size");

// Marked as weak_import because these symbols are lld-specific. The method that
// uses them will only be invoked in builds that have lld-generated partitions.
extern PartitionIndexEntry __part_index_begin[] __attribute__((weak_import));
extern PartitionIndexEntry __part_index_end[] __attribute__((weak_import));

namespace base {
namespace android {

namespace {

// Takes as input a "rel pointer", which is a pointer to a 32-bit integer that
// contains the offset to add to the pointer, in order to find the actual
// desired pointer address.
//
// PRECONDITIONS: The value in the pointer must provide an offset from the
// pointer that stays inside the same allocation.
UNSAFE_BUFFER_USAGE void* ReadRelPtr(int32_t* relptr) {
  // SAFETY: This relies on the caller to provide a valid pointer + value.
  return UNSAFE_BUFFERS(reinterpret_cast<char*>(relptr) + *relptr);
}

}  // namespace

// static
std::string BundleUtils::ResolveLibraryPath(const std::string& library_name,
                                            const std::string& split_name) {
  JNIEnv* env = AttachCurrentThread();
  return Java_BundleUtils_getNativeLibraryPath(env, library_name, split_name);
}

// static
bool BundleUtils::HasAnyInstalledSplits() {
  return Java_BundleUtils_hasAnyInstalledSplits(AttachCurrentThread());
}

// static
void* BundleUtils::DlOpenModuleLibraryPartition(const std::string& library_name,
                                                const std::string& partition,
                                                const std::string& split_name) {
  // TODO(crbug.com/40656179): Remove this tolerance.
  std::string library_path = ResolveLibraryPath(library_name, split_name);
  if (library_path.empty()) {
    return nullptr;
  }

  // Linear search is required here because the partition descriptors are not
  // ordered. If a large number of partitions come into existence, lld could be
  // modified to sort the partitions.
  DCHECK(__part_index_begin != nullptr);
  DCHECK(__part_index_end != nullptr);
  // SAFETY: `__part_index_begin` and `__part_index_end` are provided by the
  // linker (https://lld.llvm.org/Partitions.html) and we rely on the linker to
  // provide pointers that are part of the same allocation with
  // `__part_index_begin <= __part_index_end`.
  auto parts = UNSAFE_BUFFERS(
      span<PartitionIndexEntry>(__part_index_begin, __part_index_end));
  for (PartitionIndexEntry& part : parts) {
    std::string name(static_cast<const char*>(
        // SAFETY: `name_relptr` plus its value points to a nul-terminated
        // string containing the soname of the partition. This pointer and
        // offset is provided by the linker and thus assumed to always be
        // correct. https://lld.llvm.org/Partitions.html
        UNSAFE_BUFFERS(ReadRelPtr(&part.name_relptr))));
    if (name == partition) {
      android_dlextinfo info = {};
      info.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
      info.reserved_addr =
          // SAFETY: `addr_offset` field is a relative pointer to the
          // partition's load address. This pointer and offset is provided by
          // the linker and thus assumed to always be correct.
          // https://lld.llvm.org/Partitions.html
          UNSAFE_BUFFERS(ReadRelPtr(&part.addr_relptr));
      info.reserved_size = part.size;

#if __ANDROID_API__ >= 24
      return android_dlopen_ext(library_path.c_str(), RTLD_LOCAL, &info);
#else
      // When targeting pre-N, such as for Cronet, android_dlopen_ext() might
      // not be available on the system.
      NOTREACHED() << "android_dlopen_ext not available";
#endif
    }
  }

  NOTREACHED();
}

}  // namespace android
}  // namespace base