Merge changes from topic "jni-errno-exception" am: 4a6c12e650

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1619421

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: If704b8e72e74f0e1afcfc59d5ad03c3003e2643a
This commit is contained in:
Sorin Basca
2021-03-15 11:34:48 +00:00
committed by Automerger Merge Worker
3 changed files with 11 additions and 65 deletions

View File

@@ -52,27 +52,6 @@ constexpr int MAXPACKETSIZE = 8 * 1024;
// FrameworkListener limits the size of commands to 4096 bytes. // FrameworkListener limits the size of commands to 4096 bytes.
constexpr int MAXCMDSIZE = 4096; constexpr int MAXCMDSIZE = 4096;
static void throwErrnoException(JNIEnv* env, const char* functionName, int error) {
ScopedLocalRef<jstring> detailMessage(env, env->NewStringUTF(functionName));
if (detailMessage.get() == NULL) {
// Not really much we can do here. We're probably dead in the water,
// but let's try to stumble on...
env->ExceptionClear();
}
static jclass errnoExceptionClass =
MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/system/ErrnoException"));
static jmethodID errnoExceptionCtor =
GetMethodIDOrDie(env, errnoExceptionClass,
"<init>", "(Ljava/lang/String;I)V");
jobject exception = env->NewObject(errnoExceptionClass,
errnoExceptionCtor,
detailMessage.get(),
error);
env->Throw(reinterpret_cast<jthrowable>(exception));
}
static void android_net_utils_attachDropAllBPFFilter(JNIEnv *env, jobject clazz, jobject javaFd) static void android_net_utils_attachDropAllBPFFilter(JNIEnv *env, jobject clazz, jobject javaFd)
{ {
struct sock_filter filter_code[] = { struct sock_filter filter_code[] = {
@@ -150,7 +129,7 @@ static jobject android_net_utils_resNetworkQuery(JNIEnv *env, jobject thiz, jint
int fd = resNetworkQuery(netId, queryname.data(), ns_class, ns_type, flags); int fd = resNetworkQuery(netId, queryname.data(), ns_class, ns_type, flags);
if (fd < 0) { if (fd < 0) {
throwErrnoException(env, "resNetworkQuery", -fd); jniThrowErrnoException(env, "resNetworkQuery", -fd);
return nullptr; return nullptr;
} }
@@ -165,7 +144,7 @@ static jobject android_net_utils_resNetworkSend(JNIEnv *env, jobject thiz, jint
int fd = resNetworkSend(netId, data, msgLen, flags); int fd = resNetworkSend(netId, data, msgLen, flags);
if (fd < 0) { if (fd < 0) {
throwErrnoException(env, "resNetworkSend", -fd); jniThrowErrnoException(env, "resNetworkSend", -fd);
return nullptr; return nullptr;
} }
@@ -180,13 +159,13 @@ static jobject android_net_utils_resNetworkResult(JNIEnv *env, jobject thiz, job
int res = resNetworkResult(fd, &rcode, buf.data(), MAXPACKETSIZE); int res = resNetworkResult(fd, &rcode, buf.data(), MAXPACKETSIZE);
jniSetFileDescriptorOfFD(env, javaFd, -1); jniSetFileDescriptorOfFD(env, javaFd, -1);
if (res < 0) { if (res < 0) {
throwErrnoException(env, "resNetworkResult", -res); jniThrowErrnoException(env, "resNetworkResult", -res);
return nullptr; return nullptr;
} }
jbyteArray answer = env->NewByteArray(res); jbyteArray answer = env->NewByteArray(res);
if (answer == nullptr) { if (answer == nullptr) {
throwErrnoException(env, "resNetworkResult", ENOMEM); jniThrowErrnoException(env, "resNetworkResult", ENOMEM);
return nullptr; return nullptr;
} else { } else {
env->SetByteArrayRegion(answer, 0, res, env->SetByteArrayRegion(answer, 0, res,
@@ -208,7 +187,7 @@ static void android_net_utils_resNetworkCancel(JNIEnv *env, jobject thiz, jobjec
static jobject android_net_utils_getDnsNetwork(JNIEnv *env, jobject thiz) { static jobject android_net_utils_getDnsNetwork(JNIEnv *env, jobject thiz) {
unsigned dnsNetId = 0; unsigned dnsNetId = 0;
if (int res = getNetworkForDns(&dnsNetId) < 0) { if (int res = getNetworkForDns(&dnsNetId) < 0) {
throwErrnoException(env, "getDnsNetId", -res); jniThrowErrnoException(env, "getDnsNetId", -res);
return nullptr; return nullptr;
} }
bool privateDnsBypass = dnsNetId & NETID_USE_LOCAL_NAMESERVERS; bool privateDnsBypass = dnsNetId & NETID_USE_LOCAL_NAMESERVERS;
@@ -233,8 +212,8 @@ static jobject android_net_utils_getTcpRepairWindow(JNIEnv *env, jobject thiz, j
// Obtain the parameters of the TCP repair window. // Obtain the parameters of the TCP repair window.
int rc = getsockopt(fd, IPPROTO_TCP, TCP_REPAIR_WINDOW, &trw, &size); int rc = getsockopt(fd, IPPROTO_TCP, TCP_REPAIR_WINDOW, &trw, &size);
if (rc == -1) { if (rc == -1) {
throwErrnoException(env, "getsockopt : TCP_REPAIR_WINDOW", errno); jniThrowErrnoException(env, "getsockopt : TCP_REPAIR_WINDOW", errno);
return NULL; return NULL;
} }
struct tcp_info tcpinfo = {}; struct tcp_info tcpinfo = {};
@@ -244,8 +223,8 @@ static jobject android_net_utils_getTcpRepairWindow(JNIEnv *env, jobject thiz, j
// should be applied to the window size. // should be applied to the window size.
rc = getsockopt(fd, IPPROTO_TCP, TCP_INFO, &tcpinfo, &tcpinfo_size); rc = getsockopt(fd, IPPROTO_TCP, TCP_INFO, &tcpinfo, &tcpinfo_size);
if (rc == -1) { if (rc == -1) {
throwErrnoException(env, "getsockopt : TCP_INFO", errno); jniThrowErrnoException(env, "getsockopt : TCP_INFO", errno);
return NULL; return NULL;
} }
jclass class_TcpRepairWindow = env->FindClass("android/net/TcpRepairWindow"); jclass class_TcpRepairWindow = env->FindClass("android/net/TcpRepairWindow");

View File

@@ -35,21 +35,6 @@ namespace {
jclass errnoExceptionClass; jclass errnoExceptionClass;
jmethodID errnoExceptionCtor; // MethodID for ErrnoException.<init>(String,I) jmethodID errnoExceptionCtor; // MethodID for ErrnoException.<init>(String,I)
void throwErrnoException(JNIEnv* env, const char* functionName, int error) {
ScopedLocalRef<jstring> detailMessage(env, env->NewStringUTF(functionName));
if (detailMessage.get() == NULL) {
// Not really much we can do here. We're probably dead in the water,
// but let's try to stumble on...
env->ExceptionClear();
}
jobject exception = env->NewObject(errnoExceptionClass,
errnoExceptionCtor,
detailMessage.get(),
error);
env->Throw(reinterpret_cast<jthrowable>(exception));
}
jobject SharedMemory_nCreate(JNIEnv* env, jobject, jstring jname, jint size) { jobject SharedMemory_nCreate(JNIEnv* env, jobject, jstring jname, jint size) {
// Name is optional so we can't use ScopedUtfChars for this as it throws NPE on null // Name is optional so we can't use ScopedUtfChars for this as it throws NPE on null
@@ -65,7 +50,7 @@ jobject SharedMemory_nCreate(JNIEnv* env, jobject, jstring jname, jint size) {
} }
if (fd < 0) { if (fd < 0) {
throwErrnoException(env, "SharedMemory_create", err); jniThrowErrnoException(env, "SharedMemory_create", err);
return nullptr; return nullptr;
} }

View File

@@ -1290,24 +1290,6 @@ void android_os_Process_removeAllProcessGroups(JNIEnv* env, jobject clazz)
return removeAllProcessGroups(); return removeAllProcessGroups();
} }
static void throwErrnoException(JNIEnv* env, const char* functionName, int error) {
ScopedLocalRef<jstring> detailMessage(env, env->NewStringUTF(functionName));
if (detailMessage.get() == NULL) {
// Not really much we can do here. We're probably dead in the water,
// but let's try to stumble on...
env->ExceptionClear();
}
static jclass errnoExceptionClass =
MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/system/ErrnoException"));
static jmethodID errnoExceptionCtor =
GetMethodIDOrDie(env, errnoExceptionClass, "<init>", "(Ljava/lang/String;I)V");
jobject exception =
env->NewObject(errnoExceptionClass, errnoExceptionCtor, detailMessage.get(), error);
env->Throw(reinterpret_cast<jthrowable>(exception));
}
// Wrapper function to the syscall pidfd_open, which creates a file // Wrapper function to the syscall pidfd_open, which creates a file
// descriptor that refers to the process whose PID is specified in pid. // descriptor that refers to the process whose PID is specified in pid.
static inline int sys_pidfd_open(pid_t pid, unsigned int flags) { static inline int sys_pidfd_open(pid_t pid, unsigned int flags) {
@@ -1317,7 +1299,7 @@ static inline int sys_pidfd_open(pid_t pid, unsigned int flags) {
static jint android_os_Process_nativePidFdOpen(JNIEnv* env, jobject, jint pid, jint flags) { static jint android_os_Process_nativePidFdOpen(JNIEnv* env, jobject, jint pid, jint flags) {
int fd = sys_pidfd_open(pid, flags); int fd = sys_pidfd_open(pid, flags);
if (fd < 0) { if (fd < 0) {
throwErrnoException(env, "nativePidFdOpen", errno); jniThrowErrnoException(env, "nativePidFdOpen", errno);
return -1; return -1;
} }
return fd; return fd;