Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace hardcoded repository name in rlocation paths #280

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions bazel/FuzzTargetTestWrapper.java
Original file line number Diff line number Diff line change
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("\\.\\./", "");
}
}
15 changes: 15 additions & 0 deletions bazel/compat.bzl → bazel/constants.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@
# See the License for the specific language governing permissions and
# limitations under the License.

def jazzer_repo_name():
# Skip over the '@'.
repo_name = native.repository_name()[1:]
if repo_name:
return repo_name
else:
# repository_name returns "@" for 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".
return "jazzer"

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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