blob: dfc718e8584e74cdf1e149e90547629b0925d69e [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"
Mitch Phillipse1ee1a12019-10-17 19:20:41 -070019 "sort"
Mitch Phillipsa0a5e192019-09-27 14:00:06 -070020 "strings"
Mitch Phillips4de896e2019-08-28 16:04:36 -070021
Victor Chang00c144f2021-02-09 12:30:33 +000022 "github.com/google/blueprint/proptools"
23
Mitch Phillipsda9a4632019-07-15 09:34:09 -070024 "android/soong/android"
25 "android/soong/cc/config"
hamzehc0a671f2021-07-22 12:05:08 -070026 "android/soong/fuzz"
Mitch Phillipsda9a4632019-07-15 09:34:09 -070027)
28
29func init() {
Cory Barkera1da26f2022-06-07 20:12:06 +000030 android.RegisterModuleType("cc_fuzz", LibFuzzFactory)
Mitch Phillipsd3254b42019-09-24 13:03:28 -070031 android.RegisterSingletonType("cc_fuzz_packaging", fuzzPackagingFactory)
Cory Barkera1da26f2022-06-07 20:12:06 +000032}
33
34type FuzzProperties struct {
Cory Barker9cfcf6d2022-07-22 17:22:02 +000035 FuzzFramework fuzz.Framework `blueprint:"mutated"`
Cory Barkera1da26f2022-06-07 20:12:06 +000036}
37
38type fuzzer struct {
39 Properties FuzzProperties
40}
41
42func (fuzzer *fuzzer) flags(ctx ModuleContext, flags Flags) Flags {
Cory Barker9cfcf6d2022-07-22 17:22:02 +000043 if fuzzer.Properties.FuzzFramework == fuzz.AFL {
44 flags.Local.CFlags = append(flags.Local.CFlags, []string{
45 "-fsanitize-coverage=trace-pc-guard",
46 "-Wno-unused-result",
47 "-Wno-unused-parameter",
48 "-Wno-unused-function",
49 }...)
Cory Barkera1da26f2022-06-07 20:12:06 +000050 }
51
52 return flags
53}
54
55func (fuzzer *fuzzer) props() []interface{} {
56 return []interface{}{&fuzzer.Properties}
57}
58
59func fuzzMutatorDeps(mctx android.TopDownMutatorContext) {
60 currentModule, ok := mctx.Module().(*Module)
61 if !ok {
62 return
63 }
64
Cory Barker9cfcf6d2022-07-22 17:22:02 +000065 if currentModule.fuzzer == nil {
Cory Barkera1da26f2022-06-07 20:12:06 +000066 return
67 }
68
69 mctx.WalkDeps(func(child android.Module, parent android.Module) bool {
70 c, ok := child.(*Module)
71 if !ok {
72 return false
73 }
74
75 if c.sanitize == nil {
76 return false
77 }
78
79 isFuzzerPointer := c.sanitize.getSanitizerBoolPtr(Fuzzer)
80 if isFuzzerPointer == nil || !*isFuzzerPointer {
81 return false
82 }
83
84 if c.fuzzer == nil {
85 return false
86 }
87
Cory Barker9cfcf6d2022-07-22 17:22:02 +000088 c.fuzzer.Properties.FuzzFramework = currentModule.fuzzer.Properties.FuzzFramework
Cory Barkera1da26f2022-06-07 20:12:06 +000089 return true
90 })
91}
92
Mitch Phillipsda9a4632019-07-15 09:34:09 -070093// cc_fuzz creates a host/device fuzzer binary. Host binaries can be found at
94// $ANDROID_HOST_OUT/fuzz/, and device binaries can be found at /data/fuzz on
95// your device, or $ANDROID_PRODUCT_OUT/data/fuzz in your build tree.
Cory Barkera1da26f2022-06-07 20:12:06 +000096func LibFuzzFactory() android.Module {
Cory Barker9cfcf6d2022-07-22 17:22:02 +000097 module := NewFuzzer(android.HostAndDeviceSupported)
Cory Barkera1da26f2022-06-07 20:12:06 +000098 return module.Init()
Mitch Phillipsda9a4632019-07-15 09:34:09 -070099}
100
101type fuzzBinary struct {
102 *binaryDecorator
103 *baseCompiler
Cory Barkera1da26f2022-06-07 20:12:06 +0000104 fuzzPackagedModule fuzz.FuzzPackagedModule
hamzeh41ad8812021-07-07 14:00:07 -0700105 installedSharedDeps []string
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700106}
107
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400108func (fuzz *fuzzBinary) fuzzBinary() bool {
109 return true
110}
111
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700112func (fuzz *fuzzBinary) linkerProps() []interface{} {
113 props := fuzz.binaryDecorator.linkerProps()
hamzeh41ad8812021-07-07 14:00:07 -0700114 props = append(props, &fuzz.fuzzPackagedModule.FuzzProperties)
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000115
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700116 return props
117}
118
119func (fuzz *fuzzBinary) linkerInit(ctx BaseModuleContext) {
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700120 fuzz.binaryDecorator.linkerInit(ctx)
121}
122
Cory Barkera1da26f2022-06-07 20:12:06 +0000123func (fuzzBin *fuzzBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000124 if ctx.Config().Getenv("FUZZ_FRAMEWORK") == "AFL" {
Cory Barkera1da26f2022-06-07 20:12:06 +0000125 deps.HeaderLibs = append(deps.HeaderLibs, "libafl_headers")
Cory Barkera1da26f2022-06-07 20:12:06 +0000126 } else {
127 deps.StaticLibs = append(deps.StaticLibs, config.LibFuzzerRuntimeLibrary(ctx.toolchain()))
Cory Barkera1da26f2022-06-07 20:12:06 +0000128 }
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000129
130 deps = fuzzBin.binaryDecorator.linkerDeps(ctx, deps)
131 return deps
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700132}
133
134func (fuzz *fuzzBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
135 flags = fuzz.binaryDecorator.linkerFlags(ctx, flags)
Mitch Phillips1f7f54f2019-11-14 14:50:47 -0800136 // RunPaths on devices isn't instantiated by the base linker. `../lib` for
137 // installed fuzz targets (both host and device), and `./lib` for fuzz
138 // target packages.
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700139 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/../lib`)
Mitch Phillips1f7f54f2019-11-14 14:50:47 -0800140 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/lib`)
Cory Barkera1da26f2022-06-07 20:12:06 +0000141
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700142 return flags
143}
144
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400145func UnstrippedOutputFile(module android.Module) android.Path {
146 if mod, ok := module.(LinkableInterface); ok {
147 return mod.UnstrippedOutputFile()
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700148 }
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400149 panic("UnstrippedOutputFile called on non-LinkableInterface module: " + module.Name())
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700150}
151
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400152// IsValidSharedDependency takes a module and determines if it is a unique shared library
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700153// that should be installed in the fuzz target output directories. This function
154// returns true, unless:
Victor Chang00c144f2021-02-09 12:30:33 +0000155// - The module is not an installable shared library, or
Kris Alder756ec8d2021-08-27 22:08:29 +0000156// - The module is a header or stub, or
Martin Stjernholm02460ab2020-10-06 02:36:43 +0100157// - The module is a prebuilt and its source is available, or
158// - The module is a versioned member of an SDK snapshot.
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400159func IsValidSharedDependency(dependency android.Module) bool {
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700160 // TODO(b/144090547): We should be parsing these modules using
161 // ModuleDependencyTag instead of the current brute-force checking.
162
Colin Cross31076b32020-10-23 17:22:06 -0700163 linkable, ok := dependency.(LinkableInterface)
164 if !ok || !linkable.CcLibraryInterface() {
165 // Discard non-linkables.
166 return false
167 }
168
169 if !linkable.Shared() {
170 // Discard static libs.
171 return false
172 }
173
Colin Cross31076b32020-10-23 17:22:06 -0700174 if lib := moduleLibraryInterface(dependency); lib != nil && lib.buildStubs() && linkable.CcLibrary() {
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800175 // Discard stubs libs (only CCLibrary variants). Prebuilt libraries should not
176 // be excluded on the basis of they're not CCLibrary()'s.
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700177 return false
178 }
179
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800180 // We discarded module stubs libraries above, but the LLNDK prebuilts stubs
181 // libraries must be handled differently - by looking for the stubDecorator.
182 // Discard LLNDK prebuilts stubs as well.
183 if ccLibrary, isCcLibrary := dependency.(*Module); isCcLibrary {
184 if _, isLLndkStubLibrary := ccLibrary.linker.(*stubDecorator); isLLndkStubLibrary {
185 return false
186 }
Victor Chang00c144f2021-02-09 12:30:33 +0000187 // Discard installable:false libraries because they are expected to be absent
188 // in runtime.
Colin Cross1bc94122021-10-28 13:25:54 -0700189 if !proptools.BoolDefault(ccLibrary.Installable(), true) {
Victor Chang00c144f2021-02-09 12:30:33 +0000190 return false
191 }
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800192 }
193
Martin Stjernholm02460ab2020-10-06 02:36:43 +0100194 // If the same library is present both as source and a prebuilt we must pick
195 // only one to avoid a conflict. Always prefer the source since the prebuilt
196 // probably won't be built with sanitizers enabled.
Paul Duffinf7c99f52021-04-28 10:41:21 +0100197 if prebuilt := android.GetEmbeddedPrebuilt(dependency); prebuilt != nil && prebuilt.SourceExists() {
Martin Stjernholm02460ab2020-10-06 02:36:43 +0100198 return false
199 }
200
201 // Discard versioned members of SDK snapshots, because they will conflict with
202 // unversioned ones.
203 if sdkMember, ok := dependency.(android.SdkAware); ok && !sdkMember.ContainingSdk().Unversioned() {
204 return false
205 }
206
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700207 return true
208}
209
210func sharedLibraryInstallLocation(
Cory Barkera1da26f2022-06-07 20:12:06 +0000211 libraryPath android.Path, isHost bool, fuzzDir string, archString string) string {
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700212 installLocation := "$(PRODUCT_OUT)/data"
213 if isHost {
214 installLocation = "$(HOST_OUT)"
215 }
216 installLocation = filepath.Join(
Cory Barkera1da26f2022-06-07 20:12:06 +0000217 installLocation, fuzzDir, archString, "lib", libraryPath.Base())
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700218 return installLocation
219}
220
Mitch Phillips0bf97132020-03-06 09:38:12 -0800221// Get the device-only shared library symbols install directory.
Cory Barkera1da26f2022-06-07 20:12:06 +0000222func sharedLibrarySymbolsInstallLocation(libraryPath android.Path, fuzzDir string, archString string) string {
223 return filepath.Join("$(PRODUCT_OUT)/symbols/data/", fuzzDir, archString, "/lib/", libraryPath.Base())
Mitch Phillips0bf97132020-03-06 09:38:12 -0800224}
225
Cory Barkera1da26f2022-06-07 20:12:06 +0000226func (fuzzBin *fuzzBinary) install(ctx ModuleContext, file android.Path) {
227 installBase := "fuzz"
Mitch Phillips4e4ab8a2019-09-13 17:32:50 -0700228
Cory Barkera1da26f2022-06-07 20:12:06 +0000229 fuzzBin.binaryDecorator.baseInstaller.dir = filepath.Join(
230 installBase, ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
231 fuzzBin.binaryDecorator.baseInstaller.dir64 = filepath.Join(
232 installBase, ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
233 fuzzBin.binaryDecorator.baseInstaller.install(ctx, file)
234
235 fuzzBin.fuzzPackagedModule.Corpus = android.PathsForModuleSrc(ctx, fuzzBin.fuzzPackagedModule.FuzzProperties.Corpus)
Colin Crossf1a035e2020-11-16 17:32:30 -0800236 builder := android.NewRuleBuilder(pctx, ctx)
Mitch Phillips8a2bc0b2019-10-17 15:04:01 -0700237 intermediateDir := android.PathForModuleOut(ctx, "corpus")
Cory Barkera1da26f2022-06-07 20:12:06 +0000238 for _, entry := range fuzzBin.fuzzPackagedModule.Corpus {
Mitch Phillips8a2bc0b2019-10-17 15:04:01 -0700239 builder.Command().Text("cp").
240 Input(entry).
241 Output(intermediateDir.Join(ctx, entry.Base()))
242 }
Colin Crossf1a035e2020-11-16 17:32:30 -0800243 builder.Build("copy_corpus", "copy corpus")
Cory Barkera1da26f2022-06-07 20:12:06 +0000244 fuzzBin.fuzzPackagedModule.CorpusIntermediateDir = intermediateDir
Mitch Phillips8a2bc0b2019-10-17 15:04:01 -0700245
Cory Barkera1da26f2022-06-07 20:12:06 +0000246 fuzzBin.fuzzPackagedModule.Data = android.PathsForModuleSrc(ctx, fuzzBin.fuzzPackagedModule.FuzzProperties.Data)
Colin Crossf1a035e2020-11-16 17:32:30 -0800247 builder = android.NewRuleBuilder(pctx, ctx)
Tri Voad172d82019-11-27 13:45:45 -0800248 intermediateDir = android.PathForModuleOut(ctx, "data")
Cory Barkera1da26f2022-06-07 20:12:06 +0000249 for _, entry := range fuzzBin.fuzzPackagedModule.Data {
Tri Voad172d82019-11-27 13:45:45 -0800250 builder.Command().Text("cp").
251 Input(entry).
252 Output(intermediateDir.Join(ctx, entry.Rel()))
253 }
Colin Crossf1a035e2020-11-16 17:32:30 -0800254 builder.Build("copy_data", "copy data")
Cory Barkera1da26f2022-06-07 20:12:06 +0000255 fuzzBin.fuzzPackagedModule.DataIntermediateDir = intermediateDir
Tri Voad172d82019-11-27 13:45:45 -0800256
Cory Barkera1da26f2022-06-07 20:12:06 +0000257 if fuzzBin.fuzzPackagedModule.FuzzProperties.Dictionary != nil {
258 fuzzBin.fuzzPackagedModule.Dictionary = android.PathForModuleSrc(ctx, *fuzzBin.fuzzPackagedModule.FuzzProperties.Dictionary)
259 if fuzzBin.fuzzPackagedModule.Dictionary.Ext() != ".dict" {
Mitch Phillips4e4ab8a2019-09-13 17:32:50 -0700260 ctx.PropertyErrorf("dictionary",
261 "Fuzzer dictionary %q does not have '.dict' extension",
Cory Barkera1da26f2022-06-07 20:12:06 +0000262 fuzzBin.fuzzPackagedModule.Dictionary.String())
Mitch Phillips4e4ab8a2019-09-13 17:32:50 -0700263 }
264 }
Kris Alderf979ee32019-10-22 10:52:01 -0700265
Cory Barkera1da26f2022-06-07 20:12:06 +0000266 if fuzzBin.fuzzPackagedModule.FuzzProperties.Fuzz_config != nil {
Kris Alderdb97af42019-10-30 10:17:04 -0700267 configPath := android.PathForModuleOut(ctx, "config").Join(ctx, "config.json")
Cory Barkera1da26f2022-06-07 20:12:06 +0000268 android.WriteFileRule(ctx, configPath, fuzzBin.fuzzPackagedModule.FuzzProperties.Fuzz_config.String())
269 fuzzBin.fuzzPackagedModule.Config = configPath
Kris Alderf979ee32019-10-22 10:52:01 -0700270 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700271
272 // Grab the list of required shared libraries.
Mitch Phillipsc0b442f2020-04-27 16:44:58 -0700273 seen := make(map[string]bool)
Colin Crossdc809f92019-11-20 15:58:32 -0800274 var sharedLibraries android.Paths
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700275 ctx.WalkDeps(func(child, parent android.Module) bool {
Mitch Phillipsc0b442f2020-04-27 16:44:58 -0700276 if seen[child.Name()] {
Colin Crossdc809f92019-11-20 15:58:32 -0800277 return false
278 }
Mitch Phillipsc0b442f2020-04-27 16:44:58 -0700279 seen[child.Name()] = true
Colin Crossdc809f92019-11-20 15:58:32 -0800280
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400281 if IsValidSharedDependency(child) {
Colin Crossdc809f92019-11-20 15:58:32 -0800282 sharedLibraries = append(sharedLibraries, child.(*Module).UnstrippedOutputFile())
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700283 return true
284 }
285 return false
286 })
287
288 for _, lib := range sharedLibraries {
Cory Barkera1da26f2022-06-07 20:12:06 +0000289 fuzzBin.installedSharedDeps = append(fuzzBin.installedSharedDeps,
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700290 sharedLibraryInstallLocation(
Cory Barkera1da26f2022-06-07 20:12:06 +0000291 lib, ctx.Host(), installBase, ctx.Arch().ArchType.String()))
Mitch Phillips0bf97132020-03-06 09:38:12 -0800292
293 // Also add the dependency on the shared library symbols dir.
294 if !ctx.Host() {
Cory Barkera1da26f2022-06-07 20:12:06 +0000295 fuzzBin.installedSharedDeps = append(fuzzBin.installedSharedDeps,
296 sharedLibrarySymbolsInstallLocation(lib, installBase, ctx.Arch().ArchType.String()))
Mitch Phillips0bf97132020-03-06 09:38:12 -0800297 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700298 }
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700299}
300
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000301func NewFuzzer(hod android.HostOrDeviceSupported) *Module {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400302 module, binary := newBinary(hod, false)
Cory Barkera1da26f2022-06-07 20:12:06 +0000303 baseInstallerPath := "fuzz"
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700304
Cory Barkera1da26f2022-06-07 20:12:06 +0000305 binary.baseInstaller = NewBaseInstaller(baseInstallerPath, baseInstallerPath, InstallInData)
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500306 module.sanitize.SetSanitizer(Fuzzer, true)
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700307
Cory Barkera1da26f2022-06-07 20:12:06 +0000308 fuzzBin := &fuzzBinary{
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700309 binaryDecorator: binary,
310 baseCompiler: NewBaseCompiler(),
311 }
Cory Barkera1da26f2022-06-07 20:12:06 +0000312 module.compiler = fuzzBin
313 module.linker = fuzzBin
314 module.installer = fuzzBin
Colin Crosseec9b282019-07-18 16:20:52 -0700315
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000316 module.fuzzer.Properties.FuzzFramework = fuzz.LibFuzzer
317
Colin Crosseec9b282019-07-18 16:20:52 -0700318 // The fuzzer runtime is not present for darwin host modules, disable cc_fuzz modules when targeting darwin.
319 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Alex Light71123ec2019-07-24 13:34:19 -0700320 disableDarwinAndLinuxBionic := struct {
Colin Crosseec9b282019-07-18 16:20:52 -0700321 Target struct {
322 Darwin struct {
323 Enabled *bool
324 }
Alex Light71123ec2019-07-24 13:34:19 -0700325 Linux_bionic struct {
326 Enabled *bool
327 }
Colin Crosseec9b282019-07-18 16:20:52 -0700328 }
329 }{}
Alex Light71123ec2019-07-24 13:34:19 -0700330 disableDarwinAndLinuxBionic.Target.Darwin.Enabled = BoolPtr(false)
331 disableDarwinAndLinuxBionic.Target.Linux_bionic.Enabled = BoolPtr(false)
332 ctx.AppendProperties(&disableDarwinAndLinuxBionic)
Cory Barkera1da26f2022-06-07 20:12:06 +0000333
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000334 targetFramework := fuzz.GetFramework(ctx, fuzz.Cc)
335 if !fuzz.IsValidFrameworkForModule(targetFramework, fuzz.Cc, fuzzBin.fuzzPackagedModule.FuzzProperties.Fuzzing_frameworks) {
336 ctx.Module().Disable()
337 return
338 }
339
340 if targetFramework == fuzz.AFL {
341 fuzzBin.baseCompiler.Properties.Srcs = append(fuzzBin.baseCompiler.Properties.Srcs, ":aflpp_driver", ":afl-compiler-rt")
342 module.fuzzer.Properties.FuzzFramework = fuzz.AFL
343 }
344 })
Cory Barker74aea6c2022-08-08 15:55:12 +0000345
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700346 return module
347}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700348
349// Responsible for generating GNU Make rules that package fuzz targets into
350// their architecture & target/host specific zip file.
hamzeh41ad8812021-07-07 14:00:07 -0700351type ccFuzzPackager struct {
hamzehc0a671f2021-07-22 12:05:08 -0700352 fuzz.FuzzPackager
Cory Barkera1da26f2022-06-07 20:12:06 +0000353 fuzzPackagingArchModules string
354 fuzzTargetSharedDepsInstallPairs string
355 allFuzzTargetsName string
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700356}
357
358func fuzzPackagingFactory() android.Singleton {
Cory Barkera1da26f2022-06-07 20:12:06 +0000359
360 fuzzPackager := &ccFuzzPackager{
361 fuzzPackagingArchModules: "SOONG_FUZZ_PACKAGING_ARCH_MODULES",
362 fuzzTargetSharedDepsInstallPairs: "FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS",
363 allFuzzTargetsName: "ALL_FUZZ_TARGETS",
364 }
Cory Barkera1da26f2022-06-07 20:12:06 +0000365 return fuzzPackager
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700366}
367
hamzeh41ad8812021-07-07 14:00:07 -0700368func (s *ccFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700369 // Map between each architecture + host/device combination, and the files that
370 // need to be packaged (in the tuple of {source file, destination folder in
371 // archive}).
hamzehc0a671f2021-07-22 12:05:08 -0700372 archDirs := make(map[fuzz.ArchOs][]fuzz.FileToZip)
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700373
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700374 // List of individual fuzz targets, so that 'make fuzz' also installs the targets
375 // to the correct output directories as well.
hamzeh41ad8812021-07-07 14:00:07 -0700376 s.FuzzTargets = make(map[string]bool)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700377
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400378 // Map tracking whether each shared library has an install rule to avoid duplicate install rules from
379 // multiple fuzzers that depend on the same shared library.
380 sharedLibraryInstalled := make(map[string]bool)
381
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700382 ctx.VisitAllModules(func(module android.Module) {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700383 ccModule, ok := module.(*Module)
hamzeh41ad8812021-07-07 14:00:07 -0700384 if !ok || ccModule.Properties.PreventInstall {
385 return
386 }
387
388 // Discard non-fuzz targets.
hamzehc0a671f2021-07-22 12:05:08 -0700389 if ok := fuzz.IsValid(ccModule.FuzzModule); !ok {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700390 return
391 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700392
Cory Barkera1da26f2022-06-07 20:12:06 +0000393 sharedLibsInstallDirPrefix := "lib"
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700394 fuzzModule, ok := ccModule.compiler.(*fuzzBinary)
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000395 if !ok {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700396 return
397 }
398
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700399 hostOrTargetString := "target"
400 if ccModule.Host() {
401 hostOrTargetString = "host"
402 }
403
Cory Barkera1da26f2022-06-07 20:12:06 +0000404 fpm := fuzz.FuzzPackagedModule{}
405 if ok {
406 fpm = fuzzModule.fuzzPackagedModule
407 }
408
409 intermediatePath := "fuzz"
Cory Barkera1da26f2022-06-07 20:12:06 +0000410
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700411 archString := ccModule.Arch().ArchType.String()
Cory Barkera1da26f2022-06-07 20:12:06 +0000412 archDir := android.PathForIntermediates(ctx, intermediatePath, hostOrTargetString, archString)
hamzehc0a671f2021-07-22 12:05:08 -0700413 archOs := fuzz.ArchOs{HostOrTarget: hostOrTargetString, Arch: archString, Dir: archDir.String()}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700414
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700415 // Grab the list of required shared libraries.
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400416 sharedLibraries := fuzz.CollectAllSharedDependencies(ctx, module, UnstrippedOutputFile, IsValidSharedDependency)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700417
hamzehc0a671f2021-07-22 12:05:08 -0700418 var files []fuzz.FileToZip
Colin Crossf1a035e2020-11-16 17:32:30 -0800419 builder := android.NewRuleBuilder(pctx, ctx)
Mitch Phillips2edbe8e2019-11-13 08:36:07 -0800420
hamzeh41ad8812021-07-07 14:00:07 -0700421 // Package the corpus, data, dict and config into a zipfile.
Cory Barkera1da26f2022-06-07 20:12:06 +0000422 files = s.PackageArtifacts(ctx, module, fpm, archDir, builder)
Tri Voad172d82019-11-27 13:45:45 -0800423
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400424 // Package shared libraries
Cory Barkera1da26f2022-06-07 20:12:06 +0000425 files = append(files, GetSharedLibsToZip(sharedLibraries, ccModule, &s.FuzzPackager, archString, sharedLibsInstallDirPrefix, &sharedLibraryInstalled)...)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700426
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700427 // The executable.
hamzehc0a671f2021-07-22 12:05:08 -0700428 files = append(files, fuzz.FileToZip{ccModule.UnstrippedOutputFile(), ""})
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700429
Cory Barkera1da26f2022-06-07 20:12:06 +0000430 archDirs[archOs], ok = s.BuildZipFile(ctx, module, fpm, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs)
hamzeh41ad8812021-07-07 14:00:07 -0700431 if !ok {
432 return
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700433 }
434 })
435
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000436 s.CreateFuzzPackage(ctx, archDirs, fuzz.Cc, pctx)
Mitch Phillipsa0a5e192019-09-27 14:00:06 -0700437}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700438
hamzeh41ad8812021-07-07 14:00:07 -0700439func (s *ccFuzzPackager) MakeVars(ctx android.MakeVarsContext) {
440 packages := s.Packages.Strings()
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700441 sort.Strings(packages)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400442 sort.Strings(s.FuzzPackager.SharedLibInstallStrings)
Mitch Phillipsa0a5e192019-09-27 14:00:06 -0700443 // TODO(mitchp): Migrate this to use MakeVarsContext::DistForGoal() when it's
444 // ready to handle phony targets created in Soong. In the meantime, this
445 // exports the phony 'fuzz' target and dependencies on packages to
446 // core/main.mk so that we can use dist-for-goals.
Cory Barkera1da26f2022-06-07 20:12:06 +0000447
448 ctx.Strict(s.fuzzPackagingArchModules, strings.Join(packages, " "))
449
450 ctx.Strict(s.fuzzTargetSharedDepsInstallPairs,
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400451 strings.Join(s.FuzzPackager.SharedLibInstallStrings, " "))
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700452
453 // Preallocate the slice of fuzz targets to minimise memory allocations.
Cory Barkera1da26f2022-06-07 20:12:06 +0000454 s.PreallocateSlice(ctx, s.allFuzzTargetsName)
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700455}
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400456
457// GetSharedLibsToZip finds and marks all the transiently-dependent shared libraries for
458// packaging.
Cory Barkera1da26f2022-06-07 20:12:06 +0000459func GetSharedLibsToZip(sharedLibraries android.Paths, module LinkableInterface, s *fuzz.FuzzPackager, archString string, destinationPathPrefix string, sharedLibraryInstalled *map[string]bool) []fuzz.FileToZip {
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400460 var files []fuzz.FileToZip
461
Cory Barkera1da26f2022-06-07 20:12:06 +0000462 fuzzDir := "fuzz"
Cory Barkera1da26f2022-06-07 20:12:06 +0000463
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400464 for _, library := range sharedLibraries {
Cory Barkera1da26f2022-06-07 20:12:06 +0000465 files = append(files, fuzz.FileToZip{library, destinationPathPrefix})
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400466
467 // For each architecture-specific shared library dependency, we need to
468 // install it to the output directory. Setup the install destination here,
469 // which will be used by $(copy-many-files) in the Make backend.
470 installDestination := sharedLibraryInstallLocation(
Cory Barkera1da26f2022-06-07 20:12:06 +0000471 library, module.Host(), fuzzDir, archString)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400472 if (*sharedLibraryInstalled)[installDestination] {
473 continue
474 }
475 (*sharedLibraryInstalled)[installDestination] = true
476
477 // Escape all the variables, as the install destination here will be called
478 // via. $(eval) in Make.
479 installDestination = strings.ReplaceAll(
480 installDestination, "$", "$$")
481 s.SharedLibInstallStrings = append(s.SharedLibInstallStrings,
482 library.String()+":"+installDestination)
483
484 // Ensure that on device, the library is also reinstalled to the /symbols/
485 // dir. Symbolized DSO's are always installed to the device when fuzzing, but
486 // we want symbolization tools (like `stack`) to be able to find the symbols
487 // in $ANDROID_PRODUCT_OUT/symbols automagically.
488 if !module.Host() {
Cory Barkera1da26f2022-06-07 20:12:06 +0000489 symbolsInstallDestination := sharedLibrarySymbolsInstallLocation(library, fuzzDir, archString)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400490 symbolsInstallDestination = strings.ReplaceAll(symbolsInstallDestination, "$", "$$")
491 s.SharedLibInstallStrings = append(s.SharedLibInstallStrings,
492 library.String()+":"+symbolsInstallDestination)
493 }
494 }
495 return files
496}