1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
#!/usr/bin/env python
#
# Copyright (C) 2018 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Generate API lists for non-SDK API enforcement.
"""
import argparse
from collections import defaultdict
import os
import sys
import re
import functools
# Names of flags recognized by the `hiddenapi` tool.
FLAG_WHITELIST = "whitelist"
FLAG_GREYLIST = "greylist"
FLAG_BLACKLIST = "blacklist"
FLAG_GREYLIST_MAX_O = "greylist-max-o"
FLAG_GREYLIST_MAX_P = "greylist-max-p"
FLAG_CORE_PLATFORM_API = "core-platform-api"
FLAG_PUBLIC_API = "public-api"
FLAG_SYSTEM_API = "system-api"
FLAG_TEST_API = "test-api"
# List of all known flags.
FLAGS_API_LIST = [
FLAG_WHITELIST,
FLAG_GREYLIST,
FLAG_BLACKLIST,
FLAG_GREYLIST_MAX_O,
FLAG_GREYLIST_MAX_P,
]
ALL_FLAGS = FLAGS_API_LIST + [
FLAG_CORE_PLATFORM_API,
FLAG_PUBLIC_API,
FLAG_SYSTEM_API,
FLAG_TEST_API,
]
FLAGS_API_LIST_SET = set(FLAGS_API_LIST)
ALL_FLAGS_SET = set(ALL_FLAGS)
# Suffix used in command line args to express that only known and
# otherwise unassigned entries should be assign the given flag.
# For example, the P dark greylist is checked in as it was in P,
# but signatures have changes since then. The flag instructs this
# script to skip any entries which do not exist any more.
FLAG_IGNORE_CONFLICTS_SUFFIX = "-ignore-conflicts"
# Suffix used in command line args to express that all apis within a given set
# of packages should be assign the given flag.
FLAG_PACKAGES_SUFFIX = "-packages"
# Regex patterns of fields/methods used in serialization. These are
# considered public API despite being hidden.
SERIALIZATION_PATTERNS = [
r'readObject\(Ljava/io/ObjectInputStream;\)V',
r'readObjectNoData\(\)V',
r'readResolve\(\)Ljava/lang/Object;',
r'serialVersionUID:J',
r'serialPersistentFields:\[Ljava/io/ObjectStreamField;',
r'writeObject\(Ljava/io/ObjectOutputStream;\)V',
r'writeReplace\(\)Ljava/lang/Object;',
]
# Single regex used to match serialization API. It combines all the
# SERIALIZATION_PATTERNS into a single regular expression.
SERIALIZATION_REGEX = re.compile(r'.*->(' + '|'.join(SERIALIZATION_PATTERNS) + r')$')
# Predicates to be used with filter_apis.
HAS_NO_API_LIST_ASSIGNED = lambda api, flags: not FLAGS_API_LIST_SET.intersection(flags)
IS_SERIALIZATION = lambda api, flags: SERIALIZATION_REGEX.match(api)
def get_args():
"""Parses command line arguments.
Returns:
Namespace: dictionary of parsed arguments
"""
parser = argparse.ArgumentParser()
parser.add_argument('--output', required=True)
parser.add_argument('--csv', nargs='*', default=[], metavar='CSV_FILE',
help='CSV files to be merged into output')
for flag in ALL_FLAGS:
ignore_conflicts_flag = flag + FLAG_IGNORE_CONFLICTS_SUFFIX
packages_flag = flag + FLAG_PACKAGES_SUFFIX
parser.add_argument('--' + flag, dest=flag, nargs='*', default=[], metavar='TXT_FILE',
help='lists of entries with flag "' + flag + '"')
parser.add_argument('--' + ignore_conflicts_flag, dest=ignore_conflicts_flag, nargs='*',
default=[], metavar='TXT_FILE',
help='lists of entries with flag "' + flag +
'". skip entry if missing or flag conflict.')
parser.add_argument('--' + packages_flag, dest=packages_flag, nargs='*',
default=[], metavar='TXT_FILE',
help='lists of packages to be added to ' + flag + ' list')
return parser.parse_args()
def read_lines(filename):
"""Reads entire file and return it as a list of lines.
Lines which begin with a hash are ignored.
Args:
filename (string): Path to the file to read from.
Returns:
Lines of the file as a list of string.
"""
with open(filename, 'r') as f:
lines = f.readlines();
lines = filter(lambda line: not line.startswith('#'), lines)
lines = map(lambda line: line.strip(), lines)
return set(lines)
def write_lines(filename, lines):
"""Writes list of lines into a file, overwriting the file it it exists.
Args:
filename (string): Path to the file to be writting into.
lines (list): List of strings to write into the file.
"""
lines = map(lambda line: line + '\n', lines)
with open(filename, 'w') as f:
f.writelines(lines)
def extract_package(signature):
"""Extracts the package from a signature.
Args:
signature (string): JNI signature of a method or field.
Returns:
The package name of the class containing the field/method.
"""
full_class_name = signature.split(";->")[0]
package_name = full_class_name[1:full_class_name.rindex("/")]
return package_name.replace('/', '.')
class FlagsDict:
def __init__(self):
self._dict_keyset = set()
self._dict = defaultdict(set)
def _check_entries_set(self, keys_subset, source):
assert isinstance(keys_subset, set)
assert keys_subset.issubset(self._dict_keyset), (
"Error processing: {}\n"
"The following entries were unexpected:\n"
"{}"
"Please visit go/hiddenapi for more information.").format(
source, "".join(map(lambda x: " " + str(x), keys_subset - self._dict_keyset)))
def _check_flags_set(self, flags_subset, source):
assert isinstance(flags_subset, set)
assert flags_subset.issubset(ALL_FLAGS_SET), (
"Error processing: {}\n"
"The following flags were not recognized: \n"
"{}\n"
"Please visit go/hiddenapi for more information.").format(
source, "\n".join(flags_subset - ALL_FLAGS_SET))
def filter_apis(self, filter_fn):
"""Returns APIs which match a given predicate.
This is a helper function which allows to filter on both signatures (keys) and
flags (values). The built-in filter() invokes the lambda only with dict's keys.
Args:
filter_fn : Function which takes two arguments (signature/flags) and returns a boolean.
Returns:
A set of APIs which match the predicate.
"""
return set(filter(lambda x: filter_fn(x, self._dict[x]), self._dict_keyset))
def get_valid_subset_of_unassigned_apis(self, api_subset):
"""Sanitizes a key set input to only include keys which exist in the dictionary
and have not been assigned any API list flags.
Args:
entries_subset (set/list): Key set to be sanitized.
Returns:
Sanitized key set.
"""
assert isinstance(api_subset, set)
return api_subset.intersection(self.filter_apis(HAS_NO_API_LIST_ASSIGNED))
def generate_csv(self):
"""Constructs CSV entries from a dictionary.
Returns:
List of lines comprising a CSV file. See "parse_and_merge_csv" for format description.
"""
return sorted(map(lambda api: ",".join([api] + sorted(self._dict[api])), self._dict))
def parse_and_merge_csv(self, csv_lines, source = "<unknown>"):
"""Parses CSV entries and merges them into a given dictionary.
The expected CSV format is:
<api signature>,<flag1>,<flag2>,...,<flagN>
Args:
csv_lines (list of strings): Lines read from a CSV file.
source (string): Origin of `csv_lines`. Will be printed in error messages.
Throws:
AssertionError if parsed flags are invalid.
"""
# Split CSV lines into arrays of values.
csv_values = [ line.split(',') for line in csv_lines ]
# Update the full set of API signatures.
self._dict_keyset.update([ csv[0] for csv in csv_values ])
# Check that all flags are known.
csv_flags = set(functools.reduce(
lambda x, y: set(x).union(y),
[ csv[1:] for csv in csv_values ],
[]))
self._check_flags_set(csv_flags, source)
# Iterate over all CSV lines, find entry in dict and append flags to it.
for csv in csv_values:
flags = csv[1:]
if (FLAG_PUBLIC_API in flags) or (FLAG_SYSTEM_API in flags):
flags.append(FLAG_WHITELIST)
elif FLAG_TEST_API in flags:
flags.append(FLAG_GREYLIST)
self._dict[csv[0]].update(flags)
def assign_flag(self, flag, apis, source="<unknown>"):
"""Assigns a flag to given subset of entries.
Args:
flag (string): One of ALL_FLAGS.
apis (set): Subset of APIs to receive the flag.
source (string): Origin of `entries_subset`. Will be printed in error messages.
Throws:
AssertionError if parsed API signatures of flags are invalid.
"""
# Check that all APIs exist in the dict.
self._check_entries_set(apis, source)
# Check that the flag is known.
self._check_flags_set(set([ flag ]), source)
# Iterate over the API subset, find each entry in dict and assign the flag to it.
for api in apis:
self._dict[api].add(flag)
def main(argv):
# Parse arguments.
args = vars(get_args())
# Initialize API->flags dictionary.
flags = FlagsDict()
# Merge input CSV files into the dictionary.
# Do this first because CSV files produced by parsing API stubs will
# contain the full set of APIs. Subsequent additions from text files
# will be able to detect invalid entries, and/or filter all as-yet
# unassigned entries.
for filename in args["csv"]:
flags.parse_and_merge_csv(read_lines(filename), filename)
# Combine inputs which do not require any particular order.
# (1) Assign serialization API to whitelist.
flags.assign_flag(FLAG_WHITELIST, flags.filter_apis(IS_SERIALIZATION))
# (2) Merge text files with a known flag into the dictionary.
for flag in ALL_FLAGS:
for filename in args[flag]:
flags.assign_flag(flag, read_lines(filename), filename)
# Merge text files where conflicts should be ignored.
# This will only assign the given flag if:
# (a) the entry exists, and
# (b) it has not been assigned any other flag.
# Because of (b), this must run after all strict assignments have been performed.
for flag in ALL_FLAGS:
for filename in args[flag + FLAG_IGNORE_CONFLICTS_SUFFIX]:
valid_entries = flags.get_valid_subset_of_unassigned_apis(read_lines(filename))
flags.assign_flag(flag, valid_entries, filename)
# All members in the specified packages will be assigned the appropriate flag.
for flag in ALL_FLAGS:
for filename in args[flag + FLAG_PACKAGES_SUFFIX]:
packages_needing_list = set(read_lines(filename))
should_add_signature_to_list = lambda sig,lists: extract_package(
sig) in packages_needing_list and not lists
valid_entries = flags.filter_apis(should_add_signature_to_list)
flags.assign_flag(flag, valid_entries)
# Assign all remaining entries to the blacklist.
flags.assign_flag(FLAG_BLACKLIST, flags.filter_apis(HAS_NO_API_LIST_ASSIGNED))
# Write output.
write_lines(args["output"], flags.generate_csv())
if __name__ == "__main__":
main(sys.argv)
|