blob: c856cc36d6f69ea114741c2e5a9baf76c2d69202 [file] [log] [blame]
David Brazdil8503b902018-08-30 13:35:03 +01001#!/usr/bin/env python
2#
3# Copyright (C) 2018 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16"""
17Generate API lists for non-SDK API enforcement.
David Brazdil8503b902018-08-30 13:35:03 +010018"""
19import argparse
David Brazdil17d16e82018-12-13 17:00:09 +000020from collections import defaultdict
David Brazdil8503b902018-08-30 13:35:03 +010021import os
22import sys
23import re
Andrei Oneaa6e09b42019-03-29 15:27:55 +000024import functools
David Brazdil8503b902018-08-30 13:35:03 +010025
David Brazdil89bf0f22018-10-30 18:21:24 +000026# Names of flags recognized by the `hiddenapi` tool.
27FLAG_WHITELIST = "whitelist"
28FLAG_GREYLIST = "greylist"
29FLAG_BLACKLIST = "blacklist"
30FLAG_GREYLIST_MAX_O = "greylist-max-o"
David Brazdil5cd148f2018-11-01 09:54:25 +000031FLAG_GREYLIST_MAX_P = "greylist-max-p"
David Brazdil439d3492018-12-07 11:49:55 +000032FLAG_CORE_PLATFORM_API = "core-platform-api"
Andrei Onea80a56602019-03-01 18:49:15 +000033FLAG_PUBLIC_API = "public-api"
34FLAG_SYSTEM_API = "system-api"
35FLAG_TEST_API = "test-api"
David Brazdil89bf0f22018-10-30 18:21:24 +000036
37# List of all known flags.
David Brazdil439d3492018-12-07 11:49:55 +000038FLAGS_API_LIST = [
David Brazdil89bf0f22018-10-30 18:21:24 +000039 FLAG_WHITELIST,
40 FLAG_GREYLIST,
41 FLAG_BLACKLIST,
42 FLAG_GREYLIST_MAX_O,
David Brazdil5cd148f2018-11-01 09:54:25 +000043 FLAG_GREYLIST_MAX_P,
David Brazdil89bf0f22018-10-30 18:21:24 +000044]
Andrei Onea80a56602019-03-01 18:49:15 +000045ALL_FLAGS = FLAGS_API_LIST + [
46 FLAG_CORE_PLATFORM_API,
47 FLAG_PUBLIC_API,
48 FLAG_SYSTEM_API,
49 FLAG_TEST_API,
50 ]
David Brazdil439d3492018-12-07 11:49:55 +000051
52FLAGS_API_LIST_SET = set(FLAGS_API_LIST)
53ALL_FLAGS_SET = set(ALL_FLAGS)
David Brazdil89bf0f22018-10-30 18:21:24 +000054
55# Suffix used in command line args to express that only known and
56# otherwise unassigned entries should be assign the given flag.
57# For example, the P dark greylist is checked in as it was in P,
58# but signatures have changes since then. The flag instructs this
59# script to skip any entries which do not exist any more.
60FLAG_IGNORE_CONFLICTS_SUFFIX = "-ignore-conflicts"
61
Andrei Oneaa6e09b42019-03-29 15:27:55 +000062# Suffix used in command line args to express that all apis within a given set
63# of packages should be assign the given flag.
64FLAG_PACKAGES_SUFFIX = "-packages"
65
David Brazdil89bf0f22018-10-30 18:21:24 +000066# Regex patterns of fields/methods used in serialization. These are
67# considered public API despite being hidden.
68SERIALIZATION_PATTERNS = [
69 r'readObject\(Ljava/io/ObjectInputStream;\)V',
70 r'readObjectNoData\(\)V',
71 r'readResolve\(\)Ljava/lang/Object;',
72 r'serialVersionUID:J',
73 r'serialPersistentFields:\[Ljava/io/ObjectStreamField;',
74 r'writeObject\(Ljava/io/ObjectOutputStream;\)V',
75 r'writeReplace\(\)Ljava/lang/Object;',
76]
77
78# Single regex used to match serialization API. It combines all the
79# SERIALIZATION_PATTERNS into a single regular expression.
80SERIALIZATION_REGEX = re.compile(r'.*->(' + '|'.join(SERIALIZATION_PATTERNS) + r')$')
81
82# Predicates to be used with filter_apis.
David Brazdil439d3492018-12-07 11:49:55 +000083HAS_NO_API_LIST_ASSIGNED = lambda api, flags: not FLAGS_API_LIST_SET.intersection(flags)
David Brazdil89bf0f22018-10-30 18:21:24 +000084IS_SERIALIZATION = lambda api, flags: SERIALIZATION_REGEX.match(api)
85
David Brazdil8503b902018-08-30 13:35:03 +010086def get_args():
87 """Parses command line arguments.
88
89 Returns:
90 Namespace: dictionary of parsed arguments
91 """
92 parser = argparse.ArgumentParser()
David Brazdil89bf0f22018-10-30 18:21:24 +000093 parser.add_argument('--output', required=True)
David Brazdil89bf0f22018-10-30 18:21:24 +000094 parser.add_argument('--csv', nargs='*', default=[], metavar='CSV_FILE',
95 help='CSV files to be merged into output')
96
David Brazdil439d3492018-12-07 11:49:55 +000097 for flag in ALL_FLAGS:
David Brazdil89bf0f22018-10-30 18:21:24 +000098 ignore_conflicts_flag = flag + FLAG_IGNORE_CONFLICTS_SUFFIX
Andrei Oneaa6e09b42019-03-29 15:27:55 +000099 packages_flag = flag + FLAG_PACKAGES_SUFFIX
David Brazdil89bf0f22018-10-30 18:21:24 +0000100 parser.add_argument('--' + flag, dest=flag, nargs='*', default=[], metavar='TXT_FILE',
101 help='lists of entries with flag "' + flag + '"')
102 parser.add_argument('--' + ignore_conflicts_flag, dest=ignore_conflicts_flag, nargs='*',
103 default=[], metavar='TXT_FILE',
104 help='lists of entries with flag "' + flag +
105 '". skip entry if missing or flag conflict.')
Andrei Oneaa6e09b42019-03-29 15:27:55 +0000106 parser.add_argument('--' + packages_flag, dest=packages_flag, nargs='*',
107 default=[], metavar='TXT_FILE',
108 help='lists of packages to be added to ' + flag + ' list')
David Brazdil89bf0f22018-10-30 18:21:24 +0000109
David Brazdil8503b902018-08-30 13:35:03 +0100110 return parser.parse_args()
111
112def read_lines(filename):
113 """Reads entire file and return it as a list of lines.
114
David Brazdilae88d4e2018-09-06 14:46:55 +0100115 Lines which begin with a hash are ignored.
116
David Brazdil8503b902018-08-30 13:35:03 +0100117 Args:
118 filename (string): Path to the file to read from.
119
120 Returns:
David Brazdil89bf0f22018-10-30 18:21:24 +0000121 Lines of the file as a list of string.
David Brazdil8503b902018-08-30 13:35:03 +0100122 """
123 with open(filename, 'r') as f:
David Brazdil89bf0f22018-10-30 18:21:24 +0000124 lines = f.readlines();
125 lines = filter(lambda line: not line.startswith('#'), lines)
126 lines = map(lambda line: line.strip(), lines)
127 return set(lines)
David Brazdil8503b902018-08-30 13:35:03 +0100128
129def write_lines(filename, lines):
130 """Writes list of lines into a file, overwriting the file it it exists.
131
132 Args:
133 filename (string): Path to the file to be writting into.
134 lines (list): List of strings to write into the file.
135 """
David Brazdil89bf0f22018-10-30 18:21:24 +0000136 lines = map(lambda line: line + '\n', lines)
David Brazdil8503b902018-08-30 13:35:03 +0100137 with open(filename, 'w') as f:
138 f.writelines(lines)
139
Andrei Oneaa6e09b42019-03-29 15:27:55 +0000140def extract_package(signature):
141 """Extracts the package from a signature.
142
143 Args:
144 signature (string): JNI signature of a method or field.
145
146 Returns:
147 The package name of the class containing the field/method.
148 """
149 full_class_name = signature.split(";->")[0]
150 package_name = full_class_name[1:full_class_name.rindex("/")]
151 return package_name.replace('/', '.')
152
David Brazdil89bf0f22018-10-30 18:21:24 +0000153class FlagsDict:
David Brazdil17d16e82018-12-13 17:00:09 +0000154 def __init__(self):
155 self._dict_keyset = set()
156 self._dict = defaultdict(set)
David Brazdil4a55eeb2018-09-11 11:09:01 +0100157
David Brazdil89bf0f22018-10-30 18:21:24 +0000158 def _check_entries_set(self, keys_subset, source):
159 assert isinstance(keys_subset, set)
160 assert keys_subset.issubset(self._dict_keyset), (
161 "Error processing: {}\n"
162 "The following entries were unexpected:\n"
163 "{}"
164 "Please visit go/hiddenapi for more information.").format(
165 source, "".join(map(lambda x: " " + str(x), keys_subset - self._dict_keyset)))
David Brazdil4a55eeb2018-09-11 11:09:01 +0100166
David Brazdil89bf0f22018-10-30 18:21:24 +0000167 def _check_flags_set(self, flags_subset, source):
168 assert isinstance(flags_subset, set)
David Brazdil439d3492018-12-07 11:49:55 +0000169 assert flags_subset.issubset(ALL_FLAGS_SET), (
David Brazdil89bf0f22018-10-30 18:21:24 +0000170 "Error processing: {}\n"
171 "The following flags were not recognized: \n"
172 "{}\n"
173 "Please visit go/hiddenapi for more information.").format(
David Brazdil439d3492018-12-07 11:49:55 +0000174 source, "\n".join(flags_subset - ALL_FLAGS_SET))
David Brazdil4a55eeb2018-09-11 11:09:01 +0100175
David Brazdil89bf0f22018-10-30 18:21:24 +0000176 def filter_apis(self, filter_fn):
177 """Returns APIs which match a given predicate.
David Brazdil4a55eeb2018-09-11 11:09:01 +0100178
David Brazdil89bf0f22018-10-30 18:21:24 +0000179 This is a helper function which allows to filter on both signatures (keys) and
180 flags (values). The built-in filter() invokes the lambda only with dict's keys.
David Brazdil4a55eeb2018-09-11 11:09:01 +0100181
David Brazdil89bf0f22018-10-30 18:21:24 +0000182 Args:
183 filter_fn : Function which takes two arguments (signature/flags) and returns a boolean.
David Brazdil4a55eeb2018-09-11 11:09:01 +0100184
David Brazdil89bf0f22018-10-30 18:21:24 +0000185 Returns:
186 A set of APIs which match the predicate.
187 """
188 return set(filter(lambda x: filter_fn(x, self._dict[x]), self._dict_keyset))
David Brazdil4a55eeb2018-09-11 11:09:01 +0100189
David Brazdil89bf0f22018-10-30 18:21:24 +0000190 def get_valid_subset_of_unassigned_apis(self, api_subset):
191 """Sanitizes a key set input to only include keys which exist in the dictionary
David Brazdil439d3492018-12-07 11:49:55 +0000192 and have not been assigned any API list flags.
David Brazdil8503b902018-08-30 13:35:03 +0100193
David Brazdil89bf0f22018-10-30 18:21:24 +0000194 Args:
195 entries_subset (set/list): Key set to be sanitized.
David Brazdil8503b902018-08-30 13:35:03 +0100196
David Brazdil89bf0f22018-10-30 18:21:24 +0000197 Returns:
198 Sanitized key set.
199 """
200 assert isinstance(api_subset, set)
David Brazdil439d3492018-12-07 11:49:55 +0000201 return api_subset.intersection(self.filter_apis(HAS_NO_API_LIST_ASSIGNED))
David Brazdil8503b902018-08-30 13:35:03 +0100202
David Brazdil89bf0f22018-10-30 18:21:24 +0000203 def generate_csv(self):
204 """Constructs CSV entries from a dictionary.
David Brazdil8503b902018-08-30 13:35:03 +0100205
David Brazdil89bf0f22018-10-30 18:21:24 +0000206 Returns:
207 List of lines comprising a CSV file. See "parse_and_merge_csv" for format description.
208 """
209 return sorted(map(lambda api: ",".join([api] + sorted(self._dict[api])), self._dict))
David Brazdil8503b902018-08-30 13:35:03 +0100210
David Brazdil89bf0f22018-10-30 18:21:24 +0000211 def parse_and_merge_csv(self, csv_lines, source = "<unknown>"):
212 """Parses CSV entries and merges them into a given dictionary.
David Brazdil8503b902018-08-30 13:35:03 +0100213
David Brazdil89bf0f22018-10-30 18:21:24 +0000214 The expected CSV format is:
215 <api signature>,<flag1>,<flag2>,...,<flagN>
David Brazdil8503b902018-08-30 13:35:03 +0100216
David Brazdil89bf0f22018-10-30 18:21:24 +0000217 Args:
218 csv_lines (list of strings): Lines read from a CSV file.
219 source (string): Origin of `csv_lines`. Will be printed in error messages.
David Brazdil4a55eeb2018-09-11 11:09:01 +0100220
David Brazdil89bf0f22018-10-30 18:21:24 +0000221 Throws:
David Brazdil17d16e82018-12-13 17:00:09 +0000222 AssertionError if parsed flags are invalid.
David Brazdil89bf0f22018-10-30 18:21:24 +0000223 """
224 # Split CSV lines into arrays of values.
225 csv_values = [ line.split(',') for line in csv_lines ]
226
David Brazdil17d16e82018-12-13 17:00:09 +0000227 # Update the full set of API signatures.
228 self._dict_keyset.update([ csv[0] for csv in csv_values ])
David Brazdil89bf0f22018-10-30 18:21:24 +0000229
230 # Check that all flags are known.
Andrei Oneaa6e09b42019-03-29 15:27:55 +0000231 csv_flags = set(functools.reduce(
232 lambda x, y: set(x).union(y),
233 [ csv[1:] for csv in csv_values ],
234 []))
David Brazdil89bf0f22018-10-30 18:21:24 +0000235 self._check_flags_set(csv_flags, source)
236
237 # Iterate over all CSV lines, find entry in dict and append flags to it.
238 for csv in csv_values:
Andrei Onea80a56602019-03-01 18:49:15 +0000239 flags = csv[1:]
240 if (FLAG_PUBLIC_API in flags) or (FLAG_SYSTEM_API in flags):
241 flags.append(FLAG_WHITELIST)
242 elif FLAG_TEST_API in flags:
243 flags.append(FLAG_GREYLIST)
244 self._dict[csv[0]].update(flags)
David Brazdil89bf0f22018-10-30 18:21:24 +0000245
246 def assign_flag(self, flag, apis, source="<unknown>"):
247 """Assigns a flag to given subset of entries.
248
249 Args:
David Brazdil439d3492018-12-07 11:49:55 +0000250 flag (string): One of ALL_FLAGS.
Andrei Onea80a56602019-03-01 18:49:15 +0000251 apis (set): Subset of APIs to receive the flag.
David Brazdil89bf0f22018-10-30 18:21:24 +0000252 source (string): Origin of `entries_subset`. Will be printed in error messages.
253
254 Throws:
255 AssertionError if parsed API signatures of flags are invalid.
256 """
257 # Check that all APIs exist in the dict.
258 self._check_entries_set(apis, source)
259
260 # Check that the flag is known.
261 self._check_flags_set(set([ flag ]), source)
262
263 # Iterate over the API subset, find each entry in dict and assign the flag to it.
264 for api in apis:
265 self._dict[api].add(flag)
David Brazdil4a55eeb2018-09-11 11:09:01 +0100266
David Brazdil8503b902018-08-30 13:35:03 +0100267def main(argv):
David Brazdil89bf0f22018-10-30 18:21:24 +0000268 # Parse arguments.
269 args = vars(get_args())
David Brazdil8503b902018-08-30 13:35:03 +0100270
David Brazdil17d16e82018-12-13 17:00:09 +0000271 # Initialize API->flags dictionary.
272 flags = FlagsDict()
273
274 # Merge input CSV files into the dictionary.
275 # Do this first because CSV files produced by parsing API stubs will
276 # contain the full set of APIs. Subsequent additions from text files
277 # will be able to detect invalid entries, and/or filter all as-yet
278 # unassigned entries.
279 for filename in args["csv"]:
280 flags.parse_and_merge_csv(read_lines(filename), filename)
David Brazdil8503b902018-08-30 13:35:03 +0100281
David Brazdil89bf0f22018-10-30 18:21:24 +0000282 # Combine inputs which do not require any particular order.
283 # (1) Assign serialization API to whitelist.
284 flags.assign_flag(FLAG_WHITELIST, flags.filter_apis(IS_SERIALIZATION))
David Brazdil8503b902018-08-30 13:35:03 +0100285
David Brazdil17d16e82018-12-13 17:00:09 +0000286 # (2) Merge text files with a known flag into the dictionary.
David Brazdil439d3492018-12-07 11:49:55 +0000287 for flag in ALL_FLAGS:
David Brazdil89bf0f22018-10-30 18:21:24 +0000288 for filename in args[flag]:
289 flags.assign_flag(flag, read_lines(filename), filename)
David Brazdil8503b902018-08-30 13:35:03 +0100290
David Brazdil89bf0f22018-10-30 18:21:24 +0000291 # Merge text files where conflicts should be ignored.
292 # This will only assign the given flag if:
293 # (a) the entry exists, and
294 # (b) it has not been assigned any other flag.
295 # Because of (b), this must run after all strict assignments have been performed.
David Brazdil439d3492018-12-07 11:49:55 +0000296 for flag in ALL_FLAGS:
David Brazdil89bf0f22018-10-30 18:21:24 +0000297 for filename in args[flag + FLAG_IGNORE_CONFLICTS_SUFFIX]:
298 valid_entries = flags.get_valid_subset_of_unassigned_apis(read_lines(filename))
299 flags.assign_flag(flag, valid_entries, filename)
David Brazdil8503b902018-08-30 13:35:03 +0100300
Andrei Oneaa6e09b42019-03-29 15:27:55 +0000301 # All members in the specified packages will be assigned the appropriate flag.
302 for flag in ALL_FLAGS:
303 for filename in args[flag + FLAG_PACKAGES_SUFFIX]:
304 packages_needing_list = set(read_lines(filename))
305 should_add_signature_to_list = lambda sig,lists: extract_package(
306 sig) in packages_needing_list and not lists
307 valid_entries = flags.filter_apis(should_add_signature_to_list)
308 flags.assign_flag(flag, valid_entries)
309
David Brazdil89bf0f22018-10-30 18:21:24 +0000310 # Assign all remaining entries to the blacklist.
David Brazdil439d3492018-12-07 11:49:55 +0000311 flags.assign_flag(FLAG_BLACKLIST, flags.filter_apis(HAS_NO_API_LIST_ASSIGNED))
David Brazdil8503b902018-08-30 13:35:03 +0100312
David Brazdil89bf0f22018-10-30 18:21:24 +0000313 # Write output.
314 write_lines(args["output"], flags.generate_csv())
David Brazdil8503b902018-08-30 13:35:03 +0100315
316if __name__ == "__main__":
317 main(sys.argv)