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"` |
| 30 | // Boolean specifying whether to disable the fuzz target from running |
| 31 | // automatically in continuous fuzzing infrastructure. |
| 32 | Disable *bool `json:"disable,omitempty"` |
| 33 | // Component in Google's bug tracking system that bugs should be filed to. |
| 34 | Componentid *int64 `json:"componentid,omitempty"` |
| 35 | // Hotlists in Google's bug tracking system that bugs should be marked with. |
| 36 | Hotlists []string `json:"hotlists,omitempty"` |
| 37 | } |
| 38 | |
| 39 | func (f *FuzzConfig) String() string { |
| 40 | b, err := json.Marshal(f) |
| 41 | if err != nil { |
| 42 | panic(err) |
| 43 | } |
| 44 | |
| 45 | return string(b) |
| 46 | } |
| 47 | |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 48 | type FuzzProperties struct { |
| 49 | // Optional list of seed files to be installed to the fuzz target's output |
| 50 | // directory. |
| 51 | Corpus []string `android:"path"` |
| 52 | // Optional dictionary to be installed to the fuzz target's output directory. |
| 53 | Dictionary *string `android:"path"` |
Kris Alder | f979ee3 | 2019-10-22 10:52:01 -0700 | [diff] [blame] | 54 | // Config for running the target on fuzzing infrastructure. |
| 55 | Fuzz_config *FuzzConfig |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 58 | func init() { |
| 59 | android.RegisterModuleType("cc_fuzz", FuzzFactory) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 60 | android.RegisterSingletonType("cc_fuzz_packaging", fuzzPackagingFactory) |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | // cc_fuzz creates a host/device fuzzer binary. Host binaries can be found at |
| 64 | // $ANDROID_HOST_OUT/fuzz/, and device binaries can be found at /data/fuzz on |
| 65 | // your device, or $ANDROID_PRODUCT_OUT/data/fuzz in your build tree. |
| 66 | func FuzzFactory() android.Module { |
| 67 | module := NewFuzz(android.HostAndDeviceSupported) |
| 68 | return module.Init() |
| 69 | } |
| 70 | |
| 71 | func NewFuzzInstaller() *baseInstaller { |
| 72 | return NewBaseInstaller("fuzz", "fuzz", InstallInData) |
| 73 | } |
| 74 | |
| 75 | type fuzzBinary struct { |
| 76 | *binaryDecorator |
| 77 | *baseCompiler |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 78 | |
Mitch Phillips | 8a2bc0b | 2019-10-17 15:04:01 -0700 | [diff] [blame] | 79 | Properties FuzzProperties |
| 80 | dictionary android.Path |
| 81 | corpus android.Paths |
| 82 | corpusIntermediateDir android.Path |
Kris Alder | f979ee3 | 2019-10-22 10:52:01 -0700 | [diff] [blame] | 83 | config android.Path |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame^] | 84 | installedSharedDeps []string |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | func (fuzz *fuzzBinary) linkerProps() []interface{} { |
| 88 | props := fuzz.binaryDecorator.linkerProps() |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 89 | props = append(props, &fuzz.Properties) |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 90 | return props |
| 91 | } |
| 92 | |
| 93 | func (fuzz *fuzzBinary) linkerInit(ctx BaseModuleContext) { |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 94 | fuzz.binaryDecorator.linkerInit(ctx) |
| 95 | } |
| 96 | |
| 97 | func (fuzz *fuzzBinary) linkerDeps(ctx DepsContext, deps Deps) Deps { |
| 98 | deps.StaticLibs = append(deps.StaticLibs, |
| 99 | config.LibFuzzerRuntimeLibrary(ctx.toolchain())) |
| 100 | deps = fuzz.binaryDecorator.linkerDeps(ctx, deps) |
| 101 | return deps |
| 102 | } |
| 103 | |
| 104 | func (fuzz *fuzzBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
| 105 | flags = fuzz.binaryDecorator.linkerFlags(ctx, flags) |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame^] | 106 | // RunPaths on devices isn't instantiated by the base linker. |
| 107 | flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/../lib`) |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 108 | return flags |
| 109 | } |
| 110 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame^] | 111 | // This function performs a breadth-first search over the provided module's |
| 112 | // dependencies using `visitDirectDeps` to enumerate all shared library |
| 113 | // dependencies. We require breadth-first expansion, as otherwise we may |
| 114 | // incorrectly use the core libraries (sanitizer runtimes, libc, libdl, etc.) |
| 115 | // from a dependency. This may cause issues when dependencies have explicit |
| 116 | // sanitizer tags, as we may get a dependency on an unsanitized libc, etc. |
| 117 | func collectAllSharedDependencies( |
| 118 | module android.Module, |
| 119 | sharedDeps map[string]android.Path, |
| 120 | ctx android.SingletonContext) { |
| 121 | var fringe []android.Module |
| 122 | |
| 123 | // Enumerate the first level of dependencies, as we discard all non-library |
| 124 | // modules in the BFS loop below. |
| 125 | ctx.VisitDirectDeps(module, func(dep android.Module) { |
| 126 | fringe = append(fringe, dep) |
| 127 | }) |
| 128 | |
| 129 | for i := 0; i < len(fringe); i++ { |
| 130 | module := fringe[i] |
| 131 | if !isValidSharedDependency(module, sharedDeps) { |
| 132 | continue |
| 133 | } |
| 134 | |
| 135 | ccModule := module.(*Module) |
| 136 | sharedDeps[ccModule.Name()] = ccModule.UnstrippedOutputFile() |
| 137 | ctx.VisitDirectDeps(module, func(dep android.Module) { |
| 138 | fringe = append(fringe, dep) |
| 139 | }) |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // This function takes a module and determines if it is a unique shared library |
| 144 | // that should be installed in the fuzz target output directories. This function |
| 145 | // returns true, unless: |
| 146 | // - The module already exists in `sharedDeps`, or |
| 147 | // - The module is not a shared library, or |
| 148 | // - The module is a header, stub, or vendor-linked library. |
| 149 | func isValidSharedDependency( |
| 150 | dependency android.Module, |
| 151 | sharedDeps map[string]android.Path) bool { |
| 152 | // TODO(b/144090547): We should be parsing these modules using |
| 153 | // ModuleDependencyTag instead of the current brute-force checking. |
| 154 | |
| 155 | if linkable, ok := dependency.(LinkableInterface); !ok || // Discard non-linkables. |
| 156 | !linkable.CcLibraryInterface() || !linkable.Shared() || // Discard static libs. |
| 157 | linkable.UseVndk() || // Discard vendor linked libraries. |
| 158 | !linkable.CcLibrary() || linkable.BuildStubs() { // Discard stubs libs (only CCLibrary variants). |
| 159 | return false |
| 160 | } |
| 161 | |
| 162 | // If this library has already been traversed, we don't need to do any more work. |
| 163 | if _, exists := sharedDeps[dependency.Name()]; exists { |
| 164 | return false |
| 165 | } |
| 166 | return true |
| 167 | } |
| 168 | |
| 169 | func sharedLibraryInstallLocation( |
| 170 | libraryPath android.Path, isHost bool, archString string) string { |
| 171 | installLocation := "$(PRODUCT_OUT)/data" |
| 172 | if isHost { |
| 173 | installLocation = "$(HOST_OUT)" |
| 174 | } |
| 175 | installLocation = filepath.Join( |
| 176 | installLocation, "fuzz", archString, "lib", libraryPath.Base()) |
| 177 | return installLocation |
| 178 | } |
| 179 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 180 | func (fuzz *fuzzBinary) install(ctx ModuleContext, file android.Path) { |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 181 | fuzz.binaryDecorator.baseInstaller.dir = filepath.Join( |
| 182 | "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName()) |
| 183 | fuzz.binaryDecorator.baseInstaller.dir64 = filepath.Join( |
| 184 | "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName()) |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 185 | fuzz.binaryDecorator.baseInstaller.install(ctx, file) |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 186 | |
| 187 | fuzz.corpus = android.PathsForModuleSrc(ctx, fuzz.Properties.Corpus) |
Mitch Phillips | 8a2bc0b | 2019-10-17 15:04:01 -0700 | [diff] [blame] | 188 | builder := android.NewRuleBuilder() |
| 189 | intermediateDir := android.PathForModuleOut(ctx, "corpus") |
| 190 | for _, entry := range fuzz.corpus { |
| 191 | builder.Command().Text("cp"). |
| 192 | Input(entry). |
| 193 | Output(intermediateDir.Join(ctx, entry.Base())) |
| 194 | } |
| 195 | builder.Build(pctx, ctx, "copy_corpus", "copy corpus") |
| 196 | fuzz.corpusIntermediateDir = intermediateDir |
| 197 | |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 198 | if fuzz.Properties.Dictionary != nil { |
| 199 | fuzz.dictionary = android.PathForModuleSrc(ctx, *fuzz.Properties.Dictionary) |
| 200 | if fuzz.dictionary.Ext() != ".dict" { |
| 201 | ctx.PropertyErrorf("dictionary", |
| 202 | "Fuzzer dictionary %q does not have '.dict' extension", |
| 203 | fuzz.dictionary.String()) |
| 204 | } |
| 205 | } |
Kris Alder | f979ee3 | 2019-10-22 10:52:01 -0700 | [diff] [blame] | 206 | |
| 207 | if fuzz.Properties.Fuzz_config != nil { |
Kris Alder | db97af4 | 2019-10-30 10:17:04 -0700 | [diff] [blame] | 208 | configPath := android.PathForModuleOut(ctx, "config").Join(ctx, "config.json") |
Kris Alder | f979ee3 | 2019-10-22 10:52:01 -0700 | [diff] [blame] | 209 | ctx.Build(pctx, android.BuildParams{ |
| 210 | Rule: android.WriteFile, |
| 211 | Description: "fuzzer infrastructure configuration", |
| 212 | Output: configPath, |
| 213 | Args: map[string]string{ |
| 214 | "content": fuzz.Properties.Fuzz_config.String(), |
| 215 | }, |
| 216 | }) |
| 217 | fuzz.config = configPath |
| 218 | } |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame^] | 219 | |
| 220 | // Grab the list of required shared libraries. |
| 221 | sharedLibraries := make(map[string]android.Path) |
| 222 | ctx.WalkDeps(func(child, parent android.Module) bool { |
| 223 | if isValidSharedDependency(child, sharedLibraries) { |
| 224 | sharedLibraries[child.Name()] = child.(*Module).UnstrippedOutputFile() |
| 225 | return true |
| 226 | } |
| 227 | return false |
| 228 | }) |
| 229 | |
| 230 | for _, lib := range sharedLibraries { |
| 231 | fuzz.installedSharedDeps = append(fuzz.installedSharedDeps, |
| 232 | sharedLibraryInstallLocation( |
| 233 | lib, ctx.Host(), ctx.Arch().ArchType.String())) |
| 234 | } |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | func NewFuzz(hod android.HostOrDeviceSupported) *Module { |
| 238 | module, binary := NewBinary(hod) |
| 239 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 240 | binary.baseInstaller = NewFuzzInstaller() |
| 241 | module.sanitize.SetSanitizer(fuzzer, true) |
| 242 | |
| 243 | fuzz := &fuzzBinary{ |
| 244 | binaryDecorator: binary, |
| 245 | baseCompiler: NewBaseCompiler(), |
| 246 | } |
| 247 | module.compiler = fuzz |
| 248 | module.linker = fuzz |
| 249 | module.installer = fuzz |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 250 | |
| 251 | // The fuzzer runtime is not present for darwin host modules, disable cc_fuzz modules when targeting darwin. |
| 252 | android.AddLoadHook(module, func(ctx android.LoadHookContext) { |
Alex Light | 71123ec | 2019-07-24 13:34:19 -0700 | [diff] [blame] | 253 | disableDarwinAndLinuxBionic := struct { |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 254 | Target struct { |
| 255 | Darwin struct { |
| 256 | Enabled *bool |
| 257 | } |
Alex Light | 71123ec | 2019-07-24 13:34:19 -0700 | [diff] [blame] | 258 | Linux_bionic struct { |
| 259 | Enabled *bool |
| 260 | } |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 261 | } |
| 262 | }{} |
Alex Light | 71123ec | 2019-07-24 13:34:19 -0700 | [diff] [blame] | 263 | disableDarwinAndLinuxBionic.Target.Darwin.Enabled = BoolPtr(false) |
| 264 | disableDarwinAndLinuxBionic.Target.Linux_bionic.Enabled = BoolPtr(false) |
| 265 | ctx.AppendProperties(&disableDarwinAndLinuxBionic) |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 266 | }) |
| 267 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 268 | return module |
| 269 | } |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 270 | |
| 271 | // Responsible for generating GNU Make rules that package fuzz targets into |
| 272 | // their architecture & target/host specific zip file. |
| 273 | type fuzzPackager struct { |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame^] | 274 | packages android.Paths |
| 275 | sharedLibInstallStrings []string |
| 276 | fuzzTargets map[string]bool |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | func fuzzPackagingFactory() android.Singleton { |
| 280 | return &fuzzPackager{} |
| 281 | } |
| 282 | |
| 283 | type fileToZip struct { |
| 284 | SourceFilePath android.Path |
| 285 | DestinationPathPrefix string |
| 286 | } |
| 287 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame^] | 288 | type archAndLibraryKey struct { |
| 289 | ArchDir android.OutputPath |
| 290 | Library android.Path |
| 291 | } |
| 292 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 293 | func (s *fuzzPackager) GenerateBuildActions(ctx android.SingletonContext) { |
| 294 | // Map between each architecture + host/device combination, and the files that |
| 295 | // need to be packaged (in the tuple of {source file, destination folder in |
| 296 | // archive}). |
| 297 | archDirs := make(map[android.OutputPath][]fileToZip) |
| 298 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame^] | 299 | // List of shared library dependencies for each architecture + host/device combo. |
| 300 | archSharedLibraryDeps := make(map[archAndLibraryKey]bool) |
| 301 | |
| 302 | // List of individual fuzz targets, so that 'make fuzz' also installs the targets |
| 303 | // to the correct output directories as well. |
| 304 | s.fuzzTargets = make(map[string]bool) |
| 305 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 306 | ctx.VisitAllModules(func(module android.Module) { |
| 307 | // Discard non-fuzz targets. |
| 308 | ccModule, ok := module.(*Module) |
| 309 | if !ok { |
| 310 | return |
| 311 | } |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame^] | 312 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 313 | fuzzModule, ok := ccModule.compiler.(*fuzzBinary) |
| 314 | if !ok { |
| 315 | return |
| 316 | } |
| 317 | |
| 318 | // Discard vendor-NDK-linked modules, they're duplicates of fuzz targets |
| 319 | // we're going to package anyway. |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 320 | if ccModule.UseVndk() || !ccModule.Enabled() { |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 321 | return |
| 322 | } |
| 323 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame^] | 324 | s.fuzzTargets[module.Name()] = true |
| 325 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 326 | hostOrTargetString := "target" |
| 327 | if ccModule.Host() { |
| 328 | hostOrTargetString = "host" |
| 329 | } |
| 330 | |
| 331 | archString := ccModule.Arch().ArchType.String() |
| 332 | archDir := android.PathForIntermediates(ctx, "fuzz", hostOrTargetString, archString) |
| 333 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame^] | 334 | // Grab the list of required shared libraries. |
| 335 | sharedLibraries := make(map[string]android.Path) |
| 336 | collectAllSharedDependencies(module, sharedLibraries, ctx) |
| 337 | |
| 338 | for _, library := range sharedLibraries { |
| 339 | if _, exists := archSharedLibraryDeps[archAndLibraryKey{archDir, library}]; exists { |
| 340 | continue |
| 341 | } |
| 342 | |
| 343 | // For each architecture-specific shared library dependency, we need to |
| 344 | // install it to the output directory. Setup the install destination here, |
| 345 | // which will be used by $(copy-many-files) in the Make backend. |
| 346 | archSharedLibraryDeps[archAndLibraryKey{archDir, library}] = true |
| 347 | installDestination := sharedLibraryInstallLocation( |
| 348 | library, ccModule.Host(), archString) |
| 349 | // Escape all the variables, as the install destination here will be called |
| 350 | // via. $(eval) in Make. |
| 351 | installDestination = strings.ReplaceAll( |
| 352 | installDestination, "$", "$$") |
| 353 | s.sharedLibInstallStrings = append(s.sharedLibInstallStrings, |
| 354 | library.String()+":"+installDestination) |
| 355 | } |
| 356 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 357 | // The executable. |
| 358 | archDirs[archDir] = append(archDirs[archDir], |
Mitch Phillips | d5bd577 | 2019-10-29 17:04:22 -0700 | [diff] [blame] | 359 | fileToZip{ccModule.UnstrippedOutputFile(), ccModule.Name()}) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 360 | |
| 361 | // The corpora. |
| 362 | for _, corpusEntry := range fuzzModule.corpus { |
| 363 | archDirs[archDir] = append(archDirs[archDir], |
Mitch Phillips | 641575a | 2019-10-09 17:34:42 -0700 | [diff] [blame] | 364 | fileToZip{corpusEntry, ccModule.Name() + "/corpus"}) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | // The dictionary. |
| 368 | if fuzzModule.dictionary != nil { |
| 369 | archDirs[archDir] = append(archDirs[archDir], |
| 370 | fileToZip{fuzzModule.dictionary, ccModule.Name()}) |
| 371 | } |
Kris Alder | f979ee3 | 2019-10-22 10:52:01 -0700 | [diff] [blame] | 372 | |
| 373 | // Additional fuzz config. |
| 374 | if fuzzModule.config != nil { |
| 375 | archDirs[archDir] = append(archDirs[archDir], |
| 376 | fileToZip{fuzzModule.config, ccModule.Name()}) |
| 377 | } |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 378 | }) |
| 379 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame^] | 380 | // Add the shared library deps for packaging. |
| 381 | for key, _ := range archSharedLibraryDeps { |
| 382 | archDirs[key.ArchDir] = append(archDirs[key.ArchDir], |
| 383 | fileToZip{key.Library, "lib"}) |
| 384 | } |
| 385 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 386 | for archDir, filesToZip := range archDirs { |
| 387 | arch := archDir.Base() |
| 388 | hostOrTarget := filepath.Base(filepath.Dir(archDir.String())) |
| 389 | builder := android.NewRuleBuilder() |
| 390 | outputFile := android.PathForOutput(ctx, "fuzz-"+hostOrTarget+"-"+arch+".zip") |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 391 | s.packages = append(s.packages, outputFile) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 392 | |
| 393 | command := builder.Command().BuiltTool(ctx, "soong_zip"). |
| 394 | Flag("-j"). |
| 395 | FlagWithOutput("-o ", outputFile) |
| 396 | |
| 397 | for _, fileToZip := range filesToZip { |
| 398 | command.FlagWithArg("-P ", fileToZip.DestinationPathPrefix). |
| 399 | FlagWithInput("-f ", fileToZip.SourceFilePath) |
| 400 | } |
| 401 | |
| 402 | builder.Build(pctx, ctx, "create-fuzz-package-"+arch+"-"+hostOrTarget, |
| 403 | "Create fuzz target packages for "+arch+"-"+hostOrTarget) |
| 404 | } |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 405 | } |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 406 | |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 407 | func (s *fuzzPackager) MakeVars(ctx android.MakeVarsContext) { |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame^] | 408 | packages := s.packages.Strings() |
| 409 | sort.Strings(packages) |
| 410 | sort.Strings(s.sharedLibInstallStrings) |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 411 | // TODO(mitchp): Migrate this to use MakeVarsContext::DistForGoal() when it's |
| 412 | // ready to handle phony targets created in Soong. In the meantime, this |
| 413 | // exports the phony 'fuzz' target and dependencies on packages to |
| 414 | // core/main.mk so that we can use dist-for-goals. |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame^] | 415 | ctx.Strict("SOONG_FUZZ_PACKAGING_ARCH_MODULES", strings.Join(packages, " ")) |
| 416 | ctx.Strict("FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS", |
| 417 | strings.Join(s.sharedLibInstallStrings, " ")) |
| 418 | |
| 419 | // Preallocate the slice of fuzz targets to minimise memory allocations. |
| 420 | fuzzTargets := make([]string, 0, len(s.fuzzTargets)) |
| 421 | for target, _ := range s.fuzzTargets { |
| 422 | fuzzTargets = append(fuzzTargets, target) |
| 423 | } |
| 424 | sort.Strings(fuzzTargets) |
| 425 | ctx.Strict("ALL_FUZZ_TARGETS", strings.Join(fuzzTargets, " ")) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 426 | } |