Determine GC type based on BUILT_KERNEL_VERSION_FILE.

How it works:
1. build/make/core/Makefile generates a txt file with the kernel
   version, which is taken from an explicit BOARD_KERNEL_VERSION value,
   or extracted from the kernel image on the source tree, or extracted
   from the kernel image extracted from the prebuilt boot.img.
   The file is saved at
   $ANDROID_PRODUCT_OUT/obj/PACKAGING/check_vintf_all_intermediates/kernel_version.txt.
2. If PRODUCT_ENABLE_UFFD_GC is "default", meaning the GC type needs to
   be determined by the kernel version, build/make/core/Makefile copies
   kernel_version.txt to
   out/soong/dexpreopt/kernel_version_for_uffd_gc.txt.
3. build/soong/dexpreopt/config.go writes the the UFFD GC flag to
   out/soong/dexpreopt/uffd_gc_flag.txt. The flag is determined by an
   explicit PRODUCT_ENABLE_UFFD_GC value or by contruct_uffd_gc_flag.py,
   which reads kernel_version_for_uffd_gc.txt and determines the flag
   accordingly.
4. dex2oat takes the UFFD GC flag from uffd_gc_flag.txt.
5. post_process_props.py mangles ro.dalvik.vm.enable_uffd_gc based on
   the same logic.

Bug: 321751629
Bug: 319554951
Bug: 318763448
Bug: 319648491
Test: m --no-skip-soong-tests nothing
Test: atest uffd_gc_utils_test
Test: Build with `OVERRIDE_ENABLE_UFFD_GC=default m` for device with no
  UFFD support -
  1. Check the existence of `-Xgc:CMC` in
     out/soong/dexpreopt_arm64/dex_bootjars/android/system/framework/arm64/boot.invocation
     (dex2oat invocation for a boot image)
  2. Check the existence of `-Xgc:CMC` in
     out/soong/.intermediates/packages/apps/Settings/Settings/android_common/dexpreopt/Settings/oat/arm64/package.invocation
     (dex2oat invocation for an app defined in .bp)
  3. Check the existence of `-Xgc:CMC` in
     $ANDROID_PRODUCT_OUT/obj/APPS/Dialer_intermediates/oat/arm64/package.invocation
     (dex2oat invocation for an app defined in .mk)
  4. Check the value of ro.dalvik.vm.enable_uffd_gc in
     $ANDROID_PRODUCT_OUT/product/etc/build.prop
Test: Build with `OVERRIDE_ENABLE_UFFD_GC=default m` for device with
  UFFD support, and do the steps above.
Test: Build with `OVERRIDE_ENABLE_UFFD_GC=true m`, and do the steps
  above.
Test: Build with `OVERRIDE_ENABLE_UFFD_GC=false m`, and do the steps
  above.

Change-Id: I8df6e5be1644da05d2d5c57b3520f29601dfd7a4
diff --git a/core/Makefile b/core/Makefile
index b3870e5..94fc88e 100644
--- a/core/Makefile
+++ b/core/Makefile
@@ -287,6 +287,11 @@
 endif
 endif
 
+# Do this early because sysprop.mk depends on BUILT_KERNEL_VERSION_FILE_FOR_UFFD_GC.
+ifeq (default,$(ENABLE_UFFD_GC))
+BUILT_KERNEL_VERSION_FILE_FOR_UFFD_GC := $(OUT_DIR)/soong/dexpreopt/kernel_version_for_uffd_gc.txt
+endif # ENABLE_UFFD_GC
+
 include $(BUILD_SYSTEM)/sysprop.mk
 
 # ----------------------------------------------------------------
@@ -5256,6 +5261,34 @@
 
 endif # PRODUCT_OTA_ENFORCE_VINTF_KERNEL_REQUIREMENTS
 
+ifeq (default,$(ENABLE_UFFD_GC))
+
+ifneq (,$(BUILT_KERNEL_VERSION_FILE))
+$(BUILT_KERNEL_VERSION_FILE_FOR_UFFD_GC): $(BUILT_KERNEL_VERSION_FILE)
+$(BUILT_KERNEL_VERSION_FILE_FOR_UFFD_GC):
+	cp $(BUILT_KERNEL_VERSION_FILE) $(BUILT_KERNEL_VERSION_FILE_FOR_UFFD_GC)
+else
+# We make this a warning rather than an error to avoid breaking too many builds. When it happens,
+# we use a placeholder as the kernel version, which is consumed by uffd_gc_utils.py.
+$(BUILT_KERNEL_VERSION_FILE_FOR_UFFD_GC):
+	echo $$'\
+Unable to determine UFFD GC flag because the kernel version is not available and\n\
+PRODUCT_ENABLE_UFFD_GC is "default".\n\
+You can fix this by:\n\
+  1. [Recommended] Making the kernel version available.\n\
+    (1). Set PRODUCT_OTA_ENFORCE_VINTF_KERNEL_REQUIREMENTS to "true".\n\
+    (2). If you are still getting this message after doing so, see the warning about\n\
+         PRODUCT_OTA_ENFORCE_VINTF_KERNEL_REQUIREMENTS in the build logs.\n\
+  or\n\
+  2. Explicitly setting PRODUCT_ENABLE_UFFD_GC to "true" or "false" based on the kernel version.\n\
+    (1). Set PRODUCT_ENABLE_UFFD_GC to "true" if the kernel is a GKI kernel and is android12-5.4\n\
+         or above, or a non-GKI kernel that supports userfaultfd(2) and MREMAP_DONTUNMAP.\n\
+    (2). Set PRODUCT_ENABLE_UFFD_GC to "false" otherwise.'\
+	  && echo '<unknown-kernel>' > $@
+endif # BUILT_KERNEL_VERSION_FILE
+
+endif # ENABLE_UFFD_GC == "default"
+
 # -- Check VINTF compatibility of build.
 # Skip partial builds; only check full builds. Only check if:
 # - PRODUCT_ENFORCE_VINTF_MANIFEST is true
diff --git a/core/art_config.mk b/core/art_config.mk
index f47a8e2..54bfd6b 100644
--- a/core/art_config.mk
+++ b/core/art_config.mk
@@ -12,38 +12,15 @@
 # ENABLE_UFFD_GC: Whether to use userfaultfd GC.
 
 config_enable_uffd_gc := \
-  $(firstword $(OVERRIDE_ENABLE_UFFD_GC) $(PRODUCT_ENABLE_UFFD_GC))
+  $(firstword $(OVERRIDE_ENABLE_UFFD_GC) $(PRODUCT_ENABLE_UFFD_GC) default)
 
-ifeq (,$(filter-out default,$(config_enable_uffd_gc)))
-  ENABLE_UFFD_GC := true
-
-  # Disable userfaultfd GC if the device doesn't support it (i.e., if
-  # `min(ro.board.api_level ?? ro.board.first_api_level ?? MAX_VALUE,
-  #      ro.product.first_api_level ?? ro.build.version.sdk ?? MAX_VALUE) < 31`)
-  # This logic aligns with how `ro.vendor.api_level` is calculated in
-  # `system/core/init/property_service.cpp`.
-  # We omit the check on `ro.build.version.sdk` here because we are on the latest build system.
-  board_api_level := $(firstword $(BOARD_API_LEVEL) $(BOARD_SHIPPING_API_LEVEL))
-  ifneq (,$(board_api_level))
-    ifeq (true,$(call math_lt,$(board_api_level),31))
-      ENABLE_UFFD_GC := false
-    endif
-  endif
-
-  ifneq (,$(PRODUCT_SHIPPING_API_LEVEL))
-    ifeq (true,$(call math_lt,$(PRODUCT_SHIPPING_API_LEVEL),31))
-      ENABLE_UFFD_GC := false
-    endif
-  endif
-else ifeq (true,$(config_enable_uffd_gc))
-  ENABLE_UFFD_GC := true
-else ifeq (false,$(config_enable_uffd_gc))
-  ENABLE_UFFD_GC := false
-else
+ifeq (,$(filter default true false,$(config_enable_uffd_gc)))
   $(error Unknown PRODUCT_ENABLE_UFFD_GC value: $(config_enable_uffd_gc))
 endif
 
-ADDITIONAL_PRODUCT_PROPERTIES += ro.dalvik.vm.enable_uffd_gc=$(ENABLE_UFFD_GC)
+ENABLE_UFFD_GC := $(config_enable_uffd_gc)
+# If the value is "default", it will be mangled by post_process_props.py.
+ADDITIONAL_PRODUCT_PROPERTIES += ro.dalvik.vm.enable_uffd_gc=$(config_enable_uffd_gc)
 
 # Create APEX_BOOT_JARS_EXCLUDED which is a list of jars to be removed from
 # ApexBoorJars when built from mainline prebuilts.
diff --git a/core/dex_preopt.mk b/core/dex_preopt.mk
index 37a389f..08311ca 100644
--- a/core/dex_preopt.mk
+++ b/core/dex_preopt.mk
@@ -57,6 +57,7 @@
 # Build the boot.zip which contains the boot jars and their compilation output
 # We can do this only if preopt is enabled and if the product uses libart config (which sets the
 # default properties for preopting).
+# At the time of writing, this is only for ART Cloud.
 ifeq ($(WITH_DEXPREOPT), true)
 ifneq ($(WITH_DEXPREOPT_ART_BOOT_IMG_ONLY), true)
 ifeq ($(PRODUCT_USES_DEFAULT_ART_CONFIG), true)
@@ -95,15 +96,16 @@
 bootclasspath_locations_arg := $(subst $(space),:,$(DEXPREOPT_BOOTCLASSPATH_DEX_LOCATIONS))
 boot_images := $(subst :,$(space),$(DEXPREOPT_IMAGE_LOCATIONS_ON_DEVICE$(DEXPREOPT_INFIX)))
 boot_image_arg := $(subst $(space),:,$(patsubst /%,%,$(boot_images)))
-dex2oat_extra_args := $(if $(filter true,$(ENABLE_UFFD_GC)),--runtime-arg -Xgc:CMC)
+uffd_gc_flag_txt := $(OUT_DIR)/soong/dexpreopt/uffd_gc_flag.txt
 
 boot_zip_metadata_txt := $(dir $(boot_zip))boot_zip/METADATA.txt
+$(boot_zip_metadata_txt): $(uffd_gc_flag_txt)
 $(boot_zip_metadata_txt):
 	rm -f $@
 	echo "bootclasspath = $(bootclasspath_arg)" >> $@
 	echo "bootclasspath-locations = $(bootclasspath_locations_arg)" >> $@
 	echo "boot-image = $(boot_image_arg)" >> $@
-	echo "extra-args = $(dex2oat_extra_args)" >> $@
+	echo "extra-args = `cat $(uffd_gc_flag_txt)`" >> $@
 
 $(call dist-for-goals, droidcore, $(boot_zip_metadata_txt))
 
diff --git a/core/dex_preopt_config.mk b/core/dex_preopt_config.mk
index 10fbe8f..d51de33 100644
--- a/core/dex_preopt_config.mk
+++ b/core/dex_preopt_config.mk
@@ -122,7 +122,7 @@
   $(call add_json_str,  Dex2oatXmx,                              $(DEX2OAT_XMX))
   $(call add_json_str,  Dex2oatXms,                              $(DEX2OAT_XMS))
   $(call add_json_str,  EmptyDirectory,                          $(OUT_DIR)/empty)
-  $(call add_json_bool, EnableUffdGc,                            $(filter true,$(ENABLE_UFFD_GC)))
+  $(call add_json_str,  EnableUffdGc,                            $(ENABLE_UFFD_GC))
 
 ifdef TARGET_ARCH
   $(call add_json_map,  CpuVariant)
diff --git a/core/sysprop.mk b/core/sysprop.mk
index 4e8e976..652ca97 100644
--- a/core/sysprop.mk
+++ b/core/sysprop.mk
@@ -124,7 +124,7 @@
     $(eval _option := --allow-dup)\
 )
 
-$(2): $(POST_PROCESS_PROPS) $(INTERNAL_BUILD_ID_MAKEFILE) $(3) $(6)
+$(2): $(POST_PROCESS_PROPS) $(INTERNAL_BUILD_ID_MAKEFILE) $(3) $(6) $(BUILT_KERNEL_VERSION_FILE_FOR_UFFD_GC)
 	$(hide) echo Building $$@
 	$(hide) mkdir -p $$(dir $$@)
 	$(hide) rm -f $$@ && touch $$@
@@ -148,7 +148,10 @@
 	        echo "$$(line)" >> $$@;\
 	    )\
 	)
-	$(hide) $(POST_PROCESS_PROPS) $$(_option) --sdk-version $(PLATFORM_SDK_VERSION) $$@ $(5)
+	$(hide) $(POST_PROCESS_PROPS) $$(_option) \
+	  --sdk-version $(PLATFORM_SDK_VERSION) \
+	  --kernel-version-file-for-uffd-gc "$(BUILT_KERNEL_VERSION_FILE_FOR_UFFD_GC)" \
+	  $$@ $(5)
 	$(hide) $(foreach file,$(strip $(6)),\
 	    if [ -f "$(file)" ]; then\
 	        cat $(file) >> $$@;\
diff --git a/tools/Android.bp b/tools/Android.bp
index 5c54fcf..0a55ed4 100644
--- a/tools/Android.bp
+++ b/tools/Android.bp
@@ -18,56 +18,65 @@
 }
 
 python_binary_host {
-  name: "generate-self-extracting-archive",
-  srcs: ["generate-self-extracting-archive.py"],
+    name: "generate-self-extracting-archive",
+    srcs: ["generate-self-extracting-archive.py"],
 }
 
 python_binary_host {
-  name: "post_process_props",
-  srcs: ["post_process_props.py"],
+    name: "post_process_props",
+    srcs: ["post_process_props.py"],
+    libs: [
+        "uffd_gc_utils",
+    ],
 }
 
 python_test_host {
-  name: "post_process_props_unittest",
-  main: "test_post_process_props.py",
-  srcs: [
-    "post_process_props.py",
-    "test_post_process_props.py",
-  ],
-  test_config: "post_process_props_unittest.xml",
-  test_suites: ["general-tests"],
+    name: "post_process_props_unittest",
+    main: "test_post_process_props.py",
+    srcs: [
+        "post_process_props.py",
+        "test_post_process_props.py",
+    ],
+    libs: [
+        "uffd_gc_utils",
+    ],
+    test_config: "post_process_props_unittest.xml",
+    test_suites: ["general-tests"],
 }
 
 python_binary_host {
-  name: "extract_kernel",
-  srcs: ["extract_kernel.py"],
+    name: "extract_kernel",
+    srcs: ["extract_kernel.py"],
 }
 
 genrule_defaults {
-  name: "extract_kernel_release_defaults",
-  tools: ["extract_kernel", "lz4"],
-  out: ["kernel_release.txt"],
-  cmd: "$(location) --tools lz4:$(location lz4) --input $(in) --output-release > $(out)"
+    name: "extract_kernel_release_defaults",
+    tools: [
+        "extract_kernel",
+        "lz4",
+    ],
+    out: ["kernel_release.txt"],
+    cmd: "$(location) --tools lz4:$(location lz4) --input $(in) --output-release > $(out)",
 }
 
 cc_binary_host {
-  name: "build-runfiles",
-  srcs: ["build-runfiles.cc"],
+    name: "build-runfiles",
+    srcs: ["build-runfiles.cc"],
 }
 
 python_binary_host {
-  name: "check_radio_versions",
-  srcs: ["check_radio_versions.py"],
+    name: "check_radio_versions",
+    srcs: ["check_radio_versions.py"],
 }
 
 python_binary_host {
-  name: "check_elf_file",
-  srcs: ["check_elf_file.py"],
+    name: "check_elf_file",
+    srcs: ["check_elf_file.py"],
 }
 
 python_binary_host {
-  name: "generate_gts_shared_report",
-  srcs: ["generate_gts_shared_report.py"],
+    name: "generate_gts_shared_report",
+    srcs: ["generate_gts_shared_report.py"],
 }
 
 python_binary_host {
@@ -77,10 +86,10 @@
         "list_files.py",
     ],
     version: {
-      py3: {
-        embedded_launcher: true,
-      }
-    }
+        py3: {
+            embedded_launcher: true,
+        },
+    },
 }
 
 python_test_host {
@@ -98,11 +107,11 @@
 }
 
 python_binary_host {
-  name: "characteristics_rro_generator",
-  srcs: ["characteristics_rro_generator.py"],
-  version: {
-    py3: {
-      embedded_launcher: true,
+    name: "characteristics_rro_generator",
+    srcs: ["characteristics_rro_generator.py"],
+    version: {
+        py3: {
+            embedded_launcher: true,
+        },
     },
-  },
 }
diff --git a/tools/post_process_props.py b/tools/post_process_props.py
index 32829c1..6f429fa 100755
--- a/tools/post_process_props.py
+++ b/tools/post_process_props.py
@@ -17,6 +17,8 @@
 import argparse
 import sys
 
+from uffd_gc_utils import should_enable_uffd_gc
+
 # Usage: post_process_props.py file.prop [disallowed_key, ...]
 # Disallowed keys are removed from the property file, if present
 
@@ -27,7 +29,7 @@
 
 # Put the modifications that you need to make into the */build.prop into this
 # function.
-def mangle_build_prop(prop_list):
+def mangle_build_prop(prop_list, kernel_version_file_for_uffd_gc):
   # If ro.debuggable is 1, then enable adb on USB by default
   # (this is for userdebug builds)
   if prop_list.get_value("ro.debuggable") == "1":
@@ -38,6 +40,11 @@
       else:
         val = val + ",adb"
       prop_list.put("persist.sys.usb.config", val)
+  if prop_list.get_value("ro.dalvik.vm.enable_uffd_gc") == "default":
+    assert kernel_version_file_for_uffd_gc != ""
+    enable_uffd_gc = should_enable_uffd_gc(kernel_version_file_for_uffd_gc)
+    prop_list.put("ro.dalvik.vm.enable_uffd_gc",
+                  "true" if enable_uffd_gc else "false")
 
 def validate_grf_props(prop_list):
   """Validate GRF properties if exist.
@@ -233,6 +240,7 @@
   parser.add_argument("filename")
   parser.add_argument("disallowed_keys", metavar="KEY", type=str, nargs="*")
   parser.add_argument("--sdk-version", type=int, required=True)
+  parser.add_argument("--kernel-version-file-for-uffd-gc", required=True)
   args = parser.parse_args()
 
   if not args.filename.endswith("/build.prop"):
@@ -240,7 +248,7 @@
     sys.exit(1)
 
   props = PropList(args.filename)
-  mangle_build_prop(props)
+  mangle_build_prop(props, args.kernel_version_file_for_uffd_gc)
   if not override_optional_props(props, args.allow_dup):
     sys.exit(1)
   if not validate_grf_props(props):