Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 1 | /* |
Yifan Hong | 4463d99 | 2017-05-01 18:43:36 -0700 | [diff] [blame] | 2 | * Copyright (C) 2017 The Android Open Source Project |
Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 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 | |
| 17 | #define LOG_TAG "VintfObject" |
| 18 | //#define LOG_NDEBUG 0 |
Yifan Hong | 4463d99 | 2017-05-01 18:43:36 -0700 | [diff] [blame] | 19 | #include <android-base/logging.h> |
| 20 | |
| 21 | #include <vector> |
| 22 | #include <string> |
Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 23 | |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 24 | #include <nativehelper/JNIHelp.h> |
Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 25 | #include <vintf/VintfObject.h> |
Yifan Hong | bf49821 | 2017-05-04 13:05:24 -0700 | [diff] [blame] | 26 | #include <vintf/parse_string.h> |
Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 27 | #include <vintf/parse_xml.h> |
| 28 | |
| 29 | #include "core_jni_helpers.h" |
| 30 | |
Yifan Hong | 4463d99 | 2017-05-01 18:43:36 -0700 | [diff] [blame] | 31 | static jclass gString; |
Yifan Hong | bf49821 | 2017-05-04 13:05:24 -0700 | [diff] [blame] | 32 | static jclass gHashMapClazz; |
| 33 | static jmethodID gHashMapInit; |
| 34 | static jmethodID gHashMapPut; |
Yifan Hong | 0e5e472 | 2018-02-07 14:16:21 -0800 | [diff] [blame] | 35 | static jclass gLongClazz; |
| 36 | static jmethodID gLongValueOf; |
Yifan Hong | 4463d99 | 2017-05-01 18:43:36 -0700 | [diff] [blame] | 37 | |
Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 38 | namespace android { |
| 39 | |
Yifan Hong | bf49821 | 2017-05-04 13:05:24 -0700 | [diff] [blame] | 40 | using vintf::HalManifest; |
Yifan Hong | 0e5e472 | 2018-02-07 14:16:21 -0800 | [diff] [blame] | 41 | using vintf::Level; |
Yifan Hong | bf49821 | 2017-05-04 13:05:24 -0700 | [diff] [blame] | 42 | using vintf::SchemaType; |
Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 43 | using vintf::VintfObject; |
Yifan Hong | bf49821 | 2017-05-04 13:05:24 -0700 | [diff] [blame] | 44 | using vintf::XmlConverter; |
| 45 | using vintf::Vndk; |
Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 46 | using vintf::gHalManifestConverter; |
Yifan Hong | 4463d99 | 2017-05-01 18:43:36 -0700 | [diff] [blame] | 47 | using vintf::gCompatibilityMatrixConverter; |
Yifan Hong | bf49821 | 2017-05-04 13:05:24 -0700 | [diff] [blame] | 48 | using vintf::to_string; |
Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 49 | |
Yifan Hong | bf49821 | 2017-05-04 13:05:24 -0700 | [diff] [blame] | 50 | template<typename V> |
| 51 | static inline jobjectArray toJavaStringArray(JNIEnv* env, const V& v) { |
| 52 | size_t i; |
| 53 | typename V::const_iterator it; |
Yifan Hong | 4463d99 | 2017-05-01 18:43:36 -0700 | [diff] [blame] | 54 | jobjectArray ret = env->NewObjectArray(v.size(), gString, NULL /* init element */); |
Yifan Hong | bf49821 | 2017-05-04 13:05:24 -0700 | [diff] [blame] | 55 | for (i = 0, it = v.begin(); it != v.end(); ++i, ++it) { |
| 56 | env->SetObjectArrayElement(ret, i, env->NewStringUTF(it->c_str())); |
Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 57 | } |
Yifan Hong | 4463d99 | 2017-05-01 18:43:36 -0700 | [diff] [blame] | 58 | return ret; |
Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Yifan Hong | 4463d99 | 2017-05-01 18:43:36 -0700 | [diff] [blame] | 61 | template<typename T> |
Yifan Hong | d666d5b | 2017-08-30 10:20:02 -0700 | [diff] [blame] | 62 | static void tryAddSchema(const std::shared_ptr<const T>& object, const XmlConverter<T>& converter, |
Yifan Hong | 4463d99 | 2017-05-01 18:43:36 -0700 | [diff] [blame] | 63 | const std::string& description, |
| 64 | std::vector<std::string>* cStrings) { |
| 65 | if (object == nullptr) { |
| 66 | LOG(WARNING) << __FUNCTION__ << "Cannot get " << description; |
| 67 | } else { |
| 68 | cStrings->push_back(converter(*object)); |
Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 69 | } |
Yifan Hong | 4463d99 | 2017-05-01 18:43:36 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Yifan Hong | d666d5b | 2017-08-30 10:20:02 -0700 | [diff] [blame] | 72 | static void tryAddHalNamesAndVersions(const std::shared_ptr<const HalManifest>& manifest, |
Yifan Hong | bf49821 | 2017-05-04 13:05:24 -0700 | [diff] [blame] | 73 | const std::string& description, |
| 74 | std::set<std::string> *output) { |
| 75 | if (manifest == nullptr) { |
| 76 | LOG(WARNING) << __FUNCTION__ << "Cannot get " << description; |
| 77 | } else { |
| 78 | auto names = manifest->getHalNamesAndVersions(); |
| 79 | output->insert(names.begin(), names.end()); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | static jobjectArray android_os_VintfObject_report(JNIEnv* env, jclass) |
Yifan Hong | 4463d99 | 2017-05-01 18:43:36 -0700 | [diff] [blame] | 84 | { |
| 85 | std::vector<std::string> cStrings; |
| 86 | |
| 87 | tryAddSchema(VintfObject::GetDeviceHalManifest(), gHalManifestConverter, |
| 88 | "device manifest", &cStrings); |
| 89 | tryAddSchema(VintfObject::GetFrameworkHalManifest(), gHalManifestConverter, |
| 90 | "framework manifest", &cStrings); |
| 91 | tryAddSchema(VintfObject::GetDeviceCompatibilityMatrix(), gCompatibilityMatrixConverter, |
| 92 | "device compatibility matrix", &cStrings); |
| 93 | tryAddSchema(VintfObject::GetFrameworkCompatibilityMatrix(), gCompatibilityMatrixConverter, |
| 94 | "framework compatibility matrix", &cStrings); |
| 95 | |
| 96 | return toJavaStringArray(env, cStrings); |
Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Yifan Hong | 65d1bd1 | 2018-08-08 13:15:18 -0700 | [diff] [blame] | 99 | static jint verify(JNIEnv* env, jobjectArray packageInfo, android::vintf::CheckFlags::Type checks) { |
Bowgo Tsai | 78934c9 | 2017-11-17 17:18:50 +0800 | [diff] [blame] | 100 | std::vector<std::string> cPackageInfo; |
| 101 | if (packageInfo) { |
| 102 | size_t count = env->GetArrayLength(packageInfo); |
| 103 | cPackageInfo.resize(count); |
| 104 | for (size_t i = 0; i < count; ++i) { |
| 105 | jstring element = (jstring)env->GetObjectArrayElement(packageInfo, i); |
| 106 | const char *cString = env->GetStringUTFChars(element, NULL /* isCopy */); |
| 107 | cPackageInfo[i] = cString; |
| 108 | env->ReleaseStringUTFChars(element, cString); |
| 109 | } |
Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 110 | } |
Yifan Hong | 87167af | 2017-07-05 16:39:51 -0700 | [diff] [blame] | 111 | std::string error; |
Bowgo Tsai | 78934c9 | 2017-11-17 17:18:50 +0800 | [diff] [blame] | 112 | int32_t status = VintfObject::CheckCompatibility(cPackageInfo, &error, checks); |
Yifan Hong | 87167af | 2017-07-05 16:39:51 -0700 | [diff] [blame] | 113 | if (status) |
| 114 | LOG(WARNING) << "VintfObject.verify() returns " << status << ": " << error; |
Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 115 | return status; |
| 116 | } |
| 117 | |
Bowgo Tsai | 78934c9 | 2017-11-17 17:18:50 +0800 | [diff] [blame] | 118 | static jint android_os_VintfObject_verify(JNIEnv* env, jclass, jobjectArray packageInfo) { |
Yifan Hong | 65d1bd1 | 2018-08-08 13:15:18 -0700 | [diff] [blame] | 119 | return verify(env, packageInfo, ::android::vintf::CheckFlags::ENABLE_ALL_CHECKS); |
Bowgo Tsai | 78934c9 | 2017-11-17 17:18:50 +0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | static jint android_os_VintfObject_verifyWithoutAvb(JNIEnv* env, jclass) { |
Yifan Hong | 65d1bd1 | 2018-08-08 13:15:18 -0700 | [diff] [blame] | 123 | return verify(env, nullptr, ::android::vintf::CheckFlags::DISABLE_AVB_CHECK); |
Bowgo Tsai | 78934c9 | 2017-11-17 17:18:50 +0800 | [diff] [blame] | 124 | } |
| 125 | |
Yifan Hong | bf49821 | 2017-05-04 13:05:24 -0700 | [diff] [blame] | 126 | static jobjectArray android_os_VintfObject_getHalNamesAndVersions(JNIEnv* env, jclass) { |
| 127 | std::set<std::string> halNames; |
| 128 | tryAddHalNamesAndVersions(VintfObject::GetDeviceHalManifest(), |
| 129 | "device manifest", &halNames); |
| 130 | tryAddHalNamesAndVersions(VintfObject::GetFrameworkHalManifest(), |
| 131 | "framework manifest", &halNames); |
| 132 | return toJavaStringArray(env, halNames); |
| 133 | } |
| 134 | |
| 135 | static jstring android_os_VintfObject_getSepolicyVersion(JNIEnv* env, jclass) { |
Yifan Hong | d666d5b | 2017-08-30 10:20:02 -0700 | [diff] [blame] | 136 | std::shared_ptr<const HalManifest> manifest = VintfObject::GetDeviceHalManifest(); |
Yifan Hong | bf49821 | 2017-05-04 13:05:24 -0700 | [diff] [blame] | 137 | if (manifest == nullptr || manifest->type() != SchemaType::DEVICE) { |
| 138 | LOG(WARNING) << __FUNCTION__ << "Cannot get device manifest"; |
| 139 | return nullptr; |
| 140 | } |
| 141 | std::string cString = to_string(manifest->sepolicyVersion()); |
| 142 | return env->NewStringUTF(cString.c_str()); |
| 143 | } |
| 144 | |
| 145 | static jobject android_os_VintfObject_getVndkSnapshots(JNIEnv* env, jclass) { |
Yifan Hong | d666d5b | 2017-08-30 10:20:02 -0700 | [diff] [blame] | 146 | std::shared_ptr<const HalManifest> manifest = VintfObject::GetFrameworkHalManifest(); |
Yifan Hong | bf49821 | 2017-05-04 13:05:24 -0700 | [diff] [blame] | 147 | if (manifest == nullptr || manifest->type() != SchemaType::FRAMEWORK) { |
| 148 | LOG(WARNING) << __FUNCTION__ << "Cannot get framework manifest"; |
| 149 | return nullptr; |
| 150 | } |
| 151 | jobject jMap = env->NewObject(gHashMapClazz, gHashMapInit); |
Yifan Hong | 8da8798c | 2018-01-10 16:04:18 -0800 | [diff] [blame] | 152 | for (const auto &vndk : manifest->vendorNdks()) { |
Chih-Hung Hsieh | c134063 | 2018-07-13 13:37:03 -0700 | [diff] [blame] | 153 | const std::string& key = vndk.version(); |
Yifan Hong | bf49821 | 2017-05-04 13:05:24 -0700 | [diff] [blame] | 154 | env->CallObjectMethod(jMap, gHashMapPut, |
| 155 | env->NewStringUTF(key.c_str()), toJavaStringArray(env, vndk.libraries())); |
| 156 | } |
| 157 | return jMap; |
| 158 | } |
| 159 | |
Yifan Hong | 0e5e472 | 2018-02-07 14:16:21 -0800 | [diff] [blame] | 160 | static jobject android_os_VintfObject_getTargetFrameworkCompatibilityMatrixVersion(JNIEnv* env, jclass) { |
| 161 | std::shared_ptr<const HalManifest> manifest = VintfObject::GetDeviceHalManifest(); |
| 162 | if (manifest == nullptr || manifest->level() == Level::UNSPECIFIED) { |
| 163 | return nullptr; |
| 164 | } |
| 165 | return env->CallStaticObjectMethod(gLongClazz, gLongValueOf, static_cast<jlong>(manifest->level())); |
| 166 | } |
| 167 | |
Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 168 | // ---------------------------------------------------------------------------- |
| 169 | |
| 170 | static const JNINativeMethod gVintfObjectMethods[] = { |
Yifan Hong | 4463d99 | 2017-05-01 18:43:36 -0700 | [diff] [blame] | 171 | {"report", "()[Ljava/lang/String;", (void*)android_os_VintfObject_report}, |
| 172 | {"verify", "([Ljava/lang/String;)I", (void*)android_os_VintfObject_verify}, |
Bowgo Tsai | 78934c9 | 2017-11-17 17:18:50 +0800 | [diff] [blame] | 173 | {"verifyWithoutAvb", "()I", (void*)android_os_VintfObject_verifyWithoutAvb}, |
Yifan Hong | bf49821 | 2017-05-04 13:05:24 -0700 | [diff] [blame] | 174 | {"getHalNamesAndVersions", "()[Ljava/lang/String;", (void*)android_os_VintfObject_getHalNamesAndVersions}, |
| 175 | {"getSepolicyVersion", "()Ljava/lang/String;", (void*)android_os_VintfObject_getSepolicyVersion}, |
| 176 | {"getVndkSnapshots", "()Ljava/util/Map;", (void*)android_os_VintfObject_getVndkSnapshots}, |
Yifan Hong | 0e5e472 | 2018-02-07 14:16:21 -0800 | [diff] [blame] | 177 | {"getTargetFrameworkCompatibilityMatrixVersion", "()Ljava/lang/Long;", (void*)android_os_VintfObject_getTargetFrameworkCompatibilityMatrixVersion}, |
Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | const char* const kVintfObjectPathName = "android/os/VintfObject"; |
| 181 | |
| 182 | int register_android_os_VintfObject(JNIEnv* env) |
| 183 | { |
Yifan Hong | 4463d99 | 2017-05-01 18:43:36 -0700 | [diff] [blame] | 184 | |
| 185 | gString = MakeGlobalRefOrDie(env, FindClassOrDie(env, "java/lang/String")); |
Yifan Hong | bf49821 | 2017-05-04 13:05:24 -0700 | [diff] [blame] | 186 | gHashMapClazz = MakeGlobalRefOrDie(env, FindClassOrDie(env, "java/util/HashMap")); |
| 187 | gHashMapInit = GetMethodIDOrDie(env, gHashMapClazz, "<init>", "()V"); |
| 188 | gHashMapPut = GetMethodIDOrDie(env, gHashMapClazz, |
| 189 | "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); |
Yifan Hong | 0e5e472 | 2018-02-07 14:16:21 -0800 | [diff] [blame] | 190 | gLongClazz = MakeGlobalRefOrDie(env, FindClassOrDie(env, "java/lang/Long")); |
| 191 | gLongValueOf = GetStaticMethodIDOrDie(env, gLongClazz, "valueOf", "(J)Ljava/lang/Long;"); |
Yifan Hong | 4463d99 | 2017-05-01 18:43:36 -0700 | [diff] [blame] | 192 | |
Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 193 | return RegisterMethodsOrDie(env, kVintfObjectPathName, gVintfObjectMethods, |
| 194 | NELEM(gVintfObjectMethods)); |
| 195 | } |
| 196 | |
| 197 | }; |