blob: b0fb262df7314b9358f3c575c9f9fab266661b27 [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 (
Mitch Phillips4de896e2019-08-28 16:04:36 -070018 "path/filepath"
19
20 "github.com/google/blueprint/proptools"
21
Mitch Phillipsda9a4632019-07-15 09:34:09 -070022 "android/soong/android"
23 "android/soong/cc/config"
24)
25
26func init() {
27 android.RegisterModuleType("cc_fuzz", FuzzFactory)
28}
29
30// cc_fuzz creates a host/device fuzzer binary. Host binaries can be found at
31// $ANDROID_HOST_OUT/fuzz/, and device binaries can be found at /data/fuzz on
32// your device, or $ANDROID_PRODUCT_OUT/data/fuzz in your build tree.
33func FuzzFactory() android.Module {
34 module := NewFuzz(android.HostAndDeviceSupported)
35 return module.Init()
36}
37
38func NewFuzzInstaller() *baseInstaller {
39 return NewBaseInstaller("fuzz", "fuzz", InstallInData)
40}
41
42type fuzzBinary struct {
43 *binaryDecorator
44 *baseCompiler
45}
46
47func (fuzz *fuzzBinary) linkerProps() []interface{} {
48 props := fuzz.binaryDecorator.linkerProps()
49 return props
50}
51
52func (fuzz *fuzzBinary) linkerInit(ctx BaseModuleContext) {
53 // Add ../lib[64] to rpath so that out/host/linux-x86/fuzz/<fuzzer> can
54 // find out/host/linux-x86/lib[64]/library.so
55 runpaths := []string{"../lib"}
56 for _, runpath := range runpaths {
57 if ctx.toolchain().Is64Bit() {
58 runpath += "64"
59 }
60 fuzz.binaryDecorator.baseLinker.dynamicProperties.RunPaths = append(
61 fuzz.binaryDecorator.baseLinker.dynamicProperties.RunPaths, runpath)
62 }
63
64 // add "" to rpath so that fuzzer binaries can find libraries in their own fuzz directory
65 fuzz.binaryDecorator.baseLinker.dynamicProperties.RunPaths = append(
66 fuzz.binaryDecorator.baseLinker.dynamicProperties.RunPaths, "")
67
68 fuzz.binaryDecorator.linkerInit(ctx)
69}
70
71func (fuzz *fuzzBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
72 deps.StaticLibs = append(deps.StaticLibs,
73 config.LibFuzzerRuntimeLibrary(ctx.toolchain()))
74 deps = fuzz.binaryDecorator.linkerDeps(ctx, deps)
75 return deps
76}
77
78func (fuzz *fuzzBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
79 flags = fuzz.binaryDecorator.linkerFlags(ctx, flags)
80 return flags
81}
82
83func (fuzz *fuzzBinary) install(ctx ModuleContext, file android.Path) {
Mitch Phillips4de896e2019-08-28 16:04:36 -070084 fuzz.binaryDecorator.baseInstaller.dir = filepath.Join("fuzz", ctx.Target().Arch.ArchType.String())
85 fuzz.binaryDecorator.baseInstaller.dir64 = filepath.Join("fuzz", ctx.Target().Arch.ArchType.String())
Mitch Phillipsda9a4632019-07-15 09:34:09 -070086 fuzz.binaryDecorator.baseInstaller.install(ctx, file)
87}
88
89func NewFuzz(hod android.HostOrDeviceSupported) *Module {
90 module, binary := NewBinary(hod)
91
Mitch Phillipsda9a4632019-07-15 09:34:09 -070092 binary.baseInstaller = NewFuzzInstaller()
93 module.sanitize.SetSanitizer(fuzzer, true)
94
95 fuzz := &fuzzBinary{
96 binaryDecorator: binary,
97 baseCompiler: NewBaseCompiler(),
98 }
99 module.compiler = fuzz
100 module.linker = fuzz
101 module.installer = fuzz
Colin Crosseec9b282019-07-18 16:20:52 -0700102
103 // The fuzzer runtime is not present for darwin host modules, disable cc_fuzz modules when targeting darwin.
104 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Alex Light71123ec2019-07-24 13:34:19 -0700105 disableDarwinAndLinuxBionic := struct {
Colin Crosseec9b282019-07-18 16:20:52 -0700106 Target struct {
107 Darwin struct {
108 Enabled *bool
109 }
Alex Light71123ec2019-07-24 13:34:19 -0700110 Linux_bionic struct {
111 Enabled *bool
112 }
Colin Crosseec9b282019-07-18 16:20:52 -0700113 }
114 }{}
Alex Light71123ec2019-07-24 13:34:19 -0700115 disableDarwinAndLinuxBionic.Target.Darwin.Enabled = BoolPtr(false)
116 disableDarwinAndLinuxBionic.Target.Linux_bionic.Enabled = BoolPtr(false)
117 ctx.AppendProperties(&disableDarwinAndLinuxBionic)
Colin Crosseec9b282019-07-18 16:20:52 -0700118 })
119
Mitch Phillipsd0bd16d2019-08-22 15:47:09 -0700120 // Statically link the STL. This allows fuzz target deployment to not have to
121 // include the STL.
122 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
123 staticStlLinkage := struct {
124 Stl *string
125 }{}
126
127 staticStlLinkage.Stl = proptools.StringPtr("libc++_static")
128 ctx.AppendProperties(&staticStlLinkage)
129 })
130
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700131 return module
132}