| Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 1 | // Copyright 2020 The Android Open Source Project | 
|  | 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 rust | 
|  | 16 |  | 
|  | 17 | import ( | 
|  | 18 | "android/soong/android" | 
|  | 19 | "android/soong/cc" | 
| hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame] | 20 | "android/soong/fuzz" | 
| Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 21 | "android/soong/rust/config" | 
| Colin Cross | 5c1d5fb | 2023-11-15 12:39:40 -0800 | [diff] [blame] | 22 | "path/filepath" | 
| Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 23 | ) | 
|  | 24 |  | 
|  | 25 | func init() { | 
|  | 26 | android.RegisterModuleType("rust_fuzz", RustFuzzFactory) | 
| Ivan Lozano | 2fcbffa | 2023-07-27 10:40:52 -0400 | [diff] [blame] | 27 | android.RegisterModuleType("rust_fuzz_host", RustFuzzHostFactory) | 
| Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 28 | } | 
|  | 29 |  | 
|  | 30 | type fuzzDecorator struct { | 
|  | 31 | *binaryDecorator | 
|  | 32 |  | 
| Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 33 | fuzzPackagedModule  fuzz.FuzzPackagedModule | 
| Hamzeh Zawawy | 3891749 | 2023-04-05 22:08:46 +0000 | [diff] [blame] | 34 | sharedLibraries     android.RuleBuilderInstalls | 
| Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 35 | installedSharedDeps []string | 
| Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 36 | } | 
|  | 37 |  | 
| Ivan Lozano | 5482d6a | 2021-11-01 10:13:25 -0400 | [diff] [blame] | 38 | var _ compiler = (*fuzzDecorator)(nil) | 
| Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 39 |  | 
|  | 40 | // rust_binary produces a binary that is runnable on a device. | 
|  | 41 | func RustFuzzFactory() android.Module { | 
|  | 42 | module, _ := NewRustFuzz(android.HostAndDeviceSupported) | 
|  | 43 | return module.Init() | 
|  | 44 | } | 
|  | 45 |  | 
| Ivan Lozano | 2fcbffa | 2023-07-27 10:40:52 -0400 | [diff] [blame] | 46 | func RustFuzzHostFactory() android.Module { | 
|  | 47 | module, _ := NewRustFuzz(android.HostSupported) | 
|  | 48 | return module.Init() | 
|  | 49 | } | 
|  | 50 |  | 
| Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 51 | func NewRustFuzz(hod android.HostOrDeviceSupported) (*Module, *fuzzDecorator) { | 
|  | 52 | module, binary := NewRustBinary(hod) | 
|  | 53 | fuzz := &fuzzDecorator{ | 
|  | 54 | binaryDecorator: binary, | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 | // Change the defaults for the binaryDecorator's baseCompiler | 
|  | 58 | fuzz.binaryDecorator.baseCompiler.dir = "fuzz" | 
|  | 59 | fuzz.binaryDecorator.baseCompiler.dir64 = "fuzz" | 
|  | 60 | fuzz.binaryDecorator.baseCompiler.location = InstallInData | 
|  | 61 | module.sanitize.SetSanitizer(cc.Fuzzer, true) | 
| Ivan Lozano | 5467a39 | 2023-08-23 14:20:25 -0400 | [diff] [blame] | 62 |  | 
|  | 63 | // The fuzzer runtime is not present for darwin or bionic host modules, so disable rust_fuzz modules for these. | 
|  | 64 | android.AddLoadHook(module, func(ctx android.LoadHookContext) { | 
|  | 65 |  | 
|  | 66 | extraProps := struct { | 
|  | 67 | Target struct { | 
|  | 68 | Darwin struct { | 
|  | 69 | Enabled *bool | 
|  | 70 | } | 
|  | 71 | Linux_bionic struct { | 
|  | 72 | Enabled *bool | 
|  | 73 | } | 
|  | 74 | } | 
|  | 75 | }{} | 
|  | 76 | extraProps.Target.Darwin.Enabled = cc.BoolPtr(false) | 
|  | 77 | extraProps.Target.Linux_bionic.Enabled = cc.BoolPtr(false) | 
|  | 78 | ctx.AppendProperties(&extraProps) | 
|  | 79 | }) | 
|  | 80 |  | 
| Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 81 | module.compiler = fuzz | 
|  | 82 | return module, fuzz | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | func (fuzzer *fuzzDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags { | 
|  | 86 | flags = fuzzer.binaryDecorator.compilerFlags(ctx, flags) | 
|  | 87 |  | 
|  | 88 | // `../lib` for installed fuzz targets (both host and device), and `./lib` for fuzz target packages. | 
| Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 89 | flags.LinkFlags = append(flags.LinkFlags, `-Wl,-rpath,\$$ORIGIN/lib`) | 
|  | 90 |  | 
| Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 91 | if ctx.InstallInVendor() { | 
|  | 92 | flags.LinkFlags = append(flags.LinkFlags, `-Wl,-rpath,\$$ORIGIN/../../lib`) | 
|  | 93 | } else { | 
|  | 94 | flags.LinkFlags = append(flags.LinkFlags, `-Wl,-rpath,\$$ORIGIN/../lib`) | 
|  | 95 |  | 
|  | 96 | } | 
| Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 97 | return flags | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | func (fuzzer *fuzzDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps { | 
| Liz Kammer | 9c21086 | 2021-04-12 18:52:29 -0400 | [diff] [blame] | 101 | if libFuzzerRuntimeLibrary := config.LibFuzzerRuntimeLibrary(ctx.toolchain()); libFuzzerRuntimeLibrary != "" { | 
|  | 102 | deps.StaticLibs = append(deps.StaticLibs, libFuzzerRuntimeLibrary) | 
|  | 103 | } | 
| Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 104 | deps.SharedLibs = append(deps.SharedLibs, "libc++") | 
|  | 105 | deps.Rlibs = append(deps.Rlibs, "liblibfuzzer_sys") | 
|  | 106 |  | 
|  | 107 | deps = fuzzer.binaryDecorator.compilerDeps(ctx, deps) | 
|  | 108 |  | 
|  | 109 | return deps | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | func (fuzzer *fuzzDecorator) compilerProps() []interface{} { | 
|  | 113 | return append(fuzzer.binaryDecorator.compilerProps(), | 
| hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 114 | &fuzzer.fuzzPackagedModule.FuzzProperties) | 
| Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 115 | } | 
|  | 116 |  | 
| Colin Cross | 31d89b4 | 2022-10-04 16:35:39 -0700 | [diff] [blame] | 117 | func (fuzzer *fuzzDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput { | 
| Colin Cross | 31d89b4 | 2022-10-04 16:35:39 -0700 | [diff] [blame] | 118 |  | 
| Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 119 | out := fuzzer.binaryDecorator.compile(ctx, flags, deps) | 
| Colin Cross | 31d89b4 | 2022-10-04 16:35:39 -0700 | [diff] [blame] | 120 |  | 
|  | 121 | return out | 
|  | 122 | } | 
|  | 123 |  | 
| Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 124 | func (fuzzer *fuzzDecorator) stdLinkage(ctx *depsContext) RustLinkage { | 
|  | 125 | return RlibLinkage | 
|  | 126 | } | 
|  | 127 |  | 
| Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 128 | func (fuzzer *fuzzDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep { | 
| Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 129 | return rlibAutoDep | 
|  | 130 | } | 
| hamzeh | c651b52 | 2021-04-29 12:50:47 -0700 | [diff] [blame] | 131 |  | 
| hamzeh | c651b52 | 2021-04-29 12:50:47 -0700 | [diff] [blame] | 132 | func (fuzz *fuzzDecorator) install(ctx ModuleContext) { | 
| Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 133 | fuzz.fuzzPackagedModule = cc.PackageFuzzModule(ctx, fuzz.fuzzPackagedModule, pctx) | 
|  | 134 |  | 
|  | 135 | installBase := "fuzz" | 
|  | 136 |  | 
|  | 137 | // Grab the list of required shared libraries. | 
|  | 138 | fuzz.sharedLibraries, _ = cc.CollectAllSharedDependencies(ctx) | 
|  | 139 |  | 
| Hamzeh Zawawy | 3891749 | 2023-04-05 22:08:46 +0000 | [diff] [blame] | 140 | for _, ruleBuilderInstall := range fuzz.sharedLibraries { | 
|  | 141 | install := ruleBuilderInstall.To | 
|  | 142 |  | 
| Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 143 | fuzz.installedSharedDeps = append(fuzz.installedSharedDeps, | 
|  | 144 | cc.SharedLibraryInstallLocation( | 
| Hamzeh Zawawy | 3891749 | 2023-04-05 22:08:46 +0000 | [diff] [blame] | 145 | install, ctx.Host(), installBase, ctx.Arch().ArchType.String())) | 
| Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 146 |  | 
|  | 147 | // Also add the dependency on the shared library symbols dir. | 
|  | 148 | if !ctx.Host() { | 
|  | 149 | fuzz.installedSharedDeps = append(fuzz.installedSharedDeps, | 
| Hamzeh Zawawy | 3891749 | 2023-04-05 22:08:46 +0000 | [diff] [blame] | 150 | cc.SharedLibrarySymbolsInstallLocation(install, installBase, ctx.Arch().ArchType.String())) | 
| Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 151 | } | 
| hamzeh | c651b52 | 2021-04-29 12:50:47 -0700 | [diff] [blame] | 152 | } | 
| Colin Cross | 5c1d5fb | 2023-11-15 12:39:40 -0800 | [diff] [blame] | 153 |  | 
|  | 154 | var fuzzData []android.DataPath | 
|  | 155 | for _, d := range fuzz.fuzzPackagedModule.Corpus { | 
|  | 156 | fuzzData = append(fuzzData, android.DataPath{SrcPath: d, RelativeInstallPath: "corpus", WithoutRel: true}) | 
|  | 157 | } | 
|  | 158 |  | 
|  | 159 | for _, d := range fuzz.fuzzPackagedModule.Data { | 
|  | 160 | fuzzData = append(fuzzData, android.DataPath{SrcPath: d, RelativeInstallPath: "data"}) | 
|  | 161 | } | 
|  | 162 |  | 
|  | 163 | if d := fuzz.fuzzPackagedModule.Dictionary; d != nil { | 
|  | 164 | fuzzData = append(fuzzData, android.DataPath{SrcPath: d, WithoutRel: true}) | 
|  | 165 | } | 
|  | 166 |  | 
|  | 167 | if d := fuzz.fuzzPackagedModule.Config; d != nil { | 
|  | 168 | fuzzData = append(fuzzData, android.DataPath{SrcPath: d, WithoutRel: true}) | 
|  | 169 | } | 
|  | 170 |  | 
|  | 171 | fuzz.binaryDecorator.baseCompiler.dir = filepath.Join( | 
|  | 172 | "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName()) | 
|  | 173 | fuzz.binaryDecorator.baseCompiler.dir64 = filepath.Join( | 
|  | 174 | "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName()) | 
|  | 175 | fuzz.binaryDecorator.baseCompiler.installTestData(ctx, fuzzData) | 
|  | 176 |  | 
|  | 177 | fuzz.binaryDecorator.baseCompiler.install(ctx) | 
|  | 178 |  | 
| hamzeh | c651b52 | 2021-04-29 12:50:47 -0700 | [diff] [blame] | 179 | } |