blob: 4c04ce8292e803134d8c3de6f388217b717008ec [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)
Ivan Lozano5467a392023-08-23 14:20:25 -040063
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 Lozano6cd99e62020-02-11 08:24:25 -050082 module.compiler = fuzz
83 return module, fuzz
84}
85
86func (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 Lozano6cd99e62020-02-11 08:24:25 -050090 flags.LinkFlags = append(flags.LinkFlags, `-Wl,-rpath,\$$ORIGIN/lib`)
91
Ivan Lozano0f9963e2023-02-06 13:31:02 -050092 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 Lozano6cd99e62020-02-11 08:24:25 -050098 return flags
99}
100
101func (fuzzer *fuzzDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
Liz Kammer9c210862021-04-12 18:52:29 -0400102 if libFuzzerRuntimeLibrary := config.LibFuzzerRuntimeLibrary(ctx.toolchain()); libFuzzerRuntimeLibrary != "" {
103 deps.StaticLibs = append(deps.StaticLibs, libFuzzerRuntimeLibrary)
104 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500105 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
113func (fuzzer *fuzzDecorator) compilerProps() []interface{} {
114 return append(fuzzer.binaryDecorator.compilerProps(),
hamzeh41ad8812021-07-07 14:00:07 -0700115 &fuzzer.fuzzPackagedModule.FuzzProperties)
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500116}
117
Colin Cross31d89b42022-10-04 16:35:39 -0700118func (fuzzer *fuzzDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
Colin Cross31d89b42022-10-04 16:35:39 -0700119
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500120 out := fuzzer.binaryDecorator.compile(ctx, flags, deps)
Colin Cross31d89b42022-10-04 16:35:39 -0700121
122 return out
123}
124
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500125func (fuzzer *fuzzDecorator) stdLinkage(ctx *depsContext) RustLinkage {
126 return RlibLinkage
127}
128
Liz Kammer356f7d42021-01-26 09:18:53 -0500129func (fuzzer *fuzzDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500130 return rlibAutoDep
131}
hamzehc651b522021-04-29 12:50:47 -0700132
hamzehc651b522021-04-29 12:50:47 -0700133func (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 Lozano0f9963e2023-02-06 13:31:02 -0500140 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 Zawawy38917492023-04-05 22:08:46 +0000147 for _, ruleBuilderInstall := range fuzz.sharedLibraries {
148 install := ruleBuilderInstall.To
149
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500150 fuzz.installedSharedDeps = append(fuzz.installedSharedDeps,
151 cc.SharedLibraryInstallLocation(
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000152 install, ctx.Host(), installBase, ctx.Arch().ArchType.String()))
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500153
154 // Also add the dependency on the shared library symbols dir.
155 if !ctx.Host() {
156 fuzz.installedSharedDeps = append(fuzz.installedSharedDeps,
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000157 cc.SharedLibrarySymbolsInstallLocation(install, installBase, ctx.Arch().ArchType.String()))
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500158 }
hamzehc651b522021-04-29 12:50:47 -0700159 }
160}