Merge "Support hidden API whitelist generated by class2greylist"
diff --git a/core/Makefile b/core/Makefile
index f11e3a6..3fd867f 100644
--- a/core/Makefile
+++ b/core/Makefile
@@ -2669,6 +2669,15 @@
 endif
 endif
 
+ifdef INSTALLED_PRODUCT_SERVICESIMAGE_TARGET
+ifdef BOARD_AVB_PRODUCT_SERVICES_KEY_PATH
+$(eval $(call check-and-set-avb-chain-args,PRODUCT_SERVICES))
+else
+INTERNAL_AVB_MAKE_VBMETA_IMAGE_ARGS += \
+    --include_descriptors_from_image $(INSTALLED_PRODUCT_SERVICESIMAGE_TARGET)
+endif
+endif
+
 ifdef INSTALLED_ODMIMAGE_TARGET
 ifdef BOARD_AVB_ODM_KEY_PATH
 $(eval $(call check-and-set-avb-chain-args,ODM))
@@ -2820,32 +2829,36 @@
 # bootloader fastboot should always mark _a slot as bootable.
 ifeq ($(AB_OTA_UPDATER),true)
 $(INSTALLED_SUPERIMAGE_TARGET) $(INSTALLED_SUPERIMAGE_EMPTY_TARGET): PRIVATE_PARTITION_SUFFIX=_a
-$(INSTALLED_SUPERIMAGE_TARGET) $(INSTALLED_SUPERIMAGE_EMPTY_TARGET): PRIVATE_METADATA_SLOTS=2
-else
-$(INSTALLED_SUPERIMAGE_TARGET) $(INSTALLED_SUPERIMAGE_EMPTY_TARGET): PRIVATE_METADATA_SLOTS=1
 endif # AB_OTA_UPDATER
 
-$(INSTALLED_SUPERIMAGE_TARGET): $(HOST_OUT_EXECUTABLES)/lpmake
-	$< \
-		--sparse \
-		--metadata-size 65536 \
-		--metadata-slots $(PRIVATE_METADATA_SLOTS) \
-		--device-size $(BOARD_SUPER_PARTITION_SIZE) \
-		--output $@ \
-		$(foreach name,$(BOARD_SUPER_PARTITION_PARTITION_LIST), \
-			--partition $(name)$(PRIVATE_PARTITION_SUFFIX):$$($(UUIDGEN) $(name)$(PRIVATE_PARTITION_SUFFIX)):readonly:$(call read-size-of-partitions,$(name)) \
-			--image $(name)$(PRIVATE_PARTITION_SUFFIX)=$(call images-for-partitions,$(name)))
+$(INSTALLED_SUPERIMAGE_TARGET) $(INSTALLED_SUPERIMAGE_EMPTY_TARGET): $(HOST_OUT_EXECUTABLES)/lpmake
+
+# $(1): output image path
+# $(2): slot A suffix (_a or empty)
+# $(3): include images or not (true or empty)
+define build-superimage-target
+  $(HOST_OUT_EXECUTABLES)/lpmake \
+    $(if $(3), --sparse) \
+    --metadata-size 65536 \
+    --metadata-slots $(if $(2),2,1) \
+    --device-size $(BOARD_SUPER_PARTITION_SIZE) \
+    --output $(1) \
+    $(foreach name,$(BOARD_SUPER_PARTITION_PARTITION_LIST), \
+      --partition $(name)$(2):$$($(UUIDGEN) $(name)$(2)):readonly:$(if $(3),$(call read-size-of-partitions,$(name)),0) \
+      $(if $(3), --image $(name)$(2)=$(call images-for-partitions,$(name))) \
+      $(if $(2), --partition $(name)_b:$$($(UUIDGEN) $(name)_b):readonly:0) \
+    )
+endef
+
+$(INSTALLED_SUPERIMAGE_TARGET):
+	$(call pretty,"Target super fs image: $@")
+	$(call build-superimage-target,$@,$(PRIVATE_PARTITION_SUFFIX),true)
 
 $(call dist-for-goals,dist_files,$(INSTALLED_SUPERIMAGE_TARGET))
 
-$(INSTALLED_SUPERIMAGE_EMPTY_TARGET): $(HOST_OUT_EXECUTABLES)/lpmake
-	$< \
-		--metadata-size 65536 \
-		--metadata-slots $(PRIVATE_METADATA_SLOTS) \
-		--device-size $(BOARD_SUPER_PARTITION_SIZE) \
-		--output $@ \
-		$(foreach name,$(BOARD_SUPER_PARTITION_PARTITION_LIST), \
-			--partition $(name)$(PRIVATE_PARTITION_SUFFIX):$$($(UUIDGEN) $(name)$(PRIVATE_SUPER_PARTITION_SUFFIX)):readonly:0)
+$(INSTALLED_SUPERIMAGE_EMPTY_TARGET):
+	$(call pretty,"Target empty super fs image: $@")
+	$(call build-superimage-target,$@,$(PRIVATE_PARTITION_SUFFIX))
 
 $(call dist-for-goals,dist_files,$(INSTALLED_SUPERIMAGE_EMPTY_TARGET))
 
diff --git a/core/base_rules.mk b/core/base_rules.mk
index 3a1f396..7d96224 100644
--- a/core/base_rules.mk
+++ b/core/base_rules.mk
@@ -573,6 +573,42 @@
   $(eval my_compat_dist_config_$(suite) := ))
 
 
+# Auto-generate build config.
+ifneq (,$(LOCAL_FULL_TEST_CONFIG))
+  test_config := $(LOCAL_FULL_TEST_CONFIG)
+else ifneq (,$(LOCAL_TEST_CONFIG))
+  test_config := $(LOCAL_PATH)/$(LOCAL_TEST_CONFIG)
+else
+  test_config := $(wildcard $(LOCAL_PATH)/AndroidTest.xml)
+endif
+ifeq (,$(test_config))
+  ifneq (true,$(is_native))
+    is_instrumentation_test := true
+    ifeq (true, $(LOCAL_IS_HOST_MODULE))
+      is_instrumentation_test := false
+    endif
+    # If LOCAL_MODULE_CLASS is not APPS, it's certainly not an instrumentation
+    # test. However, some packages for test data also have LOCAL_MODULE_CLASS
+    # set to APPS. These will require flag LOCAL_DISABLE_AUTO_GENERATE_TEST_CONFIG
+    # to disable auto-generating test config file.
+    ifneq (APPS, $(LOCAL_MODULE_CLASS))
+      is_instrumentation_test := false
+    endif
+  endif
+  # CTS modules can be used for test data, so test config files must be
+  # explicitly created using AndroidTest.xml
+  ifeq (,$(filter cts, $(LOCAL_COMPATIBILITY_SUITE)))
+    ifneq (true, $(LOCAL_DISABLE_AUTO_GENERATE_TEST_CONFIG))
+      ifeq (true, $(filter true,$(is_native) $(is_instrumentation_test)))
+        include $(BUILD_SYSTEM)/autogen_test_config.mk
+        test_config := $(autogen_test_config_file)
+        autogen_test_config_file :=
+      endif
+    endif
+  endif
+endif
+is_instrumentation_test :=
+
 # Make sure we only add the files once for multilib modules.
 ifdef $(my_prefix)$(LOCAL_MODULE_CLASS)_$(LOCAL_MODULE)_compat_files
   # Sync the auto_test_config value for multilib modules.
@@ -589,41 +625,6 @@
       $(eval n := $(or $(word 2,$(p)),$(notdir $(word 1, $(p))))) \
       $(foreach dir, $(call compatibility_suite_dirs,$(suite)), \
         $(s):$(dir)/$(n)))))
-  ifneq (,$(LOCAL_FULL_TEST_CONFIG))
-    test_config := $(LOCAL_FULL_TEST_CONFIG)
-  else ifneq (,$(LOCAL_TEST_CONFIG))
-    test_config := $(LOCAL_PATH)/$(LOCAL_TEST_CONFIG)
-  else
-    test_config := $(wildcard $(LOCAL_PATH)/AndroidTest.xml)
-  endif
-  ifeq (,$(test_config))
-    ifneq (true,$(is_native))
-      is_instrumentation_test := true
-      ifeq (true, $(LOCAL_IS_HOST_MODULE))
-        is_instrumentation_test := false
-      endif
-      # If LOCAL_MODULE_CLASS is not APPS, it's certainly not an instrumentation
-      # test. However, some packages for test data also have LOCAL_MODULE_CLASS
-      # set to APPS. These will require flag LOCAL_DISABLE_AUTO_GENERATE_TEST_CONFIG
-      # to disable auto-generating test config file.
-      ifneq (APPS, $(LOCAL_MODULE_CLASS))
-        is_instrumentation_test := false
-      endif
-    endif
-    # CTS modules can be used for test data, so test config files must be
-    # explicitly created using AndroidTest.xml
-    ifeq (,$(filter cts, $(LOCAL_COMPATIBILITY_SUITE)))
-      ifneq (true, $(LOCAL_DISABLE_AUTO_GENERATE_TEST_CONFIG))
-        ifeq (true, $(filter true,$(is_native) $(is_instrumentation_test)))
-          include $(BUILD_SYSTEM)/autogen_test_config.mk
-          test_config := $(autogen_test_config_file)
-          autogen_test_config_file :=
-        endif
-      endif
-    endif
-  endif
-
-  is_instrumentation_test :=
 
   ifneq (,$(test_config))
     $(foreach suite, $(LOCAL_COMPATIBILITY_SUITE), \
@@ -631,8 +632,6 @@
         $(test_config):$(dir)/$(LOCAL_MODULE).config)))
   endif
 
-  test_config :=
-
   ifneq (,$(wildcard $(LOCAL_PATH)/DynamicConfig.xml))
     $(foreach suite, $(LOCAL_COMPATIBILITY_SUITE), \
       $(eval my_compat_dist_config_$(suite) += $(foreach dir, $(call compatibility_suite_dirs,$(suite)), \
@@ -647,19 +646,17 @@
   endif
 endif # $(my_prefix)$(LOCAL_MODULE_CLASS)_$(LOCAL_MODULE)_compat_files
 
-# HACK: pretend a soong LOCAL_FULL_TEST_CONFIG is autogenerated by copying it to
-# the location autogenerated test configs use and setting the flag in
+# HACK: pretend a soong LOCAL_FULL_TEST_CONFIG is autogenerated by setting the flag in
 # module-info.json
-ifdef LOCAL_FULL_TEST_CONFIG
+# TODO: (b/113029686) Add explicit flag from Soong to determine if a test was
+# autogenerated.
+ifneq (,$(filter $(SOONG_OUT_DIR)%,$(LOCAL_FULL_TEST_CONFIG)))
   ifeq ($(LOCAL_MODULE_MAKEFILE),$(SOONG_ANDROID_MK))
-    my_test_config_file := $(dir $(LOCAL_BUILT_MODULE))$(LOCAL_MODULE).config
-    $(eval $(call copy-one-file,$(LOCAL_FULL_TEST_CONFIG),$(my_test_config_file)))
-    $(call add-dependency,$(LOCAL_BUILT_MODULE),$(my_test_config_file))
     ALL_MODULES.$(my_register_name).auto_test_config := true
-    my_test_config_file :=
   endif
 endif
 
+
 ifneq ($(my_test_data_file_pairs),)
 $(foreach pair, $(my_test_data_file_pairs), \
   $(eval parts := $(subst :,$(space),$(pair))) \
@@ -771,6 +768,8 @@
 ALL_MODULES.$(my_register_name).FOR_HOST_CROSS := $(my_host_cross)
 ALL_MODULES.$(my_register_name).MODULE_NAME := $(LOCAL_MODULE)
 ALL_MODULES.$(my_register_name).COMPATIBILITY_SUITES := $(LOCAL_COMPATIBILITY_SUITE)
+ALL_MODULES.$(my_register_name).TEST_CONFIG := $(test_config)
+test_config :=
 
 INSTALLABLE_FILES.$(LOCAL_INSTALLED_MODULE).MODULE := $(my_register_name)
 
diff --git a/core/clang/tidy.mk b/core/clang/tidy.mk
index 868f7bc..36c6931 100644
--- a/core/clang/tidy.mk
+++ b/core/clang/tidy.mk
@@ -14,6 +14,19 @@
 # limitations under the License.
 #
 
+# clang-tidy doesn't recognize every flag that clang does. This is unlikely to
+# be a complete list, but we can populate this with the ones we know to avoid
+# issues with clang-diagnostic-unused-command-line-argument.
+# b/111885396: -flto affected header include directory;
+# -fsanitize and -fwhole-program-vtables need -flto.
+CLANG_TIDY_UNKNOWN_CFLAGS := \
+  -Wa,% \
+  -flto \
+  -flto=% \
+  -fsanitize=% \
+  -fsanitize-% \
+  -fwhole-program-vtables \
+
 # Returns 2nd word of $(1) if $(2) has prefix of the 1st word of $(1).
 define find_default_local_tidy_check2
 $(if $(filter $(word 1,$(1))%,$(2)/),$(word 2,$(1)))
diff --git a/core/definitions.mk b/core/definitions.mk
index a78c1a4..80a6312 100644
--- a/core/definitions.mk
+++ b/core/definitions.mk
@@ -105,7 +105,6 @@
 # All tests that should be skipped in presubmit check.
 ALL_DISABLED_PRESUBMIT_TESTS :=
 
-# CLANG_TIDY_UNKNOWN_CFLAGS is generated by build/soong.
 sanitize_tidy_cflags = $(filter-out $(CLANG_TIDY_UNKNOWN_CFLAGS),$1)
 
 ###########################################################
@@ -2921,12 +2920,14 @@
 ###########################################################
 define transform-jar-to-dex-r8
 @echo R8: $@
+$(hide) rm -f $(PRIVATE_PROGUARD_DICTIONARY)
 $(hide) $(R8_COMPAT_PROGUARD) -injars '$<' \
     --min-api $(PRIVATE_MIN_SDK_VERSION) \
     --force-proguard-compatibility --output $(subst classes.dex,,$@) \
     $(PRIVATE_PROGUARD_FLAGS) \
     $(addprefix -injars , $(PRIVATE_EXTRA_INPUT_JAR)) \
     $(PRIVATE_DX_FLAGS)
+$(hide) touch $(PRIVATE_PROGUARD_DICTIONARY)
 endef
 
 ###########################################################
diff --git a/core/dex_preopt.mk b/core/dex_preopt.mk
index a7caac1..8299988 100644
--- a/core/dex_preopt.mk
+++ b/core/dex_preopt.mk
@@ -21,7 +21,13 @@
 
 # The default filter for which files go into the system_other image (if it is
 # being used). To bundle everything one should set this to '%'
-SYSTEM_OTHER_ODEX_FILTER ?= app/% priv-app/% product/app/% product/priv-app/%
+SYSTEM_OTHER_ODEX_FILTER ?= \
+    app/% \
+    priv-app/% \
+    product_services/app/% \
+    product_services/priv-app/% \
+    product/app/% \
+    product/priv-app/% \
 
 # Method returning whether the install path $(1) should be for system_other.
 # Under SANITIZE_LITE, we do not want system_other. Just put things under /data/asan.
diff --git a/core/envsetup.mk b/core/envsetup.mk
index a748f91..39b9552 100644
--- a/core/envsetup.mk
+++ b/core/envsetup.mk
@@ -370,7 +370,7 @@
 else ifeq ($(filter product_services system/product_services,$(TARGET_COPY_OUT_PRODUCT_SERVICES)),)
 $(error TARGET_COPY_OUT_PRODUCT_SERVICES must be either 'product_services' or 'system/product_services', seeing '$(TARGET_COPY_OUT_PRODUCT_SERVICES)'.)
 endif
-PRODUCT_SERVICES_COPY_FILES := $(subst $(_product_services_path_placeholder),$(TARGET_COPY_OUT_PRODUCT_SERVICES),$(PRODUCT_SERVICES_COPY_FILES))
+PRODUCT_COPY_FILES := $(subst $(_product_services_path_placeholder),$(TARGET_COPY_OUT_PRODUCT_SERVICES),$(PRODUCT_COPY_FILES))
 
 BOARD_USES_PRODUCT_SERVICESIMAGE :=
 ifdef BOARD_PREBUILT_PRODUCT_SERVICESIMAGE
diff --git a/core/java.mk b/core/java.mk
index 4a4efb4..e5f1f4a 100644
--- a/core/java.mk
+++ b/core/java.mk
@@ -489,6 +489,7 @@
 ifdef LOCAL_PROGUARD_ENABLED
   $(built_dex_intermediate): PRIVATE_EXTRA_INPUT_JAR := $(extra_input_jar)
   $(built_dex_intermediate): PRIVATE_PROGUARD_FLAGS := $(legacy_proguard_flags) $(common_proguard_flags) $(LOCAL_PROGUARD_FLAGS)
+  $(built_dex_intermediate): PRIVATE_PROGUARD_DICTIONARY := $(proguard_dictionary)
   $(built_dex_intermediate) : $(full_classes_pre_proguard_jar) $(extra_input_jar) $(my_proguard_sdk_raise) $(common_proguard_flag_files) $(proguard_flag_files) $(legacy_proguard_lib_deps) $(R8_COMPAT_PROGUARD)
 	$(transform-jar-to-dex-r8)
 else # !LOCAL_PROGUARD_ENABLED
diff --git a/core/tasks/module-info.mk b/core/tasks/module-info.mk
index 38149d7..37e4831 100644
--- a/core/tasks/module-info.mk
+++ b/core/tasks/module-info.mk
@@ -14,6 +14,7 @@
 			'"compatibility_suites": [$(foreach w,$(sort $(ALL_MODULES.$(m).COMPATIBILITY_SUITES)),"$(w)", )], ' \
 			'"auto_test_config": [$(ALL_MODULES.$(m).auto_test_config)], ' \
 			'"module_name": ["$(ALL_MODULES.$(m).MODULE_NAME)"], ' \
+			'"test_config": [$(if $(ALL_MODULES.$(m).TEST_CONFIG),"$(ALL_MODULES.$(m).TEST_CONFIG)")], ' \
 			'},\n' \
 	 ) | sed -e 's/, *\]/]/g' -e 's/, *\}/ }/g' -e '$$s/,$$//' >> $@
 	$(hide) echo '}' >> $@
diff --git a/target/product/base_system.mk b/target/product/base_system.mk
index 27f5e18..832e509 100644
--- a/target/product/base_system.mk
+++ b/target/product/base_system.mk
@@ -27,6 +27,7 @@
     android.test.base \
     android.test.mock \
     android.test.runner \
+    apexd \
     applypatch \
     appops \
     app_process \
diff --git a/tools/releasetools/add_img_to_target_files.py b/tools/releasetools/add_img_to_target_files.py
index 0c9b195..e47c038 100755
--- a/tools/releasetools/add_img_to_target_files.py
+++ b/tools/releasetools/add_img_to_target_files.py
@@ -77,8 +77,9 @@
                             'odm')
 # Use a fixed timestamp (01/01/2009 00:00:00 UTC) for files when packaging
 # images. (b/24377993, b/80600931)
-FIXED_FILE_TIMESTAMP = (datetime.datetime(2009, 1, 1, 0, 0, 0, 0, None)
-                        - datetime.datetime.utcfromtimestamp(0)).total_seconds()
+FIXED_FILE_TIMESTAMP = int((
+    datetime.datetime(2009, 1, 1, 0, 0, 0, 0, None) -
+    datetime.datetime.utcfromtimestamp(0)).total_seconds())
 
 
 class OutputFile(object):
@@ -98,6 +99,7 @@
     if self._output_zip:
       common.ZipWrite(self._output_zip, self.name, self._zip_name)
 
+
 def GetCareMap(which, imgname):
   """Returns the care_map string for the given partition.
 
diff --git a/tools/releasetools/ota_from_target_files.py b/tools/releasetools/ota_from_target_files.py
index 47e61da..9ad3f6b 100755
--- a/tools/releasetools/ota_from_target_files.py
+++ b/tools/releasetools/ota_from_target_files.py
@@ -1096,7 +1096,9 @@
     def ComputeEntryOffsetSize(name):
       """Computes the zip entry offset and size."""
       info = zip_file.getinfo(name)
-      offset = info.header_offset + len(info.FileHeader())
+      offset = info.header_offset
+      offset += zipfile.sizeFileHeader
+      offset += len(info.extra) + len(info.filename)
       size = info.file_size
       return '%s:%d:%d' % (os.path.basename(name), offset, size)
 
@@ -1220,7 +1222,9 @@
     payload, till the end of 'medatada_signature_message'.
     """
     payload_info = input_zip.getinfo('payload.bin')
-    payload_offset = payload_info.header_offset + len(payload_info.FileHeader())
+    payload_offset = payload_info.header_offset
+    payload_offset += zipfile.sizeFileHeader
+    payload_offset += len(payload_info.extra) + len(payload_info.filename)
     payload_size = payload_info.file_size
 
     with input_zip.open('payload.bin', 'r') as payload_fp:
diff --git a/tools/releasetools/sign_target_files_apks.py b/tools/releasetools/sign_target_files_apks.py
index 5c6c8e1..cb0c268 100755
--- a/tools/releasetools/sign_target_files_apks.py
+++ b/tools/releasetools/sign_target_files_apks.py
@@ -597,8 +597,8 @@
   if p.returncode != 0:
     raise common.ExternalError("failed to run dumpkeys")
 
-  # system_root_image puts the recovery keys at BOOT/RAMDISK.
-  if misc_info.get("system_root_image") == "true":
+  if (misc_info.get("system_root_image") == "true" and
+      misc_info.get("recovery_as_boot") == "true"):
     recovery_keys_location = "BOOT/RAMDISK/res/keys"
   else:
     recovery_keys_location = "RECOVERY/RAMDISK/res/keys"