blob: 8dba5a1d7c3bcaecf9044cf75369f9790298ccc7 [file] [log] [blame]
Jeongik Chaef26afd2019-02-07 15:51:47 +09001#!/usr/bin/env python3
2#
3# Copyright (C) 2019 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#
Felix3769a202020-05-31 01:17:34 +020017import argparse
18import json
Jeongik Chaef26afd2019-02-07 15:51:47 +090019import os
20import subprocess
Jeongik Chaef26afd2019-02-07 15:51:47 +090021import sys
Jeongik Chaef26afd2019-02-07 15:51:47 +090022
Felix3769a202020-05-31 01:17:34 +020023from collections import defaultdict
24from glob import glob
25
26def parse_args():
27 """Parse commandline arguments."""
28 parser = argparse.ArgumentParser(description='Find sharedUserId violators')
29 parser.add_argument('--product_out', help='PRODUCT_OUT directory',
30 default=os.environ.get("PRODUCT_OUT"))
31 parser.add_argument('--aapt', help='Path to aapt or aapt2',
32 default="aapt2")
33 parser.add_argument('--copy_out_system', help='TARGET_COPY_OUT_SYSTEM',
34 default="system")
35 parser.add_argument('--copy_out_vendor', help='TARGET_COPY_OUT_VENDOR',
36 default="vendor")
37 parser.add_argument('--copy_out_product', help='TARGET_COPY_OUT_PRODUCT',
38 default="product")
39 parser.add_argument('--copy_out_system_ext', help='TARGET_COPY_OUT_SYSTEM_EXT',
40 default="system_ext")
41 return parser.parse_args()
Jeongik Chaef26afd2019-02-07 15:51:47 +090042
Jeongik Chab806c442019-02-15 15:21:00 +090043def execute(cmd):
44 p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
45 out, err = map(lambda b: b.decode('utf-8'), p.communicate())
46 return p.returncode == 0, out, err
47
48def make_aapt_cmds(file):
49 return [aapt + ' dump ' + file + ' --file AndroidManifest.xml',
Jeongik Chaef26afd2019-02-07 15:51:47 +090050 aapt + ' dump xmltree ' + file + ' --file AndroidManifest.xml']
Jeongik Chaef26afd2019-02-07 15:51:47 +090051
52def extract_shared_uid(file):
Jeongik Chab806c442019-02-15 15:21:00 +090053 for cmd in make_aapt_cmds(file):
54 success, manifest, error_msg = execute(cmd)
55 if success:
56 break
57 else:
58 print(error_msg, file=sys.stderr)
59 sys.exit()
Jeongik Chab806c442019-02-15 15:21:00 +090060
61 for l in manifest.split('\n'):
Jeongik Chaef26afd2019-02-07 15:51:47 +090062 if "sharedUserId" in l:
63 return l.split('"')[-2]
64 return None
65
66
Felix3769a202020-05-31 01:17:34 +020067args = parse_args()
68
69product_out = args.product_out
70aapt = args.aapt
71
72partitions = (
73 ("system", args.copy_out_system),
74 ("vendor", args.copy_out_vendor),
75 ("product", args.copy_out_product),
76 ("system_ext", args.copy_out_system_ext),
77)
Jeongik Chaef26afd2019-02-07 15:51:47 +090078
79shareduid_app_dict = defaultdict(list)
80
Felix3769a202020-05-31 01:17:34 +020081for part, location in partitions:
82 for f in glob(os.path.join(product_out, location, "*", "*", "*.apk")):
Jeongik Chaef26afd2019-02-07 15:51:47 +090083 apk_file = os.path.basename(f)
84 shared_uid = extract_shared_uid(f)
85
86 if shared_uid is None:
87 continue
Felix3769a202020-05-31 01:17:34 +020088 shareduid_app_dict[shared_uid].append((part, apk_file))
Jeongik Chaef26afd2019-02-07 15:51:47 +090089
90
91output = defaultdict(lambda: defaultdict(list))
92
93for uid, app_infos in shareduid_app_dict.items():
94 partitions = {p for p, _ in app_infos}
95 if len(partitions) > 1:
96 for part in partitions:
97 output[uid][part].extend([a for p, a in app_infos if p == part])
98
99print(json.dumps(output, indent=2, sort_keys=True))