blob: c35edd25196ede16247b479dae9de8284e0ea9ad [file] [log] [blame]
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Simple fuzz targets that demonstrate the Bazel extension functionality and
# serve as regression tests. Targets that are expected to crash or hang are
# disabled in the OSS-Fuzz integration using the "no-oss-fuzz" tag.
load("@rules_cc//cc:defs.bzl", "cc_binary")
load("//fuzzing:java_defs.bzl", "java_fuzz_test")
filegroup(
name = "corpus",
srcs = [
"corpus_0.txt",
"corpus_1.txt",
],
)
java_fuzz_test(
name = "EmptyFuzzTest",
srcs = ["com/example/EmptyFuzzTest.java"],
corpus = [
"corpus_0.txt",
],
)
java_fuzz_test(
name = "FuzzTest",
srcs = ["com/example/FuzzTest.java"],
tags = [
"no-oss-fuzz",
],
)
java_fuzz_test(
name = "NativeFuzzTest",
srcs = ["com/example/NativeFuzzTest.java"],
corpus = [
":corpus",
],
tags = [
"no-oss-fuzz",
],
# The JVM expects a native library on macOS to have the .dylib extension,
# but due to a bug in Bazel the shared library extension defaults to .so
# there. This can be worked around by specifying the desired extension in
# the name of the rule and selecting the correct one based on the platform.
# See https://github.com/bazelbuild/bazel/issues/11082.
deps = select({
"@platforms//os:macos": [":libnative.dylib"],
"//conditions:default": [":native"],
}),
)
# A Java fuzz test with a native library, both of which have declared data
# dependencies that they can access at runtime.
java_fuzz_test(
name = "NativeRunfileFuzzTest",
srcs = ["com/example/NativeRunfileFuzzTest.java"],
data = [
"corpus_0.txt",
],
# See NativeFuzzTest for why this uses a select.
deps = select({
"@platforms//os:macos": [":libnative_runfile.dylib"],
"//conditions:default": [":native_runfile"],
}) + [
"@bazel_tools//tools/java/runfiles",
],
)
# A Java fuzz test with a native library that calls a function through a pointer
# of an incorrect type, which is detected by UBSan.
java_fuzz_test(
name = "NativeUbsanFuncPtrFuzzTest",
srcs = ["com/example/NativeUbsanFuncPtrFuzzTest.java"],
deps = select({
"@platforms//os:macos": [":libnative_ubsan_func_ptr.dylib"],
"//conditions:default": [":native_ubsan_func_ptr"],
}),
)
# A native library that interfaces with Java through the JNI.
# It contains an out-of-bounds read is detected by ASan.
cc_binary(
name = "native",
# Build as a shared library that can be loaded by a Java application at
# runtime via System.loadLibrary().
linkshared = True,
tags = ["manual"],
deps = [
":native_lib",
],
)
# The same shared library as :native, but with the correct extension for macOS.
# See the comment on :NativeFuzzTest for why this is needed.
cc_binary(
name = "libnative.dylib",
linkshared = True,
tags = ["manual"],
deps = [
":native_lib",
],
)
# The implementation shared by :native and :libnative.dylib, which differ only
# in the name of the resulting shared library.
cc_library(
name = "native_lib",
srcs = [
"com/example/NativeFuzzTest.cpp",
"com/example/NativeFuzzTest.h",
],
deps = [
"@bazel_tools//tools/jdk:jni",
],
# Required because :native and :libnative.dylib to not reference any symbols
# of this library, which means that it wouldn't be linked at all without
# this.
alwayslink = True,
)
# A shared library that demonstrates that fuzz targets can find their Bazel
# data dependencies at runtime, both from Java and native code.
cc_binary(
name = "native_runfile",
linkshared = True,
tags = ["manual"],
deps = [
":native_runfile_lib",
],
)
# The same shared library as :native_runfile, but with the correct extension for
# macOS. See the comment on :NativeFuzzTest for why this is needed.
cc_binary(
name = "libnative_runfile.dylib",
linkshared = True,
tags = ["manual"],
deps = [
":native_runfile_lib",
],
)
cc_library(
name = "native_runfile_lib",
srcs = [
"com/example/NativeRunfileFuzzTest.cpp",
"com/example/NativeRunfileFuzzTest.h",
],
data = [
"corpus_1.txt",
],
deps = [
"@bazel_tools//tools/cpp/runfiles",
"@bazel_tools//tools/jdk:jni",
],
alwayslink = True,
)
cc_binary(
name = "native_ubsan_func_ptr",
linkshared = True,
tags = ["manual"],
deps = [
":native_ubsan_func_ptr_lib",
],
)
cc_binary(
name = "libnative_ubsan_func_ptr.dylib",
linkshared = True,
tags = ["manual"],
deps = [
":native_ubsan_func_ptr_lib",
],
)
cc_library(
name = "native_ubsan_func_ptr_lib",
srcs = [
"com/example/NativeUbsanFuncPtrFuzzTest.cpp",
"com/example/NativeUbsanFuncPtrFuzzTest.h",
],
deps = [
"@bazel_tools//tools/jdk:jni",
],
alwayslink = True,
)