1 /*
2  * Copyright (C) 2019 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 #pragma once
17 
18 #include <android-base/strings.h>
19 
20 #include <algorithm>
21 
22 #include "linkerconfig/namespace.h"
23 #include "linkerconfig/variables.h"
24 
MockVndkVariables(const std::string & partition,const std::string & vndk_ver)25 inline void MockVndkVariables(const std::string& partition,
26                               const std::string& vndk_ver) {
27   using android::linkerconfig::modules::Variables;
28 
29   Variables::AddValue(partition + "_VNDK_VERSION", vndk_ver);
30   Variables::AddValue("LLNDK_LIBRARIES_" + partition, "llndk_libraries");
31   Variables::AddValue("PRIVATE_LLNDK_LIBRARIES_" + partition,
32                       "private_llndk_libraries");
33   Variables::AddValue("VNDK_SAMEPROCESS_LIBRARIES_" + partition,
34                       "vndk_sameprocess_libraries");
35   Variables::AddValue("VNDK_CORE_LIBRARIES_" + partition, "vndk_core_libraries");
36   Variables::AddValue("SANITIZER_DEFAULT_" + partition,
37                       "sanitizer_default_libraries");
38 }
39 
40 inline void MockVariables(std::string vndk_ver = "Q") {
41   using android::linkerconfig::modules::Variables;
42 
43   MockVndkVariables("VENDOR", vndk_ver);
44   Variables::AddValue("ro.vndk.version", vndk_ver);
45 
46   MockVndkVariables("PRODUCT", vndk_ver);
47   Variables::AddValue("ro.product.vndk.version", vndk_ver);
48 
49   Variables::AddValue("SYSTEM_EXT", "/system_ext");
50   Variables::AddValue("PRODUCT", "/procut");
51 
52   Variables::AddValue("VNDK_USING_CORE_VARIANT_LIBRARIES",
53                       "vndk_using_core_variant_libraries");
54   Variables::AddValue("SANITIZER_RUNTIME_LIBRARIES",
55                       "sanitizer_runtime_libraries");
56 }
57 
ContainsPath(const std::vector<std::string> & path_list,const std::string & path)58 inline bool ContainsPath(const std::vector<std::string>& path_list,
59                          const std::string& path) {
60   return std::any_of(path_list.begin(),
61                      path_list.end(),
62                      [&](const std::string& item) { return item == path; });
63 }
64 
ContainsSearchPath(const android::linkerconfig::modules::Namespace * ns,const std::string & path)65 inline bool ContainsSearchPath(
66     const android::linkerconfig::modules::Namespace* ns,
67     const std::string& path) {
68   if (!ContainsPath(ns->SearchPaths(), path)) {
69     return false;
70   }
71 
72   auto asan_search_path = ns->AsanSearchPaths();
73 
74   if (!ContainsPath(asan_search_path, path)) {
75     return false;
76   }
77 
78   if (!android::base::StartsWith(path, "/apex") &&
79       !ContainsPath(asan_search_path, "/data/asan" + path)) {
80     return false;
81   }
82 
83   return true;
84 }
85 
ContainsPermittedPath(const android::linkerconfig::modules::Namespace * ns,const std::string & path)86 inline bool ContainsPermittedPath(
87     const android::linkerconfig::modules::Namespace* ns,
88     const std::string& path) {
89   if (!ContainsPath(ns->PermittedPaths(), path)) {
90     return false;
91   }
92 
93   auto asan_search_path = ns->AsanPermittedPaths();
94 
95   if (!ContainsPath(asan_search_path, path)) {
96     return false;
97   }
98 
99   if (!android::base::StartsWith(path, "/apex") &&
100       !ContainsPath(asan_search_path, "/data/asan" + path)) {
101     return false;
102   }
103 
104   return true;
105 }