1 /*
2 * Copyright (C) 2017 The Android Open Source Project
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
19 #include <android-base/logging.h>
20
21 #include <vector>
22 #include <string>
23
24 #include <nativehelper/JNIHelp.h>
25 #include <vintf/VintfObject.h>
26 #include <vintf/parse_string.h>
27 #include <vintf/parse_xml.h>
28
29 #include "core_jni_helpers.h"
30
31 static jclass gString;
32 static jclass gHashMapClazz;
33 static jmethodID gHashMapInit;
34 static jmethodID gHashMapPut;
35 static jclass gLongClazz;
36 static jmethodID gLongValueOf;
37
38 namespace android {
39
40 using vintf::HalManifest;
41 using vintf::Level;
42 using vintf::SchemaType;
43 using vintf::VintfObject;
44 using vintf::XmlConverter;
45 using vintf::Vndk;
46 using vintf::gHalManifestConverter;
47 using vintf::gCompatibilityMatrixConverter;
48 using vintf::to_string;
49
50 template<typename V>
toJavaStringArray(JNIEnv * env,const V & v)51 static inline jobjectArray toJavaStringArray(JNIEnv* env, const V& v) {
52 size_t i;
53 typename V::const_iterator it;
54 jobjectArray ret = env->NewObjectArray(v.size(), gString, NULL /* init element */);
55 for (i = 0, it = v.begin(); it != v.end(); ++i, ++it) {
56 env->SetObjectArrayElement(ret, i, env->NewStringUTF(it->c_str()));
57 }
58 return ret;
59 }
60
61 template<typename T>
tryAddSchema(const std::shared_ptr<const T> & object,const XmlConverter<T> & converter,const std::string & description,std::vector<std::string> * cStrings)62 static void tryAddSchema(const std::shared_ptr<const T>& object, const XmlConverter<T>& converter,
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));
69 }
70 }
71
tryAddHalNamesAndVersions(const std::shared_ptr<const HalManifest> & manifest,const std::string & description,std::set<std::string> * output)72 static void tryAddHalNamesAndVersions(const std::shared_ptr<const HalManifest>& manifest,
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
android_os_VintfObject_report(JNIEnv * env,jclass)83 static jobjectArray android_os_VintfObject_report(JNIEnv* env, jclass)
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);
97 }
98
verify(JNIEnv * env,jobjectArray packageInfo,android::vintf::DisabledChecks checks)99 static jint verify(JNIEnv* env, jobjectArray packageInfo, android::vintf::DisabledChecks checks) {
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 }
110 }
111 std::string error;
112 int32_t status = VintfObject::CheckCompatibility(cPackageInfo, &error, checks);
113 if (status)
114 LOG(WARNING) << "VintfObject.verify() returns " << status << ": " << error;
115 return status;
116 }
117
android_os_VintfObject_verify(JNIEnv * env,jclass,jobjectArray packageInfo)118 static jint android_os_VintfObject_verify(JNIEnv* env, jclass, jobjectArray packageInfo) {
119 return verify(env, packageInfo, ::android::vintf::ENABLE_ALL_CHECKS);
120 }
121
android_os_VintfObject_verifyWithoutAvb(JNIEnv * env,jclass)122 static jint android_os_VintfObject_verifyWithoutAvb(JNIEnv* env, jclass) {
123 return verify(env, nullptr, ::android::vintf::DISABLE_AVB_CHECK);
124 }
125
android_os_VintfObject_getHalNamesAndVersions(JNIEnv * env,jclass)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
android_os_VintfObject_getSepolicyVersion(JNIEnv * env,jclass)135 static jstring android_os_VintfObject_getSepolicyVersion(JNIEnv* env, jclass) {
136 std::shared_ptr<const HalManifest> manifest = VintfObject::GetDeviceHalManifest();
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
android_os_VintfObject_getVndkSnapshots(JNIEnv * env,jclass)145 static jobject android_os_VintfObject_getVndkSnapshots(JNIEnv* env, jclass) {
146 std::shared_ptr<const HalManifest> manifest = VintfObject::GetFrameworkHalManifest();
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);
152 for (const auto &vndk : manifest->vendorNdks()) {
153 std::string key = vndk.version();
154 env->CallObjectMethod(jMap, gHashMapPut,
155 env->NewStringUTF(key.c_str()), toJavaStringArray(env, vndk.libraries()));
156 }
157 return jMap;
158 }
159
android_os_VintfObject_getTargetFrameworkCompatibilityMatrixVersion(JNIEnv * env,jclass)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
168 // ----------------------------------------------------------------------------
169
170 static const JNINativeMethod gVintfObjectMethods[] = {
171 {"report", "()[Ljava/lang/String;", (void*)android_os_VintfObject_report},
172 {"verify", "([Ljava/lang/String;)I", (void*)android_os_VintfObject_verify},
173 {"verifyWithoutAvb", "()I", (void*)android_os_VintfObject_verifyWithoutAvb},
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},
177 {"getTargetFrameworkCompatibilityMatrixVersion", "()Ljava/lang/Long;", (void*)android_os_VintfObject_getTargetFrameworkCompatibilityMatrixVersion},
178 };
179
180 const char* const kVintfObjectPathName = "android/os/VintfObject";
181
register_android_os_VintfObject(JNIEnv * env)182 int register_android_os_VintfObject(JNIEnv* env)
183 {
184
185 gString = MakeGlobalRefOrDie(env, FindClassOrDie(env, "java/lang/String"));
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;");
190 gLongClazz = MakeGlobalRefOrDie(env, FindClassOrDie(env, "java/lang/Long"));
191 gLongValueOf = GetStaticMethodIDOrDie(env, gLongClazz, "valueOf", "(J)Ljava/lang/Long;");
192
193 return RegisterMethodsOrDie(env, kVintfObjectPathName, gVintfObjectMethods,
194 NELEM(gVintfObjectMethods));
195 }
196
197 };
198