blob: 1c3f36d8ca24b7b359579463eaf190d865a10b4d [file] [log] [blame]
Alex Light639e73b2019-05-17 21:44:36 +00001/*
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
26namespace art {
27namespace Test1963AddToDexClassLoaderInMemory {
28
29using AddToDexClassLoaderInMemory = jvmtiError (*)(jvmtiEnv* env,
30 jobject loader,
31 const unsigned char* dex_file,
32 jint dex_file_length);
33
34template <typename T> static void Dealloc(T* t) {
35 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(t));
36}
37
38template <typename T, typename... Rest> static void Dealloc(T* t, Rest... rs) {
39 Dealloc(t);
40 Dealloc(rs...);
41}
42static void DeallocParams(jvmtiParamInfo* params, jint n_params) {
43 for (jint i = 0; i < n_params; i++) {
44 Dealloc(params[i].name);
45 }
46}
47
48AddToDexClassLoaderInMemory GetAddFunction(JNIEnv* env) {
49 // Get the extensions.
50 jint n_ext = 0;
51 jvmtiExtensionFunctionInfo* infos = nullptr;
52 if (JvmtiErrorToException(env, jvmti_env, jvmti_env->GetExtensionFunctions(&n_ext, &infos))) {
53 return nullptr;
54 }
55 AddToDexClassLoaderInMemory result = nullptr;
56 for (jint i = 0; i < n_ext; i++) {
57 jvmtiExtensionFunctionInfo* cur_info = &infos[i];
58 if (strcmp("com.android.art.classloader.add_to_dex_class_loader_in_memory", cur_info->id) ==
59 0) {
60 result = reinterpret_cast<AddToDexClassLoaderInMemory>(cur_info->func);
61 }
62 // Cleanup the cur_info
63 DeallocParams(cur_info->params, cur_info->param_count);
64 Dealloc(cur_info->id, cur_info->short_description, cur_info->params, cur_info->errors);
65 }
66 // Cleanup the array.
67 Dealloc(infos);
68 return result;
69}
70
71extern "C" JNIEXPORT void JNICALL Java_art_Test1963_addToClassLoaderNative(JNIEnv* env,
72 jclass,
73 jobject loader,
74 jobject bytebuffer) {
75 AddToDexClassLoaderInMemory add_func = GetAddFunction(env);
76 if (add_func == nullptr) {
77 env->ThrowNew(env->FindClass("java/lang/RuntimeError"), "Failed to find extension function");
78 return;
79 }
80 JvmtiErrorToException(
81 env,
82 jvmti_env,
83 add_func(jvmti_env,
84 loader,
85 reinterpret_cast<unsigned char*>(env->GetDirectBufferAddress(bytebuffer)),
86 env->GetDirectBufferCapacity(bytebuffer)));
87}
88
89} // namespace Test1963AddToDexClassLoaderInMemory
90} // namespace art