Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
native_symbol_posix.cc
Go to the documentation of this file.
1// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#include "vm/globals.h"
6#if defined(DART_HOST_OS_ANDROID) || defined(DART_HOST_OS_FUCHSIA) || \
7 defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_MACOS)
8
10#include "vm/native_symbol.h"
11
12#include <cxxabi.h> // NOLINT
13#include <dlfcn.h> // NOLINT
14
15namespace dart {
16
17// On Fuchsia, in lieu of the ELF dynamic symbol table consumed through dladdr,
18// we consumes symbols produced by //topaz/runtime/dart/profiler_symbols and
19// provided to the VM by the embedder through Dart_AddSymbols. They have the
20// format
21//
22// struct {
23// uint32_t num_entries;
24// struct {
25// uint32_t offset;
26// uint32_t size;
27// uint32_t string_table_offset;
28// } entries[num_entries];
29// const char* string_table;
30// }
31//
32// Entries are sorted by offset. String table entries are NUL-terminated.
33class NativeSymbols {
34 public:
35 NativeSymbols(const char* dso_name, void* buffer, size_t size)
36 : next_(nullptr), dso_name_(dso_name) {
37 num_entries_ = *reinterpret_cast<uint32_t*>(buffer);
38 entries_ =
39 reinterpret_cast<Entry*>(reinterpret_cast<uint32_t*>(buffer) + 1);
40 name_table_ = reinterpret_cast<const char*>(entries_ + num_entries_);
41 }
42
43 NativeSymbols* next() const { return next_; }
44 void set_next(NativeSymbols* symbols) { next_ = symbols; }
45
46 bool Lookup(const char* dso_name,
47 uword dso_offset,
48 uword* start_offset,
49 const char** name) {
50 if (strcmp(dso_name, dso_name_) != 0) {
51 return false;
52 }
53
54 intptr_t lo = 0;
55 intptr_t hi = num_entries_ - 1;
56 while (lo <= hi) {
57 intptr_t mid = (hi - lo + 1) / 2 + lo;
58 ASSERT(mid >= lo);
59 ASSERT(mid <= hi);
60 const Entry& entry = entries_[mid];
61 if (dso_offset < entry.offset) {
62 hi = mid - 1;
63 } else if (dso_offset >= (entry.offset + entry.size)) {
64 lo = mid + 1;
65 } else {
66 *start_offset = entry.offset;
67 *name = &name_table_[entry.name_offset];
68 return true;
69 }
70 }
71
72 return false;
73 }
74
75 private:
76 struct Entry {
77 uint32_t offset;
78 uint32_t size;
79 uint32_t name_offset;
80 };
81
82 NativeSymbols* next_;
83 const char* const dso_name_;
84 intptr_t num_entries_;
85 Entry* entries_;
86 const char* name_table_;
87
88 DISALLOW_COPY_AND_ASSIGN(NativeSymbols);
89};
90
91static NativeSymbols* symbols_ = nullptr;
92
94
96 NativeSymbols* symbols = symbols_;
97 symbols_ = nullptr;
98 while (symbols != nullptr) {
99 NativeSymbols* next = symbols->next();
100 delete symbols;
101 symbols = next;
102 }
103}
104
106 Dl_info info;
107 int r = dladdr(reinterpret_cast<void*>(pc), &info);
108 if (r == 0) {
109 return nullptr;
110 }
111
112 auto const dso_name = info.dli_fname;
113 const auto dso_base = reinterpret_cast<uword>(info.dli_fbase);
114 const auto dso_offset = pc - dso_base;
115
116 for (NativeSymbols* symbols = symbols_; symbols != nullptr;
117 symbols = symbols->next()) {
118 uword symbol_start_offset;
119 const char* symbol_name;
120 if (symbols->Lookup(dso_name, dso_offset, &symbol_start_offset,
121 &symbol_name)) {
122 if (start != nullptr) {
123 *start = symbol_start_offset + dso_base;
124 }
125 return strdup(symbol_name);
126 }
127 }
128
129#if !defined(DART_HOST_OS_FUCHSIA)
130 // Fallback for libc, etc.
131 if (info.dli_sname == nullptr) {
132 return nullptr;
133 }
134 if (start != nullptr) {
135 *start = reinterpret_cast<uword>(info.dli_saddr);
136 }
137 int status = 0;
138 size_t len = 0;
139 char* demangled = abi::__cxa_demangle(info.dli_sname, nullptr, &len, &status);
140 MSAN_UNPOISON(demangled, len);
141 if (status == 0) {
142 return demangled;
143 }
144 return strdup(info.dli_sname);
145#else
146 // Never works on Fuchsia; avoid linking in cxa_demangle.
147 return nullptr;
148#endif
149}
150
152 free(name);
153}
154
156 uword* dso_base,
157 char** dso_name) {
158 Dl_info info;
159 int r = dladdr(reinterpret_cast<void*>(pc), &info);
160 if (r == 0) {
161 return false;
162 }
163 if (dso_base != nullptr) {
164 *dso_base = reinterpret_cast<uword>(info.dli_fbase);
165 }
166 if (dso_name != nullptr) {
167 *dso_name = strdup(info.dli_fname);
168 }
169 return true;
170}
171
172void NativeSymbolResolver::AddSymbols(const char* dso_name,
173 void* buffer,
174 size_t size) {
175 NativeSymbols* symbols = new NativeSymbols(dso_name, buffer, size);
176 symbols->set_next(symbols_);
177 symbols_ = symbols;
178}
179
180} // namespace dart
181
182#endif // defined(DART_HOST_OS_ANDROID) || defined(DART_HOST_OS_FUCHSIA) || \
183 // defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_MACOS)
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
static float next(float f)
static void AddSymbols(const char *dso_name, void *buffer, size_t size)
static void FreeSymbolName(char *name)
static bool LookupSharedObject(uword pc, uword *dso_base=nullptr, char **dso_name=nullptr)
static char * LookupSymbolName(uword pc, uword *start)
#define ASSERT(E)
static const uint8_t buffer[]
const char * name
Definition fuchsia.cc:50
#define MSAN_UNPOISON(ptr, len)
const char *const name
uintptr_t uword
Definition globals.h:501
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition switches.h:259
char * strdup(const char *str1)
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition globals.h:581
Point offset