blob: 13c94adb33e7c2a728ea309854d404876d0eed0a [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
Colin Cross31d89b42022-10-04 16:35:39 -0700106 sharedLibraries android.Paths
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700107}
108
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400109func (fuzz *fuzzBinary) fuzzBinary() bool {
110 return true
111}
112
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700113func (fuzz *fuzzBinary) linkerProps() []interface{} {
114 props := fuzz.binaryDecorator.linkerProps()
hamzeh41ad8812021-07-07 14:00:07 -0700115 props = append(props, &fuzz.fuzzPackagedModule.FuzzProperties)
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000116
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700117 return props
118}
119
120func (fuzz *fuzzBinary) linkerInit(ctx BaseModuleContext) {
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700121 fuzz.binaryDecorator.linkerInit(ctx)
122}
123
Cory Barkera1da26f2022-06-07 20:12:06 +0000124func (fuzzBin *fuzzBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000125 if ctx.Config().Getenv("FUZZ_FRAMEWORK") == "AFL" {
Cory Barkera1da26f2022-06-07 20:12:06 +0000126 deps.HeaderLibs = append(deps.HeaderLibs, "libafl_headers")
Cory Barkera1da26f2022-06-07 20:12:06 +0000127 } else {
128 deps.StaticLibs = append(deps.StaticLibs, config.LibFuzzerRuntimeLibrary(ctx.toolchain()))
Cory Barkera1da26f2022-06-07 20:12:06 +0000129 }
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000130
131 deps = fuzzBin.binaryDecorator.linkerDeps(ctx, deps)
132 return deps
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700133}
134
135func (fuzz *fuzzBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
136 flags = fuzz.binaryDecorator.linkerFlags(ctx, flags)
Mitch Phillips1f7f54f2019-11-14 14:50:47 -0800137 // RunPaths on devices isn't instantiated by the base linker. `../lib` for
138 // installed fuzz targets (both host and device), and `./lib` for fuzz
139 // target packages.
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700140 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/../lib`)
Mitch Phillips1f7f54f2019-11-14 14:50:47 -0800141 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/lib`)
Cory Barkera1da26f2022-06-07 20:12:06 +0000142
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700143 return flags
144}
145
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400146// IsValidSharedDependency takes a module and determines if it is a unique shared library
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700147// that should be installed in the fuzz target output directories. This function
148// returns true, unless:
Colin Crossd079e0b2022-08-16 10:27:33 -0700149// - The module is not an installable shared library, or
150// - The module is a header or stub, or
151// - The module is a prebuilt and its source is available, or
152// - The module is a versioned member of an SDK snapshot.
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400153func IsValidSharedDependency(dependency android.Module) bool {
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700154 // TODO(b/144090547): We should be parsing these modules using
155 // ModuleDependencyTag instead of the current brute-force checking.
156
Colin Cross31076b32020-10-23 17:22:06 -0700157 linkable, ok := dependency.(LinkableInterface)
158 if !ok || !linkable.CcLibraryInterface() {
159 // Discard non-linkables.
160 return false
161 }
162
163 if !linkable.Shared() {
164 // Discard static libs.
165 return false
166 }
167
Colin Cross31076b32020-10-23 17:22:06 -0700168 if lib := moduleLibraryInterface(dependency); lib != nil && lib.buildStubs() && linkable.CcLibrary() {
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800169 // Discard stubs libs (only CCLibrary variants). Prebuilt libraries should not
170 // be excluded on the basis of they're not CCLibrary()'s.
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700171 return false
172 }
173
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800174 // We discarded module stubs libraries above, but the LLNDK prebuilts stubs
175 // libraries must be handled differently - by looking for the stubDecorator.
176 // Discard LLNDK prebuilts stubs as well.
177 if ccLibrary, isCcLibrary := dependency.(*Module); isCcLibrary {
178 if _, isLLndkStubLibrary := ccLibrary.linker.(*stubDecorator); isLLndkStubLibrary {
179 return false
180 }
Victor Chang00c144f2021-02-09 12:30:33 +0000181 // Discard installable:false libraries because they are expected to be absent
182 // in runtime.
Colin Cross1bc94122021-10-28 13:25:54 -0700183 if !proptools.BoolDefault(ccLibrary.Installable(), true) {
Victor Chang00c144f2021-02-09 12:30:33 +0000184 return false
185 }
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800186 }
187
Martin Stjernholm02460ab2020-10-06 02:36:43 +0100188 // If the same library is present both as source and a prebuilt we must pick
189 // only one to avoid a conflict. Always prefer the source since the prebuilt
190 // probably won't be built with sanitizers enabled.
Paul Duffinf7c99f52021-04-28 10:41:21 +0100191 if prebuilt := android.GetEmbeddedPrebuilt(dependency); prebuilt != nil && prebuilt.SourceExists() {
Martin Stjernholm02460ab2020-10-06 02:36:43 +0100192 return false
193 }
194
195 // Discard versioned members of SDK snapshots, because they will conflict with
196 // unversioned ones.
197 if sdkMember, ok := dependency.(android.SdkAware); ok && !sdkMember.ContainingSdk().Unversioned() {
198 return false
199 }
200
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700201 return true
202}
203
204func sharedLibraryInstallLocation(
Cory Barkera1da26f2022-06-07 20:12:06 +0000205 libraryPath android.Path, isHost bool, fuzzDir string, archString string) string {
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700206 installLocation := "$(PRODUCT_OUT)/data"
207 if isHost {
208 installLocation = "$(HOST_OUT)"
209 }
210 installLocation = filepath.Join(
Cory Barkera1da26f2022-06-07 20:12:06 +0000211 installLocation, fuzzDir, archString, "lib", libraryPath.Base())
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700212 return installLocation
213}
214
Mitch Phillips0bf97132020-03-06 09:38:12 -0800215// Get the device-only shared library symbols install directory.
Cory Barkera1da26f2022-06-07 20:12:06 +0000216func sharedLibrarySymbolsInstallLocation(libraryPath android.Path, fuzzDir string, archString string) string {
217 return filepath.Join("$(PRODUCT_OUT)/symbols/data/", fuzzDir, archString, "/lib/", libraryPath.Base())
Mitch Phillips0bf97132020-03-06 09:38:12 -0800218}
219
Cory Barkera1da26f2022-06-07 20:12:06 +0000220func (fuzzBin *fuzzBinary) install(ctx ModuleContext, file android.Path) {
221 installBase := "fuzz"
Mitch Phillips4e4ab8a2019-09-13 17:32:50 -0700222
Cory Barkera1da26f2022-06-07 20:12:06 +0000223 fuzzBin.binaryDecorator.baseInstaller.dir = filepath.Join(
224 installBase, ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
225 fuzzBin.binaryDecorator.baseInstaller.dir64 = filepath.Join(
226 installBase, ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
227 fuzzBin.binaryDecorator.baseInstaller.install(ctx, file)
228
229 fuzzBin.fuzzPackagedModule.Corpus = android.PathsForModuleSrc(ctx, fuzzBin.fuzzPackagedModule.FuzzProperties.Corpus)
Colin Crossf1a035e2020-11-16 17:32:30 -0800230 builder := android.NewRuleBuilder(pctx, ctx)
Mitch Phillips8a2bc0b2019-10-17 15:04:01 -0700231 intermediateDir := android.PathForModuleOut(ctx, "corpus")
Cory Barkera1da26f2022-06-07 20:12:06 +0000232 for _, entry := range fuzzBin.fuzzPackagedModule.Corpus {
Mitch Phillips8a2bc0b2019-10-17 15:04:01 -0700233 builder.Command().Text("cp").
234 Input(entry).
235 Output(intermediateDir.Join(ctx, entry.Base()))
236 }
Colin Crossf1a035e2020-11-16 17:32:30 -0800237 builder.Build("copy_corpus", "copy corpus")
Cory Barkera1da26f2022-06-07 20:12:06 +0000238 fuzzBin.fuzzPackagedModule.CorpusIntermediateDir = intermediateDir
Mitch Phillips8a2bc0b2019-10-17 15:04:01 -0700239
Cory Barkera1da26f2022-06-07 20:12:06 +0000240 fuzzBin.fuzzPackagedModule.Data = android.PathsForModuleSrc(ctx, fuzzBin.fuzzPackagedModule.FuzzProperties.Data)
Colin Crossf1a035e2020-11-16 17:32:30 -0800241 builder = android.NewRuleBuilder(pctx, ctx)
Tri Voad172d82019-11-27 13:45:45 -0800242 intermediateDir = android.PathForModuleOut(ctx, "data")
Cory Barkera1da26f2022-06-07 20:12:06 +0000243 for _, entry := range fuzzBin.fuzzPackagedModule.Data {
Tri Voad172d82019-11-27 13:45:45 -0800244 builder.Command().Text("cp").
245 Input(entry).
246 Output(intermediateDir.Join(ctx, entry.Rel()))
247 }
Colin Crossf1a035e2020-11-16 17:32:30 -0800248 builder.Build("copy_data", "copy data")
Cory Barkera1da26f2022-06-07 20:12:06 +0000249 fuzzBin.fuzzPackagedModule.DataIntermediateDir = intermediateDir
Tri Voad172d82019-11-27 13:45:45 -0800250
Cory Barkera1da26f2022-06-07 20:12:06 +0000251 if fuzzBin.fuzzPackagedModule.FuzzProperties.Dictionary != nil {
252 fuzzBin.fuzzPackagedModule.Dictionary = android.PathForModuleSrc(ctx, *fuzzBin.fuzzPackagedModule.FuzzProperties.Dictionary)
253 if fuzzBin.fuzzPackagedModule.Dictionary.Ext() != ".dict" {
Mitch Phillips4e4ab8a2019-09-13 17:32:50 -0700254 ctx.PropertyErrorf("dictionary",
255 "Fuzzer dictionary %q does not have '.dict' extension",
Cory Barkera1da26f2022-06-07 20:12:06 +0000256 fuzzBin.fuzzPackagedModule.Dictionary.String())
Mitch Phillips4e4ab8a2019-09-13 17:32:50 -0700257 }
258 }
Kris Alderf979ee32019-10-22 10:52:01 -0700259
Cory Barkera1da26f2022-06-07 20:12:06 +0000260 if fuzzBin.fuzzPackagedModule.FuzzProperties.Fuzz_config != nil {
Kris Alderdb97af42019-10-30 10:17:04 -0700261 configPath := android.PathForModuleOut(ctx, "config").Join(ctx, "config.json")
Cory Barkera1da26f2022-06-07 20:12:06 +0000262 android.WriteFileRule(ctx, configPath, fuzzBin.fuzzPackagedModule.FuzzProperties.Fuzz_config.String())
263 fuzzBin.fuzzPackagedModule.Config = configPath
Kris Alderf979ee32019-10-22 10:52:01 -0700264 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700265
266 // Grab the list of required shared libraries.
Colin Cross31d89b42022-10-04 16:35:39 -0700267 fuzzBin.sharedLibraries = CollectAllSharedDependencies(ctx)
Colin Crossdc809f92019-11-20 15:58:32 -0800268
Colin Cross31d89b42022-10-04 16:35:39 -0700269 for _, lib := range fuzzBin.sharedLibraries {
Cory Barkera1da26f2022-06-07 20:12:06 +0000270 fuzzBin.installedSharedDeps = append(fuzzBin.installedSharedDeps,
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700271 sharedLibraryInstallLocation(
Cory Barkera1da26f2022-06-07 20:12:06 +0000272 lib, ctx.Host(), installBase, ctx.Arch().ArchType.String()))
Mitch Phillips0bf97132020-03-06 09:38:12 -0800273
274 // Also add the dependency on the shared library symbols dir.
275 if !ctx.Host() {
Cory Barkera1da26f2022-06-07 20:12:06 +0000276 fuzzBin.installedSharedDeps = append(fuzzBin.installedSharedDeps,
277 sharedLibrarySymbolsInstallLocation(lib, installBase, ctx.Arch().ArchType.String()))
Mitch Phillips0bf97132020-03-06 09:38:12 -0800278 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700279 }
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700280}
281
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000282func NewFuzzer(hod android.HostOrDeviceSupported) *Module {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400283 module, binary := newBinary(hod, false)
Cory Barkera1da26f2022-06-07 20:12:06 +0000284 baseInstallerPath := "fuzz"
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700285
Cory Barkera1da26f2022-06-07 20:12:06 +0000286 binary.baseInstaller = NewBaseInstaller(baseInstallerPath, baseInstallerPath, InstallInData)
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500287 module.sanitize.SetSanitizer(Fuzzer, true)
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700288
Cory Barkera1da26f2022-06-07 20:12:06 +0000289 fuzzBin := &fuzzBinary{
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700290 binaryDecorator: binary,
291 baseCompiler: NewBaseCompiler(),
292 }
Cory Barkera1da26f2022-06-07 20:12:06 +0000293 module.compiler = fuzzBin
294 module.linker = fuzzBin
295 module.installer = fuzzBin
Colin Crosseec9b282019-07-18 16:20:52 -0700296
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000297 module.fuzzer.Properties.FuzzFramework = fuzz.LibFuzzer
298
Colin Crosseec9b282019-07-18 16:20:52 -0700299 // The fuzzer runtime is not present for darwin host modules, disable cc_fuzz modules when targeting darwin.
300 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Alex Light71123ec2019-07-24 13:34:19 -0700301 disableDarwinAndLinuxBionic := struct {
Colin Crosseec9b282019-07-18 16:20:52 -0700302 Target struct {
303 Darwin struct {
304 Enabled *bool
305 }
Alex Light71123ec2019-07-24 13:34:19 -0700306 Linux_bionic struct {
307 Enabled *bool
308 }
Colin Crosseec9b282019-07-18 16:20:52 -0700309 }
310 }{}
Alex Light71123ec2019-07-24 13:34:19 -0700311 disableDarwinAndLinuxBionic.Target.Darwin.Enabled = BoolPtr(false)
312 disableDarwinAndLinuxBionic.Target.Linux_bionic.Enabled = BoolPtr(false)
313 ctx.AppendProperties(&disableDarwinAndLinuxBionic)
Cory Barkera1da26f2022-06-07 20:12:06 +0000314
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000315 targetFramework := fuzz.GetFramework(ctx, fuzz.Cc)
316 if !fuzz.IsValidFrameworkForModule(targetFramework, fuzz.Cc, fuzzBin.fuzzPackagedModule.FuzzProperties.Fuzzing_frameworks) {
317 ctx.Module().Disable()
318 return
319 }
320
321 if targetFramework == fuzz.AFL {
322 fuzzBin.baseCompiler.Properties.Srcs = append(fuzzBin.baseCompiler.Properties.Srcs, ":aflpp_driver", ":afl-compiler-rt")
323 module.fuzzer.Properties.FuzzFramework = fuzz.AFL
324 }
325 })
Cory Barker74aea6c2022-08-08 15:55:12 +0000326
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700327 return module
328}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700329
330// Responsible for generating GNU Make rules that package fuzz targets into
331// their architecture & target/host specific zip file.
hamzeh41ad8812021-07-07 14:00:07 -0700332type ccFuzzPackager struct {
hamzehc0a671f2021-07-22 12:05:08 -0700333 fuzz.FuzzPackager
Cory Barkera1da26f2022-06-07 20:12:06 +0000334 fuzzPackagingArchModules string
335 fuzzTargetSharedDepsInstallPairs string
336 allFuzzTargetsName string
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700337}
338
339func fuzzPackagingFactory() android.Singleton {
Cory Barkera1da26f2022-06-07 20:12:06 +0000340
341 fuzzPackager := &ccFuzzPackager{
342 fuzzPackagingArchModules: "SOONG_FUZZ_PACKAGING_ARCH_MODULES",
343 fuzzTargetSharedDepsInstallPairs: "FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS",
344 allFuzzTargetsName: "ALL_FUZZ_TARGETS",
345 }
Cory Barkera1da26f2022-06-07 20:12:06 +0000346 return fuzzPackager
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700347}
348
hamzeh41ad8812021-07-07 14:00:07 -0700349func (s *ccFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700350 // Map between each architecture + host/device combination, and the files that
351 // need to be packaged (in the tuple of {source file, destination folder in
352 // archive}).
hamzehc0a671f2021-07-22 12:05:08 -0700353 archDirs := make(map[fuzz.ArchOs][]fuzz.FileToZip)
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700354
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700355 // List of individual fuzz targets, so that 'make fuzz' also installs the targets
356 // to the correct output directories as well.
hamzeh41ad8812021-07-07 14:00:07 -0700357 s.FuzzTargets = make(map[string]bool)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700358
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400359 // Map tracking whether each shared library has an install rule to avoid duplicate install rules from
360 // multiple fuzzers that depend on the same shared library.
361 sharedLibraryInstalled := make(map[string]bool)
362
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700363 ctx.VisitAllModules(func(module android.Module) {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700364 ccModule, ok := module.(*Module)
hamzeh41ad8812021-07-07 14:00:07 -0700365 if !ok || ccModule.Properties.PreventInstall {
366 return
367 }
368
369 // Discard non-fuzz targets.
hamzehc0a671f2021-07-22 12:05:08 -0700370 if ok := fuzz.IsValid(ccModule.FuzzModule); !ok {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700371 return
372 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700373
Cory Barkera1da26f2022-06-07 20:12:06 +0000374 sharedLibsInstallDirPrefix := "lib"
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700375 fuzzModule, ok := ccModule.compiler.(*fuzzBinary)
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000376 if !ok {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700377 return
378 }
379
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700380 hostOrTargetString := "target"
381 if ccModule.Host() {
382 hostOrTargetString = "host"
383 }
384
Cory Barkera1da26f2022-06-07 20:12:06 +0000385 fpm := fuzz.FuzzPackagedModule{}
386 if ok {
387 fpm = fuzzModule.fuzzPackagedModule
388 }
389
390 intermediatePath := "fuzz"
Cory Barkera1da26f2022-06-07 20:12:06 +0000391
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700392 archString := ccModule.Arch().ArchType.String()
Cory Barkera1da26f2022-06-07 20:12:06 +0000393 archDir := android.PathForIntermediates(ctx, intermediatePath, hostOrTargetString, archString)
hamzehc0a671f2021-07-22 12:05:08 -0700394 archOs := fuzz.ArchOs{HostOrTarget: hostOrTargetString, Arch: archString, Dir: archDir.String()}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700395
hamzehc0a671f2021-07-22 12:05:08 -0700396 var files []fuzz.FileToZip
Colin Crossf1a035e2020-11-16 17:32:30 -0800397 builder := android.NewRuleBuilder(pctx, ctx)
Mitch Phillips2edbe8e2019-11-13 08:36:07 -0800398
hamzeh41ad8812021-07-07 14:00:07 -0700399 // Package the corpus, data, dict and config into a zipfile.
Cory Barkera1da26f2022-06-07 20:12:06 +0000400 files = s.PackageArtifacts(ctx, module, fpm, archDir, builder)
Tri Voad172d82019-11-27 13:45:45 -0800401
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400402 // Package shared libraries
Colin Cross31d89b42022-10-04 16:35:39 -0700403 files = append(files, GetSharedLibsToZip(fuzzModule.sharedLibraries, ccModule, &s.FuzzPackager, archString, sharedLibsInstallDirPrefix, &sharedLibraryInstalled)...)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700404
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700405 // The executable.
Colin Cross31d89b42022-10-04 16:35:39 -0700406 files = append(files, fuzz.FileToZip{android.OutputFileForModule(ctx, ccModule, "unstripped"), ""})
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700407
Cory Barkera1da26f2022-06-07 20:12:06 +0000408 archDirs[archOs], ok = s.BuildZipFile(ctx, module, fpm, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs)
hamzeh41ad8812021-07-07 14:00:07 -0700409 if !ok {
410 return
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700411 }
412 })
413
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000414 s.CreateFuzzPackage(ctx, archDirs, fuzz.Cc, pctx)
Mitch Phillipsa0a5e192019-09-27 14:00:06 -0700415}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700416
hamzeh41ad8812021-07-07 14:00:07 -0700417func (s *ccFuzzPackager) MakeVars(ctx android.MakeVarsContext) {
418 packages := s.Packages.Strings()
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700419 sort.Strings(packages)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400420 sort.Strings(s.FuzzPackager.SharedLibInstallStrings)
Mitch Phillipsa0a5e192019-09-27 14:00:06 -0700421 // TODO(mitchp): Migrate this to use MakeVarsContext::DistForGoal() when it's
422 // ready to handle phony targets created in Soong. In the meantime, this
423 // exports the phony 'fuzz' target and dependencies on packages to
424 // core/main.mk so that we can use dist-for-goals.
Cory Barkera1da26f2022-06-07 20:12:06 +0000425
426 ctx.Strict(s.fuzzPackagingArchModules, strings.Join(packages, " "))
427
428 ctx.Strict(s.fuzzTargetSharedDepsInstallPairs,
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400429 strings.Join(s.FuzzPackager.SharedLibInstallStrings, " "))
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700430
431 // Preallocate the slice of fuzz targets to minimise memory allocations.
Cory Barkera1da26f2022-06-07 20:12:06 +0000432 s.PreallocateSlice(ctx, s.allFuzzTargetsName)
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700433}
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400434
435// GetSharedLibsToZip finds and marks all the transiently-dependent shared libraries for
436// packaging.
Cory Barkera1da26f2022-06-07 20:12:06 +0000437func 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 -0400438 var files []fuzz.FileToZip
439
Cory Barkera1da26f2022-06-07 20:12:06 +0000440 fuzzDir := "fuzz"
Cory Barkera1da26f2022-06-07 20:12:06 +0000441
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400442 for _, library := range sharedLibraries {
Cory Barkera1da26f2022-06-07 20:12:06 +0000443 files = append(files, fuzz.FileToZip{library, destinationPathPrefix})
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400444
445 // For each architecture-specific shared library dependency, we need to
446 // install it to the output directory. Setup the install destination here,
447 // which will be used by $(copy-many-files) in the Make backend.
448 installDestination := sharedLibraryInstallLocation(
Cory Barkera1da26f2022-06-07 20:12:06 +0000449 library, module.Host(), fuzzDir, archString)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400450 if (*sharedLibraryInstalled)[installDestination] {
451 continue
452 }
453 (*sharedLibraryInstalled)[installDestination] = true
454
455 // Escape all the variables, as the install destination here will be called
456 // via. $(eval) in Make.
457 installDestination = strings.ReplaceAll(
458 installDestination, "$", "$$")
459 s.SharedLibInstallStrings = append(s.SharedLibInstallStrings,
460 library.String()+":"+installDestination)
461
462 // Ensure that on device, the library is also reinstalled to the /symbols/
463 // dir. Symbolized DSO's are always installed to the device when fuzzing, but
464 // we want symbolization tools (like `stack`) to be able to find the symbols
465 // in $ANDROID_PRODUCT_OUT/symbols automagically.
466 if !module.Host() {
Cory Barkera1da26f2022-06-07 20:12:06 +0000467 symbolsInstallDestination := sharedLibrarySymbolsInstallLocation(library, fuzzDir, archString)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400468 symbolsInstallDestination = strings.ReplaceAll(symbolsInstallDestination, "$", "$$")
469 s.SharedLibInstallStrings = append(s.SharedLibInstallStrings,
470 library.String()+":"+symbolsInstallDestination)
471 }
472 }
473 return files
474}
Colin Cross31d89b42022-10-04 16:35:39 -0700475
476// CollectAllSharedDependencies search over the provided module's dependencies using
477// VisitDirectDeps and WalkDeps to enumerate all shared library dependencies.
478// VisitDirectDeps is used first to avoid incorrectly using the core libraries (sanitizer
479// runtimes, libc, libdl, etc.) from a dependency. This may cause issues when dependencies
480// have explicit sanitizer tags, as we may get a dependency on an unsanitized libc, etc.
481func CollectAllSharedDependencies(ctx android.ModuleContext) android.Paths {
482 seen := make(map[string]bool)
483 recursed := make(map[string]bool)
484
485 var sharedLibraries android.Paths
486
487 // Enumerate the first level of dependencies, as we discard all non-library
488 // modules in the BFS loop below.
489 ctx.VisitDirectDeps(func(dep android.Module) {
490 if !IsValidSharedDependency(dep) {
491 return
492 }
493 if seen[ctx.OtherModuleName(dep)] {
494 return
495 }
496 seen[ctx.OtherModuleName(dep)] = true
497 sharedLibraries = append(sharedLibraries, android.OutputFileForModule(ctx, dep, "unstripped"))
498 })
499
500 ctx.WalkDeps(func(child, parent android.Module) bool {
501 if !IsValidSharedDependency(child) {
502 return false
503 }
504 if !seen[ctx.OtherModuleName(child)] {
505 seen[ctx.OtherModuleName(child)] = true
506 sharedLibraries = append(sharedLibraries, android.OutputFileForModule(ctx, child, "unstripped"))
507 }
508
509 if recursed[ctx.OtherModuleName(child)] {
510 return false
511 }
512 recursed[ctx.OtherModuleName(child)] = true
513 return true
514 })
515
516 return sharedLibraries
517}