Repository navigation
Expand file tree
/
Copy pathjni_helper.cpp
More file actions
177 lines (160 loc) · 5.64 KB
/
Copy pathjni_helper.cpp
File metadata and controls
177 lines (160 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <executorch/extension/android/jni/jni_helper.h>
namespace executorch::jni_helper {
void throwExecutorchException(uint32_t errorCode, const std::string& details) {
// Get the current JNI environment
auto env = facebook::jni::Environment::current();
if (!env) {
return;
}
// stable/global class ref — safe to cache
static const auto exceptionClass =
JExecutorchRuntimeException::javaClassStatic();
// Find the static factory method: makeExecutorchException(int, String)
static auto makeExceptionMethod =
exceptionClass
->getStaticMethod<facebook::jni::local_ref<facebook::jni::JThrowable>(
int, facebook::jni::alias_ref<facebook::jni::JString>)>(
"makeExecutorchException",
"(ILjava/lang/String;)Ljava/lang/RuntimeException;");
auto jDetails = facebook::jni::make_jstring(details);
// Call the factory method to create the exception object
auto exception = makeExceptionMethod(exceptionClass, errorCode, jDetails);
facebook::jni::throwNewJavaException(exception.get());
}
void setExecutorchPendingException(
JNIEnv* env,
uint32_t errorCode,
const std::string& details) {
if (!env) {
return;
}
if (env->ExceptionCheck()) {
// Preserve any preexisting pending exception; do not overwrite it here.
return;
}
jclass exceptionClass =
env->FindClass("org/pytorch/executorch/ExecutorchRuntimeException");
if (env->ExceptionCheck()) {
if (exceptionClass) {
env->DeleteLocalRef(exceptionClass);
}
// Preserve the original exception; do not clear or overwrite it.
return;
}
if (!exceptionClass) {
// FindClass failed. It should have set a pending exception; leave it as is.
return;
}
jmethodID factoryMethod = env->GetStaticMethodID(
exceptionClass,
"makeExecutorchException",
"(ILjava/lang/String;)Ljava/lang/RuntimeException;");
if (env->ExceptionCheck()) {
// Preserve the original exception; do not overwrite it.
env->DeleteLocalRef(exceptionClass);
return;
}
if (!factoryMethod) {
// Factory unavailable; try the (int, String) constructor directly so
// callers still receive a structured ExecutorchRuntimeException.
jmethodID ctor =
env->GetMethodID(exceptionClass, "<init>", "(ILjava/lang/String;)V");
if (!env->ExceptionCheck() && ctor) {
jstring jDetails = env->NewStringUTF(details.c_str());
if (!env->ExceptionCheck() && jDetails) {
jobject exObj = env->NewObject(
exceptionClass, ctor, static_cast<jint>(errorCode), jDetails);
if (!env->ExceptionCheck() && exObj) {
env->Throw(reinterpret_cast<jthrowable>(exObj));
env->DeleteLocalRef(exObj);
}
env->DeleteLocalRef(jDetails);
}
} else {
// Clear any NoSuchMethodError from GetMethodID before falling back.
if (env->ExceptionCheck()) {
env->ExceptionClear();
}
jclass runtimeExClass = env->FindClass("java/lang/RuntimeException");
if (!env->ExceptionCheck() && runtimeExClass) {
env->ThrowNew(runtimeExClass, details.c_str());
env->DeleteLocalRef(runtimeExClass);
}
}
env->DeleteLocalRef(exceptionClass);
return;
}
jstring jDetails = env->NewStringUTF(details.c_str());
if (env->ExceptionCheck()) {
// Preserve the original exception; do not overwrite it.
if (jDetails) {
env->DeleteLocalRef(jDetails);
}
env->DeleteLocalRef(exceptionClass);
return;
}
if (!jDetails) {
// NewStringUTF returned null without setting an exception; fall back to
// a standard RuntimeException since ExecutorchRuntimeException lacks a
// (String) ctor.
jclass runtimeExClass = env->FindClass("java/lang/RuntimeException");
if (!env->ExceptionCheck() && runtimeExClass) {
env->ThrowNew(runtimeExClass, details.c_str());
env->DeleteLocalRef(runtimeExClass);
}
env->DeleteLocalRef(exceptionClass);
return;
}
auto exception = static_cast<jthrowable>(env->CallStaticObjectMethod(
exceptionClass, factoryMethod, static_cast<jint>(errorCode), jDetails));
if (env->ExceptionCheck() || !exception) {
// If a Java exception was thrown, it is already pending; just clean up.
if (exception) {
env->DeleteLocalRef(exception);
}
env->DeleteLocalRef(jDetails);
env->DeleteLocalRef(exceptionClass);
return;
}
env->Throw(exception);
env->DeleteLocalRef(exception);
env->DeleteLocalRef(jDetails);
env->DeleteLocalRef(exceptionClass);
}
bool utf8_check_validity(const char* str, size_t length) {
for (size_t i = 0; i < length; ++i) {
uint8_t byte = static_cast<uint8_t>(str[i]);
if (byte >= 0x80) {
if (i + 1 >= length) {
return false;
}
uint8_t next_byte = static_cast<uint8_t>(str[i + 1]);
if ((byte & 0xE0) == 0xC0 && (next_byte & 0xC0) == 0x80) {
i += 1;
} else if (
(byte & 0xF0) == 0xE0 && (next_byte & 0xC0) == 0x80 &&
(i + 2 < length) &&
(static_cast<uint8_t>(str[i + 2]) & 0xC0) == 0x80) {
i += 2;
} else if (
(byte & 0xF8) == 0xF0 && (next_byte & 0xC0) == 0x80 &&
(i + 2 < length) &&
(static_cast<uint8_t>(str[i + 2]) & 0xC0) == 0x80 &&
(i + 3 < length) &&
(static_cast<uint8_t>(str[i + 3]) & 0xC0) == 0x80) {
i += 3;
} else {
return false;
}
}
}
return true;
}
} // namespace executorch::jni_helper