javalib: Remove JNI boilerplate code

Our JNI_OnLoad functions only register native methods with the VM. The
VM can do this automatically if the functions follow its naming scheme,
and if C++ name mangling is disabled.

Test: atest MicrodroidTestApp
Change-Id: Ida323baefae226e8f643b6842edf6317eb572411
This commit is contained in:
David Brazdil 2023-01-09 13:11:33 +00:00
parent 45713c7586
commit 6e322ebd09
2 changed files with 12 additions and 67 deletions

View File

@ -27,7 +27,8 @@
#include "common.h"
JNIEXPORT jobject JNICALL android_system_virtualmachine_VirtualMachine_connectToVsockServer(
extern "C" JNIEXPORT jobject JNICALL
Java_android_system_virtualmachine_VirtualMachine_nativeConnectToVsockServer(
JNIEnv* env, [[maybe_unused]] jclass clazz, jobject vmBinder, jint port) {
using aidl::android::system::virtualizationservice::IVirtualMachine;
using ndk::ScopedFileDescriptor;
@ -59,32 +60,3 @@ JNIEXPORT jobject JNICALL android_system_virtualmachine_VirtualMachine_connectTo
auto client = ARpcSession_setupPreconnectedClient(session.get(), requestFunc, &args);
return AIBinder_toJavaBinder(env, client);
}
JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* /*reserved*/) {
JNIEnv* env;
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
ALOGE("%s: Failed to get the environment", __FUNCTION__);
return JNI_ERR;
}
jclass c = env->FindClass("android/system/virtualmachine/VirtualMachine");
if (c == nullptr) {
ALOGE("%s: Failed to find class android.system.virtualmachine.VirtualMachine",
__FUNCTION__);
return JNI_ERR;
}
// Register your class' native methods.
static const JNINativeMethod methods[] = {
{"nativeConnectToVsockServer", "(Landroid/os/IBinder;I)Landroid/os/IBinder;",
reinterpret_cast<void*>(
android_system_virtualmachine_VirtualMachine_connectToVsockServer)},
};
int rc = env->RegisterNatives(c, methods, sizeof(methods) / sizeof(JNINativeMethod));
if (rc != JNI_OK) {
ALOGE("%s: Failed to register natives", __FUNCTION__);
return rc;
}
return JNI_VERSION_1_6;
}

View File

@ -31,7 +31,8 @@ using namespace android::base;
static constexpr const char VIRTMGR_PATH[] = "/apex/com.android.virt/bin/virtmgr";
static constexpr size_t VIRTMGR_THREADS = 16;
JNIEXPORT jint JNICALL android_system_virtualmachine_VirtualizationService_spawn(
extern "C" JNIEXPORT jint JNICALL
Java_android_system_virtualmachine_VirtualizationService_nativeSpawn(
JNIEnv* env, [[maybe_unused]] jclass clazz) {
unique_fd serverFd, clientFd;
if (!Socketpair(SOCK_STREAM, &serverFd, &clientFd)) {
@ -74,8 +75,10 @@ JNIEXPORT jint JNICALL android_system_virtualmachine_VirtualizationService_spawn
return clientFd.release();
}
JNIEXPORT jobject JNICALL android_system_virtualmachine_VirtualizationService_connect(
JNIEnv* env, [[maybe_unused]] jobject obj, int clientFd) {
extern "C" JNIEXPORT jobject JNICALL
Java_android_system_virtualmachine_VirtualizationService_nativeConnect(JNIEnv* env,
[[maybe_unused]] jobject obj,
int clientFd) {
RpcSessionHandle session;
ARpcSession_setFileDescriptorTransportMode(session.get(),
ARpcSession_FileDescriptorTransportMode::Unix);
@ -86,8 +89,10 @@ JNIEXPORT jobject JNICALL android_system_virtualmachine_VirtualizationService_co
return AIBinder_toJavaBinder(env, client);
}
JNIEXPORT jboolean JNICALL android_system_virtualmachine_VirtualizationService_isOk(
JNIEnv* env, [[maybe_unused]] jobject obj, int clientFd) {
extern "C" JNIEXPORT jboolean JNICALL
Java_android_system_virtualmachine_VirtualizationService_nativeIsOk(JNIEnv* env,
[[maybe_unused]] jobject obj,
int clientFd) {
/* Setting events=0 only returns POLLERR, POLLHUP or POLLNVAL. */
struct pollfd pfds[] = {{.fd = clientFd, .events = 0}};
if (poll(pfds, /*nfds*/ 1, /*timeout*/ 0) < 0) {
@ -97,35 +102,3 @@ JNIEXPORT jboolean JNICALL android_system_virtualmachine_VirtualizationService_i
}
return pfds[0].revents == 0;
}
JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* /*reserved*/) {
JNIEnv* env;
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
ALOGE("%s: Failed to get the environment", __FUNCTION__);
return JNI_ERR;
}
jclass c = env->FindClass("android/system/virtualmachine/VirtualizationService");
if (c == nullptr) {
ALOGE("%s: Failed to find class android.system.virtualmachine.VirtualizationService",
__FUNCTION__);
return JNI_ERR;
}
// Register your class' native methods.
static const JNINativeMethod methods[] = {
{"nativeSpawn", "()I",
reinterpret_cast<void*>(android_system_virtualmachine_VirtualizationService_spawn)},
{"nativeConnect", "(I)Landroid/os/IBinder;",
reinterpret_cast<void*>(android_system_virtualmachine_VirtualizationService_connect)},
{"nativeIsOk", "(I)Z",
reinterpret_cast<void*>(android_system_virtualmachine_VirtualizationService_isOk)},
};
int rc = env->RegisterNatives(c, methods, sizeof(methods) / sizeof(JNINativeMethod));
if (rc != JNI_OK) {
ALOGE("%s: Failed to register natives", __FUNCTION__);
return rc;
}
return JNI_VERSION_1_6;
}