blob: 3b0c5c83a8597a814a0680481de35619c9a84669 [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"
20)
21
22func init() {
23 android.RegisterModuleType("cc_fuzz", FuzzFactory)
24}
25
26// cc_fuzz creates a host/device fuzzer binary. Host binaries can be found at
27// $ANDROID_HOST_OUT/fuzz/, and device binaries can be found at /data/fuzz on
28// your device, or $ANDROID_PRODUCT_OUT/data/fuzz in your build tree.
29func FuzzFactory() android.Module {
30 module := NewFuzz(android.HostAndDeviceSupported)
31 return module.Init()
32}
33
34func NewFuzzInstaller() *baseInstaller {
35 return NewBaseInstaller("fuzz", "fuzz", InstallInData)
36}
37
38type fuzzBinary struct {
39 *binaryDecorator
40 *baseCompiler
41}
42
43func (fuzz *fuzzBinary) linkerProps() []interface{} {
44 props := fuzz.binaryDecorator.linkerProps()
45 return props
46}
47
48func (fuzz *fuzzBinary) linkerInit(ctx BaseModuleContext) {
49 // Add ../lib[64] to rpath so that out/host/linux-x86/fuzz/<fuzzer> can
50 // find out/host/linux-x86/lib[64]/library.so
51 runpaths := []string{"../lib"}
52 for _, runpath := range runpaths {
53 if ctx.toolchain().Is64Bit() {
54 runpath += "64"
55 }
56 fuzz.binaryDecorator.baseLinker.dynamicProperties.RunPaths = append(
57 fuzz.binaryDecorator.baseLinker.dynamicProperties.RunPaths, runpath)
58 }
59
60 // add "" to rpath so that fuzzer binaries can find libraries in their own fuzz directory
61 fuzz.binaryDecorator.baseLinker.dynamicProperties.RunPaths = append(
62 fuzz.binaryDecorator.baseLinker.dynamicProperties.RunPaths, "")
63
64 fuzz.binaryDecorator.linkerInit(ctx)
65}
66
67func (fuzz *fuzzBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
68 deps.StaticLibs = append(deps.StaticLibs,
69 config.LibFuzzerRuntimeLibrary(ctx.toolchain()))
70 deps = fuzz.binaryDecorator.linkerDeps(ctx, deps)
71 return deps
72}
73
74func (fuzz *fuzzBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
75 flags = fuzz.binaryDecorator.linkerFlags(ctx, flags)
76 return flags
77}
78
79func (fuzz *fuzzBinary) install(ctx ModuleContext, file android.Path) {
80 fuzz.binaryDecorator.baseInstaller.dir = "fuzz"
81 fuzz.binaryDecorator.baseInstaller.dir64 = "fuzz"
82 fuzz.binaryDecorator.baseInstaller.install(ctx, file)
83}
84
85func NewFuzz(hod android.HostOrDeviceSupported) *Module {
86 module, binary := NewBinary(hod)
87
88 // TODO(mitchp): The toolchain does not currently export the x86 (32-bit)
89 // variant of libFuzzer for host. There is no way to only disable the host
90 // 32-bit variant, so we specify cc_fuzz targets as 64-bit only. This doesn't
91 // hurt anyone, as cc_fuzz is mostly for experimental targets as of this
92 // moment.
93 module.multilib = "64"
94
95 binary.baseInstaller = NewFuzzInstaller()
96 module.sanitize.SetSanitizer(fuzzer, true)
97
98 fuzz := &fuzzBinary{
99 binaryDecorator: binary,
100 baseCompiler: NewBaseCompiler(),
101 }
102 module.compiler = fuzz
103 module.linker = fuzz
104 module.installer = fuzz
Colin Crosseec9b282019-07-18 16:20:52 -0700105
106 // The fuzzer runtime is not present for darwin host modules, disable cc_fuzz modules when targeting darwin.
107 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
108 disableDarwin := struct {
109 Target struct {
110 Darwin struct {
111 Enabled *bool
112 }
113 }
114 }{}
115 disableDarwin.Target.Darwin.Enabled = BoolPtr(false)
116 ctx.AppendProperties(&disableDarwin)
117 })
118
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700119 return module
120}