1 /*
2 * Copyright (C) 2020 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 "berberis/base/config_globals.h"
18
19 #include <bitset>
20 #include <cerrno>
21 #include <cstdint>
22 #include <cstdlib> // strtoull
23 #include <string>
24
25 #include "berberis/base/logging.h"
26 #include "berberis/base/strings.h"
27
28 namespace berberis {
29
30 namespace {
31
ToString(ConfigFlag flag)32 std::string ToString(ConfigFlag flag) {
33 switch (flag) {
34 case kVerboseTranslation:
35 return "verbose-translation";
36 case kAccurateSigsegv:
37 return "accurate-sigsegv";
38 case kTopByteIgnore:
39 return "top-byte-ignore";
40 case kDisableRegMap:
41 return "disable-reg-map";
42 case kEnableDisjointRegionsTranslation:
43 return "enable-disjoint-regions-translation";
44 case kNumConfigFlags:
45 break;
46 }
47 return "<unknown-config-flag>";
48 }
49
MakeConfigFlagsSet()50 std::bitset<kNumConfigFlags> MakeConfigFlagsSet() {
51 ConfigStr var("BERBERIS_FLAGS", "ro.berberis.flags");
52 std::bitset<kNumConfigFlags> flags_set;
53 if (!var.get()) {
54 return flags_set;
55 }
56 auto token_vector = Split(var.get(), ",");
57 for (const auto& token : token_vector) {
58 bool found = false;
59 for (int flag = 0; flag < kNumConfigFlags; flag++) {
60 if (token == ToString(ConfigFlag(flag))) {
61 flags_set.set(flag);
62 found = true;
63 break;
64 }
65 }
66 if (!found) {
67 ALOGW("Unrecognized config flag '%s' - ignoring", token.c_str());
68 }
69 }
70 return flags_set;
71 }
72
ParseAddr(const char * addr_cstr)73 uintptr_t ParseAddr(const char* addr_cstr) {
74 if (!addr_cstr) {
75 return 0;
76 }
77 char* end_ptr = nullptr;
78 errno = 0;
79 uintptr_t addr = static_cast<uintptr_t>(strtoull(addr_cstr, &end_ptr, 16));
80
81 // Warning: setting errno on failure is implementation defined. So we also use extra heuristics.
82 if (errno != 0 || (*end_ptr != '\n' && *end_ptr != '\0')) {
83 ALOGE("Cannot convert \"%s\" to integer: %s\n",
84 addr_cstr,
85 errno != 0 ? strerror(errno) : "unexpected end of string");
86 return 0;
87 }
88 return addr;
89 }
90
91 } // namespace
92
GetTracingConfig()93 const char* GetTracingConfig() {
94 static ConfigStr var("BERBERIS_TRACING", "berberis.tracing");
95 return var.get();
96 }
97
GetTranslationModeConfig()98 const char* GetTranslationModeConfig() {
99 static ConfigStr var("BERBERIS_MODE", "berberis.mode");
100 return var.get();
101 }
102
GetProfilingConfig()103 const char* GetProfilingConfig() {
104 static ConfigStr var("BERBERIS_PROFILING", "berberis.profiling");
105 return var.get();
106 }
107
GetEntryPointOverride()108 uintptr_t GetEntryPointOverride() {
109 static ConfigStr var("BERBERIS_ENTRY_POINT", "berberis.entry_point");
110 static uintptr_t entry_point = ParseAddr(var.get());
111 return entry_point;
112 }
113
IsConfigFlagSet(ConfigFlag flag)114 bool IsConfigFlagSet(ConfigFlag flag) {
115 static auto flags_set = MakeConfigFlagsSet();
116 return flags_set.test(flag);
117 }
118
119 } // namespace berberis
120