blob: d44c02de6da52d55a49bb50b8f0186420aa6b83a [file] [log] [blame]
Mitch Phillipsda9a4632019-07-15 09:34:09 -07001// Copyright 2016 Google Inc. All rights reserved.
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
15package cc
16
17import (
18 "android/soong/android"
19 "android/soong/cc/config"
Mitch Phillipsd0bd16d2019-08-22 15:47:09 -070020 "github.com/google/blueprint/proptools"
Mitch Phillipsda9a4632019-07-15 09:34:09 -070021)
22
23func init() {
24 android.RegisterModuleType("cc_fuzz", FuzzFactory)
25}
26
27// cc_fuzz creates a host/device fuzzer binary. Host binaries can be found at
28// $ANDROID_HOST_OUT/fuzz/, and device binaries can be found at /data/fuzz on
29// your device, or $ANDROID_PRODUCT_OUT/data/fuzz in your build tree.
30func FuzzFactory() android.Module {
31 module := NewFuzz(android.HostAndDeviceSupported)
32 return module.Init()
33}
34
35func NewFuzzInstaller() *baseInstaller {
36 return NewBaseInstaller("fuzz", "fuzz", InstallInData)
37}
38
39type fuzzBinary struct {
40 *binaryDecorator
41 *baseCompiler
42}
43
44func (fuzz *fuzzBinary) linkerProps() []interface{} {
45 props := fuzz.binaryDecorator.linkerProps()
46 return props
47}
48
49func (fuzz *fuzzBinary) linkerInit(ctx BaseModuleContext) {
50 // Add ../lib[64] to rpath so that out/host/linux-x86/fuzz/<fuzzer> can
51 // find out/host/linux-x86/lib[64]/library.so
52 runpaths := []string{"../lib"}
53 for _, runpath := range runpaths {
54 if ctx.toolchain().Is64Bit() {
55 runpath += "64"
56 }
57 fuzz.binaryDecorator.baseLinker.dynamicProperties.RunPaths = append(
58 fuzz.binaryDecorator.baseLinker.dynamicProperties.RunPaths, runpath)
59 }
60
61 // add "" to rpath so that fuzzer binaries can find libraries in their own fuzz directory
62 fuzz.binaryDecorator.baseLinker.dynamicProperties.RunPaths = append(
63 fuzz.binaryDecorator.baseLinker.dynamicProperties.RunPaths, "")
64
65 fuzz.binaryDecorator.linkerInit(ctx)
66}
67
68func (fuzz *fuzzBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
69 deps.StaticLibs = append(deps.StaticLibs,
70 config.LibFuzzerRuntimeLibrary(ctx.toolchain()))
71 deps = fuzz.binaryDecorator.linkerDeps(ctx, deps)
72 return deps
73}
74
75func (fuzz *fuzzBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
76 flags = fuzz.binaryDecorator.linkerFlags(ctx, flags)
77 return flags
78}
79
80func (fuzz *fuzzBinary) install(ctx ModuleContext, file android.Path) {
81 fuzz.binaryDecorator.baseInstaller.dir = "fuzz"
82 fuzz.binaryDecorator.baseInstaller.dir64 = "fuzz"
83 fuzz.binaryDecorator.baseInstaller.install(ctx, file)
84}
85
86func NewFuzz(hod android.HostOrDeviceSupported) *Module {
87 module, binary := NewBinary(hod)
88
89 // TODO(mitchp): The toolchain does not currently export the x86 (32-bit)
90 // variant of libFuzzer for host. There is no way to only disable the host
91 // 32-bit variant, so we specify cc_fuzz targets as 64-bit only. This doesn't
92 // hurt anyone, as cc_fuzz is mostly for experimental targets as of this
93 // moment.
94 module.multilib = "64"
95
96 binary.baseInstaller = NewFuzzInstaller()
97 module.sanitize.SetSanitizer(fuzzer, true)
98
99 fuzz := &fuzzBinary{
100 binaryDecorator: binary,
101 baseCompiler: NewBaseCompiler(),
102 }
103 module.compiler = fuzz
104 module.linker = fuzz
105 module.installer = fuzz
Colin Crosseec9b282019-07-18 16:20:52 -0700106
107 // The fuzzer runtime is not present for darwin host modules, disable cc_fuzz modules when targeting darwin.
108 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Alex Light71123ec2019-07-24 13:34:19 -0700109 disableDarwinAndLinuxBionic := struct {
Colin Crosseec9b282019-07-18 16:20:52 -0700110 Target struct {
111 Darwin struct {
112 Enabled *bool
113 }
Alex Light71123ec2019-07-24 13:34:19 -0700114 Linux_bionic struct {
115 Enabled *bool
116 }
Colin Crosseec9b282019-07-18 16:20:52 -0700117 }
118 }{}
Alex Light71123ec2019-07-24 13:34:19 -0700119 disableDarwinAndLinuxBionic.Target.Darwin.Enabled = BoolPtr(false)
120 disableDarwinAndLinuxBionic.Target.Linux_bionic.Enabled = BoolPtr(false)
121 ctx.AppendProperties(&disableDarwinAndLinuxBionic)
Colin Crosseec9b282019-07-18 16:20:52 -0700122 })
123
Mitch Phillipsd0bd16d2019-08-22 15:47:09 -0700124 // Statically link the STL. This allows fuzz target deployment to not have to
125 // include the STL.
126 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
127 staticStlLinkage := struct {
128 Stl *string
129 }{}
130
131 staticStlLinkage.Stl = proptools.StringPtr("libc++_static")
132 ctx.AppendProperties(&staticStlLinkage)
133 })
134
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700135 return module
136}