fuzz_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. package rust
  15. import (
  16. "strings"
  17. "testing"
  18. "android/soong/android"
  19. "android/soong/cc"
  20. )
  21. func TestRustFuzz(t *testing.T) {
  22. ctx := testRust(t, `
  23. rust_library {
  24. name: "libtest_fuzzing",
  25. crate_name: "test_fuzzing",
  26. srcs: ["foo.rs"],
  27. }
  28. rust_fuzz {
  29. name: "fuzz_libtest",
  30. srcs: ["foo.rs"],
  31. rustlibs: ["libtest_fuzzing"],
  32. }
  33. `)
  34. // Check that appropriate dependencies are added and that the rustlib linkage is correct.
  35. fuzz_libtest_mod := ctx.ModuleForTests("fuzz_libtest", "android_arm64_armv8-a_fuzzer").Module().(*Module)
  36. if !android.InList("liblibfuzzer_sys.rlib-std", fuzz_libtest_mod.Properties.AndroidMkRlibs) {
  37. t.Errorf("liblibfuzzer_sys rlib library dependency missing for rust_fuzz module. %#v", fuzz_libtest_mod.Properties.AndroidMkRlibs)
  38. }
  39. if !android.InList("libtest_fuzzing.rlib-std", fuzz_libtest_mod.Properties.AndroidMkRlibs) {
  40. t.Errorf("rustlibs not linked as rlib for rust_fuzz module.")
  41. }
  42. // Check that compiler flags are set appropriately .
  43. fuzz_libtest := ctx.ModuleForTests("fuzz_libtest", "android_arm64_armv8-a_fuzzer").Rule("rustc")
  44. if !strings.Contains(fuzz_libtest.Args["rustcFlags"], "-C passes='sancov-module'") ||
  45. !strings.Contains(fuzz_libtest.Args["rustcFlags"], "--cfg fuzzing") {
  46. t.Errorf("rust_fuzz module does not contain the expected flags (sancov-module, cfg fuzzing).")
  47. }
  48. // Check that dependencies have 'fuzzer' variants produced for them as well.
  49. libtest_fuzzer := ctx.ModuleForTests("libtest_fuzzing", "android_arm64_armv8-a_rlib_rlib-std_fuzzer").Output("libtest_fuzzing.rlib")
  50. if !strings.Contains(libtest_fuzzer.Args["rustcFlags"], "-C passes='sancov-module'") ||
  51. !strings.Contains(libtest_fuzzer.Args["rustcFlags"], "--cfg fuzzing") {
  52. t.Errorf("rust_fuzz dependent library does not contain the expected flags (sancov-module, cfg fuzzing).")
  53. }
  54. }
  55. func TestRustFuzzDepBundling(t *testing.T) {
  56. ctx := testRust(t, `
  57. cc_library {
  58. name: "libcc_transitive_dep",
  59. }
  60. cc_library {
  61. name: "libcc_direct_dep",
  62. }
  63. rust_library {
  64. name: "libtest_fuzzing",
  65. crate_name: "test_fuzzing",
  66. srcs: ["foo.rs"],
  67. shared_libs: ["libcc_transitive_dep"],
  68. }
  69. rust_fuzz {
  70. name: "fuzz_libtest",
  71. srcs: ["foo.rs"],
  72. rustlibs: ["libtest_fuzzing"],
  73. shared_libs: ["libcc_direct_dep"],
  74. }
  75. `)
  76. fuzz_libtest := ctx.ModuleForTests("fuzz_libtest", "android_arm64_armv8-a_fuzzer").Module().(*Module)
  77. if !strings.Contains(fuzz_libtest.FuzzSharedLibraries().String(), ":libcc_direct_dep.so") {
  78. t.Errorf("rust_fuzz does not contain the expected bundled direct shared libs ('libcc_direct_dep'): %#v", fuzz_libtest.FuzzSharedLibraries().String())
  79. }
  80. if !strings.Contains(fuzz_libtest.FuzzSharedLibraries().String(), ":libcc_transitive_dep.so") {
  81. t.Errorf("rust_fuzz does not contain the expected bundled transitive shared libs ('libcc_transitive_dep'): %#v", fuzz_libtest.FuzzSharedLibraries().String())
  82. }
  83. }
  84. func TestCCFuzzDepBundling(t *testing.T) {
  85. ctx := testRust(t, `
  86. cc_library {
  87. name: "libcc_transitive_dep",
  88. }
  89. rust_ffi {
  90. name: "libtest_fuzzing",
  91. crate_name: "test_fuzzing",
  92. srcs: ["foo.rs"],
  93. shared_libs: ["libcc_transitive_dep"],
  94. }
  95. cc_fuzz {
  96. name: "fuzz_shared_libtest",
  97. shared_libs: ["libtest_fuzzing"],
  98. }
  99. cc_fuzz {
  100. name: "fuzz_static_libtest",
  101. static_libs: ["libtest_fuzzing"],
  102. }
  103. `)
  104. fuzz_shared_libtest := ctx.ModuleForTests("fuzz_shared_libtest", "android_arm64_armv8-a_fuzzer").Module().(cc.LinkableInterface)
  105. fuzz_static_libtest := ctx.ModuleForTests("fuzz_static_libtest", "android_arm64_armv8-a_fuzzer").Module().(cc.LinkableInterface)
  106. if !strings.Contains(fuzz_shared_libtest.FuzzSharedLibraries().String(), ":libcc_transitive_dep.so") {
  107. t.Errorf("cc_fuzz does not contain the expected bundled transitive shared libs from rust_ffi_shared ('libcc_transitive_dep'): %#v", fuzz_shared_libtest.FuzzSharedLibraries().String())
  108. }
  109. if !strings.Contains(fuzz_static_libtest.FuzzSharedLibraries().String(), ":libcc_transitive_dep.so") {
  110. t.Errorf("cc_fuzz does not contain the expected bundled transitive shared libs from rust_ffi_static ('libcc_transitive_dep'): %#v", fuzz_static_libtest.FuzzSharedLibraries().String())
  111. }
  112. }