Keystore 2.0: Make keystore2 early boot ready.

Test: Keystore starts early and quickly without panicking.
Change-Id: I1c694e5b45a0f15075c625bb4d0604c6e17588f9
diff --git a/keystore2/src/vintf/Android.bp b/keystore2/src/vintf/Android.bp
index 31a1de9..da7935a 100644
--- a/keystore2/src/vintf/Android.bp
+++ b/keystore2/src/vintf/Android.bp
@@ -46,6 +46,7 @@
         "--size_t-is-usize",
         "--whitelist-function", "getHalNames",
         "--whitelist-function", "getHalNamesAndVersions",
+        "--whitelist-function", "getAidlInstances",
         "--whitelist-function", "freeNames",
     ],
 }
diff --git a/keystore2/src/vintf/lib.rs b/keystore2/src/vintf/lib.rs
index 3a10ece..c3d6d8a 100644
--- a/keystore2/src/vintf/lib.rs
+++ b/keystore2/src/vintf/lib.rs
@@ -14,8 +14,8 @@
 
 //! Bindings for getting the list of HALs.
 
-use keystore2_vintf_bindgen::{freeNames, getHalNames, getHalNamesAndVersions};
-use std::ffi::CStr;
+use keystore2_vintf_bindgen::{freeNames, getAidlInstances, getHalNames, getHalNamesAndVersions};
+use std::ffi::{CStr, CString};
 use std::os::raw::c_char;
 use std::str::Utf8Error;
 
@@ -63,6 +63,20 @@
     HalNames { data: raw_strs, len }
 }
 
+/// Gets the instances of the given package, version, and interface tuple.
+/// Note that this is not a zero-cost shim: it will make copies of the strings.
+pub fn get_aidl_instances(package: &str, version: usize, interface_name: &str) -> HalNames {
+    let mut len: usize = 0;
+    let packages = CString::new(package).expect("Failed to make CString from package.");
+    let interface_name =
+        CString::new(interface_name).expect("Failed to make CString from interface_name.");
+    // Safety: We'll wrap this in HalNames to free the memory it allocates.
+    // It stores the size of the array it returns in len.
+    let raw_strs =
+        unsafe { getAidlInstances(&mut len, packages.as_ptr(), version, interface_name.as_ptr()) };
+    HalNames { data: raw_strs, len }
+}
+
 #[cfg(test)]
 mod tests {
 
diff --git a/keystore2/src/vintf/vintf.cpp b/keystore2/src/vintf/vintf.cpp
index 1e69e7e..dbdc046 100644
--- a/keystore2/src/vintf/vintf.cpp
+++ b/keystore2/src/vintf/vintf.cpp
@@ -43,6 +43,14 @@
     return convert(names);
 }
 
+char** getAidlInstances(size_t* len, const char* package, size_t version,
+                        const char* interfaceName) {
+    auto manifest = android::vintf::VintfObject::GetDeviceHalManifest();
+    const auto names = manifest->getAidlInstances(package, version, interfaceName);
+    *len = names.size();
+    return convert(names);
+}
+
 void freeNames(char** names, size_t len) {
     for (int i = 0; i < len; i++) {
         free(names[i]);
diff --git a/keystore2/src/vintf/vintf.hpp b/keystore2/src/vintf/vintf.hpp
index 4d2c905..75e80f6 100644
--- a/keystore2/src/vintf/vintf.hpp
+++ b/keystore2/src/vintf/vintf.hpp
@@ -23,8 +23,9 @@
 
 char** getHalNames(size_t* len);
 char** getHalNamesAndVersions(size_t* len);
+char** getAidlInstances(size_t* len, const char* package, size_t version,
+                        const char* interfaceName);
 void freeNames(char** names, size_t len);
-
 }
 
 #endif  //  __VINTF_H__