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