Skip to content

Commit

Permalink
Replace hardcoded repository name in rlocation paths
Browse files Browse the repository at this point in the history
This is in preparation for Bazel module support, where repositories are
assigned internal names different from their declared user-facing name.
  • Loading branch information
fmeum committed Jan 10, 2022
1 parent 9df81b7 commit 4d27926
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
18 changes: 9 additions & 9 deletions bazel/FuzzTargetTestWrapper.java
Expand Up @@ -15,6 +15,7 @@
import com.google.devtools.build.runfiles.Runfiles;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
Expand All @@ -28,8 +29,8 @@ public static void main(String[] args) {
Runfiles runfiles;
try {
runfiles = Runfiles.create();
driverActualPath = runfiles.rlocation(rlocationPath(args[0]));
jarActualPath = runfiles.rlocation(rlocationPath(args[1]));
driverActualPath = runfiles.rlocation(normalizeRlocationPath(args[0]));
jarActualPath = runfiles.rlocation(normalizeRlocationPath(args[1]));
} catch (IOException | ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
System.exit(1);
Expand Down Expand Up @@ -76,12 +77,11 @@ public static void main(String[] args) {
System.exit(0);
}

// Turns the result of Bazel's `$(rootpath ...)` into the correct format for rlocation.
private static String rlocationPath(String rootpath) {
if (rootpath.startsWith("external/")) {
return rootpath.substring("external/".length());
} else {
return "jazzer/" + rootpath;
}
// Canonicalizes paths to external repositories (e.g. jazzer/../external_repo/foo.txt becomes
// external_repo/foo.txt) before passing them to rlocation.
private static String normalizeRlocationPath(String path) {
// Note: We can't use Paths#normalize here as Bazel runfiles paths are always Unix style paths,
// but the behavior of Paths#normalize depends on the OS.
return path.replaceAll("\\.\\./", "");
}
}
13 changes: 13 additions & 0 deletions bazel/compat.bzl → bazel/constants.bzl
Expand Up @@ -12,6 +12,19 @@
# See the License for the specific language governing permissions and
# limitations under the License.

def jazzer_repo_name():
# '@' is the identifier of the main repo. In this case, we know that the
# name of the repository is also the name of the workspace or module we
# define, which is always just "jazzer".
if native.repository_name() == "@":
return "jazzer"
else:
return native.repository_name()

def jazzer_repo_name_define_value():
# One level of quoting for Starlark, one for the shell.
return "\\\"%s\\\"" % jazzer_repo_name()

SKIP_ON_MACOS = select({
"@platforms//os:macos": ["@platforms//:incompatible"],
"//conditions:default": [],
Expand Down
6 changes: 4 additions & 2 deletions bazel/fuzz_target.bzl
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load(":constants.bzl", "jazzer_repo_name")

def java_fuzz_target_test(
name,
target_class = None,
Expand Down Expand Up @@ -64,8 +66,8 @@ def java_fuzz_target_test(
size = size or "enormous",
timeout = timeout or "moderate",
args = [
"$(rootpath %s)" % driver,
"$(rootpath :%s_deploy.jar)" % target_name,
"%s/$(rootpath %s)" % (jazzer_repo_name(), driver),
"%s/$(rootpath :%s_deploy.jar)" % (jazzer_repo_name(), target_name),
] + additional_args + fuzzer_args,
data = [
":%s_deploy.jar" % target_name,
Expand Down
5 changes: 4 additions & 1 deletion driver/BUILD.bazel
@@ -1,4 +1,5 @@
load("//bazel:cc.bzl", "cc_17_library")
load("//bazel:constants.bzl", "jazzer_repo_name_define_value")

cc_library(
name = "sanitizer_hooks_with_pc",
Expand Down Expand Up @@ -71,7 +72,9 @@ cc_library(
# libFuzzer. Instead, trigger a hard exit.
"@platforms//os:windows": ["SIGUSR1=SIGTERM"],
"//conditions:default": [],
}),
}) + [
"JAZZER_REPO_NAME=%s" % jazzer_repo_name_define_value(),
],
tags = [
# Should be built through the cc_17_library driver_lib.
"manual",
Expand Down
4 changes: 3 additions & 1 deletion driver/jvm_tooling.cpp
Expand Up @@ -116,7 +116,9 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad_jazzer_initialize(JavaVM *vm,
}

namespace {
constexpr auto kAgentBazelRunfilesPath = "jazzer/agent/jazzer_agent_deploy.jar";
// The name of the Jazzer repository is injected as a define.
constexpr auto kAgentBazelRunfilesPath =
JAZZER_REPO_NAME "/agent/jazzer_agent_deploy.jar";
constexpr auto kAgentFileName = "jazzer_agent_deploy.jar";
constexpr const char kExceptionUtilsClassName[] =
"com/code_intelligence/jazzer/runtime/ExceptionUtils";
Expand Down
6 changes: 3 additions & 3 deletions examples/BUILD.bazel
@@ -1,6 +1,6 @@
load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
load("@fmeum_rules_jni//jni:defs.bzl", "java_jni_library")
load("//bazel:compat.bzl", "SKIP_ON_MACOS", "SKIP_ON_WINDOWS")
load("//bazel:constants.bzl", "SKIP_ON_MACOS", "SKIP_ON_WINDOWS", "jazzer_repo_name")
load("//bazel:fuzz_target.bzl", "java_fuzz_target_test")

java_fuzz_target_test(
Expand Down Expand Up @@ -207,9 +207,9 @@ sh_test(
name = "JsonSanitizerReplayerCrashTest",
srcs = ["check_for_finding.sh"],
args = [
"jazzer/$(rootpath :JsonSanitizerReplayerCrash)",
"%s/$(rootpath :JsonSanitizerReplayerCrash)" % jazzer_repo_name(),
"com.example.JsonSanitizerDenylistFuzzer",
"jazzer/$(rootpath :json_sanitizer_denylist_crash)",
"%s/$(rootpath :json_sanitizer_denylist_crash)" % jazzer_repo_name(),
],
data = [
":JsonSanitizerReplayerCrash",
Expand Down

0 comments on commit 4d27926

Please sign in to comment.