Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
Mitch Phillips | 4de896e | 2019-08-28 16:04:36 -0700 | [diff] [blame] | 18 | "path/filepath" |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 19 | "sort" |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 20 | "strings" |
Mitch Phillips | 4de896e | 2019-08-28 16:04:36 -0700 | [diff] [blame] | 21 | |
Victor Chang | 00c144f | 2021-02-09 12:30:33 +0000 | [diff] [blame] | 22 | "github.com/google/blueprint/proptools" |
| 23 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 24 | "android/soong/android" |
| 25 | "android/soong/cc/config" |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame] | 26 | "android/soong/fuzz" |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | func init() { |
Cory Barker | 74aea6c | 2022-08-08 15:55:12 +0000 | [diff] [blame^] | 30 | android.RegisterModuleType("cc_afl_fuzz", AFLFuzzFactory) |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 31 | android.RegisterModuleType("cc_fuzz", LibFuzzFactory) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 32 | android.RegisterSingletonType("cc_fuzz_packaging", fuzzPackagingFactory) |
Cory Barker | 74aea6c | 2022-08-08 15:55:12 +0000 | [diff] [blame^] | 33 | android.RegisterSingletonType("cc_afl_fuzz_packaging", fuzzAFLPackagingFactory) |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | type FuzzProperties struct { |
Cory Barker | 74aea6c | 2022-08-08 15:55:12 +0000 | [diff] [blame^] | 37 | AFLEnabled bool `blueprint:"mutated"` |
| 38 | AFLAddFlags bool `blueprint:"mutated"` |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | type fuzzer struct { |
| 42 | Properties FuzzProperties |
| 43 | } |
| 44 | |
| 45 | func (fuzzer *fuzzer) flags(ctx ModuleContext, flags Flags) Flags { |
Cory Barker | 74aea6c | 2022-08-08 15:55:12 +0000 | [diff] [blame^] | 46 | if fuzzer.Properties.AFLAddFlags { |
| 47 | flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-coverage=trace-pc-guard") |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | return flags |
| 51 | } |
| 52 | |
| 53 | func (fuzzer *fuzzer) props() []interface{} { |
| 54 | return []interface{}{&fuzzer.Properties} |
| 55 | } |
| 56 | |
| 57 | func fuzzMutatorDeps(mctx android.TopDownMutatorContext) { |
| 58 | currentModule, ok := mctx.Module().(*Module) |
| 59 | if !ok { |
| 60 | return |
| 61 | } |
| 62 | |
Cory Barker | 74aea6c | 2022-08-08 15:55:12 +0000 | [diff] [blame^] | 63 | if currentModule.fuzzer == nil || !currentModule.fuzzer.Properties.AFLEnabled { |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 64 | return |
| 65 | } |
| 66 | |
| 67 | mctx.WalkDeps(func(child android.Module, parent android.Module) bool { |
| 68 | c, ok := child.(*Module) |
| 69 | if !ok { |
| 70 | return false |
| 71 | } |
| 72 | |
| 73 | if c.sanitize == nil { |
| 74 | return false |
| 75 | } |
| 76 | |
| 77 | isFuzzerPointer := c.sanitize.getSanitizerBoolPtr(Fuzzer) |
| 78 | if isFuzzerPointer == nil || !*isFuzzerPointer { |
| 79 | return false |
| 80 | } |
| 81 | |
| 82 | if c.fuzzer == nil { |
| 83 | return false |
| 84 | } |
| 85 | |
Cory Barker | 74aea6c | 2022-08-08 15:55:12 +0000 | [diff] [blame^] | 86 | c.fuzzer.Properties.AFLEnabled = true |
| 87 | c.fuzzer.Properties.AFLAddFlags = true |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 88 | return true |
| 89 | }) |
| 90 | } |
| 91 | |
Cory Barker | 74aea6c | 2022-08-08 15:55:12 +0000 | [diff] [blame^] | 92 | func fuzzMutator(mctx android.BottomUpMutatorContext) { |
| 93 | if c, ok := mctx.Module().(*Module); ok && c.fuzzer != nil { |
| 94 | if !c.fuzzer.Properties.AFLEnabled { |
| 95 | return |
| 96 | } |
| 97 | |
| 98 | if c.Binary() { |
| 99 | m := mctx.CreateVariations("afl") |
| 100 | m[0].(*Module).fuzzer.Properties.AFLEnabled = true |
| 101 | m[0].(*Module).fuzzer.Properties.AFLAddFlags = true |
| 102 | } else { |
| 103 | m := mctx.CreateVariations("", "afl") |
| 104 | m[0].(*Module).fuzzer.Properties.AFLEnabled = false |
| 105 | m[0].(*Module).fuzzer.Properties.AFLAddFlags = false |
| 106 | |
| 107 | m[1].(*Module).fuzzer.Properties.AFLEnabled = true |
| 108 | m[1].(*Module).fuzzer.Properties.AFLAddFlags = true |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 113 | // cc_fuzz creates a host/device fuzzer binary. Host binaries can be found at |
| 114 | // $ANDROID_HOST_OUT/fuzz/, and device binaries can be found at /data/fuzz on |
| 115 | // your device, or $ANDROID_PRODUCT_OUT/data/fuzz in your build tree. |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 116 | func LibFuzzFactory() android.Module { |
Cory Barker | 74aea6c | 2022-08-08 15:55:12 +0000 | [diff] [blame^] | 117 | module := NewFuzzer(android.HostAndDeviceSupported, fuzz.Cc) |
| 118 | return module.Init() |
| 119 | } |
| 120 | |
| 121 | // cc_afl_fuzz creates a host/device AFL++ fuzzer binary. |
| 122 | // AFL++ is an open source framework used to fuzz libraries |
| 123 | // Host binaries can be found at $ANDROID_HOST_OUT/afl_fuzz/ and device |
| 124 | // binaries can be found at $ANDROID_PRODUCT_OUT/data/afl_fuzz in your |
| 125 | // build tree |
| 126 | func AFLFuzzFactory() android.Module { |
| 127 | module := NewFuzzer(android.HostAndDeviceSupported, fuzz.AFL) |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 128 | return module.Init() |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | type fuzzBinary struct { |
| 132 | *binaryDecorator |
| 133 | *baseCompiler |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 134 | fuzzPackagedModule fuzz.FuzzPackagedModule |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 135 | installedSharedDeps []string |
Cory Barker | 74aea6c | 2022-08-08 15:55:12 +0000 | [diff] [blame^] | 136 | fuzzType fuzz.FuzzType |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 139 | func (fuzz *fuzzBinary) fuzzBinary() bool { |
| 140 | return true |
| 141 | } |
| 142 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 143 | func (fuzz *fuzzBinary) linkerProps() []interface{} { |
| 144 | props := fuzz.binaryDecorator.linkerProps() |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 145 | props = append(props, &fuzz.fuzzPackagedModule.FuzzProperties) |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 146 | return props |
| 147 | } |
| 148 | |
| 149 | func (fuzz *fuzzBinary) linkerInit(ctx BaseModuleContext) { |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 150 | fuzz.binaryDecorator.linkerInit(ctx) |
| 151 | } |
| 152 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 153 | func (fuzzBin *fuzzBinary) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Cory Barker | 74aea6c | 2022-08-08 15:55:12 +0000 | [diff] [blame^] | 154 | if fuzzBin.fuzzType == fuzz.AFL { |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 155 | deps.HeaderLibs = append(deps.HeaderLibs, "libafl_headers") |
Cory Barker | 74aea6c | 2022-08-08 15:55:12 +0000 | [diff] [blame^] | 156 | deps = fuzzBin.binaryDecorator.linkerDeps(ctx, deps) |
| 157 | return deps |
| 158 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 159 | } else { |
| 160 | deps.StaticLibs = append(deps.StaticLibs, config.LibFuzzerRuntimeLibrary(ctx.toolchain())) |
Cory Barker | 74aea6c | 2022-08-08 15:55:12 +0000 | [diff] [blame^] | 161 | deps = fuzzBin.binaryDecorator.linkerDeps(ctx, deps) |
| 162 | return deps |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 163 | } |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | func (fuzz *fuzzBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
| 167 | flags = fuzz.binaryDecorator.linkerFlags(ctx, flags) |
Mitch Phillips | 1f7f54f | 2019-11-14 14:50:47 -0800 | [diff] [blame] | 168 | // RunPaths on devices isn't instantiated by the base linker. `../lib` for |
| 169 | // installed fuzz targets (both host and device), and `./lib` for fuzz |
| 170 | // target packages. |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 171 | flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/../lib`) |
Mitch Phillips | 1f7f54f | 2019-11-14 14:50:47 -0800 | [diff] [blame] | 172 | flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/lib`) |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 173 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 174 | return flags |
| 175 | } |
| 176 | |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 177 | func UnstrippedOutputFile(module android.Module) android.Path { |
| 178 | if mod, ok := module.(LinkableInterface); ok { |
| 179 | return mod.UnstrippedOutputFile() |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 180 | } |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 181 | panic("UnstrippedOutputFile called on non-LinkableInterface module: " + module.Name()) |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 184 | // IsValidSharedDependency takes a module and determines if it is a unique shared library |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 185 | // that should be installed in the fuzz target output directories. This function |
| 186 | // returns true, unless: |
Victor Chang | 00c144f | 2021-02-09 12:30:33 +0000 | [diff] [blame] | 187 | // - The module is not an installable shared library, or |
Kris Alder | 756ec8d | 2021-08-27 22:08:29 +0000 | [diff] [blame] | 188 | // - The module is a header or stub, or |
Martin Stjernholm | 02460ab | 2020-10-06 02:36:43 +0100 | [diff] [blame] | 189 | // - The module is a prebuilt and its source is available, or |
| 190 | // - The module is a versioned member of an SDK snapshot. |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 191 | func IsValidSharedDependency(dependency android.Module) bool { |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 192 | // TODO(b/144090547): We should be parsing these modules using |
| 193 | // ModuleDependencyTag instead of the current brute-force checking. |
| 194 | |
Colin Cross | 31076b3 | 2020-10-23 17:22:06 -0700 | [diff] [blame] | 195 | linkable, ok := dependency.(LinkableInterface) |
| 196 | if !ok || !linkable.CcLibraryInterface() { |
| 197 | // Discard non-linkables. |
| 198 | return false |
| 199 | } |
| 200 | |
| 201 | if !linkable.Shared() { |
| 202 | // Discard static libs. |
| 203 | return false |
| 204 | } |
| 205 | |
Colin Cross | 31076b3 | 2020-10-23 17:22:06 -0700 | [diff] [blame] | 206 | if lib := moduleLibraryInterface(dependency); lib != nil && lib.buildStubs() && linkable.CcLibrary() { |
Mitch Phillips | f50bddb | 2019-11-12 14:03:31 -0800 | [diff] [blame] | 207 | // Discard stubs libs (only CCLibrary variants). Prebuilt libraries should not |
| 208 | // be excluded on the basis of they're not CCLibrary()'s. |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 209 | return false |
| 210 | } |
| 211 | |
Mitch Phillips | f50bddb | 2019-11-12 14:03:31 -0800 | [diff] [blame] | 212 | // We discarded module stubs libraries above, but the LLNDK prebuilts stubs |
| 213 | // libraries must be handled differently - by looking for the stubDecorator. |
| 214 | // Discard LLNDK prebuilts stubs as well. |
| 215 | if ccLibrary, isCcLibrary := dependency.(*Module); isCcLibrary { |
| 216 | if _, isLLndkStubLibrary := ccLibrary.linker.(*stubDecorator); isLLndkStubLibrary { |
| 217 | return false |
| 218 | } |
Victor Chang | 00c144f | 2021-02-09 12:30:33 +0000 | [diff] [blame] | 219 | // Discard installable:false libraries because they are expected to be absent |
| 220 | // in runtime. |
Colin Cross | 1bc9412 | 2021-10-28 13:25:54 -0700 | [diff] [blame] | 221 | if !proptools.BoolDefault(ccLibrary.Installable(), true) { |
Victor Chang | 00c144f | 2021-02-09 12:30:33 +0000 | [diff] [blame] | 222 | return false |
| 223 | } |
Mitch Phillips | f50bddb | 2019-11-12 14:03:31 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Martin Stjernholm | 02460ab | 2020-10-06 02:36:43 +0100 | [diff] [blame] | 226 | // If the same library is present both as source and a prebuilt we must pick |
| 227 | // only one to avoid a conflict. Always prefer the source since the prebuilt |
| 228 | // probably won't be built with sanitizers enabled. |
Paul Duffin | f7c99f5 | 2021-04-28 10:41:21 +0100 | [diff] [blame] | 229 | if prebuilt := android.GetEmbeddedPrebuilt(dependency); prebuilt != nil && prebuilt.SourceExists() { |
Martin Stjernholm | 02460ab | 2020-10-06 02:36:43 +0100 | [diff] [blame] | 230 | return false |
| 231 | } |
| 232 | |
| 233 | // Discard versioned members of SDK snapshots, because they will conflict with |
| 234 | // unversioned ones. |
| 235 | if sdkMember, ok := dependency.(android.SdkAware); ok && !sdkMember.ContainingSdk().Unversioned() { |
| 236 | return false |
| 237 | } |
| 238 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 239 | return true |
| 240 | } |
| 241 | |
| 242 | func sharedLibraryInstallLocation( |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 243 | libraryPath android.Path, isHost bool, fuzzDir string, archString string) string { |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 244 | installLocation := "$(PRODUCT_OUT)/data" |
| 245 | if isHost { |
| 246 | installLocation = "$(HOST_OUT)" |
| 247 | } |
| 248 | installLocation = filepath.Join( |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 249 | installLocation, fuzzDir, archString, "lib", libraryPath.Base()) |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 250 | return installLocation |
| 251 | } |
| 252 | |
Mitch Phillips | 0bf9713 | 2020-03-06 09:38:12 -0800 | [diff] [blame] | 253 | // Get the device-only shared library symbols install directory. |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 254 | func sharedLibrarySymbolsInstallLocation(libraryPath android.Path, fuzzDir string, archString string) string { |
| 255 | return filepath.Join("$(PRODUCT_OUT)/symbols/data/", fuzzDir, archString, "/lib/", libraryPath.Base()) |
Mitch Phillips | 0bf9713 | 2020-03-06 09:38:12 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 258 | func (fuzzBin *fuzzBinary) install(ctx ModuleContext, file android.Path) { |
| 259 | installBase := "fuzz" |
Cory Barker | 74aea6c | 2022-08-08 15:55:12 +0000 | [diff] [blame^] | 260 | if fuzzBin.fuzzType == fuzz.AFL { |
| 261 | installBase = "afl_fuzz" |
| 262 | } |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 263 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 264 | fuzzBin.binaryDecorator.baseInstaller.dir = filepath.Join( |
| 265 | installBase, ctx.Target().Arch.ArchType.String(), ctx.ModuleName()) |
| 266 | fuzzBin.binaryDecorator.baseInstaller.dir64 = filepath.Join( |
| 267 | installBase, ctx.Target().Arch.ArchType.String(), ctx.ModuleName()) |
| 268 | fuzzBin.binaryDecorator.baseInstaller.install(ctx, file) |
| 269 | |
| 270 | fuzzBin.fuzzPackagedModule.Corpus = android.PathsForModuleSrc(ctx, fuzzBin.fuzzPackagedModule.FuzzProperties.Corpus) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 271 | builder := android.NewRuleBuilder(pctx, ctx) |
Mitch Phillips | 8a2bc0b | 2019-10-17 15:04:01 -0700 | [diff] [blame] | 272 | intermediateDir := android.PathForModuleOut(ctx, "corpus") |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 273 | for _, entry := range fuzzBin.fuzzPackagedModule.Corpus { |
Mitch Phillips | 8a2bc0b | 2019-10-17 15:04:01 -0700 | [diff] [blame] | 274 | builder.Command().Text("cp"). |
| 275 | Input(entry). |
| 276 | Output(intermediateDir.Join(ctx, entry.Base())) |
| 277 | } |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 278 | builder.Build("copy_corpus", "copy corpus") |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 279 | fuzzBin.fuzzPackagedModule.CorpusIntermediateDir = intermediateDir |
Mitch Phillips | 8a2bc0b | 2019-10-17 15:04:01 -0700 | [diff] [blame] | 280 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 281 | fuzzBin.fuzzPackagedModule.Data = android.PathsForModuleSrc(ctx, fuzzBin.fuzzPackagedModule.FuzzProperties.Data) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 282 | builder = android.NewRuleBuilder(pctx, ctx) |
Tri Vo | ad172d8 | 2019-11-27 13:45:45 -0800 | [diff] [blame] | 283 | intermediateDir = android.PathForModuleOut(ctx, "data") |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 284 | for _, entry := range fuzzBin.fuzzPackagedModule.Data { |
Tri Vo | ad172d8 | 2019-11-27 13:45:45 -0800 | [diff] [blame] | 285 | builder.Command().Text("cp"). |
| 286 | Input(entry). |
| 287 | Output(intermediateDir.Join(ctx, entry.Rel())) |
| 288 | } |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 289 | builder.Build("copy_data", "copy data") |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 290 | fuzzBin.fuzzPackagedModule.DataIntermediateDir = intermediateDir |
Tri Vo | ad172d8 | 2019-11-27 13:45:45 -0800 | [diff] [blame] | 291 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 292 | if fuzzBin.fuzzPackagedModule.FuzzProperties.Dictionary != nil { |
| 293 | fuzzBin.fuzzPackagedModule.Dictionary = android.PathForModuleSrc(ctx, *fuzzBin.fuzzPackagedModule.FuzzProperties.Dictionary) |
| 294 | if fuzzBin.fuzzPackagedModule.Dictionary.Ext() != ".dict" { |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 295 | ctx.PropertyErrorf("dictionary", |
| 296 | "Fuzzer dictionary %q does not have '.dict' extension", |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 297 | fuzzBin.fuzzPackagedModule.Dictionary.String()) |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 298 | } |
| 299 | } |
Kris Alder | f979ee3 | 2019-10-22 10:52:01 -0700 | [diff] [blame] | 300 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 301 | if fuzzBin.fuzzPackagedModule.FuzzProperties.Fuzz_config != nil { |
Kris Alder | db97af4 | 2019-10-30 10:17:04 -0700 | [diff] [blame] | 302 | configPath := android.PathForModuleOut(ctx, "config").Join(ctx, "config.json") |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 303 | android.WriteFileRule(ctx, configPath, fuzzBin.fuzzPackagedModule.FuzzProperties.Fuzz_config.String()) |
| 304 | fuzzBin.fuzzPackagedModule.Config = configPath |
Kris Alder | f979ee3 | 2019-10-22 10:52:01 -0700 | [diff] [blame] | 305 | } |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 306 | |
| 307 | // Grab the list of required shared libraries. |
Mitch Phillips | c0b442f | 2020-04-27 16:44:58 -0700 | [diff] [blame] | 308 | seen := make(map[string]bool) |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 309 | var sharedLibraries android.Paths |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 310 | ctx.WalkDeps(func(child, parent android.Module) bool { |
Mitch Phillips | c0b442f | 2020-04-27 16:44:58 -0700 | [diff] [blame] | 311 | if seen[child.Name()] { |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 312 | return false |
| 313 | } |
Mitch Phillips | c0b442f | 2020-04-27 16:44:58 -0700 | [diff] [blame] | 314 | seen[child.Name()] = true |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 315 | |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 316 | if IsValidSharedDependency(child) { |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 317 | sharedLibraries = append(sharedLibraries, child.(*Module).UnstrippedOutputFile()) |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 318 | return true |
| 319 | } |
| 320 | return false |
| 321 | }) |
| 322 | |
| 323 | for _, lib := range sharedLibraries { |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 324 | fuzzBin.installedSharedDeps = append(fuzzBin.installedSharedDeps, |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 325 | sharedLibraryInstallLocation( |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 326 | lib, ctx.Host(), installBase, ctx.Arch().ArchType.String())) |
Mitch Phillips | 0bf9713 | 2020-03-06 09:38:12 -0800 | [diff] [blame] | 327 | |
| 328 | // Also add the dependency on the shared library symbols dir. |
| 329 | if !ctx.Host() { |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 330 | fuzzBin.installedSharedDeps = append(fuzzBin.installedSharedDeps, |
| 331 | sharedLibrarySymbolsInstallLocation(lib, installBase, ctx.Arch().ArchType.String())) |
Mitch Phillips | 0bf9713 | 2020-03-06 09:38:12 -0800 | [diff] [blame] | 332 | } |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 333 | } |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Cory Barker | 74aea6c | 2022-08-08 15:55:12 +0000 | [diff] [blame^] | 336 | func NewFuzzer(hod android.HostOrDeviceSupported, fuzzType fuzz.FuzzType) *Module { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 337 | module, binary := newBinary(hod, false) |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 338 | baseInstallerPath := "fuzz" |
Cory Barker | 74aea6c | 2022-08-08 15:55:12 +0000 | [diff] [blame^] | 339 | if fuzzType == fuzz.AFL { |
| 340 | baseInstallerPath = "afl_fuzz" |
| 341 | } |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 342 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 343 | binary.baseInstaller = NewBaseInstaller(baseInstallerPath, baseInstallerPath, InstallInData) |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 344 | module.sanitize.SetSanitizer(Fuzzer, true) |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 345 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 346 | fuzzBin := &fuzzBinary{ |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 347 | binaryDecorator: binary, |
| 348 | baseCompiler: NewBaseCompiler(), |
Cory Barker | 74aea6c | 2022-08-08 15:55:12 +0000 | [diff] [blame^] | 349 | fuzzType: fuzzType, |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 350 | } |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 351 | module.compiler = fuzzBin |
| 352 | module.linker = fuzzBin |
| 353 | module.installer = fuzzBin |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 354 | |
| 355 | // The fuzzer runtime is not present for darwin host modules, disable cc_fuzz modules when targeting darwin. |
| 356 | android.AddLoadHook(module, func(ctx android.LoadHookContext) { |
Alex Light | 71123ec | 2019-07-24 13:34:19 -0700 | [diff] [blame] | 357 | disableDarwinAndLinuxBionic := struct { |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 358 | Target struct { |
| 359 | Darwin struct { |
| 360 | Enabled *bool |
| 361 | } |
Alex Light | 71123ec | 2019-07-24 13:34:19 -0700 | [diff] [blame] | 362 | Linux_bionic struct { |
| 363 | Enabled *bool |
| 364 | } |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 365 | } |
| 366 | }{} |
Alex Light | 71123ec | 2019-07-24 13:34:19 -0700 | [diff] [blame] | 367 | disableDarwinAndLinuxBionic.Target.Darwin.Enabled = BoolPtr(false) |
| 368 | disableDarwinAndLinuxBionic.Target.Linux_bionic.Enabled = BoolPtr(false) |
| 369 | ctx.AppendProperties(&disableDarwinAndLinuxBionic) |
Cory Barker | cecb754 | 2022-07-22 17:22:02 +0000 | [diff] [blame] | 370 | }) |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 371 | |
Cory Barker | 74aea6c | 2022-08-08 15:55:12 +0000 | [diff] [blame^] | 372 | if fuzzType == fuzz.AFL { |
| 373 | // Add cc_objects to Srcs |
| 374 | fuzzBin.baseCompiler.Properties.Srcs = append(fuzzBin.baseCompiler.Properties.Srcs, ":aflpp_driver", ":afl-compiler-rt") |
| 375 | module.fuzzer.Properties.AFLEnabled = true |
| 376 | module.compiler.appendCflags([]string{ |
| 377 | "-Wno-unused-result", |
| 378 | "-Wno-unused-parameter", |
| 379 | "-Wno-unused-function", |
| 380 | }) |
| 381 | } |
| 382 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 383 | return module |
| 384 | } |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 385 | |
| 386 | // Responsible for generating GNU Make rules that package fuzz targets into |
| 387 | // their architecture & target/host specific zip file. |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 388 | type ccFuzzPackager struct { |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame] | 389 | fuzz.FuzzPackager |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 390 | fuzzPackagingArchModules string |
| 391 | fuzzTargetSharedDepsInstallPairs string |
| 392 | allFuzzTargetsName string |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | func fuzzPackagingFactory() android.Singleton { |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 396 | |
| 397 | fuzzPackager := &ccFuzzPackager{ |
| 398 | fuzzPackagingArchModules: "SOONG_FUZZ_PACKAGING_ARCH_MODULES", |
| 399 | fuzzTargetSharedDepsInstallPairs: "FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS", |
| 400 | allFuzzTargetsName: "ALL_FUZZ_TARGETS", |
| 401 | } |
Cory Barker | 74aea6c | 2022-08-08 15:55:12 +0000 | [diff] [blame^] | 402 | fuzzPackager.FuzzType = fuzz.Cc |
| 403 | return fuzzPackager |
| 404 | } |
| 405 | |
| 406 | func fuzzAFLPackagingFactory() android.Singleton { |
| 407 | fuzzPackager := &ccFuzzPackager{ |
| 408 | fuzzPackagingArchModules: "SOONG_AFL_FUZZ_PACKAGING_ARCH_MODULES", |
| 409 | fuzzTargetSharedDepsInstallPairs: "AFL_FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS", |
| 410 | allFuzzTargetsName: "ALL_AFL_FUZZ_TARGETS", |
| 411 | } |
| 412 | fuzzPackager.FuzzType = fuzz.AFL |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 413 | return fuzzPackager |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 414 | } |
| 415 | |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 416 | func (s *ccFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) { |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 417 | // Map between each architecture + host/device combination, and the files that |
| 418 | // need to be packaged (in the tuple of {source file, destination folder in |
| 419 | // archive}). |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame] | 420 | archDirs := make(map[fuzz.ArchOs][]fuzz.FileToZip) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 421 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 422 | // List of individual fuzz targets, so that 'make fuzz' also installs the targets |
| 423 | // to the correct output directories as well. |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 424 | s.FuzzTargets = make(map[string]bool) |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 425 | |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 426 | // Map tracking whether each shared library has an install rule to avoid duplicate install rules from |
| 427 | // multiple fuzzers that depend on the same shared library. |
| 428 | sharedLibraryInstalled := make(map[string]bool) |
| 429 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 430 | ctx.VisitAllModules(func(module android.Module) { |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 431 | ccModule, ok := module.(*Module) |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 432 | if !ok || ccModule.Properties.PreventInstall { |
| 433 | return |
| 434 | } |
| 435 | |
| 436 | // Discard non-fuzz targets. |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame] | 437 | if ok := fuzz.IsValid(ccModule.FuzzModule); !ok { |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 438 | return |
| 439 | } |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 440 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 441 | sharedLibsInstallDirPrefix := "lib" |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 442 | fuzzModule, ok := ccModule.compiler.(*fuzzBinary) |
Cory Barker | 74aea6c | 2022-08-08 15:55:12 +0000 | [diff] [blame^] | 443 | if !ok || fuzzModule.fuzzType != s.FuzzType { |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 444 | return |
| 445 | } |
| 446 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 447 | hostOrTargetString := "target" |
| 448 | if ccModule.Host() { |
| 449 | hostOrTargetString = "host" |
| 450 | } |
| 451 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 452 | fpm := fuzz.FuzzPackagedModule{} |
| 453 | if ok { |
| 454 | fpm = fuzzModule.fuzzPackagedModule |
| 455 | } |
| 456 | |
| 457 | intermediatePath := "fuzz" |
Cory Barker | 74aea6c | 2022-08-08 15:55:12 +0000 | [diff] [blame^] | 458 | if s.FuzzType == fuzz.AFL { |
| 459 | intermediatePath = "afl_fuzz" |
| 460 | } |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 461 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 462 | archString := ccModule.Arch().ArchType.String() |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 463 | archDir := android.PathForIntermediates(ctx, intermediatePath, hostOrTargetString, archString) |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame] | 464 | archOs := fuzz.ArchOs{HostOrTarget: hostOrTargetString, Arch: archString, Dir: archDir.String()} |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 465 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 466 | // Grab the list of required shared libraries. |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 467 | sharedLibraries := fuzz.CollectAllSharedDependencies(ctx, module, UnstrippedOutputFile, IsValidSharedDependency) |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 468 | |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame] | 469 | var files []fuzz.FileToZip |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 470 | builder := android.NewRuleBuilder(pctx, ctx) |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 471 | |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 472 | // Package the corpus, data, dict and config into a zipfile. |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 473 | files = s.PackageArtifacts(ctx, module, fpm, archDir, builder) |
Tri Vo | ad172d8 | 2019-11-27 13:45:45 -0800 | [diff] [blame] | 474 | |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 475 | // Package shared libraries |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 476 | files = append(files, GetSharedLibsToZip(sharedLibraries, ccModule, &s.FuzzPackager, archString, sharedLibsInstallDirPrefix, &sharedLibraryInstalled)...) |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 477 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 478 | // The executable. |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame] | 479 | files = append(files, fuzz.FileToZip{ccModule.UnstrippedOutputFile(), ""}) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 480 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 481 | archDirs[archOs], ok = s.BuildZipFile(ctx, module, fpm, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs) |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 482 | if !ok { |
| 483 | return |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 484 | } |
| 485 | }) |
| 486 | |
Cory Barker | 74aea6c | 2022-08-08 15:55:12 +0000 | [diff] [blame^] | 487 | s.CreateFuzzPackage(ctx, archDirs, s.FuzzType, pctx) |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 488 | } |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 489 | |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 490 | func (s *ccFuzzPackager) MakeVars(ctx android.MakeVarsContext) { |
| 491 | packages := s.Packages.Strings() |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 492 | sort.Strings(packages) |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 493 | sort.Strings(s.FuzzPackager.SharedLibInstallStrings) |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 494 | // TODO(mitchp): Migrate this to use MakeVarsContext::DistForGoal() when it's |
| 495 | // ready to handle phony targets created in Soong. In the meantime, this |
| 496 | // exports the phony 'fuzz' target and dependencies on packages to |
| 497 | // core/main.mk so that we can use dist-for-goals. |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 498 | |
| 499 | ctx.Strict(s.fuzzPackagingArchModules, strings.Join(packages, " ")) |
| 500 | |
| 501 | ctx.Strict(s.fuzzTargetSharedDepsInstallPairs, |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 502 | strings.Join(s.FuzzPackager.SharedLibInstallStrings, " ")) |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 503 | |
| 504 | // Preallocate the slice of fuzz targets to minimise memory allocations. |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 505 | s.PreallocateSlice(ctx, s.allFuzzTargetsName) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 506 | } |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 507 | |
| 508 | // GetSharedLibsToZip finds and marks all the transiently-dependent shared libraries for |
| 509 | // packaging. |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 510 | func GetSharedLibsToZip(sharedLibraries android.Paths, module LinkableInterface, s *fuzz.FuzzPackager, archString string, destinationPathPrefix string, sharedLibraryInstalled *map[string]bool) []fuzz.FileToZip { |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 511 | var files []fuzz.FileToZip |
| 512 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 513 | fuzzDir := "fuzz" |
Cory Barker | 74aea6c | 2022-08-08 15:55:12 +0000 | [diff] [blame^] | 514 | if s.FuzzType == fuzz.AFL { |
| 515 | fuzzDir = "afl_fuzz" |
| 516 | } |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 517 | |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 518 | for _, library := range sharedLibraries { |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 519 | files = append(files, fuzz.FileToZip{library, destinationPathPrefix}) |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 520 | |
| 521 | // For each architecture-specific shared library dependency, we need to |
| 522 | // install it to the output directory. Setup the install destination here, |
| 523 | // which will be used by $(copy-many-files) in the Make backend. |
| 524 | installDestination := sharedLibraryInstallLocation( |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 525 | library, module.Host(), fuzzDir, archString) |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 526 | if (*sharedLibraryInstalled)[installDestination] { |
| 527 | continue |
| 528 | } |
| 529 | (*sharedLibraryInstalled)[installDestination] = true |
| 530 | |
| 531 | // Escape all the variables, as the install destination here will be called |
| 532 | // via. $(eval) in Make. |
| 533 | installDestination = strings.ReplaceAll( |
| 534 | installDestination, "$", "$$") |
| 535 | s.SharedLibInstallStrings = append(s.SharedLibInstallStrings, |
| 536 | library.String()+":"+installDestination) |
| 537 | |
| 538 | // Ensure that on device, the library is also reinstalled to the /symbols/ |
| 539 | // dir. Symbolized DSO's are always installed to the device when fuzzing, but |
| 540 | // we want symbolization tools (like `stack`) to be able to find the symbols |
| 541 | // in $ANDROID_PRODUCT_OUT/symbols automagically. |
| 542 | if !module.Host() { |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 543 | symbolsInstallDestination := sharedLibrarySymbolsInstallLocation(library, fuzzDir, archString) |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 544 | symbolsInstallDestination = strings.ReplaceAll(symbolsInstallDestination, "$", "$$") |
| 545 | s.SharedLibInstallStrings = append(s.SharedLibInstallStrings, |
| 546 | library.String()+":"+symbolsInstallDestination) |
| 547 | } |
| 548 | } |
| 549 | return files |
| 550 | } |