1# Copyright 2021 Code Intelligence GmbH 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15def java_fuzz_target_test( 16 name, 17 target_class = None, 18 deps = [], 19 hook_classes = [], 20 native_libs = [], 21 sanitizer = None, 22 visibility = None, 23 tags = [], 24 fuzzer_args = [], 25 srcs = [], 26 size = None, 27 timeout = None, 28 **kwargs): 29 target_name = name + "_target" 30 deploy_manifest_lines = [] 31 if target_class: 32 deploy_manifest_lines.append("Jazzer-Fuzz-Target-Class: %s" % target_class) 33 if hook_classes: 34 deploy_manifest_lines.append("Jazzer-Hook-Classes: %s" % ":".join(hook_classes)) 35 36 # Deps can only be specified on java_binary targets with sources, which 37 # excludes e.g. Kotlin libraries wrapped into java_binary via runtime_deps. 38 target_deps = deps + ["//agent:jazzer_api_compile_only"] if srcs else [] 39 native.java_binary( 40 name = target_name, 41 srcs = srcs, 42 visibility = ["//visibility:private"], 43 create_executable = False, 44 deploy_manifest_lines = deploy_manifest_lines, 45 deps = target_deps, 46 testonly = True, 47 **kwargs 48 ) 49 50 additional_args = [] 51 52 if sanitizer == None: 53 driver = "//driver:jazzer_driver" 54 elif sanitizer == "address": 55 driver = "//driver:jazzer_driver_asan" 56 elif sanitizer == "undefined": 57 driver = "//driver:jazzer_driver_ubsan" 58 else: 59 fail("Invalid sanitizer: " + sanitizer) 60 61 native.java_test( 62 name = name, 63 runtime_deps = ["//bazel:fuzz_target_test_wrapper"], 64 size = size or "enormous", 65 timeout = timeout or "moderate", 66 args = [ 67 "$(rootpath %s)" % driver, 68 "$(rootpath :%s_deploy.jar)" % target_name, 69 ] + additional_args + fuzzer_args, 70 data = [ 71 ":%s_deploy.jar" % target_name, 72 "//agent:jazzer_agent_deploy.jar", 73 driver, 74 ] + native_libs, 75 main_class = "FuzzTargetTestWrapper", 76 use_testrunner = False, 77 tags = tags, 78 visibility = visibility, 79 ) 80