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