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 <jni.h>
18 #include <stdio.h>
19 
20 #include <android-base/logging.h>
21 #include <android-base/macros.h>
22 
23 #include "art_method-inl.h"
24 #include "jni/java_vm_ext.h"
25 #include "runtime.h"
26 
27 namespace art {
28 
29 constexpr jint TEST_900_ENV_VERSION_NUMBER = 0x900FFFFF;
30 constexpr uintptr_t ENV_VALUE = 900;
31 
32 // Allow this library to be used as a plugin too so we can test the stack.
GetEnvHandler(JavaVMExt * vm ATTRIBUTE_UNUSED,void ** new_env,jint version)33 static jint GetEnvHandler(JavaVMExt* vm ATTRIBUTE_UNUSED, void** new_env, jint version) {
34   printf("%s called in test 900\n", __func__);
35   if (version != TEST_900_ENV_VERSION_NUMBER) {
36     return JNI_EVERSION;
37   }
38   printf("GetEnvHandler called with version 0x%x\n", version);
39   *new_env = reinterpret_cast<void*>(ENV_VALUE);
40   return JNI_OK;
41 }
42 
ArtPlugin_Initialize()43 extern "C" bool ArtPlugin_Initialize() {
44   printf("%s called in test 900\n", __func__);
45   Runtime::Current()->GetJavaVM()->AddEnvironmentHook(GetEnvHandler);
46   return true;
47 }
48 
ArtPlugin_Deinitialize()49 extern "C" bool ArtPlugin_Deinitialize() {
50   printf("%s called in test 900\n", __func__);
51   return true;
52 }
53 
Agent_OnLoad(JavaVM * vm,char * options,void * reserved ATTRIBUTE_UNUSED)54 extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm,
55                                                char* options,
56                                                void* reserved ATTRIBUTE_UNUSED) {
57   printf("Agent_OnLoad called with options \"%s\"\n", options);
58   if (strcmp("test_900_round_2", options) == 0) {
59     return 0;
60   }
61   uintptr_t env = 0;
62   jint res = vm->GetEnv(reinterpret_cast<void**>(&env), TEST_900_ENV_VERSION_NUMBER);
63   if (res != JNI_OK) {
64     printf("GetEnv(TEST_900_ENV_VERSION_NUMBER) returned non-zero\n");
65   }
66   printf("GetEnv returned '%" PRIdPTR "' environment!\n", env);
67   return 0;
68 }
69 
Agent_OnUnload(JavaVM * vm ATTRIBUTE_UNUSED)70 extern "C" JNIEXPORT void JNICALL Agent_OnUnload(JavaVM* vm ATTRIBUTE_UNUSED) {
71   printf("Agent_OnUnload called\n");
72 }
73 
74 }  // namespace art
75