Add device and host only flags for CFI

We previously incorrectly associated the device and host only
sanitizer flags with just ubsan. This change updates that to include
CFI

Bug: 278789968
Test: Unit Tests
Change-Id: Ibfe72e9943071de9375ae8d2f0ef6cbdeeb94612
diff --git a/cc_toolchain_features.bzl b/cc_toolchain_features.bzl
index 9c5a1bf..c46f8e0 100644
--- a/cc_toolchain_features.bzl
+++ b/cc_toolchain_features.bzl
@@ -1766,7 +1766,7 @@
         ),
     ]
 
-def _ubsan_flag_feature(name, actions, flags):
+def _sanitizer_flag_feature(name, actions, flags):
     return feature(
         name = name,
         enabled = True,
@@ -1778,24 +1778,29 @@
                         flags = flags,
                     ),
                 ],
+                # Any new sanitizers that are enabled need to have a
+                # with_feature_set added here.
                 with_features = [
                     with_feature_set(
                         features = ["ubsan_enabled"],
                     ),
+                    with_feature_set(
+                        features = ["android_cfi"],
+                    ),
                 ],
             ),
         ],
     )
 
-def _host_or_device_specific_ubsan_feature(target_os):
+def _host_or_device_specific_sanitizer_feature(target_os):
     if is_os_device(target_os):
-        return _ubsan_flag_feature(
-            "ubsan_device_only_flags",
+        return _sanitizer_flag_feature(
+            "sanitizer_device_only_flags",
             _actions.compile,
             _generated_sanitizer_constants.DeviceOnlySanitizeFlags,
         )
-    return _ubsan_flag_feature(
-        "ubsan_host_only_flags",
+    return _sanitizer_flag_feature(
+        "sanitizer_host_only_flags",
         _actions.compile,
         _generated_sanitizer_constants.HostOnlySanitizeFlags,
     )
@@ -2064,7 +2069,7 @@
     )
 
     ubsan_features += [
-        _host_or_device_specific_ubsan_feature(target_os),
+        _host_or_device_specific_sanitizer_feature(target_os),
         _exclude_ubsan_rt_feature(libclang_rt_ubsan_minimal),
     ]
 
diff --git a/cc_toolchain_features_cfi_test.bzl b/cc_toolchain_features_cfi_test.bzl
index ebaeafe..fb4b99e 100644
--- a/cc_toolchain_features_cfi_test.bzl
+++ b/cc_toolchain_features_cfi_test.bzl
@@ -21,14 +21,27 @@
 )
 load(
     "//build/bazel/rules/test_common:flags.bzl",
+    "action_flags_absent_for_mnemonic_aosp_arm64_host_test",
+    "action_flags_absent_for_mnemonic_aosp_arm64_test",
     "action_flags_absent_for_mnemonic_test",
     "action_flags_present_for_mnemonic_nonexclusive_test",
+    "action_flags_present_only_for_mnemonic_aosp_arm64_host_test",
+    "action_flags_present_only_for_mnemonic_aosp_arm64_test",
     "action_flags_present_only_for_mnemonic_test",
 )
 
 compile_action_mnemonic = "CppCompile"
 link_action_mnemonic = "CppLink"
 
+# Include these different file types to make sure that all actions types are
+# triggered
+test_srcs = [
+    "foo.cpp",
+    "bar.c",
+    "baz.s",
+    "blah.S",
+]
+
 def test_cfi_c_and_cpp_has_correct_flags():
     name = "cfi_c_and_cpp_has_correct_flags"
     native.cc_binary(
@@ -370,6 +383,58 @@
 #
 #    return test_name
 
+def _test_host_only_and_device_only_features():
+    name = "cfi_host_only_and_device_only_features"
+    test_names = []
+
+    native.cc_binary(
+        name = name,
+        srcs = ["foo.c", "bar.cpp"],
+        features = ["android_cfi"],
+        tags = ["manual"],
+    )
+
+    host_with_host_flags_test_name = name + "_host_flags_present_when_host_test"
+    test_names += [host_with_host_flags_test_name]
+    action_flags_present_only_for_mnemonic_aosp_arm64_host_test(
+        name = host_with_host_flags_test_name,
+        target_under_test = name,
+        mnemonics = [compile_action_mnemonic],
+        expected_flags = generated_sanitizer_constants.HostOnlySanitizeFlags,
+    )
+
+    device_with_host_flags_test_name = name + "_host_flags_absent_when_device_test"
+    test_names += [device_with_host_flags_test_name]
+    action_flags_absent_for_mnemonic_aosp_arm64_test(
+        name = device_with_host_flags_test_name,
+        target_under_test = name,
+        mnemonics = [compile_action_mnemonic],
+        expected_absent_flags = generated_sanitizer_constants.HostOnlySanitizeFlags,
+    )
+
+    device_with_device_flags_test_name = name + "_device_flags_present_when_device_test"
+    test_names += [device_with_device_flags_test_name]
+    action_flags_present_only_for_mnemonic_aosp_arm64_test(
+        name = device_with_device_flags_test_name,
+        target_under_test = name,
+        mnemonics = [compile_action_mnemonic],
+        expected_flags = generated_sanitizer_constants.DeviceOnlySanitizeFlags,
+    )
+
+    host_with_device_flags_test_name = name + "_device_flags_absent_when_host_test"
+    test_names += [host_with_device_flags_test_name]
+    action_flags_absent_for_mnemonic_aosp_arm64_host_test(
+        name = host_with_device_flags_test_name,
+        target_under_test = name,
+        mnemonics = [compile_action_mnemonic],
+        expected_absent_flags = generated_sanitizer_constants.DeviceOnlySanitizeFlags,
+    )
+
+    return test_names
+
+def _test_device_only_and_host_only_features_absent_when_cfi_disabled():
+    pass
+
 def cc_toolchain_features_cfi_test_suite(name):
     individual_tests = [
         test_cross_dso_not_added_when_cfi_disabled(),
@@ -390,5 +455,6 @@
         tests = individual_tests +
                 test_cfi_c_and_cpp_has_correct_flags() +
                 test_cfi_s_has_correct_flags() +
-                test_cross_dso_not_added_when_static_binary(),
+                test_cross_dso_not_added_when_static_binary() +
+                _test_host_only_and_device_only_features(),
     )