felipepessoto commented on code in PR #13560:
URL: https://github.com/apache/gluten/pull/13560#discussion_r4541786540
##########
cpp/core/jni/JniError.h:
##########
@@ -28,11 +28,17 @@
#endif
#ifndef JNI_METHOD_END
-#define JNI_METHOD_END(fallback_expr)
\
- }
\
- catch (std::exception & e) {
\
- env->ThrowNew(gluten::getJniErrorState()->glutenExceptionClass(),
e.what()); \
- return fallback_expr;
\
+#define JNI_METHOD_END(fallback_expr)
\
+ }
\
+ catch (const gluten::JavaException& e) {
\
+ e.throwToJava(env);
\
+ return fallback_expr;
\
+ }
\
+ catch (std::exception & e) {
\
+ if (!env->ExceptionCheck() &&
!gluten::JavaException::throwNestedToJava(env, e)) { \
+ env->ThrowNew(gluten::getJniErrorState()->glutenExceptionClass(),
e.what()); \
+ }
\
+ return fallback_expr;
\
}
Review Comment:
The claimed catch-matching failure does not occur. For a throw by value, C++
removes top-level cv-qualifiers from the exception object's type; `catch
(std::exception&)` matches both a thrown const `std::exception` and a const
derived exception. I compiled and ran both cases under C++20 to confirm.
Using a const reference remains a reasonable style improvement, already
covered by the [earlier catch-handler
thread](https://github.com/apache/gluten/pull/13560#discussion_r4537020982).
Resolving this duplicate correctness claim, not marking that separate
suggestion as implemented.
##########
cpp/core/jni/JavaException.cc:
##########
@@ -0,0 +1,178 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "JavaException.h"
+
+#include <glog/logging.h>
+
+#include <stdexcept>
+#include <string>
+
+namespace gluten {
+namespace {
+
+class LocalFrame {
+ public:
+ explicit LocalFrame(JNIEnv* env) : env_(env), pushed_(env->PushLocalFrame(3)
== JNI_OK) {}
+
+ ~LocalFrame() {
+ if (pushed_) {
+ env_->PopLocalFrame(nullptr);
+ }
+ }
+
+ bool pushed() const {
+ return pushed_;
+ }
+
+ private:
+ JNIEnv* env_;
+ bool pushed_;
+};
+
+std::string describe(JNIEnv* env, jthrowable throwable) {
+ const std::string prefix = "Error during calling Java code from native code:
";
+ const auto unavailable = [&](const char* reason) {
+ // Describing an exception must not replace the throwable we are
transporting.
+ env->ExceptionClear();
+ return prefix + "Java stack trace unavailable (" + reason + ")";
+ };
+ LocalFrame frame(env);
+ if (!frame.pushed()) {
+ return unavailable("PushLocalFrame failed");
+ }
+ auto describer =
env->FindClass("org/apache/gluten/exception/JniExceptionDescriber");
+ if (describer == nullptr) {
+ return unavailable("JniExceptionDescriber lookup failed");
+ }
+ auto method = env->GetStaticMethodID(describer, "describe",
"(Ljava/lang/Throwable;)Ljava/lang/String;");
+ if (method == nullptr) {
+ return unavailable("describe method lookup failed");
+ }
+ auto description =
static_cast<jstring>(env->CallStaticObjectMethod(describer, method, throwable));
+ if (env->ExceptionCheck() || description == nullptr) {
+ return unavailable("describe method failed");
+ }
+ const auto release = [&](const char* chars) {
env->ReleaseStringUTFChars(description, chars); };
+ std::unique_ptr<const char, decltype(release)>
chars(env->GetStringUTFChars(description, nullptr), release);
+ if (!chars) {
+ return unavailable("GetStringUTFChars failed");
+ }
+ return prefix + chars.get();
+}
+
+} // namespace
+
+struct JavaException::State {
+ State(JNIEnv* env, jthrowable throwable) {
+ if (throwable == nullptr) {
+ throw std::invalid_argument("JavaException requires a non-null
throwable");
+ }
+ if (env->GetJavaVM(&vm) != JNI_OK) {
+ throw std::runtime_error("Unable to get JavaVM for Java exception");
+ }
+ ref = static_cast<jthrowable>(env->NewGlobalRef(throwable));
+ if (ref == nullptr) {
+ throw std::runtime_error("Unable to create global reference for Java
exception");
+ }
+ }
+
+ ~State() {
+ JNIEnv* env = nullptr;
+ auto status = vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_8);
+ const bool detached = status == JNI_EDETACHED;
+ if (detached) {
+ status = vm->AttachCurrentThreadAsDaemon(reinterpret_cast<void**>(&env),
nullptr);
+ }
Review Comment:
This matches Gluten's existing supported runtime baseline: `JniCommon.h`
already sets `jniVersion` to `JNI_VERSION_1_8`, `JNI_OnLoad` requests and
returns that version, and the minimum Java version is 8. The destructor's
`GetEnv` call therefore does not introduce a new compatibility restriction.
Lowering this call alone would not make older VMs supported.
##########
gluten-arrow/pom.xml:
##########
@@ -278,4 +278,47 @@
<testOutputDirectory>target/scala-${scala.binary.version}/test-classes</testOutputDirectory>
</build>
+ <profiles>
+ <profile>
+ <id>exclude-jni-test</id>
+ <activation>
+ <activeByDefault>true</activeByDefault>
+ </activation>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <excludes combine.children="append">
+ <!-- The jni-test profile requires the separately built native
test bridge. -->
+ <exclude>**/JavaExceptionJniTest.java</exclude>
+ </excludes>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ </profile>
+ <profile>
+ <id>jni-test</id>
Review Comment:
Maven deactivates an `activeByDefault` profile when another profile in the
**same POM** is activated, not when a profile in the parent POM is activated
([Maven
documentation](https://maven.apache.org/guides/introduction/introduction-to-profiles.html#explicit-profile-activation)).
The Spark/backend/Java profiles are in the parent; `gluten-arrow` has only
`exclude-jni-test` and `jni-test`.
Verified the current POM with all CI parent profiles enabled: without
`jni-test`, the effective POM retains the JNI exclusion and Maven succeeds
without a bridge; with `jni-test`, all four JNI cases execute. The reported
accidental activation does not occur.
##########
cpp/core/jni/JniError.h:
##########
@@ -28,11 +28,17 @@
#endif
#ifndef JNI_METHOD_END
-#define JNI_METHOD_END(fallback_expr)
\
- }
\
- catch (std::exception & e) {
\
- env->ThrowNew(gluten::getJniErrorState()->glutenExceptionClass(),
e.what()); \
- return fallback_expr;
\
+#define JNI_METHOD_END(fallback_expr)
\
+ }
\
+ catch (const gluten::JavaException& e) {
\
+ e.throwToJava(env);
\
+ return fallback_expr;
\
+ }
\
+ catch (std::exception & e) {
\
+ if (!env->ExceptionCheck() &&
!gluten::JavaException::throwNestedToJava(env, e)) { \
+ env->ThrowNew(gluten::getJniErrorState()->glutenExceptionClass(),
e.what()); \
+ }
\
+ return fallback_expr;
\
}
Review Comment:
The complete declaration is already available: `JniError.h` includes
`JniCommon.h`, which directly includes `JavaException.h`, before
`JNI_METHOD_END` is defined. A translation unit including only `JniError.h`
therefore does not depend on an external include order. This is not a
missing-declaration compilation issue.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]