fuzz.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. package rust
  15. import (
  16. "path/filepath"
  17. "android/soong/android"
  18. "android/soong/cc"
  19. "android/soong/fuzz"
  20. "android/soong/rust/config"
  21. )
  22. func init() {
  23. android.RegisterModuleType("rust_fuzz", RustFuzzFactory)
  24. }
  25. type fuzzDecorator struct {
  26. *binaryDecorator
  27. fuzzPackagedModule fuzz.FuzzPackagedModule
  28. sharedLibraries android.RuleBuilderInstalls
  29. installedSharedDeps []string
  30. }
  31. var _ compiler = (*fuzzDecorator)(nil)
  32. // rust_binary produces a binary that is runnable on a device.
  33. func RustFuzzFactory() android.Module {
  34. module, _ := NewRustFuzz(android.HostAndDeviceSupported)
  35. return module.Init()
  36. }
  37. func NewRustFuzz(hod android.HostOrDeviceSupported) (*Module, *fuzzDecorator) {
  38. module, binary := NewRustBinary(hod)
  39. fuzz := &fuzzDecorator{
  40. binaryDecorator: binary,
  41. }
  42. // Change the defaults for the binaryDecorator's baseCompiler
  43. fuzz.binaryDecorator.baseCompiler.dir = "fuzz"
  44. fuzz.binaryDecorator.baseCompiler.dir64 = "fuzz"
  45. fuzz.binaryDecorator.baseCompiler.location = InstallInData
  46. module.sanitize.SetSanitizer(cc.Fuzzer, true)
  47. module.compiler = fuzz
  48. return module, fuzz
  49. }
  50. func (fuzzer *fuzzDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
  51. flags = fuzzer.binaryDecorator.compilerFlags(ctx, flags)
  52. // `../lib` for installed fuzz targets (both host and device), and `./lib` for fuzz target packages.
  53. flags.LinkFlags = append(flags.LinkFlags, `-Wl,-rpath,\$$ORIGIN/lib`)
  54. if ctx.InstallInVendor() {
  55. flags.LinkFlags = append(flags.LinkFlags, `-Wl,-rpath,\$$ORIGIN/../../lib`)
  56. } else {
  57. flags.LinkFlags = append(flags.LinkFlags, `-Wl,-rpath,\$$ORIGIN/../lib`)
  58. }
  59. return flags
  60. }
  61. func (fuzzer *fuzzDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
  62. if libFuzzerRuntimeLibrary := config.LibFuzzerRuntimeLibrary(ctx.toolchain()); libFuzzerRuntimeLibrary != "" {
  63. deps.StaticLibs = append(deps.StaticLibs, libFuzzerRuntimeLibrary)
  64. }
  65. deps.SharedLibs = append(deps.SharedLibs, "libc++")
  66. deps.Rlibs = append(deps.Rlibs, "liblibfuzzer_sys")
  67. deps = fuzzer.binaryDecorator.compilerDeps(ctx, deps)
  68. return deps
  69. }
  70. func (fuzzer *fuzzDecorator) compilerProps() []interface{} {
  71. return append(fuzzer.binaryDecorator.compilerProps(),
  72. &fuzzer.fuzzPackagedModule.FuzzProperties)
  73. }
  74. func (fuzzer *fuzzDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
  75. out := fuzzer.binaryDecorator.compile(ctx, flags, deps)
  76. return out
  77. }
  78. func (fuzzer *fuzzDecorator) stdLinkage(ctx *depsContext) RustLinkage {
  79. return RlibLinkage
  80. }
  81. func (fuzzer *fuzzDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
  82. return rlibAutoDep
  83. }
  84. func (fuzz *fuzzDecorator) install(ctx ModuleContext) {
  85. fuzz.binaryDecorator.baseCompiler.dir = filepath.Join(
  86. "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
  87. fuzz.binaryDecorator.baseCompiler.dir64 = filepath.Join(
  88. "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
  89. fuzz.binaryDecorator.baseCompiler.install(ctx)
  90. fuzz.fuzzPackagedModule = cc.PackageFuzzModule(ctx, fuzz.fuzzPackagedModule, pctx)
  91. installBase := "fuzz"
  92. // Grab the list of required shared libraries.
  93. fuzz.sharedLibraries, _ = cc.CollectAllSharedDependencies(ctx)
  94. for _, ruleBuilderInstall := range fuzz.sharedLibraries {
  95. install := ruleBuilderInstall.To
  96. fuzz.installedSharedDeps = append(fuzz.installedSharedDeps,
  97. cc.SharedLibraryInstallLocation(
  98. install, ctx.Host(), installBase, ctx.Arch().ArchType.String()))
  99. // Also add the dependency on the shared library symbols dir.
  100. if !ctx.Host() {
  101. fuzz.installedSharedDeps = append(fuzz.installedSharedDeps,
  102. cc.SharedLibrarySymbolsInstallLocation(install, installBase, ctx.Arch().ArchType.String()))
  103. }
  104. }
  105. }