1 /*
2  * Copyright (C) 2019 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 <atomic>
18 
19 #include "jvmti.h"
20 
21 // Test infrastructure
22 #include "jvmti_helper.h"
23 #include "scoped_local_ref.h"
24 #include "test_env.h"
25 
26 namespace art {
27 namespace Test1964AddToDexClassLoader {
28 
29 using AddToDexClassLoader = jvmtiError (*)(jvmtiEnv* env,
30                                                    jobject loader,
31                                                    const char* segment);
32 
Dealloc(T * t)33 template <typename T> static void Dealloc(T* t) {
34   jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(t));
35 }
36 
Dealloc(T * t,Rest...rs)37 template <typename T, typename... Rest> static void Dealloc(T* t, Rest... rs) {
38   Dealloc(t);
39   Dealloc(rs...);
40 }
DeallocParams(jvmtiParamInfo * params,jint n_params)41 static void DeallocParams(jvmtiParamInfo* params, jint n_params) {
42   for (jint i = 0; i < n_params; i++) {
43     Dealloc(params[i].name);
44   }
45 }
46 
GetAddFunction(JNIEnv * env)47 AddToDexClassLoader GetAddFunction(JNIEnv* env) {
48   // Get the extensions.
49   jint n_ext = 0;
50   jvmtiExtensionFunctionInfo* infos = nullptr;
51   if (JvmtiErrorToException(env, jvmti_env, jvmti_env->GetExtensionFunctions(&n_ext, &infos))) {
52     return nullptr;
53   }
54   AddToDexClassLoader result = nullptr;
55   for (jint i = 0; i < n_ext; i++) {
56     jvmtiExtensionFunctionInfo* cur_info = &infos[i];
57     if (strcmp("com.android.art.classloader.add_to_dex_class_loader", cur_info->id) ==
58         0) {
59       result = reinterpret_cast<AddToDexClassLoader>(cur_info->func);
60     }
61     // Cleanup the cur_info
62     DeallocParams(cur_info->params, cur_info->param_count);
63     Dealloc(cur_info->id, cur_info->short_description, cur_info->params, cur_info->errors);
64   }
65   // Cleanup the array.
66   Dealloc(infos);
67   return result;
68 }
69 
Java_Main_addToClassLoaderNative(JNIEnv * env,jclass,jobject loader,jstring segment)70 extern "C" JNIEXPORT void JNICALL Java_Main_addToClassLoaderNative(JNIEnv* env,
71                                                                            jclass,
72                                                                            jobject loader,
73                                                                            jstring segment) {
74   AddToDexClassLoader add_func = GetAddFunction(env);
75   if (add_func == nullptr) {
76     env->ThrowNew(env->FindClass("java/lang/RuntimeError"), "Failed to find extension function");
77     return;
78   }
79   const char* chars = env->GetStringUTFChars(segment, nullptr);
80   JvmtiErrorToException(
81       env,
82       jvmti_env,
83       add_func(jvmti_env,
84                loader,
85                chars));
86   env->ReleaseStringUTFChars(segment, chars);
87 }
88 
89 }  // namespace Test1964AddToDexClassLoader
90 }  // namespace art
91