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 "linkerconfig/namespace.h"
21 #include "linkerconfig/variables.h"
22 
MockVndkVariables(std::string partition,std::string vndk_ver)23 inline void MockVndkVariables(std::string partition, std::string vndk_ver) {
24   using android::linkerconfig::modules::Variables;
25 
26   Variables::AddValue(partition + "_VNDK_VERSION", vndk_ver);
27   Variables::AddValue("LLNDK_LIBRARIES_" + partition, "llndk_libraries");
28   Variables::AddValue("PRIVATE_LLNDK_LIBRARIES_" + partition,
29                       "private_llndk_libraries");
30   Variables::AddValue("VNDK_SAMEPROCESS_LIBRARIES_" + partition,
31                       "vndk_sameprocess_libraries");
32   Variables::AddValue("VNDK_CORE_LIBRARIES_" + partition, "vndk_core_libraries");
33   Variables::AddValue("SANITIZER_DEFAULT_" + partition,
34                       "sanitizer_default_libraries");
35 }
36 
37 inline void MockVariables(std::string vndk_ver = "Q") {
38   using android::linkerconfig::modules::Variables;
39 
40   MockVndkVariables("VENDOR", vndk_ver);
41   Variables::AddValue("ro.vndk.version", vndk_ver);
42 
43   MockVndkVariables("PRODUCT", vndk_ver);
44   Variables::AddValue("ro.product.vndk.version", vndk_ver);
45 
46   Variables::AddValue("SYSTEM_EXT", "/system_ext");
47   Variables::AddValue("PRODUCT", "/procut");
48 
49   Variables::AddValue("VNDK_USING_CORE_VARIANT_LIBRARIES",
50                       "vndk_using_core_variant_libraries");
51   Variables::AddValue("STUB_LIBRARIES", "stub_libraries");
52   Variables::AddValue("SANITIZER_RUNTIME_LIBRARIES",
53                       "sanitizer_runtime_libraries");
54 }
55 
ContainsPath(const std::vector<std::string> & path_list,const std::string & path)56 inline bool ContainsPath(const std::vector<std::string>& path_list,
57                          const std::string& path) {
58   for (auto item : path_list) {
59     if (item == path) {
60       return true;
61     }
62   }
63 
64   return false;
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 }