1 /* 2 * Copyright (C) 2016 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 "909-attach-agent/attach.h" 18 19 #include <jni.h> 20 #include <stdio.h> 21 #include <string.h> 22 23 #include "android-base/macros.h" 24 25 #include "jvmti.h" 26 27 namespace art { 28 namespace Test909AttachAgent { 29 30 static void Println(const char* c) { 31 fprintf(stdout, "%s\n", c); 32 fflush(stdout); 33 } 34 35 static constexpr jint kArtTiVersion = JVMTI_VERSION_1_2 | 0x40000000; 36 37 jint OnAttach(JavaVM* vm, 38 char* options ATTRIBUTE_UNUSED, 39 void* reserved ATTRIBUTE_UNUSED) { 40 Println("Attached Agent for test 909-attach-agent"); 41 jvmtiEnv* env = nullptr; 42 jvmtiEnv* env2 = nullptr; 43 44 #define CHECK_CALL_SUCCESS(c) \ 45 do { \ 46 if ((c) != JNI_OK) { \ 47 Println("call " #c " did not succeed"); \ 48 return -1; \ 49 } \ 50 } while (false) 51 52 if (vm->GetEnv(reinterpret_cast<void**>(&env), kArtTiVersion) == JNI_OK) { 53 Println("Created env for kArtTiVersion"); 54 CHECK_CALL_SUCCESS(env->DisposeEnvironment()); 55 env = nullptr; 56 } else { 57 Println("Failed to create env for kArtTiVersion"); 58 return -1; 59 } 60 if (vm->GetEnv(reinterpret_cast<void**>(&env), JVMTI_VERSION_1_0) != JNI_OK) { 61 Println("Unable to create env for JVMTI_VERSION_1_0"); 62 return 0; 63 } 64 CHECK_CALL_SUCCESS(vm->GetEnv(reinterpret_cast<void**>(&env2), JVMTI_VERSION_1_0)); 65 if (env == env2) { 66 Println("GetEnv returned same environment twice!"); 67 return -1; 68 } 69 unsigned char* local_data = nullptr; 70 CHECK_CALL_SUCCESS(env->Allocate(8, &local_data)); 71 strcpy(reinterpret_cast<char*>(local_data), "hello!!"); 72 CHECK_CALL_SUCCESS(env->SetEnvironmentLocalStorage(local_data)); 73 unsigned char* get_data = nullptr; 74 CHECK_CALL_SUCCESS(env->GetEnvironmentLocalStorage(reinterpret_cast<void**>(&get_data))); 75 if (get_data != local_data) { 76 Println("Got different data from local storage then what was set!"); 77 return -1; 78 } 79 CHECK_CALL_SUCCESS(env2->GetEnvironmentLocalStorage(reinterpret_cast<void**>(&get_data))); 80 if (get_data != nullptr) { 81 Println("env2 did not have nullptr local storage."); 82 return -1; 83 } 84 CHECK_CALL_SUCCESS(env->Deallocate(local_data)); 85 jint version = 0; 86 CHECK_CALL_SUCCESS(env->GetVersionNumber(&version)); 87 if ((version & JVMTI_VERSION_1) != JVMTI_VERSION_1) { 88 Println("Unexpected version number!"); 89 return -1; 90 } 91 CHECK_CALL_SUCCESS(env->DisposeEnvironment()); 92 CHECK_CALL_SUCCESS(env2->DisposeEnvironment()); 93 #undef CHECK_CALL_SUCCESS 94 return JNI_OK; 95 } 96 97 } // namespace Test909AttachAgent 98 } // namespace art 99