Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 1 | // Copyright 2016 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
Mitch Phillips | 4de896e | 2019-08-28 16:04:36 -0700 | [diff] [blame] | 18 | "path/filepath" |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 19 | "sort" |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 20 | "strings" |
Mitch Phillips | 4de896e | 2019-08-28 16:04:36 -0700 | [diff] [blame] | 21 | |
Victor Chang | 00c144f | 2021-02-09 12:30:33 +0000 | [diff] [blame] | 22 | "github.com/google/blueprint/proptools" |
| 23 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 24 | "android/soong/android" |
| 25 | "android/soong/cc/config" |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame] | 26 | "android/soong/fuzz" |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | func init() { |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 30 | android.RegisterModuleType("cc_fuzz", LibFuzzFactory) |
LaMont Jones | 0c10e4d | 2023-05-16 00:58:37 +0000 | [diff] [blame] | 31 | android.RegisterParallelSingletonType("cc_fuzz_packaging", fuzzPackagingFactory) |
David Fu | fd121fc | 2023-07-07 18:11:51 +0000 | [diff] [blame] | 32 | android.RegisterParallelSingletonType("cc_fuzz_presubmit_packaging", fuzzPackagingFactoryPresubmit) |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | type FuzzProperties struct { |
Cory Barker | 9cfcf6d | 2022-07-22 17:22:02 +0000 | [diff] [blame] | 36 | FuzzFramework fuzz.Framework `blueprint:"mutated"` |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | type fuzzer struct { |
| 40 | Properties FuzzProperties |
| 41 | } |
| 42 | |
| 43 | func (fuzzer *fuzzer) flags(ctx ModuleContext, flags Flags) Flags { |
Cory Barker | 9cfcf6d | 2022-07-22 17:22:02 +0000 | [diff] [blame] | 44 | if fuzzer.Properties.FuzzFramework == fuzz.AFL { |
| 45 | flags.Local.CFlags = append(flags.Local.CFlags, []string{ |
| 46 | "-fsanitize-coverage=trace-pc-guard", |
| 47 | "-Wno-unused-result", |
| 48 | "-Wno-unused-parameter", |
| 49 | "-Wno-unused-function", |
| 50 | }...) |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | return flags |
| 54 | } |
| 55 | |
| 56 | func (fuzzer *fuzzer) props() []interface{} { |
| 57 | return []interface{}{&fuzzer.Properties} |
| 58 | } |
| 59 | |
| 60 | func fuzzMutatorDeps(mctx android.TopDownMutatorContext) { |
| 61 | currentModule, ok := mctx.Module().(*Module) |
| 62 | if !ok { |
| 63 | return |
| 64 | } |
| 65 | |
Cory Barker | 9cfcf6d | 2022-07-22 17:22:02 +0000 | [diff] [blame] | 66 | if currentModule.fuzzer == nil { |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 67 | return |
| 68 | } |
| 69 | |
| 70 | mctx.WalkDeps(func(child android.Module, parent android.Module) bool { |
| 71 | c, ok := child.(*Module) |
| 72 | if !ok { |
| 73 | return false |
| 74 | } |
| 75 | |
| 76 | if c.sanitize == nil { |
| 77 | return false |
| 78 | } |
| 79 | |
| 80 | isFuzzerPointer := c.sanitize.getSanitizerBoolPtr(Fuzzer) |
| 81 | if isFuzzerPointer == nil || !*isFuzzerPointer { |
| 82 | return false |
| 83 | } |
| 84 | |
| 85 | if c.fuzzer == nil { |
| 86 | return false |
| 87 | } |
| 88 | |
Cory Barker | 9cfcf6d | 2022-07-22 17:22:02 +0000 | [diff] [blame] | 89 | c.fuzzer.Properties.FuzzFramework = currentModule.fuzzer.Properties.FuzzFramework |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 90 | return true |
| 91 | }) |
| 92 | } |
| 93 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 94 | // cc_fuzz creates a host/device fuzzer binary. Host binaries can be found at |
| 95 | // $ANDROID_HOST_OUT/fuzz/, and device binaries can be found at /data/fuzz on |
| 96 | // your device, or $ANDROID_PRODUCT_OUT/data/fuzz in your build tree. |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 97 | func LibFuzzFactory() android.Module { |
Cory Barker | 9cfcf6d | 2022-07-22 17:22:02 +0000 | [diff] [blame] | 98 | module := NewFuzzer(android.HostAndDeviceSupported) |
Aditya Choudhary | 87b2ab2 | 2023-11-17 15:27:06 +0000 | [diff] [blame] | 99 | module.testModule = true |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 100 | return module.Init() |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | type fuzzBinary struct { |
| 104 | *binaryDecorator |
| 105 | *baseCompiler |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 106 | fuzzPackagedModule fuzz.FuzzPackagedModule |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 107 | installedSharedDeps []string |
Hamzeh Zawawy | 3891749 | 2023-04-05 22:08:46 +0000 | [diff] [blame] | 108 | sharedLibraries android.RuleBuilderInstalls |
Colin Cross | 5c1d5fb | 2023-11-15 12:39:40 -0800 | [diff] [blame] | 109 | data []android.DataPath |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 112 | func (fuzz *fuzzBinary) fuzzBinary() bool { |
| 113 | return true |
| 114 | } |
| 115 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 116 | func (fuzz *fuzzBinary) linkerProps() []interface{} { |
| 117 | props := fuzz.binaryDecorator.linkerProps() |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 118 | props = append(props, &fuzz.fuzzPackagedModule.FuzzProperties) |
Cory Barker | 9cfcf6d | 2022-07-22 17:22:02 +0000 | [diff] [blame] | 119 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 120 | return props |
| 121 | } |
| 122 | |
| 123 | func (fuzz *fuzzBinary) linkerInit(ctx BaseModuleContext) { |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 124 | fuzz.binaryDecorator.linkerInit(ctx) |
| 125 | } |
| 126 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 127 | func (fuzzBin *fuzzBinary) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Cory Barker | 9cfcf6d | 2022-07-22 17:22:02 +0000 | [diff] [blame] | 128 | if ctx.Config().Getenv("FUZZ_FRAMEWORK") == "AFL" { |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 129 | deps.HeaderLibs = append(deps.HeaderLibs, "libafl_headers") |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 130 | } else { |
| 131 | deps.StaticLibs = append(deps.StaticLibs, config.LibFuzzerRuntimeLibrary(ctx.toolchain())) |
Kris Alder | d406da1 | 2022-10-21 09:34:21 -0700 | [diff] [blame] | 132 | // Fuzzers built with HWASAN should use the interceptors for better |
| 133 | // mutation based on signals in strcmp, memcpy, etc. This is only needed for |
| 134 | // fuzz targets, not generic HWASAN-ified binaries or libraries. |
| 135 | if module, ok := ctx.Module().(*Module); ok { |
| 136 | if module.IsSanitizerEnabled(Hwasan) { |
| 137 | deps.StaticLibs = append(deps.StaticLibs, config.LibFuzzerRuntimeInterceptors(ctx.toolchain())) |
| 138 | } |
| 139 | } |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 140 | } |
Cory Barker | 9cfcf6d | 2022-07-22 17:22:02 +0000 | [diff] [blame] | 141 | |
| 142 | deps = fuzzBin.binaryDecorator.linkerDeps(ctx, deps) |
| 143 | return deps |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | func (fuzz *fuzzBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Steven Moreland | d86fec5 | 2023-12-28 01:09:40 +0000 | [diff] [blame] | 147 | subdir := "lib" |
| 148 | if ctx.inVendor() { |
| 149 | subdir = "lib/vendor" |
| 150 | } |
| 151 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 152 | flags = fuzz.binaryDecorator.linkerFlags(ctx, flags) |
Mitch Phillips | 1f7f54f | 2019-11-14 14:50:47 -0800 | [diff] [blame] | 153 | // RunPaths on devices isn't instantiated by the base linker. `../lib` for |
| 154 | // installed fuzz targets (both host and device), and `./lib` for fuzz |
| 155 | // target packages. |
Steven Moreland | d86fec5 | 2023-12-28 01:09:40 +0000 | [diff] [blame] | 156 | flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/`+subdir) |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 157 | |
Kris Alder | c263481 | 2022-10-25 10:58:59 -0700 | [diff] [blame] | 158 | // When running on device, fuzz targets with vendor: true set will be in |
| 159 | // fuzzer_name/vendor/fuzzer_name (note the extra 'vendor' and thus need to |
| 160 | // link with libraries in ../../lib/. Non-vendor binaries only need to look |
| 161 | // one level up, in ../lib/. |
| 162 | if ctx.inVendor() { |
Steven Moreland | d86fec5 | 2023-12-28 01:09:40 +0000 | [diff] [blame] | 163 | flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/../../`+subdir) |
Kris Alder | c263481 | 2022-10-25 10:58:59 -0700 | [diff] [blame] | 164 | } else { |
Steven Moreland | d86fec5 | 2023-12-28 01:09:40 +0000 | [diff] [blame] | 165 | flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/../`+subdir) |
Kris Alder | c263481 | 2022-10-25 10:58:59 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 168 | return flags |
| 169 | } |
| 170 | |
Colin Cross | 4a9e6ec | 2023-12-18 15:29:41 -0800 | [diff] [blame] | 171 | func (fuzz *fuzzBinary) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) { |
| 172 | fuzz.binaryDecorator.moduleInfoJSON(ctx, moduleInfoJSON) |
| 173 | moduleInfoJSON.Class = []string{"EXECUTABLES"} |
| 174 | } |
| 175 | |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 176 | // IsValidSharedDependency takes a module and determines if it is a unique shared library |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 177 | // that should be installed in the fuzz target output directories. This function |
| 178 | // returns true, unless: |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 179 | // - The module is not an installable shared library, or |
| 180 | // - The module is a header or stub, or |
| 181 | // - The module is a prebuilt and its source is available, or |
| 182 | // - The module is a versioned member of an SDK snapshot. |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 183 | func IsValidSharedDependency(dependency android.Module) bool { |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 184 | // TODO(b/144090547): We should be parsing these modules using |
| 185 | // ModuleDependencyTag instead of the current brute-force checking. |
| 186 | |
Colin Cross | 31076b3 | 2020-10-23 17:22:06 -0700 | [diff] [blame] | 187 | linkable, ok := dependency.(LinkableInterface) |
| 188 | if !ok || !linkable.CcLibraryInterface() { |
| 189 | // Discard non-linkables. |
| 190 | return false |
| 191 | } |
| 192 | |
| 193 | if !linkable.Shared() { |
| 194 | // Discard static libs. |
| 195 | return false |
| 196 | } |
| 197 | |
Colin Cross | 31076b3 | 2020-10-23 17:22:06 -0700 | [diff] [blame] | 198 | if lib := moduleLibraryInterface(dependency); lib != nil && lib.buildStubs() && linkable.CcLibrary() { |
Mitch Phillips | f50bddb | 2019-11-12 14:03:31 -0800 | [diff] [blame] | 199 | // Discard stubs libs (only CCLibrary variants). Prebuilt libraries should not |
| 200 | // be excluded on the basis of they're not CCLibrary()'s. |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 201 | return false |
| 202 | } |
| 203 | |
Mitch Phillips | f50bddb | 2019-11-12 14:03:31 -0800 | [diff] [blame] | 204 | // We discarded module stubs libraries above, but the LLNDK prebuilts stubs |
| 205 | // libraries must be handled differently - by looking for the stubDecorator. |
| 206 | // Discard LLNDK prebuilts stubs as well. |
| 207 | if ccLibrary, isCcLibrary := dependency.(*Module); isCcLibrary { |
| 208 | if _, isLLndkStubLibrary := ccLibrary.linker.(*stubDecorator); isLLndkStubLibrary { |
| 209 | return false |
| 210 | } |
Victor Chang | 00c144f | 2021-02-09 12:30:33 +0000 | [diff] [blame] | 211 | // Discard installable:false libraries because they are expected to be absent |
| 212 | // in runtime. |
Colin Cross | 1bc9412 | 2021-10-28 13:25:54 -0700 | [diff] [blame] | 213 | if !proptools.BoolDefault(ccLibrary.Installable(), true) { |
Victor Chang | 00c144f | 2021-02-09 12:30:33 +0000 | [diff] [blame] | 214 | return false |
| 215 | } |
Mitch Phillips | f50bddb | 2019-11-12 14:03:31 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Martin Stjernholm | 02460ab | 2020-10-06 02:36:43 +0100 | [diff] [blame] | 218 | // If the same library is present both as source and a prebuilt we must pick |
| 219 | // only one to avoid a conflict. Always prefer the source since the prebuilt |
| 220 | // probably won't be built with sanitizers enabled. |
Paul Duffin | f7c99f5 | 2021-04-28 10:41:21 +0100 | [diff] [blame] | 221 | if prebuilt := android.GetEmbeddedPrebuilt(dependency); prebuilt != nil && prebuilt.SourceExists() { |
Martin Stjernholm | 02460ab | 2020-10-06 02:36:43 +0100 | [diff] [blame] | 222 | return false |
| 223 | } |
| 224 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 225 | return true |
| 226 | } |
| 227 | |
Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 228 | func SharedLibraryInstallLocation( |
Steven Moreland | d86fec5 | 2023-12-28 01:09:40 +0000 | [diff] [blame] | 229 | libraryBase string, isHost bool, isVendor bool, fuzzDir string, archString string) string { |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 230 | installLocation := "$(PRODUCT_OUT)/data" |
| 231 | if isHost { |
| 232 | installLocation = "$(HOST_OUT)" |
| 233 | } |
Steven Moreland | d86fec5 | 2023-12-28 01:09:40 +0000 | [diff] [blame] | 234 | subdir := "lib" |
| 235 | if isVendor { |
| 236 | subdir = "lib/vendor" |
| 237 | } |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 238 | installLocation = filepath.Join( |
Steven Moreland | d86fec5 | 2023-12-28 01:09:40 +0000 | [diff] [blame] | 239 | installLocation, fuzzDir, archString, subdir, libraryBase) |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 240 | return installLocation |
| 241 | } |
| 242 | |
Mitch Phillips | 0bf9713 | 2020-03-06 09:38:12 -0800 | [diff] [blame] | 243 | // Get the device-only shared library symbols install directory. |
Steven Moreland | d86fec5 | 2023-12-28 01:09:40 +0000 | [diff] [blame] | 244 | func SharedLibrarySymbolsInstallLocation(libraryBase string, isVendor bool, fuzzDir string, archString string) string { |
| 245 | subdir := "lib" |
| 246 | if isVendor { |
| 247 | subdir = "lib/vendor" |
| 248 | } |
| 249 | return filepath.Join("$(PRODUCT_OUT)/symbols/data/", fuzzDir, archString, subdir, libraryBase) |
Mitch Phillips | 0bf9713 | 2020-03-06 09:38:12 -0800 | [diff] [blame] | 250 | } |
| 251 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 252 | func (fuzzBin *fuzzBinary) install(ctx ModuleContext, file android.Path) { |
Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 253 | fuzzBin.fuzzPackagedModule = PackageFuzzModule(ctx, fuzzBin.fuzzPackagedModule, pctx) |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 254 | |
Colin Cross | 5c1d5fb | 2023-11-15 12:39:40 -0800 | [diff] [blame] | 255 | installBase := "fuzz" |
| 256 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 257 | // Grab the list of required shared libraries. |
Muhammad Haseeb Ahmad | 431ddf9 | 2022-10-20 00:55:58 +0000 | [diff] [blame] | 258 | fuzzBin.sharedLibraries, _ = CollectAllSharedDependencies(ctx) |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 259 | |
Steven Moreland | d86fec5 | 2023-12-28 01:09:40 +0000 | [diff] [blame] | 260 | // TODO: does not mirror Android linkernamespaces |
| 261 | // the logic here has special cases for vendor, but it would need more work to |
| 262 | // work in arbitrary partitions, so just surface errors early for a few cases |
| 263 | // |
| 264 | // Even without these, there are certain situations across linkernamespaces |
| 265 | // that this won't support. For instance, you might have: |
| 266 | // |
| 267 | // my_fuzzer (vendor) -> libbinder_ndk (core) -> libbinder (vendor) |
| 268 | // |
| 269 | // This dependency chain wouldn't be possible to express in the current |
| 270 | // logic because all the deps currently match the variant of the source |
| 271 | // module. |
| 272 | |
Hamzeh Zawawy | 3891749 | 2023-04-05 22:08:46 +0000 | [diff] [blame] | 273 | for _, ruleBuilderInstall := range fuzzBin.sharedLibraries { |
| 274 | install := ruleBuilderInstall.To |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 275 | fuzzBin.installedSharedDeps = append(fuzzBin.installedSharedDeps, |
Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 276 | SharedLibraryInstallLocation( |
Steven Moreland | d86fec5 | 2023-12-28 01:09:40 +0000 | [diff] [blame] | 277 | install, ctx.Host(), ctx.inVendor(), installBase, ctx.Arch().ArchType.String())) |
Mitch Phillips | 0bf9713 | 2020-03-06 09:38:12 -0800 | [diff] [blame] | 278 | |
| 279 | // Also add the dependency on the shared library symbols dir. |
| 280 | if !ctx.Host() { |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 281 | fuzzBin.installedSharedDeps = append(fuzzBin.installedSharedDeps, |
Steven Moreland | d86fec5 | 2023-12-28 01:09:40 +0000 | [diff] [blame] | 282 | SharedLibrarySymbolsInstallLocation(install, ctx.inVendor(), installBase, ctx.Arch().ArchType.String())) |
Mitch Phillips | 0bf9713 | 2020-03-06 09:38:12 -0800 | [diff] [blame] | 283 | } |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 284 | } |
Colin Cross | 5c1d5fb | 2023-11-15 12:39:40 -0800 | [diff] [blame] | 285 | |
| 286 | for _, d := range fuzzBin.fuzzPackagedModule.Corpus { |
| 287 | fuzzBin.data = append(fuzzBin.data, android.DataPath{SrcPath: d, RelativeInstallPath: "corpus", WithoutRel: true}) |
| 288 | } |
| 289 | |
| 290 | for _, d := range fuzzBin.fuzzPackagedModule.Data { |
| 291 | fuzzBin.data = append(fuzzBin.data, android.DataPath{SrcPath: d, RelativeInstallPath: "data"}) |
| 292 | } |
| 293 | |
| 294 | if d := fuzzBin.fuzzPackagedModule.Dictionary; d != nil { |
| 295 | fuzzBin.data = append(fuzzBin.data, android.DataPath{SrcPath: d, WithoutRel: true}) |
| 296 | } |
| 297 | |
| 298 | if d := fuzzBin.fuzzPackagedModule.Config; d != nil { |
| 299 | fuzzBin.data = append(fuzzBin.data, android.DataPath{SrcPath: d, WithoutRel: true}) |
| 300 | } |
| 301 | |
| 302 | fuzzBin.binaryDecorator.baseInstaller.dir = filepath.Join( |
| 303 | installBase, ctx.Target().Arch.ArchType.String(), ctx.ModuleName()) |
| 304 | fuzzBin.binaryDecorator.baseInstaller.dir64 = filepath.Join( |
| 305 | installBase, ctx.Target().Arch.ArchType.String(), ctx.ModuleName()) |
| 306 | fuzzBin.binaryDecorator.baseInstaller.installTestData(ctx, fuzzBin.data) |
| 307 | fuzzBin.binaryDecorator.baseInstaller.install(ctx, file) |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 310 | func PackageFuzzModule(ctx android.ModuleContext, fuzzPackagedModule fuzz.FuzzPackagedModule, pctx android.PackageContext) fuzz.FuzzPackagedModule { |
| 311 | fuzzPackagedModule.Corpus = android.PathsForModuleSrc(ctx, fuzzPackagedModule.FuzzProperties.Corpus) |
Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 312 | |
| 313 | fuzzPackagedModule.Data = android.PathsForModuleSrc(ctx, fuzzPackagedModule.FuzzProperties.Data) |
Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 314 | |
| 315 | if fuzzPackagedModule.FuzzProperties.Dictionary != nil { |
| 316 | fuzzPackagedModule.Dictionary = android.PathForModuleSrc(ctx, *fuzzPackagedModule.FuzzProperties.Dictionary) |
| 317 | if fuzzPackagedModule.Dictionary.Ext() != ".dict" { |
| 318 | ctx.PropertyErrorf("dictionary", |
| 319 | "Fuzzer dictionary %q does not have '.dict' extension", |
| 320 | fuzzPackagedModule.Dictionary.String()) |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | if fuzzPackagedModule.FuzzProperties.Fuzz_config != nil { |
| 325 | configPath := android.PathForModuleOut(ctx, "config").Join(ctx, "config.json") |
| 326 | android.WriteFileRule(ctx, configPath, fuzzPackagedModule.FuzzProperties.Fuzz_config.String()) |
| 327 | fuzzPackagedModule.Config = configPath |
| 328 | } |
| 329 | return fuzzPackagedModule |
| 330 | } |
| 331 | |
Cory Barker | 9cfcf6d | 2022-07-22 17:22:02 +0000 | [diff] [blame] | 332 | func NewFuzzer(hod android.HostOrDeviceSupported) *Module { |
Colin Cross | 8ff1058 | 2023-12-07 13:10:56 -0800 | [diff] [blame] | 333 | module, binary := newBinary(hod) |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 334 | baseInstallerPath := "fuzz" |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 335 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 336 | binary.baseInstaller = NewBaseInstaller(baseInstallerPath, baseInstallerPath, InstallInData) |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 337 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 338 | fuzzBin := &fuzzBinary{ |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 339 | binaryDecorator: binary, |
| 340 | baseCompiler: NewBaseCompiler(), |
| 341 | } |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 342 | module.compiler = fuzzBin |
| 343 | module.linker = fuzzBin |
| 344 | module.installer = fuzzBin |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 345 | |
Cory Barker | 9cfcf6d | 2022-07-22 17:22:02 +0000 | [diff] [blame] | 346 | module.fuzzer.Properties.FuzzFramework = fuzz.LibFuzzer |
| 347 | |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 348 | // The fuzzer runtime is not present for darwin host modules, disable cc_fuzz modules when targeting darwin. |
| 349 | android.AddLoadHook(module, func(ctx android.LoadHookContext) { |
Liz Kammer | 2c1d6aa | 2022-10-03 15:07:37 -0400 | [diff] [blame] | 350 | |
| 351 | extraProps := struct { |
| 352 | Sanitize struct { |
| 353 | Fuzzer *bool |
| 354 | } |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 355 | Target struct { |
| 356 | Darwin struct { |
| 357 | Enabled *bool |
| 358 | } |
Alex Light | 71123ec | 2019-07-24 13:34:19 -0700 | [diff] [blame] | 359 | Linux_bionic struct { |
| 360 | Enabled *bool |
| 361 | } |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 362 | } |
| 363 | }{} |
Liz Kammer | 2c1d6aa | 2022-10-03 15:07:37 -0400 | [diff] [blame] | 364 | extraProps.Sanitize.Fuzzer = BoolPtr(true) |
| 365 | extraProps.Target.Darwin.Enabled = BoolPtr(false) |
| 366 | extraProps.Target.Linux_bionic.Enabled = BoolPtr(false) |
| 367 | ctx.AppendProperties(&extraProps) |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 368 | |
Cory Barker | 9cfcf6d | 2022-07-22 17:22:02 +0000 | [diff] [blame] | 369 | targetFramework := fuzz.GetFramework(ctx, fuzz.Cc) |
| 370 | if !fuzz.IsValidFrameworkForModule(targetFramework, fuzz.Cc, fuzzBin.fuzzPackagedModule.FuzzProperties.Fuzzing_frameworks) { |
| 371 | ctx.Module().Disable() |
| 372 | return |
| 373 | } |
| 374 | |
| 375 | if targetFramework == fuzz.AFL { |
| 376 | fuzzBin.baseCompiler.Properties.Srcs = append(fuzzBin.baseCompiler.Properties.Srcs, ":aflpp_driver", ":afl-compiler-rt") |
| 377 | module.fuzzer.Properties.FuzzFramework = fuzz.AFL |
| 378 | } |
| 379 | }) |
Cory Barker | 74aea6c | 2022-08-08 15:55:12 +0000 | [diff] [blame] | 380 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 381 | return module |
| 382 | } |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 383 | |
| 384 | // Responsible for generating GNU Make rules that package fuzz targets into |
| 385 | // their architecture & target/host specific zip file. |
Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 386 | type ccRustFuzzPackager struct { |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame] | 387 | fuzz.FuzzPackager |
Cole Faust | 06ea531 | 2023-10-18 17:38:40 -0700 | [diff] [blame] | 388 | fuzzPackagingArchModules string |
| 389 | fuzzTargetSharedDepsInstallPairs string |
| 390 | allFuzzTargetsName string |
| 391 | onlyIncludePresubmits bool |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | func fuzzPackagingFactory() android.Singleton { |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 395 | |
Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 396 | fuzzPackager := &ccRustFuzzPackager{ |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 397 | fuzzPackagingArchModules: "SOONG_FUZZ_PACKAGING_ARCH_MODULES", |
| 398 | fuzzTargetSharedDepsInstallPairs: "FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS", |
| 399 | allFuzzTargetsName: "ALL_FUZZ_TARGETS", |
Cole Faust | 06ea531 | 2023-10-18 17:38:40 -0700 | [diff] [blame] | 400 | onlyIncludePresubmits: false, |
David Fu | fd121fc | 2023-07-07 18:11:51 +0000 | [diff] [blame] | 401 | } |
| 402 | return fuzzPackager |
| 403 | } |
| 404 | |
| 405 | func fuzzPackagingFactoryPresubmit() android.Singleton { |
| 406 | |
| 407 | fuzzPackager := &ccRustFuzzPackager{ |
| 408 | fuzzPackagingArchModules: "SOONG_PRESUBMIT_FUZZ_PACKAGING_ARCH_MODULES", |
| 409 | fuzzTargetSharedDepsInstallPairs: "PRESUBMIT_FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS", |
| 410 | allFuzzTargetsName: "ALL_PRESUBMIT_FUZZ_TARGETS", |
Cole Faust | 06ea531 | 2023-10-18 17:38:40 -0700 | [diff] [blame] | 411 | onlyIncludePresubmits: true, |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 412 | } |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 413 | return fuzzPackager |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 416 | func (s *ccRustFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) { |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 417 | // Map between each architecture + host/device combination, and the files that |
| 418 | // need to be packaged (in the tuple of {source file, destination folder in |
| 419 | // archive}). |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame] | 420 | archDirs := make(map[fuzz.ArchOs][]fuzz.FileToZip) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 421 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 422 | // List of individual fuzz targets, so that 'make fuzz' also installs the targets |
| 423 | // to the correct output directories as well. |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 424 | s.FuzzTargets = make(map[string]bool) |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 425 | |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 426 | // Map tracking whether each shared library has an install rule to avoid duplicate install rules from |
| 427 | // multiple fuzzers that depend on the same shared library. |
| 428 | sharedLibraryInstalled := make(map[string]bool) |
| 429 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 430 | ctx.VisitAllModules(func(module android.Module) { |
Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 431 | ccModule, ok := module.(LinkableInterface) |
| 432 | if !ok || ccModule.PreventInstall() { |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 433 | return |
| 434 | } |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 435 | // Discard non-fuzz targets. |
Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 436 | if ok := fuzz.IsValid(ccModule.FuzzModuleStruct()); !ok { |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 437 | return |
| 438 | } |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 439 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 440 | sharedLibsInstallDirPrefix := "lib" |
Steven Moreland | d86fec5 | 2023-12-28 01:09:40 +0000 | [diff] [blame] | 441 | if ccModule.InVendor() { |
| 442 | sharedLibsInstallDirPrefix = "lib/vendor" |
| 443 | } |
| 444 | |
Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 445 | if !ccModule.IsFuzzModule() { |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 446 | return |
| 447 | } |
| 448 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 449 | hostOrTargetString := "target" |
Colin Cross | 64a4a5f | 2023-05-16 17:54:27 -0700 | [diff] [blame] | 450 | if ccModule.Target().HostCross { |
| 451 | hostOrTargetString = "host_cross" |
| 452 | } else if ccModule.Host() { |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 453 | hostOrTargetString = "host" |
| 454 | } |
David Fu | fd121fc | 2023-07-07 18:11:51 +0000 | [diff] [blame] | 455 | if s.onlyIncludePresubmits == true { |
| 456 | hostOrTargetString = "presubmit-" + hostOrTargetString |
| 457 | } |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 458 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 459 | fpm := fuzz.FuzzPackagedModule{} |
| 460 | if ok { |
Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 461 | fpm = ccModule.FuzzPackagedModule() |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | intermediatePath := "fuzz" |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 465 | |
Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 466 | archString := ccModule.Target().Arch.ArchType.String() |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 467 | archDir := android.PathForIntermediates(ctx, intermediatePath, hostOrTargetString, archString) |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame] | 468 | archOs := fuzz.ArchOs{HostOrTarget: hostOrTargetString, Arch: archString, Dir: archDir.String()} |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 469 | |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame] | 470 | var files []fuzz.FileToZip |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 471 | builder := android.NewRuleBuilder(pctx, ctx) |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 472 | |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 473 | // Package the corpus, data, dict and config into a zipfile. |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 474 | files = s.PackageArtifacts(ctx, module, fpm, archDir, builder) |
Tri Vo | ad172d8 | 2019-11-27 13:45:45 -0800 | [diff] [blame] | 475 | |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 476 | // Package shared libraries |
Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 477 | files = append(files, GetSharedLibsToZip(ccModule.FuzzSharedLibraries(), ccModule, &s.FuzzPackager, archString, sharedLibsInstallDirPrefix, &sharedLibraryInstalled)...) |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 478 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 479 | // The executable. |
Colin Cross | 80462dc | 2023-05-08 15:09:31 -0700 | [diff] [blame] | 480 | files = append(files, fuzz.FileToZip{SourceFilePath: android.OutputFileForModule(ctx, ccModule, "unstripped")}) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 481 | |
David Fu | fd121fc | 2023-07-07 18:11:51 +0000 | [diff] [blame] | 482 | if s.onlyIncludePresubmits == true { |
| 483 | if fpm.FuzzProperties.Fuzz_config == nil { |
| 484 | return |
| 485 | } |
Cole Faust | 06ea531 | 2023-10-18 17:38:40 -0700 | [diff] [blame] | 486 | if !BoolDefault(fpm.FuzzProperties.Fuzz_config.Use_for_presubmit, false) { |
David Fu | fd121fc | 2023-07-07 18:11:51 +0000 | [diff] [blame] | 487 | return |
| 488 | } |
| 489 | } |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 490 | archDirs[archOs], ok = s.BuildZipFile(ctx, module, fpm, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs) |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 491 | if !ok { |
| 492 | return |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 493 | } |
| 494 | }) |
| 495 | |
Cory Barker | 9cfcf6d | 2022-07-22 17:22:02 +0000 | [diff] [blame] | 496 | s.CreateFuzzPackage(ctx, archDirs, fuzz.Cc, pctx) |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 497 | } |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 498 | |
Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 499 | func (s *ccRustFuzzPackager) MakeVars(ctx android.MakeVarsContext) { |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 500 | packages := s.Packages.Strings() |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 501 | sort.Strings(packages) |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 502 | sort.Strings(s.FuzzPackager.SharedLibInstallStrings) |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 503 | // TODO(mitchp): Migrate this to use MakeVarsContext::DistForGoal() when it's |
| 504 | // ready to handle phony targets created in Soong. In the meantime, this |
| 505 | // exports the phony 'fuzz' target and dependencies on packages to |
| 506 | // core/main.mk so that we can use dist-for-goals. |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 507 | |
| 508 | ctx.Strict(s.fuzzPackagingArchModules, strings.Join(packages, " ")) |
| 509 | |
| 510 | ctx.Strict(s.fuzzTargetSharedDepsInstallPairs, |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 511 | strings.Join(s.FuzzPackager.SharedLibInstallStrings, " ")) |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 512 | |
| 513 | // Preallocate the slice of fuzz targets to minimise memory allocations. |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 514 | s.PreallocateSlice(ctx, s.allFuzzTargetsName) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 515 | } |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 516 | |
| 517 | // GetSharedLibsToZip finds and marks all the transiently-dependent shared libraries for |
| 518 | // packaging. |
Hamzeh Zawawy | 3891749 | 2023-04-05 22:08:46 +0000 | [diff] [blame] | 519 | func GetSharedLibsToZip(sharedLibraries android.RuleBuilderInstalls, module LinkableInterface, s *fuzz.FuzzPackager, archString string, destinationPathPrefix string, sharedLibraryInstalled *map[string]bool) []fuzz.FileToZip { |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 520 | var files []fuzz.FileToZip |
| 521 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 522 | fuzzDir := "fuzz" |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 523 | |
Hamzeh Zawawy | 3891749 | 2023-04-05 22:08:46 +0000 | [diff] [blame] | 524 | for _, ruleBuilderInstall := range sharedLibraries { |
| 525 | library := ruleBuilderInstall.From |
| 526 | install := ruleBuilderInstall.To |
Colin Cross | 80462dc | 2023-05-08 15:09:31 -0700 | [diff] [blame] | 527 | files = append(files, fuzz.FileToZip{ |
| 528 | SourceFilePath: library, |
| 529 | DestinationPathPrefix: destinationPathPrefix, |
| 530 | DestinationPath: install, |
| 531 | }) |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 532 | |
| 533 | // For each architecture-specific shared library dependency, we need to |
| 534 | // install it to the output directory. Setup the install destination here, |
| 535 | // which will be used by $(copy-many-files) in the Make backend. |
Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 536 | installDestination := SharedLibraryInstallLocation( |
Steven Moreland | d86fec5 | 2023-12-28 01:09:40 +0000 | [diff] [blame] | 537 | install, module.Host(), module.InVendor(), fuzzDir, archString) |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 538 | if (*sharedLibraryInstalled)[installDestination] { |
| 539 | continue |
| 540 | } |
| 541 | (*sharedLibraryInstalled)[installDestination] = true |
| 542 | |
| 543 | // Escape all the variables, as the install destination here will be called |
| 544 | // via. $(eval) in Make. |
| 545 | installDestination = strings.ReplaceAll( |
| 546 | installDestination, "$", "$$") |
| 547 | s.SharedLibInstallStrings = append(s.SharedLibInstallStrings, |
| 548 | library.String()+":"+installDestination) |
| 549 | |
| 550 | // Ensure that on device, the library is also reinstalled to the /symbols/ |
| 551 | // dir. Symbolized DSO's are always installed to the device when fuzzing, but |
| 552 | // we want symbolization tools (like `stack`) to be able to find the symbols |
| 553 | // in $ANDROID_PRODUCT_OUT/symbols automagically. |
| 554 | if !module.Host() { |
Steven Moreland | d86fec5 | 2023-12-28 01:09:40 +0000 | [diff] [blame] | 555 | symbolsInstallDestination := SharedLibrarySymbolsInstallLocation(install, module.InVendor(), fuzzDir, archString) |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 556 | symbolsInstallDestination = strings.ReplaceAll(symbolsInstallDestination, "$", "$$") |
| 557 | s.SharedLibInstallStrings = append(s.SharedLibInstallStrings, |
| 558 | library.String()+":"+symbolsInstallDestination) |
| 559 | } |
| 560 | } |
| 561 | return files |
| 562 | } |
Colin Cross | 31d89b4 | 2022-10-04 16:35:39 -0700 | [diff] [blame] | 563 | |
| 564 | // CollectAllSharedDependencies search over the provided module's dependencies using |
| 565 | // VisitDirectDeps and WalkDeps to enumerate all shared library dependencies. |
| 566 | // VisitDirectDeps is used first to avoid incorrectly using the core libraries (sanitizer |
| 567 | // runtimes, libc, libdl, etc.) from a dependency. This may cause issues when dependencies |
| 568 | // have explicit sanitizer tags, as we may get a dependency on an unsanitized libc, etc. |
Hamzeh Zawawy | 3891749 | 2023-04-05 22:08:46 +0000 | [diff] [blame] | 569 | func CollectAllSharedDependencies(ctx android.ModuleContext) (android.RuleBuilderInstalls, []android.Module) { |
Colin Cross | 31d89b4 | 2022-10-04 16:35:39 -0700 | [diff] [blame] | 570 | seen := make(map[string]bool) |
| 571 | recursed := make(map[string]bool) |
Muhammad Haseeb Ahmad | 431ddf9 | 2022-10-20 00:55:58 +0000 | [diff] [blame] | 572 | deps := []android.Module{} |
Colin Cross | 31d89b4 | 2022-10-04 16:35:39 -0700 | [diff] [blame] | 573 | |
Hamzeh Zawawy | 3891749 | 2023-04-05 22:08:46 +0000 | [diff] [blame] | 574 | var sharedLibraries android.RuleBuilderInstalls |
Colin Cross | 31d89b4 | 2022-10-04 16:35:39 -0700 | [diff] [blame] | 575 | |
| 576 | // Enumerate the first level of dependencies, as we discard all non-library |
| 577 | // modules in the BFS loop below. |
| 578 | ctx.VisitDirectDeps(func(dep android.Module) { |
| 579 | if !IsValidSharedDependency(dep) { |
| 580 | return |
| 581 | } |
Colin Cross | 313aa54 | 2023-12-13 13:47:44 -0800 | [diff] [blame] | 582 | sharedLibraryInfo, hasSharedLibraryInfo := android.OtherModuleProvider(ctx, dep, SharedLibraryInfoProvider) |
| 583 | if !hasSharedLibraryInfo { |
Hamzeh Zawawy | 3891749 | 2023-04-05 22:08:46 +0000 | [diff] [blame] | 584 | return |
| 585 | } |
Colin Cross | 31d89b4 | 2022-10-04 16:35:39 -0700 | [diff] [blame] | 586 | if seen[ctx.OtherModuleName(dep)] { |
| 587 | return |
| 588 | } |
| 589 | seen[ctx.OtherModuleName(dep)] = true |
Muhammad Haseeb Ahmad | 431ddf9 | 2022-10-20 00:55:58 +0000 | [diff] [blame] | 590 | deps = append(deps, dep) |
Hamzeh Zawawy | 3891749 | 2023-04-05 22:08:46 +0000 | [diff] [blame] | 591 | |
Hamzeh Zawawy | 3891749 | 2023-04-05 22:08:46 +0000 | [diff] [blame] | 592 | installDestination := sharedLibraryInfo.SharedLibrary.Base() |
| 593 | ruleBuilderInstall := android.RuleBuilderInstall{android.OutputFileForModule(ctx, dep, "unstripped"), installDestination} |
| 594 | sharedLibraries = append(sharedLibraries, ruleBuilderInstall) |
Colin Cross | 31d89b4 | 2022-10-04 16:35:39 -0700 | [diff] [blame] | 595 | }) |
| 596 | |
| 597 | ctx.WalkDeps(func(child, parent android.Module) bool { |
Ivan Lozano | 61c02cc | 2023-06-09 14:06:44 -0400 | [diff] [blame] | 598 | |
| 599 | // If this is a Rust module which is not rust_ffi_shared, we still want to bundle any transitive |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame^] | 600 | // shared dependencies (even for rust_ffi_rlib or rust_ffi_static) |
Ivan Lozano | 61c02cc | 2023-06-09 14:06:44 -0400 | [diff] [blame] | 601 | if rustmod, ok := child.(LinkableInterface); ok && rustmod.RustLibraryInterface() && !rustmod.Shared() { |
| 602 | if recursed[ctx.OtherModuleName(child)] { |
| 603 | return false |
| 604 | } |
| 605 | recursed[ctx.OtherModuleName(child)] = true |
| 606 | return true |
| 607 | } |
| 608 | |
Colin Cross | 31d89b4 | 2022-10-04 16:35:39 -0700 | [diff] [blame] | 609 | if !IsValidSharedDependency(child) { |
| 610 | return false |
| 611 | } |
Colin Cross | 313aa54 | 2023-12-13 13:47:44 -0800 | [diff] [blame] | 612 | sharedLibraryInfo, hasSharedLibraryInfo := android.OtherModuleProvider(ctx, child, SharedLibraryInfoProvider) |
| 613 | if !hasSharedLibraryInfo { |
Hamzeh Zawawy | 3891749 | 2023-04-05 22:08:46 +0000 | [diff] [blame] | 614 | return false |
| 615 | } |
Colin Cross | 31d89b4 | 2022-10-04 16:35:39 -0700 | [diff] [blame] | 616 | if !seen[ctx.OtherModuleName(child)] { |
| 617 | seen[ctx.OtherModuleName(child)] = true |
Muhammad Haseeb Ahmad | 431ddf9 | 2022-10-20 00:55:58 +0000 | [diff] [blame] | 618 | deps = append(deps, child) |
Hamzeh Zawawy | 3891749 | 2023-04-05 22:08:46 +0000 | [diff] [blame] | 619 | |
Hamzeh Zawawy | 3891749 | 2023-04-05 22:08:46 +0000 | [diff] [blame] | 620 | installDestination := sharedLibraryInfo.SharedLibrary.Base() |
| 621 | ruleBuilderInstall := android.RuleBuilderInstall{android.OutputFileForModule(ctx, child, "unstripped"), installDestination} |
| 622 | sharedLibraries = append(sharedLibraries, ruleBuilderInstall) |
Colin Cross | 31d89b4 | 2022-10-04 16:35:39 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | if recursed[ctx.OtherModuleName(child)] { |
| 626 | return false |
| 627 | } |
| 628 | recursed[ctx.OtherModuleName(child)] = true |
| 629 | return true |
| 630 | }) |
| 631 | |
Muhammad Haseeb Ahmad | 431ddf9 | 2022-10-20 00:55:58 +0000 | [diff] [blame] | 632 | return sharedLibraries, deps |
Colin Cross | 31d89b4 | 2022-10-04 16:35:39 -0700 | [diff] [blame] | 633 | } |