blob: b0fb850c512d375128a46b2643037cc7b5bf342a [file] [log] [blame]
Tri Vo438684b2018-09-29 17:47:10 -07001# Copyright 2018 - The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Tool to combine SEPolicy mapping file.
16
17Say, x, y, z are platform SEPolicy versions such that x > y > z. Then given two
18mapping files from x to y (top) and y to z (bottom), it's possible to construct
19a mapping file from x to z. We do the following to combine two maps.
201. Add all new types declarations from top to bottom.
Tri Vo7bfd7302019-08-20 15:41:53 -0700212. Add all new typeattribute declarations from top to bottom.
223. Say, a new type "bar" in top is mapped like this "foo_V_v<-bar", then we map
Tri Vo438684b2018-09-29 17:47:10 -070023"bar" to whatever "foo" is mapped to in the bottom map. We do this for all new
24types in the top map.
25
26More generally, we can correctly construct x->z from x->y' and y"->z as long as
27y">y'.
28
29This file contains the implementation of combining two mapping files.
30"""
31import argparse
32import re
33from mini_parser import MiniCilParser
34
35def Combine(top, bottom):
36 bottom.types.update(top.types)
Tri Vo7bfd7302019-08-20 15:41:53 -070037 bottom.typeattributes.update(top.typeattributes)
Tri Vo438684b2018-09-29 17:47:10 -070038
39 for top_ta in top.typeattributesets:
40 top_type_set = top.typeattributesets[top_ta]
41 if len(top_type_set) == 1:
42 continue
43
Inseob Kimb30e2f02024-03-29 11:09:48 +090044 m = re.fullmatch(r"(\w+?)_\d+(_0)?", top_ta)
45 # Typeattributes in V(.0).cil have _V(_0) suffix, but not in
46 # V(.0).ignore.cil
Tri Vo438684b2018-09-29 17:47:10 -070047 bottom_type = m.group(1) if m else top_ta
48
Tri Vo8c31ddf2020-01-08 08:53:19 -080049 # If type doesn't exist in bottom map, no need to maintain mappings to
50 # that type.
51 if bottom_type not in bottom.rTypeattributesets.keys():
52 continue
53
Tri Vo438684b2018-09-29 17:47:10 -070054 for bottom_ta in bottom.rTypeattributesets[bottom_type]:
55 bottom.typeattributesets[bottom_ta].update(top_type_set)
56
57 return bottom
58
59if __name__ == "__main__":
60 parser = argparse.ArgumentParser()
61 parser.add_argument("-t", "--top-map", dest="top_map",
62 required=True, help="top map file")
63 parser.add_argument("-b", "--bottom-map", dest="bottom_map",
64 required=True, help="bottom map file")
65 parser.add_argument("-o", "--output-file", dest="output_file",
66 required=True, help="output map file")
67 args = parser.parse_args()
68
69 top_map_cil = MiniCilParser(args.top_map)
70 bottom_map_cil = MiniCilParser(args.bottom_map)
71 result = Combine(top_map_cil, bottom_map_cil)
72
73 with open(args.output_file, "w") as output:
74 output.write(result.unparse())