blob: 23d81d600ee2785b853a5d2dc457cd04af7aa662 [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() {
30 android.RegisterModuleType("cc_fuzz", FuzzFactory)
Mitch Phillipsd3254b42019-09-24 13:03:28 -070031 android.RegisterSingletonType("cc_fuzz_packaging", fuzzPackagingFactory)
Mitch Phillipsda9a4632019-07-15 09:34:09 -070032}
33
34// cc_fuzz creates a host/device fuzzer binary. Host binaries can be found at
35// $ANDROID_HOST_OUT/fuzz/, and device binaries can be found at /data/fuzz on
36// your device, or $ANDROID_PRODUCT_OUT/data/fuzz in your build tree.
37func FuzzFactory() android.Module {
38 module := NewFuzz(android.HostAndDeviceSupported)
39 return module.Init()
40}
41
42func NewFuzzInstaller() *baseInstaller {
43 return NewBaseInstaller("fuzz", "fuzz", InstallInData)
44}
45
46type fuzzBinary struct {
47 *binaryDecorator
48 *baseCompiler
Mitch Phillips4e4ab8a2019-09-13 17:32:50 -070049
hamzehc0a671f2021-07-22 12:05:08 -070050 fuzzPackagedModule fuzz.FuzzPackagedModule
hamzeh41ad8812021-07-07 14:00:07 -070051
52 installedSharedDeps []string
Mitch Phillipsda9a4632019-07-15 09:34:09 -070053}
54
Liz Kammerbe46fcc2021-11-01 15:32:43 -040055func (fuzz *fuzzBinary) fuzzBinary() bool {
56 return true
57}
58
Mitch Phillipsda9a4632019-07-15 09:34:09 -070059func (fuzz *fuzzBinary) linkerProps() []interface{} {
60 props := fuzz.binaryDecorator.linkerProps()
hamzeh41ad8812021-07-07 14:00:07 -070061 props = append(props, &fuzz.fuzzPackagedModule.FuzzProperties)
Mitch Phillipsda9a4632019-07-15 09:34:09 -070062 return props
63}
64
65func (fuzz *fuzzBinary) linkerInit(ctx BaseModuleContext) {
Mitch Phillipsda9a4632019-07-15 09:34:09 -070066 fuzz.binaryDecorator.linkerInit(ctx)
67}
68
69func (fuzz *fuzzBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
70 deps.StaticLibs = append(deps.StaticLibs,
71 config.LibFuzzerRuntimeLibrary(ctx.toolchain()))
72 deps = fuzz.binaryDecorator.linkerDeps(ctx, deps)
73 return deps
74}
75
76func (fuzz *fuzzBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
77 flags = fuzz.binaryDecorator.linkerFlags(ctx, flags)
Mitch Phillips1f7f54f2019-11-14 14:50:47 -080078 // RunPaths on devices isn't instantiated by the base linker. `../lib` for
79 // installed fuzz targets (both host and device), and `./lib` for fuzz
80 // target packages.
Mitch Phillipse1ee1a12019-10-17 19:20:41 -070081 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/../lib`)
Mitch Phillips1f7f54f2019-11-14 14:50:47 -080082 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/lib`)
Mitch Phillipsda9a4632019-07-15 09:34:09 -070083 return flags
84}
85
Ivan Lozano39b0bf02021-10-14 12:22:09 -040086func UnstrippedOutputFile(module android.Module) android.Path {
87 if mod, ok := module.(LinkableInterface); ok {
88 return mod.UnstrippedOutputFile()
Mitch Phillipse1ee1a12019-10-17 19:20:41 -070089 }
Ivan Lozano39b0bf02021-10-14 12:22:09 -040090 panic("UnstrippedOutputFile called on non-LinkableInterface module: " + module.Name())
Mitch Phillipse1ee1a12019-10-17 19:20:41 -070091}
92
Ivan Lozano39b0bf02021-10-14 12:22:09 -040093// IsValidSharedDependency takes a module and determines if it is a unique shared library
Mitch Phillipse1ee1a12019-10-17 19:20:41 -070094// that should be installed in the fuzz target output directories. This function
95// returns true, unless:
Victor Chang00c144f2021-02-09 12:30:33 +000096// - The module is not an installable shared library, or
Kris Alder756ec8d2021-08-27 22:08:29 +000097// - The module is a header or stub, or
Martin Stjernholm02460ab2020-10-06 02:36:43 +010098// - The module is a prebuilt and its source is available, or
99// - The module is a versioned member of an SDK snapshot.
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400100func IsValidSharedDependency(dependency android.Module) bool {
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700101 // TODO(b/144090547): We should be parsing these modules using
102 // ModuleDependencyTag instead of the current brute-force checking.
103
Colin Cross31076b32020-10-23 17:22:06 -0700104 linkable, ok := dependency.(LinkableInterface)
105 if !ok || !linkable.CcLibraryInterface() {
106 // Discard non-linkables.
107 return false
108 }
109
110 if !linkable.Shared() {
111 // Discard static libs.
112 return false
113 }
114
Colin Cross31076b32020-10-23 17:22:06 -0700115 if lib := moduleLibraryInterface(dependency); lib != nil && lib.buildStubs() && linkable.CcLibrary() {
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800116 // Discard stubs libs (only CCLibrary variants). Prebuilt libraries should not
117 // be excluded on the basis of they're not CCLibrary()'s.
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700118 return false
119 }
120
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800121 // We discarded module stubs libraries above, but the LLNDK prebuilts stubs
122 // libraries must be handled differently - by looking for the stubDecorator.
123 // Discard LLNDK prebuilts stubs as well.
124 if ccLibrary, isCcLibrary := dependency.(*Module); isCcLibrary {
125 if _, isLLndkStubLibrary := ccLibrary.linker.(*stubDecorator); isLLndkStubLibrary {
126 return false
127 }
Victor Chang00c144f2021-02-09 12:30:33 +0000128 // Discard installable:false libraries because they are expected to be absent
129 // in runtime.
Colin Cross1bc94122021-10-28 13:25:54 -0700130 if !proptools.BoolDefault(ccLibrary.Installable(), true) {
Victor Chang00c144f2021-02-09 12:30:33 +0000131 return false
132 }
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800133 }
134
Martin Stjernholm02460ab2020-10-06 02:36:43 +0100135 // If the same library is present both as source and a prebuilt we must pick
136 // only one to avoid a conflict. Always prefer the source since the prebuilt
137 // probably won't be built with sanitizers enabled.
Paul Duffinf7c99f52021-04-28 10:41:21 +0100138 if prebuilt := android.GetEmbeddedPrebuilt(dependency); prebuilt != nil && prebuilt.SourceExists() {
Martin Stjernholm02460ab2020-10-06 02:36:43 +0100139 return false
140 }
141
142 // Discard versioned members of SDK snapshots, because they will conflict with
143 // unversioned ones.
144 if sdkMember, ok := dependency.(android.SdkAware); ok && !sdkMember.ContainingSdk().Unversioned() {
145 return false
146 }
147
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700148 return true
149}
150
151func sharedLibraryInstallLocation(
152 libraryPath android.Path, isHost bool, archString string) string {
153 installLocation := "$(PRODUCT_OUT)/data"
154 if isHost {
155 installLocation = "$(HOST_OUT)"
156 }
157 installLocation = filepath.Join(
158 installLocation, "fuzz", archString, "lib", libraryPath.Base())
159 return installLocation
160}
161
Mitch Phillips0bf97132020-03-06 09:38:12 -0800162// Get the device-only shared library symbols install directory.
163func sharedLibrarySymbolsInstallLocation(libraryPath android.Path, archString string) string {
164 return filepath.Join("$(PRODUCT_OUT)/symbols/data/fuzz/", archString, "/lib/", libraryPath.Base())
165}
166
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700167func (fuzz *fuzzBinary) install(ctx ModuleContext, file android.Path) {
Mitch Phillips4e4ab8a2019-09-13 17:32:50 -0700168 fuzz.binaryDecorator.baseInstaller.dir = filepath.Join(
169 "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
170 fuzz.binaryDecorator.baseInstaller.dir64 = filepath.Join(
171 "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700172 fuzz.binaryDecorator.baseInstaller.install(ctx, file)
Mitch Phillips4e4ab8a2019-09-13 17:32:50 -0700173
hamzeh41ad8812021-07-07 14:00:07 -0700174 fuzz.fuzzPackagedModule.Corpus = android.PathsForModuleSrc(ctx, fuzz.fuzzPackagedModule.FuzzProperties.Corpus)
Colin Crossf1a035e2020-11-16 17:32:30 -0800175 builder := android.NewRuleBuilder(pctx, ctx)
Mitch Phillips8a2bc0b2019-10-17 15:04:01 -0700176 intermediateDir := android.PathForModuleOut(ctx, "corpus")
hamzeh41ad8812021-07-07 14:00:07 -0700177 for _, entry := range fuzz.fuzzPackagedModule.Corpus {
Mitch Phillips8a2bc0b2019-10-17 15:04:01 -0700178 builder.Command().Text("cp").
179 Input(entry).
180 Output(intermediateDir.Join(ctx, entry.Base()))
181 }
Colin Crossf1a035e2020-11-16 17:32:30 -0800182 builder.Build("copy_corpus", "copy corpus")
hamzeh41ad8812021-07-07 14:00:07 -0700183 fuzz.fuzzPackagedModule.CorpusIntermediateDir = intermediateDir
Mitch Phillips8a2bc0b2019-10-17 15:04:01 -0700184
hamzeh41ad8812021-07-07 14:00:07 -0700185 fuzz.fuzzPackagedModule.Data = android.PathsForModuleSrc(ctx, fuzz.fuzzPackagedModule.FuzzProperties.Data)
Colin Crossf1a035e2020-11-16 17:32:30 -0800186 builder = android.NewRuleBuilder(pctx, ctx)
Tri Voad172d82019-11-27 13:45:45 -0800187 intermediateDir = android.PathForModuleOut(ctx, "data")
hamzeh41ad8812021-07-07 14:00:07 -0700188 for _, entry := range fuzz.fuzzPackagedModule.Data {
Tri Voad172d82019-11-27 13:45:45 -0800189 builder.Command().Text("cp").
190 Input(entry).
191 Output(intermediateDir.Join(ctx, entry.Rel()))
192 }
Colin Crossf1a035e2020-11-16 17:32:30 -0800193 builder.Build("copy_data", "copy data")
hamzeh41ad8812021-07-07 14:00:07 -0700194 fuzz.fuzzPackagedModule.DataIntermediateDir = intermediateDir
Tri Voad172d82019-11-27 13:45:45 -0800195
hamzeh41ad8812021-07-07 14:00:07 -0700196 if fuzz.fuzzPackagedModule.FuzzProperties.Dictionary != nil {
197 fuzz.fuzzPackagedModule.Dictionary = android.PathForModuleSrc(ctx, *fuzz.fuzzPackagedModule.FuzzProperties.Dictionary)
198 if fuzz.fuzzPackagedModule.Dictionary.Ext() != ".dict" {
Mitch Phillips4e4ab8a2019-09-13 17:32:50 -0700199 ctx.PropertyErrorf("dictionary",
200 "Fuzzer dictionary %q does not have '.dict' extension",
hamzeh41ad8812021-07-07 14:00:07 -0700201 fuzz.fuzzPackagedModule.Dictionary.String())
Mitch Phillips4e4ab8a2019-09-13 17:32:50 -0700202 }
203 }
Kris Alderf979ee32019-10-22 10:52:01 -0700204
hamzeh41ad8812021-07-07 14:00:07 -0700205 if fuzz.fuzzPackagedModule.FuzzProperties.Fuzz_config != nil {
Kris Alderdb97af42019-10-30 10:17:04 -0700206 configPath := android.PathForModuleOut(ctx, "config").Join(ctx, "config.json")
hamzeh41ad8812021-07-07 14:00:07 -0700207 android.WriteFileRule(ctx, configPath, fuzz.fuzzPackagedModule.FuzzProperties.Fuzz_config.String())
208 fuzz.fuzzPackagedModule.Config = configPath
Kris Alderf979ee32019-10-22 10:52:01 -0700209 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700210
211 // Grab the list of required shared libraries.
Mitch Phillipsc0b442f2020-04-27 16:44:58 -0700212 seen := make(map[string]bool)
Colin Crossdc809f92019-11-20 15:58:32 -0800213 var sharedLibraries android.Paths
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700214 ctx.WalkDeps(func(child, parent android.Module) bool {
Mitch Phillipsc0b442f2020-04-27 16:44:58 -0700215 if seen[child.Name()] {
Colin Crossdc809f92019-11-20 15:58:32 -0800216 return false
217 }
Mitch Phillipsc0b442f2020-04-27 16:44:58 -0700218 seen[child.Name()] = true
Colin Crossdc809f92019-11-20 15:58:32 -0800219
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400220 if IsValidSharedDependency(child) {
Colin Crossdc809f92019-11-20 15:58:32 -0800221 sharedLibraries = append(sharedLibraries, child.(*Module).UnstrippedOutputFile())
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700222 return true
223 }
224 return false
225 })
226
227 for _, lib := range sharedLibraries {
228 fuzz.installedSharedDeps = append(fuzz.installedSharedDeps,
229 sharedLibraryInstallLocation(
230 lib, ctx.Host(), ctx.Arch().ArchType.String()))
Mitch Phillips0bf97132020-03-06 09:38:12 -0800231
232 // Also add the dependency on the shared library symbols dir.
233 if !ctx.Host() {
234 fuzz.installedSharedDeps = append(fuzz.installedSharedDeps,
235 sharedLibrarySymbolsInstallLocation(lib, ctx.Arch().ArchType.String()))
236 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700237 }
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700238}
239
240func NewFuzz(hod android.HostOrDeviceSupported) *Module {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400241 module, binary := newBinary(hod, false)
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700242
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700243 binary.baseInstaller = NewFuzzInstaller()
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500244 module.sanitize.SetSanitizer(Fuzzer, true)
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700245
246 fuzz := &fuzzBinary{
247 binaryDecorator: binary,
248 baseCompiler: NewBaseCompiler(),
249 }
250 module.compiler = fuzz
251 module.linker = fuzz
252 module.installer = fuzz
Colin Crosseec9b282019-07-18 16:20:52 -0700253
254 // The fuzzer runtime is not present for darwin host modules, disable cc_fuzz modules when targeting darwin.
255 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Alex Light71123ec2019-07-24 13:34:19 -0700256 disableDarwinAndLinuxBionic := struct {
Colin Crosseec9b282019-07-18 16:20:52 -0700257 Target struct {
258 Darwin struct {
259 Enabled *bool
260 }
Alex Light71123ec2019-07-24 13:34:19 -0700261 Linux_bionic struct {
262 Enabled *bool
263 }
Colin Crosseec9b282019-07-18 16:20:52 -0700264 }
265 }{}
Alex Light71123ec2019-07-24 13:34:19 -0700266 disableDarwinAndLinuxBionic.Target.Darwin.Enabled = BoolPtr(false)
267 disableDarwinAndLinuxBionic.Target.Linux_bionic.Enabled = BoolPtr(false)
268 ctx.AppendProperties(&disableDarwinAndLinuxBionic)
Colin Crosseec9b282019-07-18 16:20:52 -0700269 })
270
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700271 return module
272}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700273
274// Responsible for generating GNU Make rules that package fuzz targets into
275// their architecture & target/host specific zip file.
hamzeh41ad8812021-07-07 14:00:07 -0700276type ccFuzzPackager struct {
hamzehc0a671f2021-07-22 12:05:08 -0700277 fuzz.FuzzPackager
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700278}
279
280func fuzzPackagingFactory() android.Singleton {
hamzeh41ad8812021-07-07 14:00:07 -0700281 return &ccFuzzPackager{}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700282}
283
hamzeh41ad8812021-07-07 14:00:07 -0700284func (s *ccFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700285 // Map between each architecture + host/device combination, and the files that
286 // need to be packaged (in the tuple of {source file, destination folder in
287 // archive}).
hamzehc0a671f2021-07-22 12:05:08 -0700288 archDirs := make(map[fuzz.ArchOs][]fuzz.FileToZip)
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700289
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700290 // List of individual fuzz targets, so that 'make fuzz' also installs the targets
291 // to the correct output directories as well.
hamzeh41ad8812021-07-07 14:00:07 -0700292 s.FuzzTargets = make(map[string]bool)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700293
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400294 // Map tracking whether each shared library has an install rule to avoid duplicate install rules from
295 // multiple fuzzers that depend on the same shared library.
296 sharedLibraryInstalled := make(map[string]bool)
297
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700298 ctx.VisitAllModules(func(module android.Module) {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700299 ccModule, ok := module.(*Module)
hamzeh41ad8812021-07-07 14:00:07 -0700300 if !ok || ccModule.Properties.PreventInstall {
301 return
302 }
303
304 // Discard non-fuzz targets.
hamzehc0a671f2021-07-22 12:05:08 -0700305 if ok := fuzz.IsValid(ccModule.FuzzModule); !ok {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700306 return
307 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700308
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700309 fuzzModule, ok := ccModule.compiler.(*fuzzBinary)
310 if !ok {
311 return
312 }
313
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700314 hostOrTargetString := "target"
315 if ccModule.Host() {
316 hostOrTargetString = "host"
317 }
318
319 archString := ccModule.Arch().ArchType.String()
320 archDir := android.PathForIntermediates(ctx, "fuzz", hostOrTargetString, archString)
hamzehc0a671f2021-07-22 12:05:08 -0700321 archOs := fuzz.ArchOs{HostOrTarget: hostOrTargetString, Arch: archString, Dir: archDir.String()}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700322
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700323 // Grab the list of required shared libraries.
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400324 sharedLibraries := fuzz.CollectAllSharedDependencies(ctx, module, UnstrippedOutputFile, IsValidSharedDependency)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700325
hamzehc0a671f2021-07-22 12:05:08 -0700326 var files []fuzz.FileToZip
Colin Crossf1a035e2020-11-16 17:32:30 -0800327 builder := android.NewRuleBuilder(pctx, ctx)
Mitch Phillips2edbe8e2019-11-13 08:36:07 -0800328
hamzeh41ad8812021-07-07 14:00:07 -0700329 // Package the corpus, data, dict and config into a zipfile.
330 files = s.PackageArtifacts(ctx, module, fuzzModule.fuzzPackagedModule, archDir, builder)
Tri Voad172d82019-11-27 13:45:45 -0800331
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400332 // Package shared libraries
333 files = append(files, GetSharedLibsToZip(sharedLibraries, ccModule, &s.FuzzPackager, archString, &sharedLibraryInstalled)...)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700334
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700335 // The executable.
hamzehc0a671f2021-07-22 12:05:08 -0700336 files = append(files, fuzz.FileToZip{ccModule.UnstrippedOutputFile(), ""})
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700337
hamzeh41ad8812021-07-07 14:00:07 -0700338 archDirs[archOs], ok = s.BuildZipFile(ctx, module, fuzzModule.fuzzPackagedModule, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs)
339 if !ok {
340 return
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700341 }
342 })
343
hamzehc0a671f2021-07-22 12:05:08 -0700344 s.CreateFuzzPackage(ctx, archDirs, fuzz.Cc, pctx)
Colin Crossdc809f92019-11-20 15:58:32 -0800345
Mitch Phillipsa0a5e192019-09-27 14:00:06 -0700346}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700347
hamzeh41ad8812021-07-07 14:00:07 -0700348func (s *ccFuzzPackager) MakeVars(ctx android.MakeVarsContext) {
349 packages := s.Packages.Strings()
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700350 sort.Strings(packages)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400351 sort.Strings(s.FuzzPackager.SharedLibInstallStrings)
Mitch Phillipsa0a5e192019-09-27 14:00:06 -0700352 // TODO(mitchp): Migrate this to use MakeVarsContext::DistForGoal() when it's
353 // ready to handle phony targets created in Soong. In the meantime, this
354 // exports the phony 'fuzz' target and dependencies on packages to
355 // core/main.mk so that we can use dist-for-goals.
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700356 ctx.Strict("SOONG_FUZZ_PACKAGING_ARCH_MODULES", strings.Join(packages, " "))
357 ctx.Strict("FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS",
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400358 strings.Join(s.FuzzPackager.SharedLibInstallStrings, " "))
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700359
360 // Preallocate the slice of fuzz targets to minimise memory allocations.
hamzeh41ad8812021-07-07 14:00:07 -0700361 s.PreallocateSlice(ctx, "ALL_FUZZ_TARGETS")
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700362}
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400363
364// GetSharedLibsToZip finds and marks all the transiently-dependent shared libraries for
365// packaging.
366func GetSharedLibsToZip(sharedLibraries android.Paths, module LinkableInterface, s *fuzz.FuzzPackager, archString string, sharedLibraryInstalled *map[string]bool) []fuzz.FileToZip {
367 var files []fuzz.FileToZip
368
369 for _, library := range sharedLibraries {
370 files = append(files, fuzz.FileToZip{library, "lib"})
371
372 // For each architecture-specific shared library dependency, we need to
373 // install it to the output directory. Setup the install destination here,
374 // which will be used by $(copy-many-files) in the Make backend.
375 installDestination := sharedLibraryInstallLocation(
376 library, module.Host(), archString)
377 if (*sharedLibraryInstalled)[installDestination] {
378 continue
379 }
380 (*sharedLibraryInstalled)[installDestination] = true
381
382 // Escape all the variables, as the install destination here will be called
383 // via. $(eval) in Make.
384 installDestination = strings.ReplaceAll(
385 installDestination, "$", "$$")
386 s.SharedLibInstallStrings = append(s.SharedLibInstallStrings,
387 library.String()+":"+installDestination)
388
389 // Ensure that on device, the library is also reinstalled to the /symbols/
390 // dir. Symbolized DSO's are always installed to the device when fuzzing, but
391 // we want symbolization tools (like `stack`) to be able to find the symbols
392 // in $ANDROID_PRODUCT_OUT/symbols automagically.
393 if !module.Host() {
394 symbolsInstallDestination := sharedLibrarySymbolsInstallLocation(library, archString)
395 symbolsInstallDestination = strings.ReplaceAll(symbolsInstallDestination, "$", "$$")
396 s.SharedLibInstallStrings = append(s.SharedLibInstallStrings,
397 library.String()+":"+symbolsInstallDestination)
398 }
399 }
400 return files
401}