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 15package rust 16 17import ( 18 "strings" 19 "testing" 20 21 "android/soong/android" 22) 23 24func 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("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 . 48 fuzz_libtest := ctx.ModuleForTests("fuzz_libtest", "android_arm64_armv8-a_fuzzer").Rule("rustc") 49 if !strings.Contains(fuzz_libtest.Args["rustcFlags"], "-Z sanitizer=hwaddress") || 50 !strings.Contains(fuzz_libtest.Args["rustcFlags"], "-C passes='sancov-module'") || 51 !strings.Contains(fuzz_libtest.Args["rustcFlags"], "--cfg fuzzing") { 52 t.Errorf("rust_fuzz module does not contain the expected flags (sancov-module, cfg fuzzing, hwaddress sanitizer).") 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") 58 if !strings.Contains(libtest_fuzzer.Args["rustcFlags"], "-Z sanitizer=hwaddress") || 59 !strings.Contains(libtest_fuzzer.Args["rustcFlags"], "-C passes='sancov-module'") || 60 !strings.Contains(libtest_fuzzer.Args["rustcFlags"], "--cfg fuzzing") { 61 t.Errorf("rust_fuzz dependent library does not contain the expected flags (sancov-module, cfg fuzzing, hwaddress sanitizer).") 62 } 63} 64