blob: 5bb832a04c213acbe8c62c10d799a7eb1be8a5bb [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 Barkerf4b1c3a2022-06-07 20:12:06 +000030 android.RegisterModuleType("cc_afl_fuzz", AFLFuzzFactory)
31 android.RegisterModuleType("cc_fuzz", LibFuzzFactory)
Mitch Phillipsd3254b42019-09-24 13:03:28 -070032 android.RegisterSingletonType("cc_fuzz_packaging", fuzzPackagingFactory)
Cory Barkerf4b1c3a2022-06-07 20:12:06 +000033 android.RegisterSingletonType("cc_afl_fuzz_packaging", fuzzAFLPackagingFactory)
34}
35
36var (
37 neededAFLTools = map[string]bool{"afl-fuzz": true, "afl-showmap": true}
38)
39
40type FuzzProperties struct {
41 AFLEnabled bool `blueprint:"mutated"`
42 AFLAddFlags bool `blueprint:"mutated"`
43}
44
45type fuzzer struct {
46 Properties FuzzProperties
47}
48
49func (fuzzer *fuzzer) flags(ctx ModuleContext, flags Flags) Flags {
50 if fuzzer.Properties.AFLAddFlags {
51 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-coverage=trace-pc-guard")
52 }
53
54 return flags
55}
56
57func (fuzzer *fuzzer) props() []interface{} {
58 return []interface{}{&fuzzer.Properties}
59}
60
61func fuzzMutatorDeps(mctx android.TopDownMutatorContext) {
62 currentModule, ok := mctx.Module().(*Module)
63 if !ok {
64 return
65 }
66
67 if currentModule.fuzzer == nil || !currentModule.fuzzer.Properties.AFLEnabled {
68 return
69 }
70
71 mctx.WalkDeps(func(child android.Module, parent android.Module) bool {
72 c, ok := child.(*Module)
73 if !ok {
74 return false
75 }
76
77 if c.sanitize == nil {
78 return false
79 }
80
81 isFuzzerPointer := c.sanitize.getSanitizerBoolPtr(Fuzzer)
82 if isFuzzerPointer == nil || !*isFuzzerPointer {
83 return false
84 }
85
86 if c.fuzzer == nil {
87 return false
88 }
89
90 c.fuzzer.Properties.AFLEnabled = true
91 c.fuzzer.Properties.AFLAddFlags = true
92 return true
93 })
94}
95
96func fuzzMutator(mctx android.BottomUpMutatorContext) {
97 if c, ok := mctx.Module().(*Module); ok && c.fuzzer != nil {
98 if !c.fuzzer.Properties.AFLEnabled {
99 return
100 }
101
102 if c.Binary() {
103 m := mctx.CreateVariations("afl")
104 m[0].(*Module).fuzzer.Properties.AFLEnabled = true
105 m[0].(*Module).fuzzer.Properties.AFLAddFlags = true
106 } else {
107 m := mctx.CreateVariations("", "afl")
108 m[0].(*Module).fuzzer.Properties.AFLEnabled = false
109 m[0].(*Module).fuzzer.Properties.AFLAddFlags = false
110
111 m[1].(*Module).fuzzer.Properties.AFLEnabled = true
112 m[1].(*Module).fuzzer.Properties.AFLAddFlags = true
113 }
114 }
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700115}
116
117// cc_fuzz creates a host/device fuzzer binary. Host binaries can be found at
118// $ANDROID_HOST_OUT/fuzz/, and device binaries can be found at /data/fuzz on
119// your device, or $ANDROID_PRODUCT_OUT/data/fuzz in your build tree.
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000120func LibFuzzFactory() android.Module {
121 module := NewFuzzer(android.HostAndDeviceSupported, fuzz.Cc)
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700122 return module.Init()
123}
124
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000125// cc_afl_fuzz creates a host/device AFL++ fuzzer binary.
126// AFL++ is an open source framework used to fuzz libraries
127// Host binaries can be found at $ANDROID_HOST_OUT/afl_fuzz/ and device
128// binaries can be found at $ANDROID_PRODUCT_OUT/data/afl_fuzz in your
129// build tree
130func AFLFuzzFactory() android.Module {
131 module := NewFuzzer(android.HostAndDeviceSupported, fuzz.AFL)
132 return module.Init()
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700133}
134
135type fuzzBinary struct {
136 *binaryDecorator
137 *baseCompiler
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000138 fuzzPackagedModule fuzz.FuzzPackagedModule
hamzeh41ad8812021-07-07 14:00:07 -0700139 installedSharedDeps []string
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000140 fuzzType fuzz.FuzzType
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700141}
142
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400143func (fuzz *fuzzBinary) fuzzBinary() bool {
144 return true
145}
146
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700147func (fuzz *fuzzBinary) linkerProps() []interface{} {
148 props := fuzz.binaryDecorator.linkerProps()
hamzeh41ad8812021-07-07 14:00:07 -0700149 props = append(props, &fuzz.fuzzPackagedModule.FuzzProperties)
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700150 return props
151}
152
153func (fuzz *fuzzBinary) linkerInit(ctx BaseModuleContext) {
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700154 fuzz.binaryDecorator.linkerInit(ctx)
155}
156
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000157func (fuzzBin *fuzzBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
158 if fuzzBin.fuzzType == fuzz.AFL {
159 deps.HeaderLibs = append(deps.HeaderLibs, "libafl_headers")
160 deps = fuzzBin.binaryDecorator.linkerDeps(ctx, deps)
161 return deps
162
163 } else {
164 deps.StaticLibs = append(deps.StaticLibs, config.LibFuzzerRuntimeLibrary(ctx.toolchain()))
165 deps = fuzzBin.binaryDecorator.linkerDeps(ctx, deps)
166 return deps
167 }
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700168}
169
170func (fuzz *fuzzBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
171 flags = fuzz.binaryDecorator.linkerFlags(ctx, flags)
Mitch Phillips1f7f54f2019-11-14 14:50:47 -0800172 // RunPaths on devices isn't instantiated by the base linker. `../lib` for
173 // installed fuzz targets (both host and device), and `./lib` for fuzz
174 // target packages.
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700175 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/../lib`)
Mitch Phillips1f7f54f2019-11-14 14:50:47 -0800176 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/lib`)
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000177
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700178 return flags
179}
180
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400181func UnstrippedOutputFile(module android.Module) android.Path {
182 if mod, ok := module.(LinkableInterface); ok {
183 return mod.UnstrippedOutputFile()
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700184 }
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400185 panic("UnstrippedOutputFile called on non-LinkableInterface module: " + module.Name())
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700186}
187
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400188// IsValidSharedDependency takes a module and determines if it is a unique shared library
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700189// that should be installed in the fuzz target output directories. This function
190// returns true, unless:
Victor Chang00c144f2021-02-09 12:30:33 +0000191// - The module is not an installable shared library, or
Kris Alder756ec8d2021-08-27 22:08:29 +0000192// - The module is a header or stub, or
Martin Stjernholm02460ab2020-10-06 02:36:43 +0100193// - The module is a prebuilt and its source is available, or
194// - The module is a versioned member of an SDK snapshot.
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400195func IsValidSharedDependency(dependency android.Module) bool {
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700196 // TODO(b/144090547): We should be parsing these modules using
197 // ModuleDependencyTag instead of the current brute-force checking.
198
Colin Cross31076b32020-10-23 17:22:06 -0700199 linkable, ok := dependency.(LinkableInterface)
200 if !ok || !linkable.CcLibraryInterface() {
201 // Discard non-linkables.
202 return false
203 }
204
205 if !linkable.Shared() {
206 // Discard static libs.
207 return false
208 }
209
Colin Cross31076b32020-10-23 17:22:06 -0700210 if lib := moduleLibraryInterface(dependency); lib != nil && lib.buildStubs() && linkable.CcLibrary() {
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800211 // Discard stubs libs (only CCLibrary variants). Prebuilt libraries should not
212 // be excluded on the basis of they're not CCLibrary()'s.
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700213 return false
214 }
215
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800216 // We discarded module stubs libraries above, but the LLNDK prebuilts stubs
217 // libraries must be handled differently - by looking for the stubDecorator.
218 // Discard LLNDK prebuilts stubs as well.
219 if ccLibrary, isCcLibrary := dependency.(*Module); isCcLibrary {
220 if _, isLLndkStubLibrary := ccLibrary.linker.(*stubDecorator); isLLndkStubLibrary {
221 return false
222 }
Victor Chang00c144f2021-02-09 12:30:33 +0000223 // Discard installable:false libraries because they are expected to be absent
224 // in runtime.
Colin Cross1bc94122021-10-28 13:25:54 -0700225 if !proptools.BoolDefault(ccLibrary.Installable(), true) {
Victor Chang00c144f2021-02-09 12:30:33 +0000226 return false
227 }
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800228 }
229
Martin Stjernholm02460ab2020-10-06 02:36:43 +0100230 // If the same library is present both as source and a prebuilt we must pick
231 // only one to avoid a conflict. Always prefer the source since the prebuilt
232 // probably won't be built with sanitizers enabled.
Paul Duffinf7c99f52021-04-28 10:41:21 +0100233 if prebuilt := android.GetEmbeddedPrebuilt(dependency); prebuilt != nil && prebuilt.SourceExists() {
Martin Stjernholm02460ab2020-10-06 02:36:43 +0100234 return false
235 }
236
237 // Discard versioned members of SDK snapshots, because they will conflict with
238 // unversioned ones.
239 if sdkMember, ok := dependency.(android.SdkAware); ok && !sdkMember.ContainingSdk().Unversioned() {
240 return false
241 }
242
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700243 return true
244}
245
246func sharedLibraryInstallLocation(
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000247 libraryPath android.Path, isHost bool, fuzzDir string, archString string) string {
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700248 installLocation := "$(PRODUCT_OUT)/data"
249 if isHost {
250 installLocation = "$(HOST_OUT)"
251 }
252 installLocation = filepath.Join(
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000253 installLocation, fuzzDir, archString, "lib", libraryPath.Base())
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700254 return installLocation
255}
256
Mitch Phillips0bf97132020-03-06 09:38:12 -0800257// Get the device-only shared library symbols install directory.
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000258func sharedLibrarySymbolsInstallLocation(libraryPath android.Path, fuzzDir string, archString string) string {
259 return filepath.Join("$(PRODUCT_OUT)/symbols/data/", fuzzDir, archString, "/lib/", libraryPath.Base())
Mitch Phillips0bf97132020-03-06 09:38:12 -0800260}
261
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000262func (fuzzBin *fuzzBinary) install(ctx ModuleContext, file android.Path) {
263 installBase := "fuzz"
264 if fuzzBin.fuzzType == fuzz.AFL {
265 installBase = "afl_fuzz"
266 }
Mitch Phillips4e4ab8a2019-09-13 17:32:50 -0700267
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000268 fuzzBin.binaryDecorator.baseInstaller.dir = filepath.Join(
269 installBase, ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
270 fuzzBin.binaryDecorator.baseInstaller.dir64 = filepath.Join(
271 installBase, ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
272 fuzzBin.binaryDecorator.baseInstaller.install(ctx, file)
273
274 fuzzBin.fuzzPackagedModule.Corpus = android.PathsForModuleSrc(ctx, fuzzBin.fuzzPackagedModule.FuzzProperties.Corpus)
Colin Crossf1a035e2020-11-16 17:32:30 -0800275 builder := android.NewRuleBuilder(pctx, ctx)
Mitch Phillips8a2bc0b2019-10-17 15:04:01 -0700276 intermediateDir := android.PathForModuleOut(ctx, "corpus")
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000277 for _, entry := range fuzzBin.fuzzPackagedModule.Corpus {
Mitch Phillips8a2bc0b2019-10-17 15:04:01 -0700278 builder.Command().Text("cp").
279 Input(entry).
280 Output(intermediateDir.Join(ctx, entry.Base()))
281 }
Colin Crossf1a035e2020-11-16 17:32:30 -0800282 builder.Build("copy_corpus", "copy corpus")
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000283 fuzzBin.fuzzPackagedModule.CorpusIntermediateDir = intermediateDir
Mitch Phillips8a2bc0b2019-10-17 15:04:01 -0700284
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000285 fuzzBin.fuzzPackagedModule.Data = android.PathsForModuleSrc(ctx, fuzzBin.fuzzPackagedModule.FuzzProperties.Data)
Colin Crossf1a035e2020-11-16 17:32:30 -0800286 builder = android.NewRuleBuilder(pctx, ctx)
Tri Voad172d82019-11-27 13:45:45 -0800287 intermediateDir = android.PathForModuleOut(ctx, "data")
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000288 for _, entry := range fuzzBin.fuzzPackagedModule.Data {
Tri Voad172d82019-11-27 13:45:45 -0800289 builder.Command().Text("cp").
290 Input(entry).
291 Output(intermediateDir.Join(ctx, entry.Rel()))
292 }
Colin Crossf1a035e2020-11-16 17:32:30 -0800293 builder.Build("copy_data", "copy data")
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000294 fuzzBin.fuzzPackagedModule.DataIntermediateDir = intermediateDir
Tri Voad172d82019-11-27 13:45:45 -0800295
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000296 if fuzzBin.fuzzPackagedModule.FuzzProperties.Dictionary != nil {
297 fuzzBin.fuzzPackagedModule.Dictionary = android.PathForModuleSrc(ctx, *fuzzBin.fuzzPackagedModule.FuzzProperties.Dictionary)
298 if fuzzBin.fuzzPackagedModule.Dictionary.Ext() != ".dict" {
Mitch Phillips4e4ab8a2019-09-13 17:32:50 -0700299 ctx.PropertyErrorf("dictionary",
300 "Fuzzer dictionary %q does not have '.dict' extension",
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000301 fuzzBin.fuzzPackagedModule.Dictionary.String())
Mitch Phillips4e4ab8a2019-09-13 17:32:50 -0700302 }
303 }
Kris Alderf979ee32019-10-22 10:52:01 -0700304
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000305 if fuzzBin.fuzzPackagedModule.FuzzProperties.Fuzz_config != nil {
Kris Alderdb97af42019-10-30 10:17:04 -0700306 configPath := android.PathForModuleOut(ctx, "config").Join(ctx, "config.json")
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000307 android.WriteFileRule(ctx, configPath, fuzzBin.fuzzPackagedModule.FuzzProperties.Fuzz_config.String())
308 fuzzBin.fuzzPackagedModule.Config = configPath
Kris Alderf979ee32019-10-22 10:52:01 -0700309 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700310
311 // Grab the list of required shared libraries.
Mitch Phillipsc0b442f2020-04-27 16:44:58 -0700312 seen := make(map[string]bool)
Colin Crossdc809f92019-11-20 15:58:32 -0800313 var sharedLibraries android.Paths
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700314 ctx.WalkDeps(func(child, parent android.Module) bool {
Mitch Phillipsc0b442f2020-04-27 16:44:58 -0700315 if seen[child.Name()] {
Colin Crossdc809f92019-11-20 15:58:32 -0800316 return false
317 }
Mitch Phillipsc0b442f2020-04-27 16:44:58 -0700318 seen[child.Name()] = true
Colin Crossdc809f92019-11-20 15:58:32 -0800319
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400320 if IsValidSharedDependency(child) {
Colin Crossdc809f92019-11-20 15:58:32 -0800321 sharedLibraries = append(sharedLibraries, child.(*Module).UnstrippedOutputFile())
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700322 return true
323 }
324 return false
325 })
326
327 for _, lib := range sharedLibraries {
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000328 fuzzBin.installedSharedDeps = append(fuzzBin.installedSharedDeps,
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700329 sharedLibraryInstallLocation(
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000330 lib, ctx.Host(), installBase, ctx.Arch().ArchType.String()))
Mitch Phillips0bf97132020-03-06 09:38:12 -0800331
332 // Also add the dependency on the shared library symbols dir.
333 if !ctx.Host() {
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000334 fuzzBin.installedSharedDeps = append(fuzzBin.installedSharedDeps,
335 sharedLibrarySymbolsInstallLocation(lib, installBase, ctx.Arch().ArchType.String()))
Mitch Phillips0bf97132020-03-06 09:38:12 -0800336 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700337 }
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700338}
339
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000340func NewFuzzer(hod android.HostOrDeviceSupported, fuzzType fuzz.FuzzType) *Module {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400341 module, binary := newBinary(hod, false)
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000342 baseInstallerPath := "fuzz"
343 if fuzzType == fuzz.AFL {
344 baseInstallerPath = "afl_fuzz"
345 }
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700346
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000347 binary.baseInstaller = NewBaseInstaller(baseInstallerPath, baseInstallerPath, InstallInData)
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500348 module.sanitize.SetSanitizer(Fuzzer, true)
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700349
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000350 fuzzBin := &fuzzBinary{
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700351 binaryDecorator: binary,
352 baseCompiler: NewBaseCompiler(),
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000353 fuzzType: fuzzType,
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700354 }
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000355 module.compiler = fuzzBin
356 module.linker = fuzzBin
357 module.installer = fuzzBin
Colin Crosseec9b282019-07-18 16:20:52 -0700358
359 // The fuzzer runtime is not present for darwin host modules, disable cc_fuzz modules when targeting darwin.
360 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Alex Light71123ec2019-07-24 13:34:19 -0700361 disableDarwinAndLinuxBionic := struct {
Colin Crosseec9b282019-07-18 16:20:52 -0700362 Target struct {
363 Darwin struct {
364 Enabled *bool
365 }
Alex Light71123ec2019-07-24 13:34:19 -0700366 Linux_bionic struct {
367 Enabled *bool
368 }
Colin Crosseec9b282019-07-18 16:20:52 -0700369 }
370 }{}
Alex Light71123ec2019-07-24 13:34:19 -0700371 disableDarwinAndLinuxBionic.Target.Darwin.Enabled = BoolPtr(false)
372 disableDarwinAndLinuxBionic.Target.Linux_bionic.Enabled = BoolPtr(false)
373 ctx.AppendProperties(&disableDarwinAndLinuxBionic)
Colin Crosseec9b282019-07-18 16:20:52 -0700374 })
375
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000376 if fuzzType == fuzz.AFL {
377 // Add cc_objects to Srcs
378 fuzzBin.baseCompiler.Properties.Srcs = append(fuzzBin.baseCompiler.Properties.Srcs, ":aflpp_driver", ":afl-compiler-rt")
379 module.fuzzer.Properties.AFLEnabled = true
380 module.compiler.appendCflags([]string{
381 "-Wno-unused-result",
382 "-Wno-unused-parameter",
383 "-Wno-unused-function",
384 })
385 }
386
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700387 return module
388}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700389
390// Responsible for generating GNU Make rules that package fuzz targets into
391// their architecture & target/host specific zip file.
hamzeh41ad8812021-07-07 14:00:07 -0700392type ccFuzzPackager struct {
hamzehc0a671f2021-07-22 12:05:08 -0700393 fuzz.FuzzPackager
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000394 fuzzPackagingArchModules string
395 fuzzTargetSharedDepsInstallPairs string
396 allFuzzTargetsName string
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700397}
398
399func fuzzPackagingFactory() android.Singleton {
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000400
401 fuzzPackager := &ccFuzzPackager{
402 fuzzPackagingArchModules: "SOONG_FUZZ_PACKAGING_ARCH_MODULES",
403 fuzzTargetSharedDepsInstallPairs: "FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS",
404 allFuzzTargetsName: "ALL_FUZZ_TARGETS",
405 }
406 fuzzPackager.FuzzType = fuzz.Cc
407 return fuzzPackager
408}
409
410func fuzzAFLPackagingFactory() android.Singleton {
411 fuzzPackager := &ccFuzzPackager{
412 fuzzPackagingArchModules: "SOONG_AFL_FUZZ_PACKAGING_ARCH_MODULES",
413 fuzzTargetSharedDepsInstallPairs: "AFL_FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS",
414 allFuzzTargetsName: "ALL_AFL_FUZZ_TARGETS",
415 }
416 fuzzPackager.FuzzType = fuzz.AFL
417 return fuzzPackager
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700418}
419
hamzeh41ad8812021-07-07 14:00:07 -0700420func (s *ccFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700421 // Map between each architecture + host/device combination, and the files that
422 // need to be packaged (in the tuple of {source file, destination folder in
423 // archive}).
hamzehc0a671f2021-07-22 12:05:08 -0700424 archDirs := make(map[fuzz.ArchOs][]fuzz.FileToZip)
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700425
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700426 // List of individual fuzz targets, so that 'make fuzz' also installs the targets
427 // to the correct output directories as well.
hamzeh41ad8812021-07-07 14:00:07 -0700428 s.FuzzTargets = make(map[string]bool)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700429
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400430 // Map tracking whether each shared library has an install rule to avoid duplicate install rules from
431 // multiple fuzzers that depend on the same shared library.
432 sharedLibraryInstalled := make(map[string]bool)
433
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700434 ctx.VisitAllModules(func(module android.Module) {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700435 ccModule, ok := module.(*Module)
hamzeh41ad8812021-07-07 14:00:07 -0700436 if !ok || ccModule.Properties.PreventInstall {
437 return
438 }
439
440 // Discard non-fuzz targets.
hamzehc0a671f2021-07-22 12:05:08 -0700441 if ok := fuzz.IsValid(ccModule.FuzzModule); !ok {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700442 return
443 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700444
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000445 sharedLibsInstallDirPrefix := "lib"
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700446 fuzzModule, ok := ccModule.compiler.(*fuzzBinary)
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000447 if !ok || fuzzModule.fuzzType != s.FuzzType {
448 // check is module is a tool needed to be zipped for AFL
449 if _, aflTool := neededAFLTools[ccModule.Name()]; aflTool && s.FuzzType == fuzz.AFL {
450 sharedLibsInstallDirPrefix = "lib64"
451 } else {
452 return
453 }
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700454 }
455
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700456 hostOrTargetString := "target"
457 if ccModule.Host() {
458 hostOrTargetString = "host"
459 }
460
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000461 fpm := fuzz.FuzzPackagedModule{}
462 if ok {
463 fpm = fuzzModule.fuzzPackagedModule
464 }
465
466 intermediatePath := "fuzz"
467 if s.FuzzType == fuzz.AFL {
468 intermediatePath = "afl_fuzz"
469 }
470
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700471 archString := ccModule.Arch().ArchType.String()
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000472 archDir := android.PathForIntermediates(ctx, intermediatePath, hostOrTargetString, archString)
hamzehc0a671f2021-07-22 12:05:08 -0700473 archOs := fuzz.ArchOs{HostOrTarget: hostOrTargetString, Arch: archString, Dir: archDir.String()}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700474
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700475 // Grab the list of required shared libraries.
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400476 sharedLibraries := fuzz.CollectAllSharedDependencies(ctx, module, UnstrippedOutputFile, IsValidSharedDependency)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700477
hamzehc0a671f2021-07-22 12:05:08 -0700478 var files []fuzz.FileToZip
Colin Crossf1a035e2020-11-16 17:32:30 -0800479 builder := android.NewRuleBuilder(pctx, ctx)
Mitch Phillips2edbe8e2019-11-13 08:36:07 -0800480
hamzeh41ad8812021-07-07 14:00:07 -0700481 // Package the corpus, data, dict and config into a zipfile.
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000482 files = s.PackageArtifacts(ctx, module, fpm, archDir, builder)
Tri Voad172d82019-11-27 13:45:45 -0800483
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400484 // Package shared libraries
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000485 files = append(files, GetSharedLibsToZip(sharedLibraries, ccModule, &s.FuzzPackager, archString, sharedLibsInstallDirPrefix, &sharedLibraryInstalled)...)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700486
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700487 // The executable.
hamzehc0a671f2021-07-22 12:05:08 -0700488 files = append(files, fuzz.FileToZip{ccModule.UnstrippedOutputFile(), ""})
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700489
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000490 archDirs[archOs], ok = s.BuildZipFile(ctx, module, fpm, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs)
hamzeh41ad8812021-07-07 14:00:07 -0700491 if !ok {
492 return
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700493 }
494 })
495
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000496 s.CreateFuzzPackage(ctx, archDirs, s.FuzzType, pctx)
Mitch Phillipsa0a5e192019-09-27 14:00:06 -0700497}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700498
hamzeh41ad8812021-07-07 14:00:07 -0700499func (s *ccFuzzPackager) MakeVars(ctx android.MakeVarsContext) {
500 packages := s.Packages.Strings()
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700501 sort.Strings(packages)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400502 sort.Strings(s.FuzzPackager.SharedLibInstallStrings)
Mitch Phillipsa0a5e192019-09-27 14:00:06 -0700503 // TODO(mitchp): Migrate this to use MakeVarsContext::DistForGoal() when it's
504 // ready to handle phony targets created in Soong. In the meantime, this
505 // exports the phony 'fuzz' target and dependencies on packages to
506 // core/main.mk so that we can use dist-for-goals.
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000507 if s.FuzzType == fuzz.AFL {
508 s.FuzzTargets["afl-fuzz"] = true
509 s.FuzzTargets["afl-showmap"] = true
510 }
511
512 ctx.Strict(s.fuzzPackagingArchModules, strings.Join(packages, " "))
513
514 ctx.Strict(s.fuzzTargetSharedDepsInstallPairs,
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400515 strings.Join(s.FuzzPackager.SharedLibInstallStrings, " "))
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700516
517 // Preallocate the slice of fuzz targets to minimise memory allocations.
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000518 s.PreallocateSlice(ctx, s.allFuzzTargetsName)
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700519}
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400520
521// GetSharedLibsToZip finds and marks all the transiently-dependent shared libraries for
522// packaging.
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000523func 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 -0400524 var files []fuzz.FileToZip
525
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000526 fuzzDir := "fuzz"
527 if s.FuzzType == fuzz.AFL {
528 fuzzDir = "afl_fuzz"
529 }
530
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400531 for _, library := range sharedLibraries {
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000532 files = append(files, fuzz.FileToZip{library, destinationPathPrefix})
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400533
534 // For each architecture-specific shared library dependency, we need to
535 // install it to the output directory. Setup the install destination here,
536 // which will be used by $(copy-many-files) in the Make backend.
537 installDestination := sharedLibraryInstallLocation(
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000538 library, module.Host(), fuzzDir, archString)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400539 if (*sharedLibraryInstalled)[installDestination] {
540 continue
541 }
542 (*sharedLibraryInstalled)[installDestination] = true
543
544 // Escape all the variables, as the install destination here will be called
545 // via. $(eval) in Make.
546 installDestination = strings.ReplaceAll(
547 installDestination, "$", "$$")
548 s.SharedLibInstallStrings = append(s.SharedLibInstallStrings,
549 library.String()+":"+installDestination)
550
551 // Ensure that on device, the library is also reinstalled to the /symbols/
552 // dir. Symbolized DSO's are always installed to the device when fuzzing, but
553 // we want symbolization tools (like `stack`) to be able to find the symbols
554 // in $ANDROID_PRODUCT_OUT/symbols automagically.
555 if !module.Host() {
Cory Barkerf4b1c3a2022-06-07 20:12:06 +0000556 symbolsInstallDestination := sharedLibrarySymbolsInstallLocation(library, fuzzDir, archString)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400557 symbolsInstallDestination = strings.ReplaceAll(symbolsInstallDestination, "$", "$$")
558 s.SharedLibInstallStrings = append(s.SharedLibInstallStrings,
559 library.String()+":"+symbolsInstallDestination)
560 }
561 }
562 return files
563}