1 /*
2  * Copyright (C) 2017 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 <stdio.h>
18 
19 #include "android-base/macros.h"
20 #include "jni.h"
21 #include "jvmti.h"
22 #include "scoped_utf_chars.h"
23 
24 // Test infrastructure
25 #include "jni_helper.h"
26 #include "jvmti_helper.h"
27 #include "test_env.h"
28 
29 namespace art {
30 namespace Test922Properties {
31 
Java_art_Test922_getSystemProperties(JNIEnv * env,jclass Main_klass ATTRIBUTE_UNUSED)32 extern "C" JNIEXPORT jobjectArray JNICALL Java_art_Test922_getSystemProperties(
33     JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED) {
34   jint count;
35   char** properties;
36   jvmtiError result = jvmti_env->GetSystemProperties(&count, &properties);
37   if (JvmtiErrorToException(env, jvmti_env, result)) {
38     return nullptr;
39   }
40 
41   auto callback = [&](jint i) -> jstring {
42     char* data = properties[i];
43     if (data == nullptr) {
44       return nullptr;
45     }
46     jstring ret = env->NewStringUTF(data);
47     jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(data));
48     return ret;
49   };
50   jobjectArray ret = CreateObjectArray(env, count, "java/lang/String", callback);
51 
52   jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(properties));
53 
54   return ret;
55 }
56 
Java_art_Test922_getSystemProperty(JNIEnv * env,jclass Main_klass ATTRIBUTE_UNUSED,jstring key)57 extern "C" JNIEXPORT jstring JNICALL Java_art_Test922_getSystemProperty(
58     JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jstring key) {
59   ScopedUtfChars string(env, key);
60   if (string.c_str() == nullptr) {
61     return nullptr;
62   }
63 
64   char* value = nullptr;
65   jvmtiError result = jvmti_env->GetSystemProperty(string.c_str(), &value);
66   if (JvmtiErrorToException(env, jvmti_env, result)) {
67     return nullptr;
68   }
69 
70   jstring ret = (value == nullptr) ? nullptr : env->NewStringUTF(value);
71 
72   jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(value));
73 
74   return ret;
75 }
76 
Java_art_Test922_setSystemProperty(JNIEnv * env,jclass Main_klass ATTRIBUTE_UNUSED,jstring key,jstring value)77 extern "C" JNIEXPORT void JNICALL Java_art_Test922_setSystemProperty(
78     JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jstring key, jstring value) {
79   ScopedUtfChars key_string(env, key);
80   if (key_string.c_str() == nullptr) {
81     return;
82   }
83   ScopedUtfChars value_string(env, value);
84   if (value_string.c_str() == nullptr) {
85     return;
86   }
87 
88   jvmtiError result = jvmti_env->SetSystemProperty(key_string.c_str(), value_string.c_str());
89   if (JvmtiErrorToException(env, jvmti_env, result)) {
90     return;
91   }
92 }
93 
94 }  // namespace Test922Properties
95 }  // namespace art
96