Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 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 | |
| 15 | package rust |
| 16 | |
| 17 | import ( |
| 18 | "strings" |
| 19 | "testing" |
| 20 | |
| 21 | "android/soong/android" |
| 22 | ) |
| 23 | |
| 24 | func TestRustFuzz(t *testing.T) { |
| 25 | ctx := testRust(t, ` |
| 26 | rust_library { |
| 27 | name: "libtest_fuzzing", |
| 28 | crate_name: "test_fuzzing", |
| 29 | srcs: ["foo.rs"], |
| 30 | } |
| 31 | rust_fuzz { |
| 32 | name: "fuzz_libtest", |
| 33 | srcs: ["foo.rs"], |
| 34 | rustlibs: ["libtest_fuzzing"], |
| 35 | } |
| 36 | `) |
| 37 | |
| 38 | // Check that appropriate dependencies are added and that the rustlib linkage is correct. |
| 39 | fuzz_libtest_mod := ctx.ModuleForTests("fuzz_libtest", "android_arm64_armv8-a_fuzzer").Module().(*Module) |
| 40 | if !android.InList("libclang_rt.asan-aarch64-android", fuzz_libtest_mod.Properties.AndroidMkSharedLibs) { |
| 41 | t.Errorf("libclang_rt.asan-aarch64-android shared library dependency missing for rust_fuzz module.") |
| 42 | } |
| 43 | if !android.InList("liblibfuzzer_sys.rlib-std", fuzz_libtest_mod.Properties.AndroidMkRlibs) { |
| 44 | t.Errorf("liblibfuzzer_sys rlib library dependency missing for rust_fuzz module. %#v", fuzz_libtest_mod.Properties.AndroidMkRlibs) |
| 45 | } |
| 46 | if !android.InList("libtest_fuzzing.rlib-std", fuzz_libtest_mod.Properties.AndroidMkRlibs) { |
| 47 | t.Errorf("rustlibs not linked as rlib for rust_fuzz module.") |
| 48 | } |
| 49 | |
| 50 | // Check that compiler flags are set appropriately . |
| 51 | fuzz_libtest := ctx.ModuleForTests("fuzz_libtest", "android_arm64_armv8-a_fuzzer").Output("fuzz_libtest") |
| 52 | if !strings.Contains(fuzz_libtest.Args["rustcFlags"], "-Z sanitizer=address") || |
| 53 | !strings.Contains(fuzz_libtest.Args["rustcFlags"], "-C passes='sancov'") || |
| 54 | !strings.Contains(fuzz_libtest.Args["rustcFlags"], "--cfg fuzzing") { |
| 55 | t.Errorf("rust_fuzz module does not contain the expected flags (sancov, cfg fuzzing, address sanitizer).") |
| 56 | |
| 57 | } |
| 58 | |
| 59 | // Check that dependencies have 'fuzzer' variants produced for them as well. |
| 60 | libtest_fuzzer := ctx.ModuleForTests("libtest_fuzzing", "android_arm64_armv8-a_rlib_rlib-std_fuzzer").Output("libtest_fuzzing.rlib") |
| 61 | if !strings.Contains(libtest_fuzzer.Args["rustcFlags"], "-Z sanitizer=address") || |
| 62 | !strings.Contains(libtest_fuzzer.Args["rustcFlags"], "-C passes='sancov'") || |
| 63 | !strings.Contains(libtest_fuzzer.Args["rustcFlags"], "--cfg fuzzing") { |
| 64 | t.Errorf("rust_fuzz dependent library does not contain the expected flags (sancov, cfg fuzzing, address sanitizer).") |
| 65 | } |
| 66 | } |