Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions dotnet/fuzz/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
load("@rules_cc//cc:defs.bzl", "cc_binary")
load("@rules_dx//libs/starlark:fuzz.bzl", "dx_fuzz_binary")
load("@rules_dx//quality:fixtures.bzl", "real_source_target")
load("@rules_dx//rust/rules:defs.bzl", "rust_binary")

_SUPPORTED = select({
"@platforms//os:linux": [],
"//conditions:default": ["@platforms//:incompatible"],
})

rust_binary(
name = "instrument",
srcs = ["instrument/main.rs"],
crate_name = "instrument",
crate_root = "instrument/main.rs",
visibility = ["//visibility:public"],
)

cc_binary(
name = "libfuzzer_dotnet_bin",
srcs = ["@dx_libfuzzer_dotnet//:libfuzzer-dotnet.cc"],
copts = ["-fsanitize=fuzzer"],
linkopts = ["-fsanitize=fuzzer"],
target_compatible_with = ["@platforms//os:linux"],
target_compatible_with = _SUPPORTED,
testonly = True,
visibility = ["//visibility:private"],
)

dx_fuzz_binary(
name = "libfuzzer_dotnet",
binary = ":libfuzzer_dotnet_bin",
target_compatible_with = ["@platforms//os:linux"],
target_compatible_with = _SUPPORTED,
testonly = True,
visibility = ["//visibility:public"],
)
Expand Down
31 changes: 31 additions & 0 deletions dotnet/fuzz/instrument/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//! Copies an assembly and runs SharpFuzz on the copy: `instrument <sharpfuzz> <input> <output>`.

use std::process::{Command, ExitCode};
use std::{env, fs};

fn main() -> ExitCode {
let args: Vec<String> = env::args().skip(1).collect();
let [sharpfuzz, input, output] = args.as_slice() else {
eprintln!("usage: instrument <sharpfuzz> <input> <output>");
return ExitCode::from(2);
};
if let Err(err) = fs::copy(input, output).and_then(|_| {
let mut perms = fs::metadata(output)?.permissions();
perms.set_readonly(false);
fs::set_permissions(output, perms)
}) {
eprintln!("instrument: copying {input} to {output}: {err}");
return ExitCode::from(1);
}
match Command::new(sharpfuzz).arg(output).status() {
Ok(status) if status.success() => ExitCode::SUCCESS,
Ok(status) => {
eprintln!("instrument: sharpfuzz failed: {status}");
ExitCode::from(1)
}
Err(err) => {
eprintln!("instrument: running {sharpfuzz}: {err}");
ExitCode::from(1)
}
}
}
24 changes: 7 additions & 17 deletions families/dotnet/MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 13 additions & 5 deletions families/dotnet/csharp/rules/sharpfuzz.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ SHARPFUZZ = [
Label("@paket.main//sharpfuzz"),
]

_LINUX = ["@platforms//os:linux"]
_SUPPORTED = select({
"@platforms//os:linux": [],
"//conditions:default": ["@platforms//:incompatible"],
})

DxSharpFuzzInfo = provider(
doc = "A target's runtime info with first-party assemblies instrumented by SharpFuzz.",
Expand All @@ -29,12 +32,12 @@ def _aspect_impl(target, ctx):
libs = []
for dll in runtime.libs:
copy = ctx.actions.declare_file("{}_sharpfuzz/{}".format(ctx.label.name, dll.basename))
ctx.actions.run_shell(
ctx.actions.run(
inputs = [dll],
outputs = [copy],
executable = ctx.executable._instrument,
tools = [ctx.executable._sharpfuzz],
command = 'cp "$1" "$2" && chmod u+w "$2" && "$3" "$2"',
arguments = [dll.path, copy.path, ctx.executable._sharpfuzz.path],
arguments = [ctx.executable._sharpfuzz.path, dll.path, copy.path],
mnemonic = "SharpFuzzInstrument",
progress_message = "Instrumenting %{output} with SharpFuzz",
)
Expand All @@ -61,6 +64,11 @@ dx_sharpfuzz_aspect = aspect(
implementation = _aspect_impl,
attr_aspects = ["deps"],
attrs = {
"_instrument": attr.label(
default = Label("@rules_dx//dotnet/fuzz:instrument"),
executable = True,
cfg = "exec",
),
"_sharpfuzz": attr.label(
default = Label("@paket.main//sharpfuzz.commandline/tools:sharpfuzz"),
executable = True,
Expand Down Expand Up @@ -111,7 +119,7 @@ def sharpfuzz_fuzz(name, binary, srcs, deps, visibility, tags, kwargs):
)
instrumented.append(":" + wrapped)
kwargs = dict(kwargs)
kwargs["target_compatible_with"] = kwargs.get("target_compatible_with", _LINUX)
kwargs["target_compatible_with"] = kwargs.get("target_compatible_with", _SUPPORTED)
binary(
name = name,
srcs = srcs,
Expand Down
Loading