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