blob: 235f51779cbf1d1a1f17af8f7c1b5dac7a16306f [file] [log] [blame]
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001// 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
15package rust
16
17import (
hamzehc651b522021-04-29 12:50:47 -070018 "path/filepath"
hamzehc651b522021-04-29 12:50:47 -070019
Ivan Lozano6cd99e62020-02-11 08:24:25 -050020 "android/soong/android"
21 "android/soong/cc"
hamzehc0a671f2021-07-22 12:05:08 -070022 "android/soong/fuzz"
Ivan Lozano6cd99e62020-02-11 08:24:25 -050023 "android/soong/rust/config"
24)
25
26func init() {
27 android.RegisterModuleType("rust_fuzz", RustFuzzFactory)
Ivan Lozano2fcbffa2023-07-27 10:40:52 -040028 android.RegisterModuleType("rust_fuzz_host", RustFuzzHostFactory)
Ivan Lozano6cd99e62020-02-11 08:24:25 -050029}
30
31type fuzzDecorator struct {
32 *binaryDecorator
33
Ivan Lozano0f9963e2023-02-06 13:31:02 -050034 fuzzPackagedModule fuzz.FuzzPackagedModule
Hamzeh Zawawy38917492023-04-05 22:08:46 +000035 sharedLibraries android.RuleBuilderInstalls
Ivan Lozano0f9963e2023-02-06 13:31:02 -050036 installedSharedDeps []string
Ivan Lozano6cd99e62020-02-11 08:24:25 -050037}
38
Ivan Lozano5482d6a2021-11-01 10:13:25 -040039var _ compiler = (*fuzzDecorator)(nil)
Ivan Lozano6cd99e62020-02-11 08:24:25 -050040
41// rust_binary produces a binary that is runnable on a device.
42func RustFuzzFactory() android.Module {
43 module, _ := NewRustFuzz(android.HostAndDeviceSupported)
44 return module.Init()
45}
46
Ivan Lozano2fcbffa2023-07-27 10:40:52 -040047func RustFuzzHostFactory() android.Module {
48 module, _ := NewRustFuzz(android.HostSupported)
49 return module.Init()
50}
51
Ivan Lozano6cd99e62020-02-11 08:24:25 -050052func 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
67func (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 Lozano6cd99e62020-02-11 08:24:25 -050071 flags.LinkFlags = append(flags.LinkFlags, `-Wl,-rpath,\$$ORIGIN/lib`)
72
Ivan Lozano0f9963e2023-02-06 13:31:02 -050073 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 Lozano6cd99e62020-02-11 08:24:25 -050079 return flags
80}
81
82func (fuzzer *fuzzDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
Liz Kammer9c210862021-04-12 18:52:29 -040083 if libFuzzerRuntimeLibrary := config.LibFuzzerRuntimeLibrary(ctx.toolchain()); libFuzzerRuntimeLibrary != "" {
84 deps.StaticLibs = append(deps.StaticLibs, libFuzzerRuntimeLibrary)
85 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -050086 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
94func (fuzzer *fuzzDecorator) compilerProps() []interface{} {
95 return append(fuzzer.binaryDecorator.compilerProps(),
hamzeh41ad8812021-07-07 14:00:07 -070096 &fuzzer.fuzzPackagedModule.FuzzProperties)
Ivan Lozano6cd99e62020-02-11 08:24:25 -050097}
98
Colin Cross31d89b42022-10-04 16:35:39 -070099func (fuzzer *fuzzDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
Colin Cross31d89b42022-10-04 16:35:39 -0700100
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500101 out := fuzzer.binaryDecorator.compile(ctx, flags, deps)
Colin Cross31d89b42022-10-04 16:35:39 -0700102
103 return out
104}
105
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500106func (fuzzer *fuzzDecorator) stdLinkage(ctx *depsContext) RustLinkage {
107 return RlibLinkage
108}
109
Liz Kammer356f7d42021-01-26 09:18:53 -0500110func (fuzzer *fuzzDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500111 return rlibAutoDep
112}
hamzehc651b522021-04-29 12:50:47 -0700113
hamzehc651b522021-04-29 12:50:47 -0700114func (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 Lozano0f9963e2023-02-06 13:31:02 -0500121 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 Zawawy38917492023-04-05 22:08:46 +0000128 for _, ruleBuilderInstall := range fuzz.sharedLibraries {
129 install := ruleBuilderInstall.To
130
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500131 fuzz.installedSharedDeps = append(fuzz.installedSharedDeps,
132 cc.SharedLibraryInstallLocation(
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000133 install, ctx.Host(), installBase, ctx.Arch().ArchType.String()))
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500134
135 // Also add the dependency on the shared library symbols dir.
136 if !ctx.Host() {
137 fuzz.installedSharedDeps = append(fuzz.installedSharedDeps,
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000138 cc.SharedLibrarySymbolsInstallLocation(install, installBase, ctx.Arch().ArchType.String()))
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500139 }
hamzehc651b522021-04-29 12:50:47 -0700140 }
141}