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 ( |
Kris Alder | f979ee3 | 2019-10-22 10:52:01 -0700 | [diff] [blame] | 18 | "encoding/json" |
Mitch Phillips | 4de896e | 2019-08-28 16:04:36 -0700 | [diff] [blame] | 19 | "path/filepath" |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 20 | "sort" |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 21 | "strings" |
Mitch Phillips | 4de896e | 2019-08-28 16:04:36 -0700 | [diff] [blame] | 22 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 23 | "android/soong/android" |
| 24 | "android/soong/cc/config" |
| 25 | ) |
| 26 | |
Kris Alder | f979ee3 | 2019-10-22 10:52:01 -0700 | [diff] [blame] | 27 | type FuzzConfig struct { |
| 28 | // Email address of people to CC on bugs or contact about this fuzz target. |
| 29 | Cc []string `json:"cc,omitempty"` |
hamzeh | 3478a0d | 2019-12-16 16:25:50 -0800 | [diff] [blame] | 30 | // Specify whether to enable continuous fuzzing on devices. Defaults to true. |
| 31 | Fuzz_on_haiku_device *bool `json:"fuzz_on_haiku_device,omitempty"` |
| 32 | // Specify whether to enable continuous fuzzing on host. Defaults to true. |
| 33 | Fuzz_on_haiku_host *bool `json:"fuzz_on_haiku_host,omitempty"` |
Kris Alder | f979ee3 | 2019-10-22 10:52:01 -0700 | [diff] [blame] | 34 | // Component in Google's bug tracking system that bugs should be filed to. |
| 35 | Componentid *int64 `json:"componentid,omitempty"` |
| 36 | // Hotlists in Google's bug tracking system that bugs should be marked with. |
| 37 | Hotlists []string `json:"hotlists,omitempty"` |
Kris Alder | e051d0d | 2020-04-28 18:32:23 +0000 | [diff] [blame] | 38 | // Specify whether this fuzz target was submitted by a researcher. Defaults |
| 39 | // to false. |
| 40 | Researcher_submitted *bool `json:"researcher_submitted,omitempty"` |
Kris Alder | 2598c9b | 2020-09-29 22:09:36 +0000 | [diff] [blame] | 41 | // Specify who should be acknowledged for CVEs in the Android Security |
| 42 | // Bulletin. |
| 43 | Acknowledgement []string `json:"acknowledgement,omitempty"` |
Kris Alder | c81f59f | 2021-01-07 20:57:23 +0000 | [diff] [blame^] | 44 | // Additional options to be passed to libfuzzer when run in Haiku. |
| 45 | Libfuzzer_options []string `json:"libfuzzer_options,omitempty"` |
| 46 | // Additional options to be passed to HWASAN when running on-device in Haiku. |
| 47 | Hwasan_options []string `json:"hwasan_options,omitempty"` |
| 48 | // Additional options to be passed to HWASAN when running on host in Haiku. |
| 49 | Asan_options []string `json:"asan_options,omitempty"` |
Kris Alder | f979ee3 | 2019-10-22 10:52:01 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | func (f *FuzzConfig) String() string { |
| 53 | b, err := json.Marshal(f) |
| 54 | if err != nil { |
| 55 | panic(err) |
| 56 | } |
| 57 | |
| 58 | return string(b) |
| 59 | } |
| 60 | |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 61 | type FuzzProperties struct { |
| 62 | // Optional list of seed files to be installed to the fuzz target's output |
| 63 | // directory. |
| 64 | Corpus []string `android:"path"` |
Tri Vo | ad172d8 | 2019-11-27 13:45:45 -0800 | [diff] [blame] | 65 | // Optional list of data files to be installed to the fuzz target's output |
| 66 | // directory. Directory structure relative to the module is preserved. |
| 67 | Data []string `android:"path"` |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 68 | // Optional dictionary to be installed to the fuzz target's output directory. |
| 69 | Dictionary *string `android:"path"` |
Kris Alder | f979ee3 | 2019-10-22 10:52:01 -0700 | [diff] [blame] | 70 | // Config for running the target on fuzzing infrastructure. |
| 71 | Fuzz_config *FuzzConfig |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 74 | func init() { |
| 75 | android.RegisterModuleType("cc_fuzz", FuzzFactory) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 76 | android.RegisterSingletonType("cc_fuzz_packaging", fuzzPackagingFactory) |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // cc_fuzz creates a host/device fuzzer binary. Host binaries can be found at |
| 80 | // $ANDROID_HOST_OUT/fuzz/, and device binaries can be found at /data/fuzz on |
| 81 | // your device, or $ANDROID_PRODUCT_OUT/data/fuzz in your build tree. |
| 82 | func FuzzFactory() android.Module { |
| 83 | module := NewFuzz(android.HostAndDeviceSupported) |
| 84 | return module.Init() |
| 85 | } |
| 86 | |
| 87 | func NewFuzzInstaller() *baseInstaller { |
| 88 | return NewBaseInstaller("fuzz", "fuzz", InstallInData) |
| 89 | } |
| 90 | |
| 91 | type fuzzBinary struct { |
| 92 | *binaryDecorator |
| 93 | *baseCompiler |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 94 | |
Mitch Phillips | 8a2bc0b | 2019-10-17 15:04:01 -0700 | [diff] [blame] | 95 | Properties FuzzProperties |
| 96 | dictionary android.Path |
| 97 | corpus android.Paths |
| 98 | corpusIntermediateDir android.Path |
Kris Alder | f979ee3 | 2019-10-22 10:52:01 -0700 | [diff] [blame] | 99 | config android.Path |
Tri Vo | ad172d8 | 2019-11-27 13:45:45 -0800 | [diff] [blame] | 100 | data android.Paths |
| 101 | dataIntermediateDir android.Path |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 102 | installedSharedDeps []string |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | func (fuzz *fuzzBinary) linkerProps() []interface{} { |
| 106 | props := fuzz.binaryDecorator.linkerProps() |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 107 | props = append(props, &fuzz.Properties) |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 108 | return props |
| 109 | } |
| 110 | |
| 111 | func (fuzz *fuzzBinary) linkerInit(ctx BaseModuleContext) { |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 112 | fuzz.binaryDecorator.linkerInit(ctx) |
| 113 | } |
| 114 | |
| 115 | func (fuzz *fuzzBinary) linkerDeps(ctx DepsContext, deps Deps) Deps { |
| 116 | deps.StaticLibs = append(deps.StaticLibs, |
| 117 | config.LibFuzzerRuntimeLibrary(ctx.toolchain())) |
| 118 | deps = fuzz.binaryDecorator.linkerDeps(ctx, deps) |
| 119 | return deps |
| 120 | } |
| 121 | |
| 122 | func (fuzz *fuzzBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
| 123 | flags = fuzz.binaryDecorator.linkerFlags(ctx, flags) |
Mitch Phillips | 1f7f54f | 2019-11-14 14:50:47 -0800 | [diff] [blame] | 124 | // RunPaths on devices isn't instantiated by the base linker. `../lib` for |
| 125 | // installed fuzz targets (both host and device), and `./lib` for fuzz |
| 126 | // target packages. |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 127 | flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/../lib`) |
Mitch Phillips | 1f7f54f | 2019-11-14 14:50:47 -0800 | [diff] [blame] | 128 | flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/lib`) |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 129 | return flags |
| 130 | } |
| 131 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 132 | // This function performs a breadth-first search over the provided module's |
| 133 | // dependencies using `visitDirectDeps` to enumerate all shared library |
| 134 | // dependencies. We require breadth-first expansion, as otherwise we may |
| 135 | // incorrectly use the core libraries (sanitizer runtimes, libc, libdl, etc.) |
| 136 | // from a dependency. This may cause issues when dependencies have explicit |
| 137 | // sanitizer tags, as we may get a dependency on an unsanitized libc, etc. |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 138 | func collectAllSharedDependencies(ctx android.SingletonContext, module android.Module) android.Paths { |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 139 | var fringe []android.Module |
| 140 | |
Mitch Phillips | c0b442f | 2020-04-27 16:44:58 -0700 | [diff] [blame] | 141 | seen := make(map[string]bool) |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 142 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 143 | // Enumerate the first level of dependencies, as we discard all non-library |
| 144 | // modules in the BFS loop below. |
| 145 | ctx.VisitDirectDeps(module, func(dep android.Module) { |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 146 | if isValidSharedDependency(dep) { |
Mitch Phillips | f50bddb | 2019-11-12 14:03:31 -0800 | [diff] [blame] | 147 | fringe = append(fringe, dep) |
| 148 | } |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 149 | }) |
| 150 | |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 151 | var sharedLibraries android.Paths |
| 152 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 153 | for i := 0; i < len(fringe); i++ { |
| 154 | module := fringe[i] |
Mitch Phillips | c0b442f | 2020-04-27 16:44:58 -0700 | [diff] [blame] | 155 | if seen[module.Name()] { |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 156 | continue |
| 157 | } |
Mitch Phillips | c0b442f | 2020-04-27 16:44:58 -0700 | [diff] [blame] | 158 | seen[module.Name()] = true |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 159 | |
| 160 | ccModule := module.(*Module) |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 161 | sharedLibraries = append(sharedLibraries, ccModule.UnstrippedOutputFile()) |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 162 | ctx.VisitDirectDeps(module, func(dep android.Module) { |
Mitch Phillips | c0b442f | 2020-04-27 16:44:58 -0700 | [diff] [blame] | 163 | if isValidSharedDependency(dep) && !seen[dep.Name()] { |
Mitch Phillips | f50bddb | 2019-11-12 14:03:31 -0800 | [diff] [blame] | 164 | fringe = append(fringe, dep) |
| 165 | } |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 166 | }) |
| 167 | } |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 168 | |
| 169 | return sharedLibraries |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | // This function takes a module and determines if it is a unique shared library |
| 173 | // that should be installed in the fuzz target output directories. This function |
| 174 | // returns true, unless: |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 175 | // - The module is not a shared library, or |
Martin Stjernholm | 02460ab | 2020-10-06 02:36:43 +0100 | [diff] [blame] | 176 | // - The module is a header, stub, or vendor-linked library, or |
| 177 | // - The module is a prebuilt and its source is available, or |
| 178 | // - The module is a versioned member of an SDK snapshot. |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 179 | func isValidSharedDependency(dependency android.Module) bool { |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 180 | // TODO(b/144090547): We should be parsing these modules using |
| 181 | // ModuleDependencyTag instead of the current brute-force checking. |
| 182 | |
Colin Cross | 31076b3 | 2020-10-23 17:22:06 -0700 | [diff] [blame] | 183 | linkable, ok := dependency.(LinkableInterface) |
| 184 | if !ok || !linkable.CcLibraryInterface() { |
| 185 | // Discard non-linkables. |
| 186 | return false |
| 187 | } |
| 188 | |
| 189 | if !linkable.Shared() { |
| 190 | // Discard static libs. |
| 191 | return false |
| 192 | } |
| 193 | |
| 194 | if linkable.UseVndk() { |
| 195 | // Discard vendor linked libraries. |
| 196 | return false |
| 197 | } |
| 198 | |
| 199 | if lib := moduleLibraryInterface(dependency); lib != nil && lib.buildStubs() && linkable.CcLibrary() { |
Mitch Phillips | f50bddb | 2019-11-12 14:03:31 -0800 | [diff] [blame] | 200 | // Discard stubs libs (only CCLibrary variants). Prebuilt libraries should not |
| 201 | // be excluded on the basis of they're not CCLibrary()'s. |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 202 | return false |
| 203 | } |
| 204 | |
Mitch Phillips | f50bddb | 2019-11-12 14:03:31 -0800 | [diff] [blame] | 205 | // We discarded module stubs libraries above, but the LLNDK prebuilts stubs |
| 206 | // libraries must be handled differently - by looking for the stubDecorator. |
| 207 | // Discard LLNDK prebuilts stubs as well. |
| 208 | if ccLibrary, isCcLibrary := dependency.(*Module); isCcLibrary { |
| 209 | if _, isLLndkStubLibrary := ccLibrary.linker.(*stubDecorator); isLLndkStubLibrary { |
| 210 | return false |
| 211 | } |
| 212 | } |
| 213 | |
Martin Stjernholm | 02460ab | 2020-10-06 02:36:43 +0100 | [diff] [blame] | 214 | // If the same library is present both as source and a prebuilt we must pick |
| 215 | // only one to avoid a conflict. Always prefer the source since the prebuilt |
| 216 | // probably won't be built with sanitizers enabled. |
| 217 | if prebuilt, ok := dependency.(android.PrebuiltInterface); ok && |
| 218 | prebuilt.Prebuilt() != nil && prebuilt.Prebuilt().SourceExists() { |
| 219 | return false |
| 220 | } |
| 221 | |
| 222 | // Discard versioned members of SDK snapshots, because they will conflict with |
| 223 | // unversioned ones. |
| 224 | if sdkMember, ok := dependency.(android.SdkAware); ok && !sdkMember.ContainingSdk().Unversioned() { |
| 225 | return false |
| 226 | } |
| 227 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 228 | return true |
| 229 | } |
| 230 | |
| 231 | func sharedLibraryInstallLocation( |
| 232 | libraryPath android.Path, isHost bool, archString string) string { |
| 233 | installLocation := "$(PRODUCT_OUT)/data" |
| 234 | if isHost { |
| 235 | installLocation = "$(HOST_OUT)" |
| 236 | } |
| 237 | installLocation = filepath.Join( |
| 238 | installLocation, "fuzz", archString, "lib", libraryPath.Base()) |
| 239 | return installLocation |
| 240 | } |
| 241 | |
Mitch Phillips | 0bf9713 | 2020-03-06 09:38:12 -0800 | [diff] [blame] | 242 | // Get the device-only shared library symbols install directory. |
| 243 | func sharedLibrarySymbolsInstallLocation(libraryPath android.Path, archString string) string { |
| 244 | return filepath.Join("$(PRODUCT_OUT)/symbols/data/fuzz/", archString, "/lib/", libraryPath.Base()) |
| 245 | } |
| 246 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 247 | func (fuzz *fuzzBinary) install(ctx ModuleContext, file android.Path) { |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 248 | fuzz.binaryDecorator.baseInstaller.dir = filepath.Join( |
| 249 | "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName()) |
| 250 | fuzz.binaryDecorator.baseInstaller.dir64 = filepath.Join( |
| 251 | "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName()) |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 252 | fuzz.binaryDecorator.baseInstaller.install(ctx, file) |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 253 | |
| 254 | fuzz.corpus = android.PathsForModuleSrc(ctx, fuzz.Properties.Corpus) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 255 | builder := android.NewRuleBuilder(pctx, ctx) |
Mitch Phillips | 8a2bc0b | 2019-10-17 15:04:01 -0700 | [diff] [blame] | 256 | intermediateDir := android.PathForModuleOut(ctx, "corpus") |
| 257 | for _, entry := range fuzz.corpus { |
| 258 | builder.Command().Text("cp"). |
| 259 | Input(entry). |
| 260 | Output(intermediateDir.Join(ctx, entry.Base())) |
| 261 | } |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 262 | builder.Build("copy_corpus", "copy corpus") |
Mitch Phillips | 8a2bc0b | 2019-10-17 15:04:01 -0700 | [diff] [blame] | 263 | fuzz.corpusIntermediateDir = intermediateDir |
| 264 | |
Tri Vo | ad172d8 | 2019-11-27 13:45:45 -0800 | [diff] [blame] | 265 | fuzz.data = android.PathsForModuleSrc(ctx, fuzz.Properties.Data) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 266 | builder = android.NewRuleBuilder(pctx, ctx) |
Tri Vo | ad172d8 | 2019-11-27 13:45:45 -0800 | [diff] [blame] | 267 | intermediateDir = android.PathForModuleOut(ctx, "data") |
| 268 | for _, entry := range fuzz.data { |
| 269 | builder.Command().Text("cp"). |
| 270 | Input(entry). |
| 271 | Output(intermediateDir.Join(ctx, entry.Rel())) |
| 272 | } |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 273 | builder.Build("copy_data", "copy data") |
Tri Vo | ad172d8 | 2019-11-27 13:45:45 -0800 | [diff] [blame] | 274 | fuzz.dataIntermediateDir = intermediateDir |
| 275 | |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 276 | if fuzz.Properties.Dictionary != nil { |
| 277 | fuzz.dictionary = android.PathForModuleSrc(ctx, *fuzz.Properties.Dictionary) |
| 278 | if fuzz.dictionary.Ext() != ".dict" { |
| 279 | ctx.PropertyErrorf("dictionary", |
| 280 | "Fuzzer dictionary %q does not have '.dict' extension", |
| 281 | fuzz.dictionary.String()) |
| 282 | } |
| 283 | } |
Kris Alder | f979ee3 | 2019-10-22 10:52:01 -0700 | [diff] [blame] | 284 | |
| 285 | if fuzz.Properties.Fuzz_config != nil { |
Kris Alder | db97af4 | 2019-10-30 10:17:04 -0700 | [diff] [blame] | 286 | configPath := android.PathForModuleOut(ctx, "config").Join(ctx, "config.json") |
Colin Cross | cf371cc | 2020-11-13 11:48:42 -0800 | [diff] [blame] | 287 | android.WriteFileRule(ctx, configPath, fuzz.Properties.Fuzz_config.String()) |
Kris Alder | f979ee3 | 2019-10-22 10:52:01 -0700 | [diff] [blame] | 288 | fuzz.config = configPath |
| 289 | } |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 290 | |
| 291 | // Grab the list of required shared libraries. |
Mitch Phillips | c0b442f | 2020-04-27 16:44:58 -0700 | [diff] [blame] | 292 | seen := make(map[string]bool) |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 293 | var sharedLibraries android.Paths |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 294 | ctx.WalkDeps(func(child, parent android.Module) bool { |
Mitch Phillips | c0b442f | 2020-04-27 16:44:58 -0700 | [diff] [blame] | 295 | if seen[child.Name()] { |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 296 | return false |
| 297 | } |
Mitch Phillips | c0b442f | 2020-04-27 16:44:58 -0700 | [diff] [blame] | 298 | seen[child.Name()] = true |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 299 | |
| 300 | if isValidSharedDependency(child) { |
| 301 | sharedLibraries = append(sharedLibraries, child.(*Module).UnstrippedOutputFile()) |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 302 | return true |
| 303 | } |
| 304 | return false |
| 305 | }) |
| 306 | |
| 307 | for _, lib := range sharedLibraries { |
| 308 | fuzz.installedSharedDeps = append(fuzz.installedSharedDeps, |
| 309 | sharedLibraryInstallLocation( |
| 310 | lib, ctx.Host(), ctx.Arch().ArchType.String())) |
Mitch Phillips | 0bf9713 | 2020-03-06 09:38:12 -0800 | [diff] [blame] | 311 | |
| 312 | // Also add the dependency on the shared library symbols dir. |
| 313 | if !ctx.Host() { |
| 314 | fuzz.installedSharedDeps = append(fuzz.installedSharedDeps, |
| 315 | sharedLibrarySymbolsInstallLocation(lib, ctx.Arch().ArchType.String())) |
| 316 | } |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 317 | } |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | func NewFuzz(hod android.HostOrDeviceSupported) *Module { |
| 321 | module, binary := NewBinary(hod) |
| 322 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 323 | binary.baseInstaller = NewFuzzInstaller() |
| 324 | module.sanitize.SetSanitizer(fuzzer, true) |
| 325 | |
| 326 | fuzz := &fuzzBinary{ |
| 327 | binaryDecorator: binary, |
| 328 | baseCompiler: NewBaseCompiler(), |
| 329 | } |
| 330 | module.compiler = fuzz |
| 331 | module.linker = fuzz |
| 332 | module.installer = fuzz |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 333 | |
| 334 | // The fuzzer runtime is not present for darwin host modules, disable cc_fuzz modules when targeting darwin. |
| 335 | android.AddLoadHook(module, func(ctx android.LoadHookContext) { |
Alex Light | 71123ec | 2019-07-24 13:34:19 -0700 | [diff] [blame] | 336 | disableDarwinAndLinuxBionic := struct { |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 337 | Target struct { |
| 338 | Darwin struct { |
| 339 | Enabled *bool |
| 340 | } |
Alex Light | 71123ec | 2019-07-24 13:34:19 -0700 | [diff] [blame] | 341 | Linux_bionic struct { |
| 342 | Enabled *bool |
| 343 | } |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 344 | } |
| 345 | }{} |
Alex Light | 71123ec | 2019-07-24 13:34:19 -0700 | [diff] [blame] | 346 | disableDarwinAndLinuxBionic.Target.Darwin.Enabled = BoolPtr(false) |
| 347 | disableDarwinAndLinuxBionic.Target.Linux_bionic.Enabled = BoolPtr(false) |
| 348 | ctx.AppendProperties(&disableDarwinAndLinuxBionic) |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 349 | }) |
| 350 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 351 | return module |
| 352 | } |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 353 | |
| 354 | // Responsible for generating GNU Make rules that package fuzz targets into |
| 355 | // their architecture & target/host specific zip file. |
| 356 | type fuzzPackager struct { |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 357 | packages android.Paths |
| 358 | sharedLibInstallStrings []string |
| 359 | fuzzTargets map[string]bool |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | func fuzzPackagingFactory() android.Singleton { |
| 363 | return &fuzzPackager{} |
| 364 | } |
| 365 | |
| 366 | type fileToZip struct { |
| 367 | SourceFilePath android.Path |
| 368 | DestinationPathPrefix string |
| 369 | } |
| 370 | |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 371 | type archOs struct { |
| 372 | hostOrTarget string |
| 373 | arch string |
| 374 | dir string |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 377 | func (s *fuzzPackager) GenerateBuildActions(ctx android.SingletonContext) { |
| 378 | // Map between each architecture + host/device combination, and the files that |
| 379 | // need to be packaged (in the tuple of {source file, destination folder in |
| 380 | // archive}). |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 381 | archDirs := make(map[archOs][]fileToZip) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 382 | |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 383 | // Map tracking whether each shared library has an install rule to avoid duplicate install rules from |
| 384 | // multiple fuzzers that depend on the same shared library. |
| 385 | sharedLibraryInstalled := make(map[string]bool) |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 386 | |
| 387 | // List of individual fuzz targets, so that 'make fuzz' also installs the targets |
| 388 | // to the correct output directories as well. |
| 389 | s.fuzzTargets = make(map[string]bool) |
| 390 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 391 | ctx.VisitAllModules(func(module android.Module) { |
| 392 | // Discard non-fuzz targets. |
| 393 | ccModule, ok := module.(*Module) |
| 394 | if !ok { |
| 395 | return |
| 396 | } |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 397 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 398 | fuzzModule, ok := ccModule.compiler.(*fuzzBinary) |
| 399 | if !ok { |
| 400 | return |
| 401 | } |
| 402 | |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 403 | // Discard ramdisk + vendor_ramdisk + recovery modules, they're duplicates of |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 404 | // fuzz targets we're going to package anyway. |
| 405 | if !ccModule.Enabled() || ccModule.Properties.PreventInstall || |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 406 | ccModule.InRamdisk() || ccModule.InVendorRamdisk() || ccModule.InRecovery() { |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 407 | return |
| 408 | } |
| 409 | |
Mitch Phillips | 6a9bf21 | 2019-12-05 07:36:11 -0800 | [diff] [blame] | 410 | // Discard modules that are in an unavailable namespace. |
| 411 | if !ccModule.ExportedToMake() { |
| 412 | return |
| 413 | } |
| 414 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 415 | hostOrTargetString := "target" |
| 416 | if ccModule.Host() { |
| 417 | hostOrTargetString = "host" |
| 418 | } |
| 419 | |
| 420 | archString := ccModule.Arch().ArchType.String() |
| 421 | archDir := android.PathForIntermediates(ctx, "fuzz", hostOrTargetString, archString) |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 422 | archOs := archOs{hostOrTarget: hostOrTargetString, arch: archString, dir: archDir.String()} |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 423 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 424 | // Grab the list of required shared libraries. |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 425 | sharedLibraries := collectAllSharedDependencies(ctx, module) |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 426 | |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 427 | var files []fileToZip |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 428 | builder := android.NewRuleBuilder(pctx, ctx) |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 429 | |
| 430 | // Package the corpora into a zipfile. |
| 431 | if fuzzModule.corpus != nil { |
| 432 | corpusZip := archDir.Join(ctx, module.Name()+"_seed_corpus.zip") |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 433 | command := builder.Command().BuiltTool("soong_zip"). |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 434 | Flag("-j"). |
| 435 | FlagWithOutput("-o ", corpusZip) |
Colin Cross | 053fca1 | 2020-08-19 13:51:47 -0700 | [diff] [blame] | 436 | command.FlagWithRspFileInputList("-r ", fuzzModule.corpus) |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 437 | files = append(files, fileToZip{corpusZip, ""}) |
| 438 | } |
| 439 | |
Tri Vo | ad172d8 | 2019-11-27 13:45:45 -0800 | [diff] [blame] | 440 | // Package the data into a zipfile. |
| 441 | if fuzzModule.data != nil { |
| 442 | dataZip := archDir.Join(ctx, module.Name()+"_data.zip") |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 443 | command := builder.Command().BuiltTool("soong_zip"). |
Tri Vo | ad172d8 | 2019-11-27 13:45:45 -0800 | [diff] [blame] | 444 | FlagWithOutput("-o ", dataZip) |
| 445 | for _, f := range fuzzModule.data { |
| 446 | intermediateDir := strings.TrimSuffix(f.String(), f.Rel()) |
| 447 | command.FlagWithArg("-C ", intermediateDir) |
| 448 | command.FlagWithInput("-f ", f) |
| 449 | } |
| 450 | files = append(files, fileToZip{dataZip, ""}) |
| 451 | } |
| 452 | |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 453 | // Find and mark all the transiently-dependent shared libraries for |
| 454 | // packaging. |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 455 | for _, library := range sharedLibraries { |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 456 | files = append(files, fileToZip{library, "lib"}) |
Mitch Phillips | 13ed3f5 | 2019-11-12 11:12:10 -0800 | [diff] [blame] | 457 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 458 | // For each architecture-specific shared library dependency, we need to |
| 459 | // install it to the output directory. Setup the install destination here, |
| 460 | // which will be used by $(copy-many-files) in the Make backend. |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 461 | installDestination := sharedLibraryInstallLocation( |
| 462 | library, ccModule.Host(), archString) |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 463 | if sharedLibraryInstalled[installDestination] { |
| 464 | continue |
| 465 | } |
| 466 | sharedLibraryInstalled[installDestination] = true |
Mitch Phillips | 0bf9713 | 2020-03-06 09:38:12 -0800 | [diff] [blame] | 467 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 468 | // Escape all the variables, as the install destination here will be called |
| 469 | // via. $(eval) in Make. |
| 470 | installDestination = strings.ReplaceAll( |
| 471 | installDestination, "$", "$$") |
| 472 | s.sharedLibInstallStrings = append(s.sharedLibInstallStrings, |
| 473 | library.String()+":"+installDestination) |
Mitch Phillips | 0bf9713 | 2020-03-06 09:38:12 -0800 | [diff] [blame] | 474 | |
| 475 | // Ensure that on device, the library is also reinstalled to the /symbols/ |
| 476 | // dir. Symbolized DSO's are always installed to the device when fuzzing, but |
| 477 | // we want symbolization tools (like `stack`) to be able to find the symbols |
| 478 | // in $ANDROID_PRODUCT_OUT/symbols automagically. |
| 479 | if !ccModule.Host() { |
| 480 | symbolsInstallDestination := sharedLibrarySymbolsInstallLocation(library, archString) |
| 481 | symbolsInstallDestination = strings.ReplaceAll(symbolsInstallDestination, "$", "$$") |
| 482 | s.sharedLibInstallStrings = append(s.sharedLibInstallStrings, |
| 483 | library.String()+":"+symbolsInstallDestination) |
| 484 | } |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 487 | // The executable. |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 488 | files = append(files, fileToZip{ccModule.UnstrippedOutputFile(), ""}) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 489 | |
| 490 | // The dictionary. |
| 491 | if fuzzModule.dictionary != nil { |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 492 | files = append(files, fileToZip{fuzzModule.dictionary, ""}) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 493 | } |
Kris Alder | f979ee3 | 2019-10-22 10:52:01 -0700 | [diff] [blame] | 494 | |
| 495 | // Additional fuzz config. |
| 496 | if fuzzModule.config != nil { |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 497 | files = append(files, fileToZip{fuzzModule.config, ""}) |
Kris Alder | f979ee3 | 2019-10-22 10:52:01 -0700 | [diff] [blame] | 498 | } |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 499 | |
| 500 | fuzzZip := archDir.Join(ctx, module.Name()+".zip") |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 501 | command := builder.Command().BuiltTool("soong_zip"). |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 502 | Flag("-j"). |
| 503 | FlagWithOutput("-o ", fuzzZip) |
| 504 | for _, file := range files { |
| 505 | if file.DestinationPathPrefix != "" { |
| 506 | command.FlagWithArg("-P ", file.DestinationPathPrefix) |
| 507 | } else { |
| 508 | command.Flag("-P ''") |
| 509 | } |
| 510 | command.FlagWithInput("-f ", file.SourceFilePath) |
| 511 | } |
| 512 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 513 | builder.Build("create-"+fuzzZip.String(), |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 514 | "Package "+module.Name()+" for "+archString+"-"+hostOrTargetString) |
| 515 | |
Mitch Phillips | 18e6719 | 2020-02-24 08:26:20 -0800 | [diff] [blame] | 516 | // Don't add modules to 'make haiku' that are set to not be exported to the |
| 517 | // fuzzing infrastructure. |
| 518 | if config := fuzzModule.Properties.Fuzz_config; config != nil { |
| 519 | if ccModule.Host() && !BoolDefault(config.Fuzz_on_haiku_host, true) { |
| 520 | return |
| 521 | } else if !BoolDefault(config.Fuzz_on_haiku_device, true) { |
| 522 | return |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | s.fuzzTargets[module.Name()] = true |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 527 | archDirs[archOs] = append(archDirs[archOs], fileToZip{fuzzZip, ""}) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 528 | }) |
| 529 | |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 530 | var archOsList []archOs |
| 531 | for archOs := range archDirs { |
| 532 | archOsList = append(archOsList, archOs) |
| 533 | } |
| 534 | sort.Slice(archOsList, func(i, j int) bool { return archOsList[i].dir < archOsList[j].dir }) |
| 535 | |
| 536 | for _, archOs := range archOsList { |
| 537 | filesToZip := archDirs[archOs] |
| 538 | arch := archOs.arch |
| 539 | hostOrTarget := archOs.hostOrTarget |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 540 | builder := android.NewRuleBuilder(pctx, ctx) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 541 | outputFile := android.PathForOutput(ctx, "fuzz-"+hostOrTarget+"-"+arch+".zip") |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 542 | s.packages = append(s.packages, outputFile) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 543 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 544 | command := builder.Command().BuiltTool("soong_zip"). |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 545 | Flag("-j"). |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 546 | FlagWithOutput("-o ", outputFile). |
| 547 | Flag("-L 0") // No need to try and re-compress the zipfiles. |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 548 | |
| 549 | for _, fileToZip := range filesToZip { |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 550 | if fileToZip.DestinationPathPrefix != "" { |
| 551 | command.FlagWithArg("-P ", fileToZip.DestinationPathPrefix) |
| 552 | } else { |
| 553 | command.Flag("-P ''") |
| 554 | } |
| 555 | command.FlagWithInput("-f ", fileToZip.SourceFilePath) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 558 | builder.Build("create-fuzz-package-"+arch+"-"+hostOrTarget, |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 559 | "Create fuzz target packages for "+arch+"-"+hostOrTarget) |
| 560 | } |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 561 | } |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 562 | |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 563 | func (s *fuzzPackager) MakeVars(ctx android.MakeVarsContext) { |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 564 | packages := s.packages.Strings() |
| 565 | sort.Strings(packages) |
| 566 | sort.Strings(s.sharedLibInstallStrings) |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 567 | // TODO(mitchp): Migrate this to use MakeVarsContext::DistForGoal() when it's |
| 568 | // ready to handle phony targets created in Soong. In the meantime, this |
| 569 | // exports the phony 'fuzz' target and dependencies on packages to |
| 570 | // core/main.mk so that we can use dist-for-goals. |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 571 | ctx.Strict("SOONG_FUZZ_PACKAGING_ARCH_MODULES", strings.Join(packages, " ")) |
| 572 | ctx.Strict("FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS", |
| 573 | strings.Join(s.sharedLibInstallStrings, " ")) |
| 574 | |
| 575 | // Preallocate the slice of fuzz targets to minimise memory allocations. |
| 576 | fuzzTargets := make([]string, 0, len(s.fuzzTargets)) |
| 577 | for target, _ := range s.fuzzTargets { |
| 578 | fuzzTargets = append(fuzzTargets, target) |
| 579 | } |
| 580 | sort.Strings(fuzzTargets) |
| 581 | ctx.Strict("ALL_FUZZ_TARGETS", strings.Join(fuzzTargets, " ")) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 582 | } |