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