Make sh tests more portable by setting the location of test_util_sh in env.

Needed because test_util_sh (as data dep) may be deployed in some other directory than the test script, depending on the platform.

PiperOrigin-RevId: 985321963
diff --git a/centipede/BUILD b/centipede/BUILD
index a9badf0..4440a86 100644
--- a/centipede/BUILD
+++ b/centipede/BUILD
@@ -1297,10 +1297,17 @@
 )
 
 sh_library(
-    name = "test_util_sh",
+    name = "test_util_sh_lib",
     srcs = ["test_util.sh"],
 )
 
+# Rule that has the .sh file as the single output, so sh_test can find it with $(rootpath :test_util_sh).
+filegroup(
+    name = "test_util_sh",
+    srcs = [":test_util.sh"],
+    data = [":test_util_sh_lib"],
+)
+
 ################################################################################
 #                               Unit tests
 ################################################################################
@@ -2024,6 +2031,9 @@
         ":centipede",
         ":test_util_sh",
     ],
+    env = {
+        "TEST_UTIL_SH": "$(rootpath :test_util_sh)",
+    },
 )
 
 cc_library(
diff --git a/centipede/centipede_binary_test.sh b/centipede/centipede_binary_test.sh
index 7ab2096..8ce8e5f 100755
--- a/centipede/centipede_binary_test.sh
+++ b/centipede/centipede_binary_test.sh
@@ -18,7 +18,11 @@
 
 set -eu
 
-source "$(dirname "$0")/test_util.sh"
+if ! [[ -e "${TEST_UTIL_SH:=$(dirname "$0")/test_util.sh}" ]]; then
+  echo "TEST_UTIL_SH must be set to the location of :test_util_sh" >&2
+  exit 1
+fi
+source "${TEST_UTIL_SH}"
 
 centipede_test_srcdir="$(fuzztest::internal::get_centipede_test_srcdir)"
 centipede_binary="${centipede_test_srcdir}/centipede"
diff --git a/centipede/puzzles/puzzle.bzl b/centipede/puzzles/puzzle.bzl
index a446b96..d3d2e69 100644
--- a/centipede/puzzles/puzzle.bzl
+++ b/centipede/puzzles/puzzle.bzl
@@ -45,4 +45,7 @@
                 "@com_google_fuzztest//centipede:centipede_uninstrumented",
                 "@com_google_fuzztest//centipede:test_util_sh",
             ],
+            env = {
+                "TEST_UTIL_SH": "$(rootpath @com_google_fuzztest//centipede:test_util_sh)",
+            },
         )
diff --git a/centipede/puzzles/run_puzzle.sh b/centipede/puzzles/run_puzzle.sh
index d6d12c8..3bc9f3a 100755
--- a/centipede/puzzles/run_puzzle.sh
+++ b/centipede/puzzles/run_puzzle.sh
@@ -25,7 +25,11 @@
 
 ls -la "$(dirname "$0")"
 
-source "$(dirname "$0")/../test_util.sh"
+if ! [[ -e "${TEST_UTIL_SH:=$(dirname "$0")/../test_util.sh}" ]]; then
+  echo "TEST_UTIL_SH must be set to the location of :test_util_sh" >&2
+  exit 1
+fi
+source "${TEST_UTIL_SH}"
 
 readonly centipede_dir="$(fuzztest::internal::get_centipede_test_srcdir)"
 fuzztest::internal::maybe_set_var_to_executable_path centipede "${centipede_dir}/centipede_uninstrumented"
diff --git a/centipede/testing/BUILD b/centipede/testing/BUILD
index 1ddf5e5..83af2dd 100644
--- a/centipede/testing/BUILD
+++ b/centipede/testing/BUILD
@@ -364,6 +364,9 @@
         "@com_google_fuzztest//centipede:test_fuzzing_util_sh",
         "@com_google_fuzztest//centipede:test_util_sh",
     ],
+    env = {
+        "TEST_UTIL_SH": "$(rootpath @com_google_fuzztest//centipede:test_util_sh)",
+    },
 )
 
 sh_test(
@@ -375,6 +378,9 @@
         "@com_google_fuzztest//centipede",
         "@com_google_fuzztest//centipede:test_util_sh",
     ],
+    env = {
+        "TEST_UTIL_SH": "$(rootpath @com_google_fuzztest//centipede:test_util_sh)",
+    },
 )
 
 sh_test(
@@ -384,6 +390,9 @@
         ":empty_fuzz_target",
         "@com_google_fuzztest//centipede:test_util_sh",
     ],
+    env = {
+        "TEST_UTIL_SH": "$(rootpath @com_google_fuzztest//centipede:test_util_sh)",
+    },
 )
 
 sh_test(
@@ -393,6 +402,9 @@
         ":multi_dso_target",
         "@com_google_fuzztest//centipede:test_util_sh",
     ],
+    env = {
+        "TEST_UTIL_SH": "$(rootpath @com_google_fuzztest//centipede:test_util_sh)",
+    },
 )
 
 sh_test(
@@ -403,6 +415,9 @@
         "@com_google_fuzztest//centipede",
         "@com_google_fuzztest//centipede:test_util_sh",
     ],
+    env = {
+        "TEST_UTIL_SH": "$(rootpath @com_google_fuzztest//centipede:test_util_sh)",
+    },
 )
 
 sh_test(
@@ -413,6 +428,9 @@
         "@com_google_fuzztest//centipede",
         "@com_google_fuzztest//centipede:test_util_sh",
     ],
+    env = {
+        "TEST_UTIL_SH": "$(rootpath @com_google_fuzztest//centipede:test_util_sh)",
+    },
 )
 
 sh_test(
@@ -423,6 +441,9 @@
         "@com_google_fuzztest//centipede",
         "@com_google_fuzztest//centipede:test_util_sh",
     ],
+    env = {
+        "TEST_UTIL_SH": "$(rootpath @com_google_fuzztest//centipede:test_util_sh)",
+    },
 )
 
 sh_test(
@@ -433,6 +454,9 @@
         "@com_google_fuzztest//centipede",
         "@com_google_fuzztest//centipede:test_util_sh",
     ],
+    env = {
+        "TEST_UTIL_SH": "$(rootpath @com_google_fuzztest//centipede:test_util_sh)",
+    },
 )
 
 sh_test(
@@ -443,6 +467,9 @@
         "@com_google_fuzztest//centipede",
         "@com_google_fuzztest//centipede:test_util_sh",
     ],
+    env = {
+        "TEST_UTIL_SH": "$(rootpath @com_google_fuzztest//centipede:test_util_sh)",
+    },
 )
 
 sh_test(
@@ -453,6 +480,9 @@
         "@com_google_fuzztest//centipede",
         "@com_google_fuzztest//centipede:test_util_sh",
     ],
+    env = {
+        "TEST_UTIL_SH": "$(rootpath @com_google_fuzztest//centipede:test_util_sh)",
+    },
 )
 
 sh_test(
@@ -463,6 +493,9 @@
         "@com_google_fuzztest//centipede",
         "@com_google_fuzztest//centipede:test_util_sh",
     ],
+    env = {
+        "TEST_UTIL_SH": "$(rootpath @com_google_fuzztest//centipede:test_util_sh)",
+    },
 )
 
 sh_test(
@@ -473,6 +506,9 @@
         "@com_google_fuzztest//centipede",
         "@com_google_fuzztest//centipede:test_util_sh",
     ],
+    env = {
+        "TEST_UTIL_SH": "$(rootpath @com_google_fuzztest//centipede:test_util_sh)",
+    },
 )
 
 # Runs Centipede and check its output format.
@@ -485,6 +521,9 @@
         "@com_google_fuzztest//centipede",
         "@com_google_fuzztest//centipede:test_util_sh",
     ],
+    env = {
+        "TEST_UTIL_SH": "$(rootpath @com_google_fuzztest//centipede:test_util_sh)",
+    },
 )
 
 sh_test(
@@ -496,6 +535,9 @@
         "@com_google_fuzztest//centipede",
         "@com_google_fuzztest//centipede:test_util_sh",
     ],
+    env = {
+        "TEST_UTIL_SH": "$(rootpath @com_google_fuzztest//centipede:test_util_sh)",
+    },
 )
 
 cc_binary(
diff --git a/centipede/testing/centipede_main_test.sh b/centipede/testing/centipede_main_test.sh
index 79ac691..a74e8ad 100755
--- a/centipede/testing/centipede_main_test.sh
+++ b/centipede/testing/centipede_main_test.sh
@@ -18,10 +18,13 @@
 
 set -eu
 
-source "$(dirname "$0")/../test_fuzzing_util.sh"
-source "$(dirname "$0")/../test_util.sh"
-
+if ! [[ -e "${TEST_UTIL_SH:=$(dirname "$0")/../test_util.sh}" ]]; then
+  echo "TEST_UTIL_SH must be set to the location of :test_util_sh" >&2
+  exit 1
+fi
+source "${TEST_UTIL_SH}"
 CENTIPEDE_TEST_SRCDIR="$(fuzztest::internal::get_centipede_test_srcdir)"
+source "${CENTIPEDE_TEST_SRCDIR}/test_fuzzing_util.sh"
 
 # The following variables can be overridden externally by passing --test_env to
 # the build command, e.g. --test_env=EXAMPLE_TARGET_BINARY="/some/path".
diff --git a/centipede/testing/clusterfuzz_format_test.sh b/centipede/testing/clusterfuzz_format_test.sh
index 8003a91..5f6de03 100755
--- a/centipede/testing/clusterfuzz_format_test.sh
+++ b/centipede/testing/clusterfuzz_format_test.sh
@@ -19,7 +19,11 @@
 
 set -eu
 
-source "$(dirname "$0")/../test_util.sh"
+if ! [[ -e "${TEST_UTIL_SH:=$(dirname "$0")/../test_util.sh}" ]]; then
+  echo "TEST_UTIL_SH must be set to the location of :test_util_sh" >&2
+  exit 1
+fi
+source "${TEST_UTIL_SH}"
 
 # Centipede and target binaries.
 declare centipede
diff --git a/centipede/testing/data_only_dso_test.sh b/centipede/testing/data_only_dso_test.sh
index b917f9f..f4d8229 100755
--- a/centipede/testing/data_only_dso_test.sh
+++ b/centipede/testing/data_only_dso_test.sh
@@ -17,7 +17,11 @@
 # Tests basic functionality for a target with data-only DSOs.
 set -eu
 
-source "$(dirname "$0")/../test_util.sh"
+if ! [[ -e "${TEST_UTIL_SH:=$(dirname "$0")/../test_util.sh}" ]]; then
+  echo "TEST_UTIL_SH must be set to the location of :test_util_sh" >&2
+  exit 1
+fi
+source "${TEST_UTIL_SH}"
 
 CENTIPEDE_TEST_SRCDIR="$(fuzztest::internal::get_centipede_test_srcdir)"
 
diff --git a/centipede/testing/dump_binary_info_test.sh b/centipede/testing/dump_binary_info_test.sh
index 79af128..eac9e5d 100755
--- a/centipede/testing/dump_binary_info_test.sh
+++ b/centipede/testing/dump_binary_info_test.sh
@@ -15,7 +15,11 @@
 # limitations under the License.
 set -eu
 
-source "$(dirname "$0")/../test_util.sh"
+if ! [[ -e "${TEST_UTIL_SH:=$(dirname "$0")/../test_util.sh}" ]]; then
+  echo "TEST_UTIL_SH must be set to the location of :test_util_sh" >&2
+  exit 1
+fi
+source "${TEST_UTIL_SH}"
 
 binary="$(fuzztest::internal::get_centipede_test_srcdir)/testing/multi_dso_target"
 
diff --git a/centipede/testing/external_target_test.sh b/centipede/testing/external_target_test.sh
index 600bf26..f9eaf90 100755
--- a/centipede/testing/external_target_test.sh
+++ b/centipede/testing/external_target_test.sh
@@ -16,7 +16,11 @@
 
 set -euo pipefail
 
-source "$(dirname "$0")/../test_util.sh"
+if ! [[ -e "${TEST_UTIL_SH:=$(dirname "$0")/../test_util.sh}" ]]; then
+  echo "TEST_UTIL_SH must be set to the location of :test_util_sh" >&2
+  exit 1
+fi
+source "${TEST_UTIL_SH}"
 
 CENTIPEDE_TEST_SRCDIR="$(fuzztest::internal::get_centipede_test_srcdir)"
 
diff --git a/centipede/testing/inline_8bit_counters_test.sh b/centipede/testing/inline_8bit_counters_test.sh
index a73c7dc..f171590 100755
--- a/centipede/testing/inline_8bit_counters_test.sh
+++ b/centipede/testing/inline_8bit_counters_test.sh
@@ -18,7 +18,11 @@
 
 set -eu
 
-source "$(dirname "$0")/../test_util.sh"
+if ! [[ -e "${TEST_UTIL_SH:=$(dirname "$0")/../test_util.sh}" ]]; then
+  echo "TEST_UTIL_SH must be set to the location of :test_util_sh" >&2
+  exit 1
+fi
+source "${TEST_UTIL_SH}"
 
 CENTIPEDE_TEST_SRCDIR="$(fuzztest::internal::get_centipede_test_srcdir)"
 
diff --git a/centipede/testing/instrumentation_test.sh b/centipede/testing/instrumentation_test.sh
index 509dea9..af52378 100755
--- a/centipede/testing/instrumentation_test.sh
+++ b/centipede/testing/instrumentation_test.sh
@@ -20,7 +20,11 @@
 
 set -eu
 
-source "$(dirname "$0")/../test_util.sh"
+if ! [[ -e "${TEST_UTIL_SH:=$(dirname "$0")/../test_util.sh}" ]]; then
+  echo "TEST_UTIL_SH must be set to the location of :test_util_sh" >&2
+  exit 1
+fi
+source "${TEST_UTIL_SH}"
 
 # Max allowed PC table size is 16 bytes, i.e. one entry.
 ALLOWED_SIZE=16
diff --git a/centipede/testing/minimize_crash_test.sh b/centipede/testing/minimize_crash_test.sh
index cb4e352..7505164 100755
--- a/centipede/testing/minimize_crash_test.sh
+++ b/centipede/testing/minimize_crash_test.sh
@@ -18,7 +18,11 @@
 
 set -eu
 
-source "$(dirname "$0")/../test_util.sh"
+if ! [[ -e "${TEST_UTIL_SH:=$(dirname "$0")/../test_util.sh}" ]]; then
+  echo "TEST_UTIL_SH must be set to the location of :test_util_sh" >&2
+  exit 1
+fi
+source "${TEST_UTIL_SH}"
 
 CENTIPEDE_TEST_SRCDIR="$(fuzztest::internal::get_centipede_test_srcdir)"
 
diff --git a/centipede/testing/multi_dso_test.sh b/centipede/testing/multi_dso_test.sh
index bef20fe..fd564ee 100755
--- a/centipede/testing/multi_dso_test.sh
+++ b/centipede/testing/multi_dso_test.sh
@@ -17,7 +17,11 @@
 # Tests basic functionality for a target with multiple instrumented DSOs.
 set -eu
 
-source "$(dirname "$0")/../test_util.sh"
+if ! [[ -e "${TEST_UTIL_SH:=$(dirname "$0")/../test_util.sh}" ]]; then
+  echo "TEST_UTIL_SH must be set to the location of :test_util_sh" >&2
+  exit 1
+fi
+source "${TEST_UTIL_SH}"
 
 
 CENTIPEDE_TEST_SRCDIR="$(fuzztest::internal::get_centipede_test_srcdir)"
diff --git a/centipede/testing/no_startup_features_test.sh b/centipede/testing/no_startup_features_test.sh
index 3a07de1..9f94ee0 100755
--- a/centipede/testing/no_startup_features_test.sh
+++ b/centipede/testing/no_startup_features_test.sh
@@ -18,7 +18,11 @@
 
 set -eu
 
-source "$(dirname "$0")/../test_util.sh"
+if ! [[ -e "${TEST_UTIL_SH:=$(dirname "$0")/../test_util.sh}" ]]; then
+  echo "TEST_UTIL_SH must be set to the location of :test_util_sh" >&2
+  exit 1
+fi
+source "${TEST_UTIL_SH}"
 
 CENTIPEDE_TEST_SRCDIR="$(fuzztest::internal::get_centipede_test_srcdir)"
 
diff --git a/centipede/testing/runner_cleanup_test.sh b/centipede/testing/runner_cleanup_test.sh
index fdde02f..a1d9edf 100755
--- a/centipede/testing/runner_cleanup_test.sh
+++ b/centipede/testing/runner_cleanup_test.sh
@@ -18,7 +18,11 @@
 
 set -eu
 
-source "$(dirname "$0")/../test_util.sh"
+if ! [[ -e "${TEST_UTIL_SH:=$(dirname "$0")/../test_util.sh}" ]]; then
+  echo "TEST_UTIL_SH must be set to the location of :test_util_sh" >&2
+  exit 1
+fi
+source "${TEST_UTIL_SH}"
 
 CENTIPEDE_TEST_SRCDIR="$(fuzztest::internal::get_centipede_test_srcdir)"
 
diff --git a/centipede/testing/runner_test.sh b/centipede/testing/runner_test.sh
index 0dbd043..1d56693 100755
--- a/centipede/testing/runner_test.sh
+++ b/centipede/testing/runner_test.sh
@@ -19,7 +19,11 @@
 
 set -eu
 
-source "$(dirname "$0")/../test_util.sh"
+if ! [[ -e "${TEST_UTIL_SH:=$(dirname "$0")/../test_util.sh}" ]]; then
+  echo "TEST_UTIL_SH must be set to the location of :test_util_sh" >&2
+  exit 1
+fi
+source "${TEST_UTIL_SH}"
 
 target="$(fuzztest::internal::get_centipede_test_srcdir)/testing/test_fuzz_target"
 non_pie_target="$(fuzztest::internal::get_centipede_test_srcdir)/testing/test_fuzz_target_non_pie"
diff --git a/centipede/testing/trace_pc_test.sh b/centipede/testing/trace_pc_test.sh
index 80b8b1d..7ce917c 100755
--- a/centipede/testing/trace_pc_test.sh
+++ b/centipede/testing/trace_pc_test.sh
@@ -18,7 +18,11 @@
 
 set -eu
 
-source "$(dirname "$0")/../test_util.sh"
+if ! [[ -e "${TEST_UTIL_SH:=$(dirname "$0")/../test_util.sh}" ]]; then
+  echo "TEST_UTIL_SH must be set to the location of :test_util_sh" >&2
+  exit 1
+fi
+source "${TEST_UTIL_SH}"
 
 CENTIPEDE_TEST_SRCDIR="$(fuzztest::internal::get_centipede_test_srcdir)"
 
diff --git a/centipede/testing/user_defined_features_test.sh b/centipede/testing/user_defined_features_test.sh
index 1c17eed..c5f4960 100755
--- a/centipede/testing/user_defined_features_test.sh
+++ b/centipede/testing/user_defined_features_test.sh
@@ -18,7 +18,11 @@
 
 set -eu
 
-source "$(dirname "$0")/../test_util.sh"
+if ! [[ -e "${TEST_UTIL_SH:=$(dirname "$0")/../test_util.sh}" ]]; then
+  echo "TEST_UTIL_SH must be set to the location of :test_util_sh" >&2
+  exit 1
+fi
+source "${TEST_UTIL_SH}"
 
 CENTIPEDE_TEST_SRCDIR="$(fuzztest::internal::get_centipede_test_srcdir)"