1 /*
2 * Copyright (C) 2020 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 #include <queue>
18 #include <vector>
19
20 #include "jvmti.h"
21
22 // Test infrastructure
23 #include "jvmti_helper.h"
24 #include "nativehelper/scoped_local_ref.h"
25 #include "nativehelper/scoped_primitive_array.h"
26 #include "test_env.h"
27
28 namespace art {
29 namespace Test2038HiddenApiExt {
30
31 template <typename T>
Dealloc(T * t)32 static void Dealloc(T* t) {
33 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(t));
34 }
35
36 template <typename T, typename... Rest>
Dealloc(T * t,Rest...rs)37 static void Dealloc(T* t, Rest... rs) {
38 Dealloc(t);
39 Dealloc(rs...);
40 }
41
DeallocParams(jvmtiParamInfo * params,jint n_params)42 static void DeallocParams(jvmtiParamInfo* params, jint n_params) {
43 for (jint i = 0; i < n_params; i++) {
44 Dealloc(params[i].name);
45 }
46 }
47
48 static constexpr std::string_view kDisablePolicyName =
49 "com.android.art.misc.disable_hidden_api_enforcement_policy";
50 static constexpr std::string_view kGetPolicyName =
51 "com.android.art.misc.get_hidden_api_enforcement_policy";
52 static constexpr std::string_view kSetPolicyName =
53 "com.android.art.misc.set_hidden_api_enforcement_policy";
54 using GetPolicy = jvmtiError (*)(jvmtiEnv*, jint*);
55 using SetPolicy = jvmtiError (*)(jvmtiEnv*, jint);
56 using DisablePolicy = jvmtiError (*)(jvmtiEnv*);
57
GetExtension(JNIEnv * env,const std::string_view & name)58 void* GetExtension(JNIEnv* env, const std::string_view& name) {
59 // Get the extensions.
60 jint n_ext = 0;
61 void* result = nullptr;
62 jvmtiExtensionFunctionInfo* infos = nullptr;
63 if (JvmtiErrorToException(env, jvmti_env, jvmti_env->GetExtensionFunctions(&n_ext, &infos))) {
64 return nullptr;
65 }
66 for (jint i = 0; i < n_ext; i++) {
67 jvmtiExtensionFunctionInfo* cur_info = &infos[i];
68 if (name == std::string_view(cur_info->id)) {
69 result = reinterpret_cast<void*>(cur_info->func);
70 }
71 // Cleanup the cur_info
72 DeallocParams(cur_info->params, cur_info->param_count);
73 Dealloc(cur_info->id, cur_info->short_description, cur_info->params, cur_info->errors);
74 }
75 // Cleanup the array.
76 Dealloc(infos);
77 if (result == nullptr) {
78 ScopedLocalRef<jclass> rt_exception(env, env->FindClass("java/lang/RuntimeException"));
79 env->ThrowNew(rt_exception.get(), "Unable to find policy extensions.");
80 return nullptr;
81 }
82 return result;
83 }
84
Java_Main_disablePolicy(JNIEnv * env,jclass)85 extern "C" JNIEXPORT jint JNICALL Java_Main_disablePolicy(JNIEnv* env, jclass) {
86 jint res;
87 GetPolicy get_policy = reinterpret_cast<GetPolicy>(GetExtension(env, kGetPolicyName));
88 if (get_policy == nullptr) {
89 return -1;
90 }
91 DisablePolicy disable_policy =
92 reinterpret_cast<DisablePolicy>(GetExtension(env, kDisablePolicyName));
93 if (disable_policy == nullptr) {
94 return -1;
95 }
96 if (JvmtiErrorToException(env, jvmti_env, get_policy(jvmti_env, &res))) {
97 return -1;
98 }
99 JvmtiErrorToException(env, jvmti_env, disable_policy(jvmti_env));
100 return res;
101 }
102
Java_Main_setPolicy(JNIEnv * env,jclass,jint pol)103 extern "C" JNIEXPORT jint JNICALL Java_Main_setPolicy(JNIEnv* env, jclass, jint pol) {
104 jint res;
105 GetPolicy get_policy = reinterpret_cast<GetPolicy>(GetExtension(env, kGetPolicyName));
106 if (get_policy == nullptr) {
107 return -1;
108 }
109 SetPolicy set_policy = reinterpret_cast<SetPolicy>(GetExtension(env, kSetPolicyName));
110 if (set_policy == nullptr) {
111 return -1;
112 }
113 if (JvmtiErrorToException(env, jvmti_env, get_policy(jvmti_env, &res))) {
114 return -1;
115 }
116 JvmtiErrorToException(env, jvmti_env, set_policy(jvmti_env, pol));
117 return res;
118 }
119
120 } // namespace Test2038HiddenApiExt
121 } // namespace art
122