fuzz: support fuzzers with statically built parameters
am: 3eed136642

Change-Id: I2178efdf2f9dc3128ebe4457e422de85f53581ea
diff --git a/iface_fuzzer/Android.bp b/iface_fuzzer/Android.bp
index 719e11e..2cd6e2f 100644
--- a/iface_fuzzer/Android.bp
+++ b/iface_fuzzer/Android.bp
@@ -12,16 +12,24 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-cc_fuzz {
-    name: "vts_proto_fuzzer",
+filegroup {
+    name: "vts_proto_fuzzer_srcs",
     srcs: [
         "ProtoFuzzerMain.cpp",
         "ProtoFuzzerMutateFns.cpp",
         "ProtoFuzzerMutator.cpp",
         "ProtoFuzzerRunner.cpp",
+        "ProtoFuzzerStaticParams.cpp",
         "ProtoFuzzerStats.cpp",
         "ProtoFuzzerUtils.cpp",
     ],
+}
+
+cc_defaults {
+    name: "vts_proto_fuzzer_default",
+    srcs: [
+        ":vts_proto_fuzzer_srcs",
+    ],
     include_dirs: [
         "external/llvm/lib/Fuzzer",
         "test/vts/drivers/hal/common/include",
@@ -55,3 +63,8 @@
         disable: true,
     },
 }
+
+cc_fuzz {
+    name: "vts_proto_fuzzer",
+    defaults: ["vts_proto_fuzzer_default"],
+}
diff --git a/iface_fuzzer/ProtoFuzzerMain.cpp b/iface_fuzzer/ProtoFuzzerMain.cpp
index 58a7d9a..597eecb 100644
--- a/iface_fuzzer/ProtoFuzzerMain.cpp
+++ b/iface_fuzzer/ProtoFuzzerMain.cpp
@@ -50,6 +50,11 @@
 namespace vts {
 namespace fuzzer {
 
+#ifdef STATIC_TARGET_FQ_NAME
+// Returns parameters used for static fuzzer executables.
+extern ProtoFuzzerParams ExtractProtoFuzzerStaticParams(int argc, char **argv);
+#endif
+
 // 64-bit random number generator.
 static unique_ptr<Random> random;
 // Parameters that were passed in to fuzzer.
@@ -97,7 +102,12 @@
 }
 
 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) {
+#ifdef STATIC_TARGET_FQ_NAME
+  params = ExtractProtoFuzzerStaticParams(*argc, *argv);
+#else
   params = ExtractProtoFuzzerParams(*argc, *argv);
+#endif
+
   cerr << params.DebugString() << endl;
 
   random = make_unique<Random>(params.seed_);
diff --git a/iface_fuzzer/ProtoFuzzerStaticParams.cpp b/iface_fuzzer/ProtoFuzzerStaticParams.cpp
new file mode 100644
index 0000000..df61dda
--- /dev/null
+++ b/iface_fuzzer/ProtoFuzzerStaticParams.cpp
@@ -0,0 +1,89 @@
+/*
+ * Copyright 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "ProtoFuzzerUtils.h"
+
+#include <vintf/VintfObject.h>
+
+#define STRINGIFY(x) STRINGIFY_INTERNAL(x)
+#define STRINGIFY_INTERNAL(x) #x
+
+using android::FQName;
+using android::vintf::Version;
+using android::vintf::VintfObject;
+using std::cerr;
+using std::cout;
+using std::string;
+
+namespace android {
+namespace vts {
+namespace fuzzer {
+
+// TODO(b/145220086): fuzzer should attempt to fuzz all interfaces and instances
+// it can find.
+static FQName FindAnyIfaceFQName(const FQName &package_and_version,
+                                 const vector<CompSpec> &comp_specs) {
+  for (const auto &spec : comp_specs) {
+    auto package = package_and_version.package();
+    auto major_version = package_and_version.getPackageMajorVersion();
+    auto minor_version = package_and_version.getPackageMinorVersion();
+
+    if (package == spec.package() &&
+        major_version == spec.component_type_version_major() &&
+        minor_version == spec.component_type_version_minor()) {
+      auto iface_name = spec.component_name();
+      auto instance_names =
+          VintfObject::GetDeviceHalManifest()->getHidlInstances(
+              package,
+              Version(spec.component_type_version_major(),
+                      spec.component_type_version_minor()),
+              iface_name);
+
+      if (!instance_names.empty()) {
+        auto version =
+            std::to_string(major_version) + "." + std::to_string(minor_version);
+        return FQName{package, version, iface_name};
+      }
+    }
+  }
+  return FQName{};
+}
+
+ProtoFuzzerParams ExtractProtoFuzzerStaticParams(int argc, char **argv) {
+  ProtoFuzzerParams params = ExtractProtoFuzzerParams(argc, argv);
+  FQName package_and_version;
+
+  if (!FQName::parse(STRINGIFY(STATIC_TARGET_FQ_NAME), &package_and_version)) {
+    cerr << "STATIC_TARGET_FQ_NAME is malformed" << endl;
+    std::abort();
+  }
+
+  // Find first interface in the given package that fits the bill.
+  params.target_fq_name_ =
+      FindAnyIfaceFQName(package_and_version, params.comp_specs_);
+  if (!params.target_fq_name_.isFullyQualified()) {
+    cerr << "HAL service name not available in VINTF." << endl;
+    std::abort();
+  }
+
+  // Hard-coded values
+  params.exec_size_ = 16;
+  return params;
+}
+
+}  // namespace fuzzer
+}  // namespace vts
+}  // namespace android