Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 1 | // Copyright 2016 Google Inc. All rights reserved. |
| 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 cc |
| 16 | |
| 17 | import ( |
Mitch Phillips | 4de896e | 2019-08-28 16:04:36 -0700 | [diff] [blame^] | 18 | "path/filepath" |
| 19 | |
| 20 | "github.com/google/blueprint/proptools" |
| 21 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 22 | "android/soong/android" |
| 23 | "android/soong/cc/config" |
| 24 | ) |
| 25 | |
| 26 | func init() { |
| 27 | android.RegisterModuleType("cc_fuzz", FuzzFactory) |
| 28 | } |
| 29 | |
| 30 | // cc_fuzz creates a host/device fuzzer binary. Host binaries can be found at |
| 31 | // $ANDROID_HOST_OUT/fuzz/, and device binaries can be found at /data/fuzz on |
| 32 | // your device, or $ANDROID_PRODUCT_OUT/data/fuzz in your build tree. |
| 33 | func FuzzFactory() android.Module { |
| 34 | module := NewFuzz(android.HostAndDeviceSupported) |
| 35 | return module.Init() |
| 36 | } |
| 37 | |
| 38 | func NewFuzzInstaller() *baseInstaller { |
| 39 | return NewBaseInstaller("fuzz", "fuzz", InstallInData) |
| 40 | } |
| 41 | |
| 42 | type fuzzBinary struct { |
| 43 | *binaryDecorator |
| 44 | *baseCompiler |
| 45 | } |
| 46 | |
| 47 | func (fuzz *fuzzBinary) linkerProps() []interface{} { |
| 48 | props := fuzz.binaryDecorator.linkerProps() |
| 49 | return props |
| 50 | } |
| 51 | |
| 52 | func (fuzz *fuzzBinary) linkerInit(ctx BaseModuleContext) { |
| 53 | // Add ../lib[64] to rpath so that out/host/linux-x86/fuzz/<fuzzer> can |
| 54 | // find out/host/linux-x86/lib[64]/library.so |
| 55 | runpaths := []string{"../lib"} |
| 56 | for _, runpath := range runpaths { |
| 57 | if ctx.toolchain().Is64Bit() { |
| 58 | runpath += "64" |
| 59 | } |
| 60 | fuzz.binaryDecorator.baseLinker.dynamicProperties.RunPaths = append( |
| 61 | fuzz.binaryDecorator.baseLinker.dynamicProperties.RunPaths, runpath) |
| 62 | } |
| 63 | |
| 64 | // add "" to rpath so that fuzzer binaries can find libraries in their own fuzz directory |
| 65 | fuzz.binaryDecorator.baseLinker.dynamicProperties.RunPaths = append( |
| 66 | fuzz.binaryDecorator.baseLinker.dynamicProperties.RunPaths, "") |
| 67 | |
| 68 | fuzz.binaryDecorator.linkerInit(ctx) |
| 69 | } |
| 70 | |
| 71 | func (fuzz *fuzzBinary) linkerDeps(ctx DepsContext, deps Deps) Deps { |
| 72 | deps.StaticLibs = append(deps.StaticLibs, |
| 73 | config.LibFuzzerRuntimeLibrary(ctx.toolchain())) |
| 74 | deps = fuzz.binaryDecorator.linkerDeps(ctx, deps) |
| 75 | return deps |
| 76 | } |
| 77 | |
| 78 | func (fuzz *fuzzBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
| 79 | flags = fuzz.binaryDecorator.linkerFlags(ctx, flags) |
| 80 | return flags |
| 81 | } |
| 82 | |
| 83 | func (fuzz *fuzzBinary) install(ctx ModuleContext, file android.Path) { |
Mitch Phillips | 4de896e | 2019-08-28 16:04:36 -0700 | [diff] [blame^] | 84 | fuzz.binaryDecorator.baseInstaller.dir = filepath.Join("fuzz", ctx.Target().Arch.ArchType.String()) |
| 85 | fuzz.binaryDecorator.baseInstaller.dir64 = filepath.Join("fuzz", ctx.Target().Arch.ArchType.String()) |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 86 | fuzz.binaryDecorator.baseInstaller.install(ctx, file) |
| 87 | } |
| 88 | |
| 89 | func NewFuzz(hod android.HostOrDeviceSupported) *Module { |
| 90 | module, binary := NewBinary(hod) |
| 91 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 92 | binary.baseInstaller = NewFuzzInstaller() |
| 93 | module.sanitize.SetSanitizer(fuzzer, true) |
| 94 | |
| 95 | fuzz := &fuzzBinary{ |
| 96 | binaryDecorator: binary, |
| 97 | baseCompiler: NewBaseCompiler(), |
| 98 | } |
| 99 | module.compiler = fuzz |
| 100 | module.linker = fuzz |
| 101 | module.installer = fuzz |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 102 | |
| 103 | // The fuzzer runtime is not present for darwin host modules, disable cc_fuzz modules when targeting darwin. |
| 104 | android.AddLoadHook(module, func(ctx android.LoadHookContext) { |
Alex Light | 71123ec | 2019-07-24 13:34:19 -0700 | [diff] [blame] | 105 | disableDarwinAndLinuxBionic := struct { |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 106 | Target struct { |
| 107 | Darwin struct { |
| 108 | Enabled *bool |
| 109 | } |
Alex Light | 71123ec | 2019-07-24 13:34:19 -0700 | [diff] [blame] | 110 | Linux_bionic struct { |
| 111 | Enabled *bool |
| 112 | } |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 113 | } |
| 114 | }{} |
Alex Light | 71123ec | 2019-07-24 13:34:19 -0700 | [diff] [blame] | 115 | disableDarwinAndLinuxBionic.Target.Darwin.Enabled = BoolPtr(false) |
| 116 | disableDarwinAndLinuxBionic.Target.Linux_bionic.Enabled = BoolPtr(false) |
| 117 | ctx.AppendProperties(&disableDarwinAndLinuxBionic) |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 118 | }) |
| 119 | |
Mitch Phillips | d0bd16d | 2019-08-22 15:47:09 -0700 | [diff] [blame] | 120 | // Statically link the STL. This allows fuzz target deployment to not have to |
| 121 | // include the STL. |
| 122 | android.AddLoadHook(module, func(ctx android.LoadHookContext) { |
| 123 | staticStlLinkage := struct { |
| 124 | Stl *string |
| 125 | }{} |
| 126 | |
| 127 | staticStlLinkage.Stl = proptools.StringPtr("libc++_static") |
| 128 | ctx.AppendProperties(&staticStlLinkage) |
| 129 | }) |
| 130 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 131 | return module |
| 132 | } |