Coverage Report

Created: 2026-01-13 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/perfetto/buildtools/android-unwinding/libunwindstack/Global.cpp
Line
Count
Source
1
/*
2
 * Copyright (C) 2018 The Android Open Source Project
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
17
#include <stdint.h>
18
#include <string.h>
19
#include <sys/mman.h>
20
21
#include <string>
22
#include <vector>
23
24
#include <android-base/file.h>
25
26
#include <unwindstack/Global.h>
27
#include <unwindstack/MapInfo.h>
28
#include <unwindstack/Maps.h>
29
#include <unwindstack/Memory.h>
30
31
namespace unwindstack {
32
33
0
Global::Global(std::shared_ptr<Memory>& memory) : memory_(memory) {}
34
Global::Global(std::shared_ptr<Memory>& memory, std::vector<std::string>& search_libs)
35
0
    : memory_(memory), search_libs_(search_libs) {}
36
37
0
void Global::SetArch(ArchEnum arch) {
38
0
  if (arch_ == ARCH_UNKNOWN) {
39
0
    arch_ = arch;
40
0
    ProcessArch();
41
0
  }
42
0
}
43
44
0
bool Global::Searchable(const std::string& name) {
45
0
  if (search_libs_.empty()) {
46
0
    return true;
47
0
  }
48
49
0
  if (name.empty()) {
50
0
    return false;
51
0
  }
52
53
0
  std::string base_name = android::base::Basename(name);
54
0
  for (const std::string& lib : search_libs_) {
55
0
    if (base_name == lib) {
56
0
      return true;
57
0
    }
58
0
  }
59
0
  return false;
60
0
}
61
62
0
void Global::FindAndReadVariable(Maps* maps, const char* var_str) {
63
0
  std::string variable(var_str);
64
  // When looking for global variables, do not arbitrarily search every
65
  // readable map. Instead look for a specific pattern that must exist.
66
  // The pattern should be a readable map, followed by a read-write
67
  // map with a non-zero offset.
68
  // For example:
69
  //   f0000-f1000 0 r-- /system/lib/libc.so
70
  //   f1000-f2000 1000 r-x /system/lib/libc.so
71
  //   f2000-f3000 2000 rw- /system/lib/libc.so
72
  // This also works:
73
  //   f0000-f2000 0 r-- /system/lib/libc.so
74
  //   f2000-f3000 2000 rw- /system/lib/libc.so
75
  // It is also possible to see empty maps after the read-only like so:
76
  //   f0000-f1000 0 r-- /system/lib/libc.so
77
  //   f1000-f2000 0 ---
78
  //   f2000-f3000 1000 r-x /system/lib/libc.so
79
  //   f3000-f4000 2000 rw- /system/lib/libc.so
80
0
  MapInfo* map_zero = nullptr;
81
0
  for (const auto& info : *maps) {
82
0
    if ((info->flags() & (PROT_READ | PROT_WRITE)) == (PROT_READ | PROT_WRITE) &&
83
0
        map_zero != nullptr && Searchable(info->name()) && info->name() == map_zero->name()) {
84
0
      Elf* elf = map_zero->GetElf(memory_, arch());
85
0
      uint64_t ptr;
86
0
      if (elf->GetGlobalVariableOffset(variable, &ptr) && ptr != 0) {
87
0
        uint64_t offset_end = info->offset() + info->end() - info->start();
88
0
        if (ptr >= info->offset() && ptr < offset_end) {
89
0
          ptr = info->start() + ptr - info->offset();
90
0
          if (ReadVariableData(ptr)) {
91
0
            break;
92
0
          }
93
0
        }
94
0
      }
95
0
    } else if (info->offset() == 0 && !info->name().empty()) {
96
0
      map_zero = info.get();
97
0
    }
98
0
  }
99
0
}
100
101
}  // namespace unwindstack