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 "odr_common.h"
18
19 #include <sys/system_properties.h>
20
21 #include <functional>
22 #include <initializer_list>
23 #include <regex>
24 #include <sstream>
25 #include <string>
26 #include <string_view>
27
28 #include "android-base/logging.h"
29 #include "android-base/parseint.h"
30 #include "android-base/result.h"
31 #include "base/macros.h"
32
33 namespace art {
34 namespace odrefresh {
35
36 namespace {
37 using ::android::base::Result;
38 }
39
QuotePath(std::string_view path)40 std::string QuotePath(std::string_view path) { return ART_FORMAT("'{}'", path); }
41
ParseSecurityPatchStr(const std::string & security_patch_str)42 Result<int> ParseSecurityPatchStr(const std::string& security_patch_str) {
43 std::regex security_patch_regex(R"re((\d{4})-(\d{2})-(\d{2}))re");
44 std::smatch m;
45 if (!std::regex_match(security_patch_str, m, security_patch_regex)) {
46 return Errorf("Invalid security patch string \"{}\"", security_patch_str);
47 }
48 int year = 0, month = 0, day = 0;
49 if (!android::base::ParseInt(m[1], &year) ||
50 !android::base::ParseInt(m[2], &month) ||
51 !android::base::ParseInt(m[3], &day)) {
52 // This should never happen because the string already matches the regex.
53 return Errorf("Unknown error when parsing security patch string \"{}\"", security_patch_str);
54 }
55 return year * 10000 + month * 100 + day;
56 }
57
ShouldDisablePartialCompilation(const std::string & security_patch_str)58 bool ShouldDisablePartialCompilation(const std::string& security_patch_str) {
59 Result<int> security_patch_value = ParseSecurityPatchStr(security_patch_str);
60 if (!security_patch_value.ok()) {
61 LOG(ERROR) << security_patch_value.error();
62 return false;
63 }
64 return security_patch_value.value() < ParseSecurityPatchStr("2022-03-05").value();
65 }
66
ShouldDisableRefresh(const std::string & sdk_version_str)67 bool ShouldDisableRefresh(const std::string& sdk_version_str) {
68 int sdk_version = 0;
69 if (!android::base::ParseInt(sdk_version_str, &sdk_version)) {
70 return false;
71 }
72 return sdk_version >= 32;
73 }
74
SystemPropertyForeach(std::function<void (const char * name,const char * value)> action)75 void SystemPropertyForeach(std::function<void(const char* name, const char* value)> action) {
76 __system_property_foreach(
77 [](const prop_info* pi, void* cookie) {
78 __system_property_read_callback(
79 pi,
80 [](void* cookie, const char* name, const char* value, unsigned) {
81 auto action =
82 reinterpret_cast<std::function<void(const char* name, const char* value)>*>(
83 cookie);
84 (*action)(name, value);
85 },
86 cookie);
87 },
88 &action);
89 }
90
CheckBuildUserfaultFdGc(bool build_enable_uffd_gc,bool is_at_most_u,bool kernel_supports_uffd)91 bool CheckBuildUserfaultFdGc(bool build_enable_uffd_gc,
92 bool is_at_most_u,
93 bool kernel_supports_uffd) {
94 bool runtime_uses_uffd_gc = (build_enable_uffd_gc || is_at_most_u) && kernel_supports_uffd;
95 return build_enable_uffd_gc == runtime_uses_uffd_gc;
96 }
97
98 } // namespace odrefresh
99 } // namespace art
100