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