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