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) |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 40 | if !android.InList("liblibfuzzer_sys.rlib-std", fuzz_libtest_mod.Properties.AndroidMkRlibs) { |
| 41 | t.Errorf("liblibfuzzer_sys rlib library dependency missing for rust_fuzz module. %#v", fuzz_libtest_mod.Properties.AndroidMkRlibs) |
| 42 | } |
| 43 | if !android.InList("libtest_fuzzing.rlib-std", fuzz_libtest_mod.Properties.AndroidMkRlibs) { |
| 44 | t.Errorf("rustlibs not linked as rlib for rust_fuzz module.") |
| 45 | } |
| 46 | |
| 47 | // Check that compiler flags are set appropriately . |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 48 | fuzz_libtest := ctx.ModuleForTests("fuzz_libtest", "android_arm64_armv8-a_fuzzer").Rule("rustc") |
Tri Vo | 505b0e8 | 2021-04-08 15:50:48 -0700 | [diff] [blame] | 49 | if !strings.Contains(fuzz_libtest.Args["rustcFlags"], "-Z sanitizer=hwaddress") || |
Charisee | 5ddec43 | 2022-03-01 03:02:51 +0000 | [diff] [blame] | 50 | !strings.Contains(fuzz_libtest.Args["rustcFlags"], "-C passes='sancov-module'") || |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 51 | !strings.Contains(fuzz_libtest.Args["rustcFlags"], "--cfg fuzzing") { |
Charisee | 5ddec43 | 2022-03-01 03:02:51 +0000 | [diff] [blame] | 52 | t.Errorf("rust_fuzz module does not contain the expected flags (sancov-module, cfg fuzzing, hwaddress sanitizer).") |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 53 | |
| 54 | } |
| 55 | |
| 56 | // Check that dependencies have 'fuzzer' variants produced for them as well. |
| 57 | libtest_fuzzer := ctx.ModuleForTests("libtest_fuzzing", "android_arm64_armv8-a_rlib_rlib-std_fuzzer").Output("libtest_fuzzing.rlib") |
Tri Vo | 505b0e8 | 2021-04-08 15:50:48 -0700 | [diff] [blame] | 58 | if !strings.Contains(libtest_fuzzer.Args["rustcFlags"], "-Z sanitizer=hwaddress") || |
Charisee | 5ddec43 | 2022-03-01 03:02:51 +0000 | [diff] [blame] | 59 | !strings.Contains(libtest_fuzzer.Args["rustcFlags"], "-C passes='sancov-module'") || |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 60 | !strings.Contains(libtest_fuzzer.Args["rustcFlags"], "--cfg fuzzing") { |
Charisee | 5ddec43 | 2022-03-01 03:02:51 +0000 | [diff] [blame] | 61 | t.Errorf("rust_fuzz dependent library does not contain the expected flags (sancov-module, cfg fuzzing, hwaddress sanitizer).") |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 62 | } |
| 63 | } |