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("ro.treble.enabled", "true");
50 
51   Variables::AddValue("SYSTEM_EXT", "/system_ext");
52   Variables::AddValue("PRODUCT", "/procut");
53 
54   Variables::AddValue("VNDK_USING_CORE_VARIANT_LIBRARIES",
55                       "vndk_using_core_variant_libraries");
56   Variables::AddValue("SANITIZER_RUNTIME_LIBRARIES",
57                       "sanitizer_runtime_libraries");
58 }
59 
ContainsPath(const std::vector<std::string> & path_list,const std::string & path)60 inline bool ContainsPath(const std::vector<std::string>& path_list,
61                          const std::string& path) {
62   return std::any_of(path_list.begin(),
63                      path_list.end(),
64                      [&](const std::string& item) { return item == path; });
65 }
66 
ContainsSearchPath(const android::linkerconfig::modules::Namespace * ns,const std::string & path)67 inline bool ContainsSearchPath(
68     const android::linkerconfig::modules::Namespace* ns,
69     const std::string& path) {
70   if (!ContainsPath(ns->SearchPaths(), path)) {
71     return false;
72   }
73 
74   auto asan_search_path = ns->AsanSearchPaths();
75 
76   if (!ContainsPath(asan_search_path, path)) {
77     return false;
78   }
79 
80   if (!android::base::StartsWith(path, "/apex") &&
81       !ContainsPath(asan_search_path, "/data/asan" + path)) {
82     return false;
83   }
84 
85   return true;
86 }
87 
ContainsPermittedPath(const android::linkerconfig::modules::Namespace * ns,const std::string & path)88 inline bool ContainsPermittedPath(
89     const android::linkerconfig::modules::Namespace* ns,
90     const std::string& path) {
91   if (!ContainsPath(ns->PermittedPaths(), path)) {
92     return false;
93   }
94 
95   auto asan_search_path = ns->AsanPermittedPaths();
96 
97   if (!ContainsPath(asan_search_path, path)) {
98     return false;
99   }
100 
101   if (!android::base::StartsWith(path, "/apex") &&
102       !ContainsPath(asan_search_path, "/data/asan" + path)) {
103     return false;
104   }
105 
106   return true;
107 }