FSTFuzzTestsPrincipal.mm 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright 2018 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <Foundation/NSObject.h>
  17. #include <string>
  18. #include "LibFuzzer/FuzzerDefs.h"
  19. #include "Firestore/Example/FuzzTests/FuzzingTargets/FSTFuzzTestFieldPath.h"
  20. #include "Firestore/Example/FuzzTests/FuzzingTargets/FSTFuzzTestSerializer.h"
  21. #include "Firestore/core/src/firebase/firestore/util/log.h"
  22. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  23. namespace {
  24. using firebase::firestore::util::MakeString;
  25. namespace fuzzing = firebase::firestore::fuzzing;
  26. // A list of targets to fuzz test. Should be kept in sync with the method
  27. // GetFuzzingTarget().
  28. enum class FuzzingTarget { kNone, kSerializer, kFieldPath };
  29. // Directory to which crashing inputs are written. Must include the '/' at the
  30. // end because libFuzzer prepends this path to the crashing input file name.
  31. // We write crashes to the temporary directory that is available to the iOS app.
  32. NSString *kCrashingInputsDirectory = NSTemporaryDirectory();
  33. // Retrieves the fuzzing target from the FUZZING_TARGET environment variable.
  34. // Default target is kNone if the environment variable is empty, not set, or
  35. // could not be interpreted. Should be kept in sync with FuzzingTarget.
  36. FuzzingTarget GetFuzzingTarget() {
  37. char *fuzzing_target_env = std::getenv("FUZZING_TARGET");
  38. if (fuzzing_target_env == nullptr) {
  39. LOG_WARN("No value provided for FUZZING_TARGET environment variable.");
  40. return FuzzingTarget::kNone;
  41. }
  42. // Convert fuzzing_target_env to std::string after verifying it is not null.
  43. std::string fuzzing_target{fuzzing_target_env};
  44. if (fuzzing_target.empty()) {
  45. LOG_WARN("No value provided for FUZZING_TARGET environment variable.");
  46. return FuzzingTarget::kNone;
  47. }
  48. if (fuzzing_target == "NONE") {
  49. return FuzzingTarget::kNone;
  50. }
  51. if (fuzzing_target == "SERIALIZER") {
  52. return FuzzingTarget::kSerializer;
  53. }
  54. if (fuzzing_target == "FIELDPATH") {
  55. return FuzzingTarget::kFieldPath;
  56. }
  57. LOG_WARN("Invalid fuzzing target: %s", fuzzing_target);
  58. return FuzzingTarget::kNone;
  59. }
  60. // Simulates calling the main() function of libFuzzer (FuzzerMain.cpp).
  61. // Uses GetFuzzingTarget() to get the fuzzing target and sets libFuzzer's args
  62. // accordingly. It also calls an appropriate LLVMFuzzerTestOneInput-like method
  63. // for the defined target.
  64. int RunFuzzTestingMain() {
  65. // Get the fuzzing target.
  66. FuzzingTarget fuzzing_target = GetFuzzingTarget();
  67. // All fuzzing resources.
  68. std::string resources_location = "Firestore_FuzzTests_iOS.xctest/FuzzingResources";
  69. // The dictionary location for the fuzzing target.
  70. std::string dict_location;
  71. // The corpus location for the fuzzing target.
  72. std::string corpus_location;
  73. // Fuzzing target method, equivalent to LLVMFuzzerTestOneInput. Holds a pointer
  74. // to the fuzzing method that is called repeatedly by the fuzzing driver with
  75. // different inputs. Any method assigned to this variable must have the same
  76. // signature as LLVMFuzzerTestOneInput: int(const uint8_t*, size_t).
  77. fuzzer::UserCallback fuzzer_function;
  78. // Set the dictionary and corpus locations according to the fuzzing target.
  79. switch (fuzzing_target) {
  80. case FuzzingTarget::kSerializer:
  81. dict_location = fuzzing::GetSerializerDictionaryLocation(resources_location);
  82. corpus_location = fuzzing::GetSerializerCorpusLocation();
  83. fuzzer_function = fuzzing::FuzzTestDeserialization;
  84. break;
  85. case FuzzingTarget::kFieldPath:
  86. dict_location = fuzzing::GetFieldPathDictionaryLocation(resources_location);
  87. corpus_location = fuzzing::GetFieldPathCorpusLocation(resources_location);
  88. fuzzer_function = fuzzing::FuzzTestFieldPath;
  89. break;
  90. case FuzzingTarget::kNone:
  91. default:
  92. LOG_WARN("Not going to run fuzzing, exiting!");
  93. return 0;
  94. }
  95. // Get dictionary and corpus paths from resources and convert to program arguments.
  96. NSString *plugins_path = [[NSBundle mainBundle] builtInPlugInsPath];
  97. std::string dict_path = MakeString(plugins_path) + "/" + dict_location;
  98. std::string dict_arg = std::string("-dict=") + dict_path;
  99. // No argument prefix required for corpus arg.
  100. std::string corpus_arg = MakeString(plugins_path) + "/" + corpus_location;
  101. // The directory in which libFuzzer writes crashing inputs.
  102. std::string prefix_arg = std::string("-artifact_prefix=") + MakeString(kCrashingInputsDirectory);
  103. // Arguments to libFuzzer main() function should be added to this array,
  104. // e.g., dictionaries, corpus, number of runs, jobs, etc. The FuzzerDriver of
  105. // libFuzzer expects the non-const argument 'char ***argv' and it does not
  106. // modify it throughout the method.
  107. char *program_args[] = {
  108. const_cast<char *>("RunFuzzTestingMain"), // First arg is program name.
  109. const_cast<char *>(prefix_arg.c_str()), // Crashing inputs directory.
  110. const_cast<char *>(dict_arg.c_str()), // Dictionary arg.
  111. const_cast<char *>(corpus_arg.c_str()) // Corpus must be the last arg.
  112. };
  113. char **argv = program_args;
  114. int argc = sizeof(program_args) / sizeof(program_args[0]);
  115. // Start fuzzing using libFuzzer's driver.
  116. return fuzzer::FuzzerDriver(&argc, &argv, fuzzer_function);
  117. }
  118. } // namespace
  119. /**
  120. * This class is registered as the NSPrincipalClass in the
  121. * Firestore_FuzzTests_iOS bundle's Info.plist. XCTest instantiates this class
  122. * to perform one-time setup for the test bundle, as documented here:
  123. *
  124. * https://developer.apple.com/documentation/xctest/xctestobservationcenter
  125. */
  126. @interface FSTFuzzTestsPrincipal : NSObject
  127. @end
  128. @implementation FSTFuzzTestsPrincipal
  129. - (instancetype)init {
  130. self = [super init];
  131. RunFuzzTestingMain();
  132. return self;
  133. }
  134. @end