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