Prevent Skylark actions from setting arbitrary execution info

PiperOrigin-RevId: 194236287
diff --git a/src/main/java/com/google/devtools/build/lib/packages/TargetUtils.java b/src/main/java/com/google/devtools/build/lib/packages/TargetUtils.java
index 756df75..133cb71 100644
--- a/src/main/java/com/google/devtools/build/lib/packages/TargetUtils.java
+++ b/src/main/java/com/google/devtools/build/lib/packages/TargetUtils.java
@@ -20,6 +20,7 @@
 import com.google.common.base.Preconditions;
 import com.google.common.base.Predicate;
 import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
 import com.google.devtools.build.lib.cmdline.Label;
 import com.google.devtools.build.lib.events.Location;
 import com.google.devtools.build.lib.syntax.Type;
@@ -40,6 +41,23 @@
   // *_test / test_suite attribute that used to specify constraint keywords.
   private static final String CONSTRAINTS_ATTR = "tags";
 
+  // We don't want to pollute the execution info with random things, and we also need to reserve
+  // some internal tags that we don't allow to be set on targets. We also don't want to
+  // exhaustively enumerate all the legal values here. Right now, only a ~small set of tags is
+  // recognized by Bazel.
+  private static final Predicate<String> LEGAL_EXEC_INFO_KEYS = new Predicate<String>() {
+    @Override
+    public boolean apply(String tag) {
+      return tag.startsWith("block-")
+          || tag.startsWith("requires-")
+          || tag.startsWith("no-")
+          || tag.startsWith("supports-")
+          || tag.startsWith("disable-")
+          || tag.equals("local")
+          || tag.startsWith("cpu:");
+    }
+  };
+
   private TargetUtils() {} // Uninstantiable.
 
   public static boolean isTestRuleName(String name) {
@@ -215,13 +233,7 @@
       // some internal tags that we don't allow to be set on targets. We also don't want to
       // exhaustively enumerate all the legal values here. Right now, only a ~small set of tags is
       // recognized by Bazel.
-      if (tag.startsWith("block-")
-          || tag.startsWith("requires-")
-          || tag.startsWith("no-")
-          || tag.startsWith("supports-")
-          || tag.startsWith("disable-")
-          || tag.equals("local")
-          || tag.startsWith("cpu:")) {
+      if (LEGAL_EXEC_INFO_KEYS.apply(tag)) {
         map.put(tag, "");
       }
     }
@@ -229,6 +241,14 @@
   }
 
   /**
+   * Returns the execution info. These include execution requirement tags ('block-*', 'requires-*',
+   * 'no-*', 'supports-*', 'disable-*', 'local', and 'cpu:*') as keys with empty values.
+   */
+  public static Map<String, String> filter(Map<String, String> executionInfo) {
+    return Maps.filterKeys(executionInfo, LEGAL_EXEC_INFO_KEYS);
+  }
+
+  /**
    * Returns the language part of the rule name (e.g. "foo" for foo_test or foo_binary).
    *
    * <p>In practice this is the part before the "_", if any, otherwise the entire rule class name.