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) |
| 63 | module.compiler = fuzz |
| 64 | return module, fuzz |
| 65 | } |
| 66 | |
| 67 | func (fuzzer *fuzzDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags { |
| 68 | flags = fuzzer.binaryDecorator.compilerFlags(ctx, flags) |
| 69 | |
| 70 | // `../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] | 71 | flags.LinkFlags = append(flags.LinkFlags, `-Wl,-rpath,\$$ORIGIN/lib`) |
| 72 | |
Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 73 | if ctx.InstallInVendor() { |
| 74 | flags.LinkFlags = append(flags.LinkFlags, `-Wl,-rpath,\$$ORIGIN/../../lib`) |
| 75 | } else { |
| 76 | flags.LinkFlags = append(flags.LinkFlags, `-Wl,-rpath,\$$ORIGIN/../lib`) |
| 77 | |
| 78 | } |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 79 | return flags |
| 80 | } |
| 81 | |
| 82 | func (fuzzer *fuzzDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps { |
Liz Kammer | 9c21086 | 2021-04-12 18:52:29 -0400 | [diff] [blame] | 83 | if libFuzzerRuntimeLibrary := config.LibFuzzerRuntimeLibrary(ctx.toolchain()); libFuzzerRuntimeLibrary != "" { |
| 84 | deps.StaticLibs = append(deps.StaticLibs, libFuzzerRuntimeLibrary) |
| 85 | } |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 86 | deps.SharedLibs = append(deps.SharedLibs, "libc++") |
| 87 | deps.Rlibs = append(deps.Rlibs, "liblibfuzzer_sys") |
| 88 | |
| 89 | deps = fuzzer.binaryDecorator.compilerDeps(ctx, deps) |
| 90 | |
| 91 | return deps |
| 92 | } |
| 93 | |
| 94 | func (fuzzer *fuzzDecorator) compilerProps() []interface{} { |
| 95 | return append(fuzzer.binaryDecorator.compilerProps(), |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 96 | &fuzzer.fuzzPackagedModule.FuzzProperties) |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 97 | } |
| 98 | |
Colin Cross | 31d89b4 | 2022-10-04 16:35:39 -0700 | [diff] [blame] | 99 | func (fuzzer *fuzzDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput { |
Colin Cross | 31d89b4 | 2022-10-04 16:35:39 -0700 | [diff] [blame] | 100 | |
Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 101 | out := fuzzer.binaryDecorator.compile(ctx, flags, deps) |
Colin Cross | 31d89b4 | 2022-10-04 16:35:39 -0700 | [diff] [blame] | 102 | |
| 103 | return out |
| 104 | } |
| 105 | |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 106 | func (fuzzer *fuzzDecorator) stdLinkage(ctx *depsContext) RustLinkage { |
| 107 | return RlibLinkage |
| 108 | } |
| 109 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 110 | func (fuzzer *fuzzDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep { |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 111 | return rlibAutoDep |
| 112 | } |
hamzeh | c651b52 | 2021-04-29 12:50:47 -0700 | [diff] [blame] | 113 | |
hamzeh | c651b52 | 2021-04-29 12:50:47 -0700 | [diff] [blame] | 114 | func (fuzz *fuzzDecorator) install(ctx ModuleContext) { |
| 115 | fuzz.binaryDecorator.baseCompiler.dir = filepath.Join( |
| 116 | "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName()) |
| 117 | fuzz.binaryDecorator.baseCompiler.dir64 = filepath.Join( |
| 118 | "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName()) |
| 119 | fuzz.binaryDecorator.baseCompiler.install(ctx) |
| 120 | |
Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 121 | fuzz.fuzzPackagedModule = cc.PackageFuzzModule(ctx, fuzz.fuzzPackagedModule, pctx) |
| 122 | |
| 123 | installBase := "fuzz" |
| 124 | |
| 125 | // Grab the list of required shared libraries. |
| 126 | fuzz.sharedLibraries, _ = cc.CollectAllSharedDependencies(ctx) |
| 127 | |
Hamzeh Zawawy | 3891749 | 2023-04-05 22:08:46 +0000 | [diff] [blame] | 128 | for _, ruleBuilderInstall := range fuzz.sharedLibraries { |
| 129 | install := ruleBuilderInstall.To |
| 130 | |
Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 131 | fuzz.installedSharedDeps = append(fuzz.installedSharedDeps, |
| 132 | cc.SharedLibraryInstallLocation( |
Hamzeh Zawawy | 3891749 | 2023-04-05 22:08:46 +0000 | [diff] [blame] | 133 | install, ctx.Host(), installBase, ctx.Arch().ArchType.String())) |
Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 134 | |
| 135 | // Also add the dependency on the shared library symbols dir. |
| 136 | if !ctx.Host() { |
| 137 | fuzz.installedSharedDeps = append(fuzz.installedSharedDeps, |
Hamzeh Zawawy | 3891749 | 2023-04-05 22:08:46 +0000 | [diff] [blame] | 138 | cc.SharedLibrarySymbolsInstallLocation(install, installBase, ctx.Arch().ArchType.String())) |
Ivan Lozano | 0f9963e | 2023-02-06 13:31:02 -0500 | [diff] [blame] | 139 | } |
hamzeh | c651b52 | 2021-04-29 12:50:47 -0700 | [diff] [blame] | 140 | } |
| 141 | } |