blob: fbef12b5fea2a582127b87055a35d2d386e16f84 [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
55func (fuzz *fuzzBinary) linkerProps() []interface{} {
56 props := fuzz.binaryDecorator.linkerProps()
hamzeh41ad8812021-07-07 14:00:07 -070057 props = append(props, &fuzz.fuzzPackagedModule.FuzzProperties)
Mitch Phillipsda9a4632019-07-15 09:34:09 -070058 return props
59}
60
61func (fuzz *fuzzBinary) linkerInit(ctx BaseModuleContext) {
Mitch Phillipsda9a4632019-07-15 09:34:09 -070062 fuzz.binaryDecorator.linkerInit(ctx)
63}
64
65func (fuzz *fuzzBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
66 deps.StaticLibs = append(deps.StaticLibs,
67 config.LibFuzzerRuntimeLibrary(ctx.toolchain()))
68 deps = fuzz.binaryDecorator.linkerDeps(ctx, deps)
69 return deps
70}
71
72func (fuzz *fuzzBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
73 flags = fuzz.binaryDecorator.linkerFlags(ctx, flags)
Mitch Phillips1f7f54f2019-11-14 14:50:47 -080074 // RunPaths on devices isn't instantiated by the base linker. `../lib` for
75 // installed fuzz targets (both host and device), and `./lib` for fuzz
76 // target packages.
Mitch Phillipse1ee1a12019-10-17 19:20:41 -070077 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/../lib`)
Mitch Phillips1f7f54f2019-11-14 14:50:47 -080078 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/lib`)
Mitch Phillipsda9a4632019-07-15 09:34:09 -070079 return flags
80}
81
Mitch Phillipse1ee1a12019-10-17 19:20:41 -070082// This function performs a breadth-first search over the provided module's
83// dependencies using `visitDirectDeps` to enumerate all shared library
84// dependencies. We require breadth-first expansion, as otherwise we may
85// incorrectly use the core libraries (sanitizer runtimes, libc, libdl, etc.)
86// from a dependency. This may cause issues when dependencies have explicit
87// sanitizer tags, as we may get a dependency on an unsanitized libc, etc.
Colin Crossdc809f92019-11-20 15:58:32 -080088func collectAllSharedDependencies(ctx android.SingletonContext, module android.Module) android.Paths {
Mitch Phillipse1ee1a12019-10-17 19:20:41 -070089 var fringe []android.Module
90
Mitch Phillipsc0b442f2020-04-27 16:44:58 -070091 seen := make(map[string]bool)
Colin Crossdc809f92019-11-20 15:58:32 -080092
Mitch Phillipse1ee1a12019-10-17 19:20:41 -070093 // Enumerate the first level of dependencies, as we discard all non-library
94 // modules in the BFS loop below.
95 ctx.VisitDirectDeps(module, func(dep android.Module) {
Colin Crossdc809f92019-11-20 15:58:32 -080096 if isValidSharedDependency(dep) {
Mitch Phillipsf50bddb2019-11-12 14:03:31 -080097 fringe = append(fringe, dep)
98 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -070099 })
100
Colin Crossdc809f92019-11-20 15:58:32 -0800101 var sharedLibraries android.Paths
102
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700103 for i := 0; i < len(fringe); i++ {
104 module := fringe[i]
Mitch Phillipsc0b442f2020-04-27 16:44:58 -0700105 if seen[module.Name()] {
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700106 continue
107 }
Mitch Phillipsc0b442f2020-04-27 16:44:58 -0700108 seen[module.Name()] = true
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700109
110 ccModule := module.(*Module)
Colin Crossdc809f92019-11-20 15:58:32 -0800111 sharedLibraries = append(sharedLibraries, ccModule.UnstrippedOutputFile())
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700112 ctx.VisitDirectDeps(module, func(dep android.Module) {
Mitch Phillipsc0b442f2020-04-27 16:44:58 -0700113 if isValidSharedDependency(dep) && !seen[dep.Name()] {
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800114 fringe = append(fringe, dep)
115 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700116 })
117 }
Colin Crossdc809f92019-11-20 15:58:32 -0800118
119 return sharedLibraries
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700120}
121
122// This function takes a module and determines if it is a unique shared library
123// that should be installed in the fuzz target output directories. This function
124// returns true, unless:
Victor Chang00c144f2021-02-09 12:30:33 +0000125// - The module is not an installable shared library, or
Martin Stjernholm02460ab2020-10-06 02:36:43 +0100126// - The module is a header, stub, or vendor-linked library, or
127// - The module is a prebuilt and its source is available, or
128// - The module is a versioned member of an SDK snapshot.
Colin Crossdc809f92019-11-20 15:58:32 -0800129func isValidSharedDependency(dependency android.Module) bool {
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700130 // TODO(b/144090547): We should be parsing these modules using
131 // ModuleDependencyTag instead of the current brute-force checking.
132
Colin Cross31076b32020-10-23 17:22:06 -0700133 linkable, ok := dependency.(LinkableInterface)
134 if !ok || !linkable.CcLibraryInterface() {
135 // Discard non-linkables.
136 return false
137 }
138
139 if !linkable.Shared() {
140 // Discard static libs.
141 return false
142 }
143
144 if linkable.UseVndk() {
145 // Discard vendor linked libraries.
146 return false
147 }
148
149 if lib := moduleLibraryInterface(dependency); lib != nil && lib.buildStubs() && linkable.CcLibrary() {
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800150 // Discard stubs libs (only CCLibrary variants). Prebuilt libraries should not
151 // be excluded on the basis of they're not CCLibrary()'s.
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700152 return false
153 }
154
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800155 // We discarded module stubs libraries above, but the LLNDK prebuilts stubs
156 // libraries must be handled differently - by looking for the stubDecorator.
157 // Discard LLNDK prebuilts stubs as well.
158 if ccLibrary, isCcLibrary := dependency.(*Module); isCcLibrary {
159 if _, isLLndkStubLibrary := ccLibrary.linker.(*stubDecorator); isLLndkStubLibrary {
160 return false
161 }
Victor Chang00c144f2021-02-09 12:30:33 +0000162 // Discard installable:false libraries because they are expected to be absent
163 // in runtime.
164 if !proptools.BoolDefault(ccLibrary.Properties.Installable, true) {
165 return false
166 }
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800167 }
168
Martin Stjernholm02460ab2020-10-06 02:36:43 +0100169 // If the same library is present both as source and a prebuilt we must pick
170 // only one to avoid a conflict. Always prefer the source since the prebuilt
171 // probably won't be built with sanitizers enabled.
Paul Duffinf7c99f52021-04-28 10:41:21 +0100172 if prebuilt := android.GetEmbeddedPrebuilt(dependency); prebuilt != nil && prebuilt.SourceExists() {
Martin Stjernholm02460ab2020-10-06 02:36:43 +0100173 return false
174 }
175
176 // Discard versioned members of SDK snapshots, because they will conflict with
177 // unversioned ones.
178 if sdkMember, ok := dependency.(android.SdkAware); ok && !sdkMember.ContainingSdk().Unversioned() {
179 return false
180 }
181
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700182 return true
183}
184
185func sharedLibraryInstallLocation(
186 libraryPath android.Path, isHost bool, archString string) string {
187 installLocation := "$(PRODUCT_OUT)/data"
188 if isHost {
189 installLocation = "$(HOST_OUT)"
190 }
191 installLocation = filepath.Join(
192 installLocation, "fuzz", archString, "lib", libraryPath.Base())
193 return installLocation
194}
195
Mitch Phillips0bf97132020-03-06 09:38:12 -0800196// Get the device-only shared library symbols install directory.
197func sharedLibrarySymbolsInstallLocation(libraryPath android.Path, archString string) string {
198 return filepath.Join("$(PRODUCT_OUT)/symbols/data/fuzz/", archString, "/lib/", libraryPath.Base())
199}
200
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700201func (fuzz *fuzzBinary) install(ctx ModuleContext, file android.Path) {
Mitch Phillips4e4ab8a2019-09-13 17:32:50 -0700202 fuzz.binaryDecorator.baseInstaller.dir = filepath.Join(
203 "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
204 fuzz.binaryDecorator.baseInstaller.dir64 = filepath.Join(
205 "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700206 fuzz.binaryDecorator.baseInstaller.install(ctx, file)
Mitch Phillips4e4ab8a2019-09-13 17:32:50 -0700207
hamzeh41ad8812021-07-07 14:00:07 -0700208 fuzz.fuzzPackagedModule.Corpus = android.PathsForModuleSrc(ctx, fuzz.fuzzPackagedModule.FuzzProperties.Corpus)
Colin Crossf1a035e2020-11-16 17:32:30 -0800209 builder := android.NewRuleBuilder(pctx, ctx)
Mitch Phillips8a2bc0b2019-10-17 15:04:01 -0700210 intermediateDir := android.PathForModuleOut(ctx, "corpus")
hamzeh41ad8812021-07-07 14:00:07 -0700211 for _, entry := range fuzz.fuzzPackagedModule.Corpus {
Mitch Phillips8a2bc0b2019-10-17 15:04:01 -0700212 builder.Command().Text("cp").
213 Input(entry).
214 Output(intermediateDir.Join(ctx, entry.Base()))
215 }
Colin Crossf1a035e2020-11-16 17:32:30 -0800216 builder.Build("copy_corpus", "copy corpus")
hamzeh41ad8812021-07-07 14:00:07 -0700217 fuzz.fuzzPackagedModule.CorpusIntermediateDir = intermediateDir
Mitch Phillips8a2bc0b2019-10-17 15:04:01 -0700218
hamzeh41ad8812021-07-07 14:00:07 -0700219 fuzz.fuzzPackagedModule.Data = android.PathsForModuleSrc(ctx, fuzz.fuzzPackagedModule.FuzzProperties.Data)
Colin Crossf1a035e2020-11-16 17:32:30 -0800220 builder = android.NewRuleBuilder(pctx, ctx)
Tri Voad172d82019-11-27 13:45:45 -0800221 intermediateDir = android.PathForModuleOut(ctx, "data")
hamzeh41ad8812021-07-07 14:00:07 -0700222 for _, entry := range fuzz.fuzzPackagedModule.Data {
Tri Voad172d82019-11-27 13:45:45 -0800223 builder.Command().Text("cp").
224 Input(entry).
225 Output(intermediateDir.Join(ctx, entry.Rel()))
226 }
Colin Crossf1a035e2020-11-16 17:32:30 -0800227 builder.Build("copy_data", "copy data")
hamzeh41ad8812021-07-07 14:00:07 -0700228 fuzz.fuzzPackagedModule.DataIntermediateDir = intermediateDir
Tri Voad172d82019-11-27 13:45:45 -0800229
hamzeh41ad8812021-07-07 14:00:07 -0700230 if fuzz.fuzzPackagedModule.FuzzProperties.Dictionary != nil {
231 fuzz.fuzzPackagedModule.Dictionary = android.PathForModuleSrc(ctx, *fuzz.fuzzPackagedModule.FuzzProperties.Dictionary)
232 if fuzz.fuzzPackagedModule.Dictionary.Ext() != ".dict" {
Mitch Phillips4e4ab8a2019-09-13 17:32:50 -0700233 ctx.PropertyErrorf("dictionary",
234 "Fuzzer dictionary %q does not have '.dict' extension",
hamzeh41ad8812021-07-07 14:00:07 -0700235 fuzz.fuzzPackagedModule.Dictionary.String())
Mitch Phillips4e4ab8a2019-09-13 17:32:50 -0700236 }
237 }
Kris Alderf979ee32019-10-22 10:52:01 -0700238
hamzeh41ad8812021-07-07 14:00:07 -0700239 if fuzz.fuzzPackagedModule.FuzzProperties.Fuzz_config != nil {
Kris Alderdb97af42019-10-30 10:17:04 -0700240 configPath := android.PathForModuleOut(ctx, "config").Join(ctx, "config.json")
hamzeh41ad8812021-07-07 14:00:07 -0700241 android.WriteFileRule(ctx, configPath, fuzz.fuzzPackagedModule.FuzzProperties.Fuzz_config.String())
242 fuzz.fuzzPackagedModule.Config = configPath
Kris Alderf979ee32019-10-22 10:52:01 -0700243 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700244
245 // Grab the list of required shared libraries.
Mitch Phillipsc0b442f2020-04-27 16:44:58 -0700246 seen := make(map[string]bool)
Colin Crossdc809f92019-11-20 15:58:32 -0800247 var sharedLibraries android.Paths
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700248 ctx.WalkDeps(func(child, parent android.Module) bool {
Mitch Phillipsc0b442f2020-04-27 16:44:58 -0700249 if seen[child.Name()] {
Colin Crossdc809f92019-11-20 15:58:32 -0800250 return false
251 }
Mitch Phillipsc0b442f2020-04-27 16:44:58 -0700252 seen[child.Name()] = true
Colin Crossdc809f92019-11-20 15:58:32 -0800253
254 if isValidSharedDependency(child) {
255 sharedLibraries = append(sharedLibraries, child.(*Module).UnstrippedOutputFile())
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700256 return true
257 }
258 return false
259 })
260
261 for _, lib := range sharedLibraries {
262 fuzz.installedSharedDeps = append(fuzz.installedSharedDeps,
263 sharedLibraryInstallLocation(
264 lib, ctx.Host(), ctx.Arch().ArchType.String()))
Mitch Phillips0bf97132020-03-06 09:38:12 -0800265
266 // Also add the dependency on the shared library symbols dir.
267 if !ctx.Host() {
268 fuzz.installedSharedDeps = append(fuzz.installedSharedDeps,
269 sharedLibrarySymbolsInstallLocation(lib, ctx.Arch().ArchType.String()))
270 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700271 }
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700272}
273
274func NewFuzz(hod android.HostOrDeviceSupported) *Module {
275 module, binary := NewBinary(hod)
276
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700277 binary.baseInstaller = NewFuzzInstaller()
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500278 module.sanitize.SetSanitizer(Fuzzer, true)
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700279
280 fuzz := &fuzzBinary{
281 binaryDecorator: binary,
282 baseCompiler: NewBaseCompiler(),
283 }
284 module.compiler = fuzz
285 module.linker = fuzz
286 module.installer = fuzz
Colin Crosseec9b282019-07-18 16:20:52 -0700287
288 // The fuzzer runtime is not present for darwin host modules, disable cc_fuzz modules when targeting darwin.
289 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Alex Light71123ec2019-07-24 13:34:19 -0700290 disableDarwinAndLinuxBionic := struct {
Colin Crosseec9b282019-07-18 16:20:52 -0700291 Target struct {
292 Darwin struct {
293 Enabled *bool
294 }
Alex Light71123ec2019-07-24 13:34:19 -0700295 Linux_bionic struct {
296 Enabled *bool
297 }
Colin Crosseec9b282019-07-18 16:20:52 -0700298 }
299 }{}
Alex Light71123ec2019-07-24 13:34:19 -0700300 disableDarwinAndLinuxBionic.Target.Darwin.Enabled = BoolPtr(false)
301 disableDarwinAndLinuxBionic.Target.Linux_bionic.Enabled = BoolPtr(false)
302 ctx.AppendProperties(&disableDarwinAndLinuxBionic)
Colin Crosseec9b282019-07-18 16:20:52 -0700303 })
304
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700305 return module
306}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700307
308// Responsible for generating GNU Make rules that package fuzz targets into
309// their architecture & target/host specific zip file.
hamzeh41ad8812021-07-07 14:00:07 -0700310type ccFuzzPackager struct {
hamzehc0a671f2021-07-22 12:05:08 -0700311 fuzz.FuzzPackager
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700312 sharedLibInstallStrings []string
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700313}
314
315func fuzzPackagingFactory() android.Singleton {
hamzeh41ad8812021-07-07 14:00:07 -0700316 return &ccFuzzPackager{}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700317}
318
hamzeh41ad8812021-07-07 14:00:07 -0700319func (s *ccFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700320 // Map between each architecture + host/device combination, and the files that
321 // need to be packaged (in the tuple of {source file, destination folder in
322 // archive}).
hamzehc0a671f2021-07-22 12:05:08 -0700323 archDirs := make(map[fuzz.ArchOs][]fuzz.FileToZip)
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700324
Colin Crossdc809f92019-11-20 15:58:32 -0800325 // Map tracking whether each shared library has an install rule to avoid duplicate install rules from
326 // multiple fuzzers that depend on the same shared library.
327 sharedLibraryInstalled := make(map[string]bool)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700328
329 // List of individual fuzz targets, so that 'make fuzz' also installs the targets
330 // to the correct output directories as well.
hamzeh41ad8812021-07-07 14:00:07 -0700331 s.FuzzTargets = make(map[string]bool)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700332
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700333 ctx.VisitAllModules(func(module android.Module) {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700334 ccModule, ok := module.(*Module)
hamzeh41ad8812021-07-07 14:00:07 -0700335 if !ok || ccModule.Properties.PreventInstall {
336 return
337 }
338
339 // Discard non-fuzz targets.
hamzehc0a671f2021-07-22 12:05:08 -0700340 if ok := fuzz.IsValid(ccModule.FuzzModule); !ok {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700341 return
342 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700343
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700344 fuzzModule, ok := ccModule.compiler.(*fuzzBinary)
345 if !ok {
346 return
347 }
348
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700349 hostOrTargetString := "target"
350 if ccModule.Host() {
351 hostOrTargetString = "host"
352 }
353
354 archString := ccModule.Arch().ArchType.String()
355 archDir := android.PathForIntermediates(ctx, "fuzz", hostOrTargetString, archString)
hamzehc0a671f2021-07-22 12:05:08 -0700356 archOs := fuzz.ArchOs{HostOrTarget: hostOrTargetString, Arch: archString, Dir: archDir.String()}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700357
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700358 // Grab the list of required shared libraries.
Colin Crossdc809f92019-11-20 15:58:32 -0800359 sharedLibraries := collectAllSharedDependencies(ctx, module)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700360
hamzehc0a671f2021-07-22 12:05:08 -0700361 var files []fuzz.FileToZip
Colin Crossf1a035e2020-11-16 17:32:30 -0800362 builder := android.NewRuleBuilder(pctx, ctx)
Mitch Phillips2edbe8e2019-11-13 08:36:07 -0800363
hamzeh41ad8812021-07-07 14:00:07 -0700364 // Package the corpus, data, dict and config into a zipfile.
365 files = s.PackageArtifacts(ctx, module, fuzzModule.fuzzPackagedModule, archDir, builder)
Tri Voad172d82019-11-27 13:45:45 -0800366
Mitch Phillips2edbe8e2019-11-13 08:36:07 -0800367 // Find and mark all the transiently-dependent shared libraries for
368 // packaging.
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700369 for _, library := range sharedLibraries {
hamzehc0a671f2021-07-22 12:05:08 -0700370 files = append(files, fuzz.FileToZip{library, "lib"})
Mitch Phillips13ed3f52019-11-12 11:12:10 -0800371
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700372 // 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.
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700375 installDestination := sharedLibraryInstallLocation(
376 library, ccModule.Host(), archString)
Colin Crossdc809f92019-11-20 15:58:32 -0800377 if sharedLibraryInstalled[installDestination] {
378 continue
379 }
380 sharedLibraryInstalled[installDestination] = true
Mitch Phillips0bf97132020-03-06 09:38:12 -0800381
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700382 // 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)
Mitch Phillips0bf97132020-03-06 09:38:12 -0800388
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 !ccModule.Host() {
394 symbolsInstallDestination := sharedLibrarySymbolsInstallLocation(library, archString)
395 symbolsInstallDestination = strings.ReplaceAll(symbolsInstallDestination, "$", "$$")
396 s.sharedLibInstallStrings = append(s.sharedLibInstallStrings,
397 library.String()+":"+symbolsInstallDestination)
398 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700399 }
400
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700401 // The executable.
hamzehc0a671f2021-07-22 12:05:08 -0700402 files = append(files, fuzz.FileToZip{ccModule.UnstrippedOutputFile(), ""})
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700403
hamzeh41ad8812021-07-07 14:00:07 -0700404 archDirs[archOs], ok = s.BuildZipFile(ctx, module, fuzzModule.fuzzPackagedModule, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs)
405 if !ok {
406 return
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700407 }
408 })
409
hamzehc0a671f2021-07-22 12:05:08 -0700410 s.CreateFuzzPackage(ctx, archDirs, fuzz.Cc, pctx)
Colin Crossdc809f92019-11-20 15:58:32 -0800411
Mitch Phillipsa0a5e192019-09-27 14:00:06 -0700412}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700413
hamzeh41ad8812021-07-07 14:00:07 -0700414func (s *ccFuzzPackager) MakeVars(ctx android.MakeVarsContext) {
415 packages := s.Packages.Strings()
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700416 sort.Strings(packages)
417 sort.Strings(s.sharedLibInstallStrings)
Mitch Phillipsa0a5e192019-09-27 14:00:06 -0700418 // TODO(mitchp): Migrate this to use MakeVarsContext::DistForGoal() when it's
419 // ready to handle phony targets created in Soong. In the meantime, this
420 // exports the phony 'fuzz' target and dependencies on packages to
421 // core/main.mk so that we can use dist-for-goals.
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700422 ctx.Strict("SOONG_FUZZ_PACKAGING_ARCH_MODULES", strings.Join(packages, " "))
423 ctx.Strict("FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS",
424 strings.Join(s.sharedLibInstallStrings, " "))
425
426 // Preallocate the slice of fuzz targets to minimise memory allocations.
hamzeh41ad8812021-07-07 14:00:07 -0700427 s.PreallocateSlice(ctx, "ALL_FUZZ_TARGETS")
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700428}