1# Copyright 2020 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import("//build_overrides/build.gni")
6import("//testing/libfuzzer/fuzzer_test.gni")
7import("//third_party/libprotobuf-mutator/fuzzable_proto_library.gni")
8
9config("include_config") {
10  include_dirs = [ "src/" ]
11}
12
13source_set("libprotobuf-mutator") {
14  testonly = true
15
16  configs += [ ":include_config" ]
17
18  public_configs = [ ":include_config" ]
19  sources = [
20    "src/src/binary_format.cc",
21    "src/src/libfuzzer/libfuzzer_macro.cc",
22    "src/src/libfuzzer/libfuzzer_mutator.cc",
23    "src/src/mutator.cc",
24    "src/src/text_format.cc",
25    "src/src/utf8_fix.cc",
26  ]
27
28  # Allow users of LPM to use protobuf reflection and other features from
29  # protobuf_full.
30  public_deps = [ "//third_party/protobuf:protobuf_full" ]
31}
32
33# This protoc plugin, like the compiler, should only be built for the host
34# architecture.
35if (current_toolchain == host_toolchain) {
36  # This plugin will be needed to fuzz most protobuf code in Chromium. That's
37  # because production protobuf code must contain the line:
38  # "option optimize_for = LITE_RUNTIME", which instructs the proto compiler not
39  # to compile the proto using the full protobuf runtime. This allows Chromium
40  # not to depend on the full protobuf library, but prevents
41  # libprotobuf-mutator from fuzzing because the lite runtime lacks needed
42  # features (such as reflection).  The plugin simply compiles a proto library
43  # as normal but ensures that is compiled with the full protobuf runtime.
44  executable("override_lite_runtime_plugin") {
45    sources = [ "protoc_plugin/protoc_plugin.cc" ]
46    deps = [ "//third_party/protobuf:protoc_lib" ]
47    public_configs = [ "//third_party/protobuf:protobuf_config" ]
48  }
49  # To use the plugin in a proto_library you want to fuzz, change the build
50  # target to fuzzable_proto_library (defined in
51  # //third_party/libprotobuf-mutator/fuzzable_proto_library.gni)
52}
53
54# The CQ will try building this target without "use_libfuzzer" if it is defined.
55# That will cause the build to fail, so don't define it when "use_libfuzzer" is
56# is false.
57if (use_libfuzzer) {
58  # Test that override_lite_runtime_plugin is working when built. This target
59  # contains files that are optimized for LITE_RUNTIME and which import other
60  # files that are also optimized for LITE_RUNTIME.
61  openscreen_fuzzer_test("override_lite_runtime_plugin_test_fuzzer") {
62    sources = [ "protoc_plugin/test_fuzzer.cc" ]
63    deps = [
64      ":libprotobuf-mutator",
65      ":override_lite_runtime_plugin_test_fuzzer_proto",
66    ]
67  }
68}
69
70# Proto library for override_lite_runtime_plugin_test_fuzzer
71fuzzable_proto_library("override_lite_runtime_plugin_test_fuzzer_proto") {
72  sources = [
73    "protoc_plugin/imported.proto",
74    "protoc_plugin/imported_publicly.proto",
75    "protoc_plugin/test_fuzzer_input.proto",
76  ]
77}
78
79# Avoid CQ complaints on platforms we don't care about (ie: iOS).
80# Also prevent people from using this to include protobuf_full into a production
81# build of Chrome.
82if (use_libfuzzer) {
83  # Component that can provide protobuf_full to non-testonly targets
84  static_library("protobuf_full") {
85    public_deps = [ "//third_party/protobuf:protobuf_full" ]
86  }
87}
88