1 /*
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 #ifndef LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_LIBNATIVEHELPER_API_H_
18 #define LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_LIBNATIVEHELPER_API_H_
19 
20 #include <stddef.h>
21 
22 #include "jni.h"
23 
24 // This is the stable C API exported by libnativehelper.
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif  // __cplusplus
29 
30 /* ------------------------------------ C API for JNIHelp.h ------------------------------------- */
31 
32 /*
33  * Register one or more native methods with a particular class.  "className" looks like
34  * "java/lang/String". Aborts on failure, returns 0 on success.
35  */
36 int jniRegisterNativeMethods(C_JNIEnv* env,
37                              const char* className,
38                              const JNINativeMethod* gMethods,
39                              int numMethods);
40 
41 /*
42  * Throw an exception with the specified class and an optional message.
43  *
44  * The "className" argument will be passed directly to FindClass, which
45  * takes strings with slashes (e.g. "java/lang/Object").
46  *
47  * If an exception is currently pending, we log a warning message and
48  * clear it.
49  *
50  * Returns 0 on success, nonzero if something failed (e.g. the exception
51  * class couldn't be found, so *an* exception will still be pending).
52  *
53  * Currently aborts the VM if it can't throw the exception.
54  */
55 int jniThrowException(C_JNIEnv* env, const char* className, const char* msg);
56 
57 /*
58  * Throw an exception with the specified class and formatted error message.
59  *
60  * The "className" argument will be passed directly to FindClass, which
61  * takes strings with slashes (e.g. "java/lang/Object").
62  *
63  * If an exception is currently pending, we log a warning message and
64  * clear it.
65  *
66  * Returns 0 on success, nonzero if something failed (e.g. the exception
67  * class couldn't be found, so *an* exception will still be pending).
68  *
69  * Currently aborts the VM if it can't throw the exception.
70  */
71 int jniThrowExceptionFmt(C_JNIEnv* env, const char* className, const char* fmt, va_list args);
72 
73 /*
74  * Throw a java.lang.NullPointerException, with an optional message.
75  */
76 int jniThrowNullPointerException(C_JNIEnv* env, const char* msg);
77 
78 /*
79  * Throw a java.lang.RuntimeException, with an optional message.
80  */
81 int jniThrowRuntimeException(C_JNIEnv* env, const char* msg);
82 
83 /*
84  * Throw a java.io.IOException, generating the message from errno.
85  */
86 int jniThrowIOException(C_JNIEnv* env, int errnum);
87 
88 /*
89  * Return a pointer to a locale-dependent error string explaining errno
90  * value 'errnum'. The returned pointer may or may not be equal to 'buf'.
91  * This function is thread-safe (unlike strerror) and portable (unlike
92  * strerror_r).
93  */
94 const char* jniStrError(int errnum, char* buf, size_t buflen);
95 
96 /*
97  * Returns a new java.io.FileDescriptor for the given int fd.
98  */
99 jobject jniCreateFileDescriptor(C_JNIEnv* env, int fd);
100 
101 /*
102  * Returns the int fd from a java.io.FileDescriptor.
103  */
104 int jniGetFDFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor);
105 
106 /*
107  * Sets the int fd in a java.io.FileDescriptor.  Throws java.lang.NullPointerException
108  * if fileDescriptor is null.
109  */
110 void jniSetFileDescriptorOfFD(C_JNIEnv* env, jobject fileDescriptor, int value);
111 
112 /*
113  * Returns the long ownerId from a java.io.FileDescriptor.
114  */
115 jlong jniGetOwnerIdFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor);
116 
117 /*
118  * Gets the managed heap array backing a java.nio.Buffer instance.
119  *
120  * Returns nullptr if there is no array backing.
121  *
122  * This method performs a JNI call to java.nio.NIOAccess.getBaseArray().
123  */
124 jarray jniGetNioBufferBaseArray(C_JNIEnv* env, jobject nioBuffer);
125 
126 /*
127  * Gets the offset in bytes from the start of the managed heap array backing the buffer.
128  *
129  * Returns 0 if there is no array backing.
130  *
131  * This method performs a JNI call to java.nio.NIOAccess.getBaseArrayOffset().
132  */
133 jint jniGetNioBufferBaseArrayOffset(C_JNIEnv* env, jobject nioBuffer);
134 
135 /*
136  * Gets field information from a java.nio.Buffer instance.
137  *
138  * Reads the |position|, |limit|, and |elementSizeShift| fields from the buffer instance.
139  *
140  * Returns the |address| field of the java.nio.Buffer instance which is only valid (non-zero) when
141  * the buffer is backed by a direct buffer.
142  */
143 jlong jniGetNioBufferFields(C_JNIEnv* env,
144                             jobject nioBuffer,
145                             /*out*/jint* position,
146                             /*out*/jint* limit,
147                             /*out*/jint* elementSizeShift);
148 
149 /*
150  * Gets the current position from a java.nio.Buffer as a pointer to memory in a fixed buffer.
151  *
152  * Returns 0 if |nioBuffer| is not backed by a direct buffer.
153  *
154  * This method reads the |address|, |position|, and |elementSizeShift| fields from the
155  * java.nio.Buffer instance to calculate the pointer address for the current position.
156  */
157 jlong jniGetNioBufferPointer(C_JNIEnv* env, jobject nioBuffer);
158 
159 /*
160  * Returns the reference from a java.lang.ref.Reference.
161  */
162 jobject jniGetReferent(C_JNIEnv* env, jobject ref);
163 
164 /*
165  * Returns a Java String object created from UTF-16 data either from jchar or,
166  * if called from C++11, char16_t (a bitwise identical distinct type).
167  */
168 jstring jniCreateString(C_JNIEnv* env, const jchar* unicodeChars, jsize len);
169 
170 /*
171  * Log a message and an exception.
172  * If exception is NULL, logs the current exception in the JNI environment.
173  */
174 void jniLogException(C_JNIEnv* env, int priority, const char* tag, jthrowable exception);
175 
176 /*
177  * Clear the cache of constants libnativehelper is using.
178  */
179 void jniUninitializeConstants();
180 
181 /* ---------------------------------- C API for JniInvocation.h --------------------------------- */
182 
183 /*
184  * The JNI invocation API exists to allow a choice of library responsible for managing virtual
185  * machines.
186  */
187 
188 /*
189  * Opaque structure used to hold JNI invocation internal state.
190  */
191 struct JniInvocationImpl;
192 
193 /*
194  * Creates an instance of a JniInvocationImpl.
195  */
196 struct JniInvocationImpl* JniInvocationCreate();
197 
198 /*
199  * Associates a library with a JniInvocationImpl instance. The library should export C symbols for
200  * JNI_GetDefaultJavaVMInitArgs, JNI_CreateJavaVM and JNI_GetDefaultJavaVMInitArgs.
201  *
202  * The specified |library| should be the filename of a shared library. The |library| is opened with
203  * dlopen(3).
204  *
205  * If there is an error opening the specified |library|, then function will fallback to the
206  * default library "libart.so". If the fallback library is successfully used then a warning is
207  * written to the Android log buffer. Use of the fallback library is not considered an error.
208  *
209  * If the fallback library cannot be opened or the expected symbols are not found in the library
210  * opened, then an error message is written to the Android log buffer and the function returns 0.
211  *
212  * Returns 1 on success, 0 otherwise.
213  */
214 int JniInvocationInit(struct JniInvocationImpl* instance, const char* library);
215 
216 /*
217  * Release resources associated with JniInvocationImpl instance.
218  */
219 void JniInvocationDestroy(struct JniInvocationImpl* instance);
220 
221 /*
222  * Gets the default library for JNI invocation. The default library is "libart.so". This value may
223  * be overridden for debuggable builds using the persist.sys.dalvik.vm.lib.2 system property.
224  *
225  * The |library| argument is the preferred library to use on debuggable builds (when
226  * ro.debuggable=1). If the |library| argument is nullptr, then the system preferred value will be
227  * queried from persist.sys.dalvik.vm.lib.2 if the caller has provided |buffer| argument.
228  *
229  * The |buffer| argument is used for reading system properties in debuggable builds. It is
230  * optional, but should be provisioned to be PROP_VALUE_MAX bytes if provided to ensure it is
231  * large enough to hold a system property.
232  *
233  * Returns the filename of the invocation library determined from the inputs and system
234  * properties. The returned value may be |library|, |buffer|, or a pointer to a string constant
235  * "libart.so".
236  */
237 const char* JniInvocationGetLibrary(const char* library, char* buffer);
238 
239 /* ---------------------------------- C API for toStringArray.h --------------------------------- */
240 
241 /*
242  * Allocates a new array for java/lang/String instances with space for |count| elements. Elements
243  * are initially null.
244  *
245  * Returns a new array on success or nullptr in case of failure. This method raises an
246  * OutOfMemoryError exception if allocation fails.
247  */
248 jobjectArray newStringArray(JNIEnv* env, size_t count);
249 
250 /*
251  * Converts an array of C strings into a managed array of Java strings. The size of the C array is
252  * determined by the presence of a final element containing a nullptr.
253  *
254  * Returns a new array on success or nullptr in case of failure. This method raises an
255  * OutOfMemoryError exception if allocation fails.
256  */
257 jobjectArray toStringArray(JNIEnv* env, const char* const* strings);
258 
259 #ifdef __cplusplus
260 }
261 #endif  // __cplusplus
262 
263 #endif  // LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_LIBNATIVEHELPER_API_H_
264