felipepessoto commented on code in PR #13560:
URL: https://github.com/apache/gluten/pull/13560#discussion_r4541950663
##########
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:
Changed the handler to `const std::exception&` in 07417444. The existing
carrier/allocation tests and all four real-JVM cases pass against the rebuilt
code.
I'm deferring the `catch (...)` suggestion: converting previously unhandled
native exceptions into Java errors is a separate behavior change that needs
dedicated coverage. This patch keeps that behavior unchanged; leaving this
thread open for that remaining suggestion.
##########
cpp/core/tests/JavaExceptionAllocationTest.cc:
##########
@@ -0,0 +1,102 @@
+/*
+ * 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 <gtest/gtest.h>
+
+#include <cstdlib>
+#include <new>
+#include <utility>
+
+#include "jni/JavaException.h"
+#include "tests/utils/TestJniEnvironment.h"
+
+namespace {
+thread_local bool failNextAllocation = false;
+}
+
+// This executable isolates the replacement allocator from other core tests.
+void* operator new(std::size_t size) {
+ if (std::exchange(failNextAllocation, false)) {
+ throw std::bad_alloc();
+ }
+ if (auto* memory = std::malloc(size == 0 ? 1 : size)) {
+ return memory;
+ }
+ throw std::bad_alloc();
+}
+
+void operator delete(void* memory) noexcept {
+ std::free(memory);
+}
+
+void operator delete(void* memory, std::size_t) noexcept {
+ std::free(memory);
+}
+
+TEST(JavaExceptionAllocationTest, stateAllocationFailureRetainsOriginal) {
+ gluten::TestJniEnvironment jni;
+ jni.pending = &jni.original;
+ failNextAllocation = true;
+ try {
+ gluten::JavaException::check(jni.env());
+ failNextAllocation = false;
+ FAIL() << "Expected std::bad_alloc";
+ } catch (const std::bad_alloc&) {
+ EXPECT_FALSE(std::exchange(failNextAllocation, false));
+ } catch (...) {
+ failNextAllocation = false;
+ throw;
+ }
+ EXPECT_EQ(jni.pending, &jni.original);
+ EXPECT_EQ(jni.createdGlobalRefs, 0);
+ EXPECT_EQ(jni.deletedGlobalRefs, 0);
+ EXPECT_EQ(jni.deletedLocalRefs, 1);
+ EXPECT_EQ(jni.pushedFrames, 0);
+ EXPECT_EQ(jni.poppedFrames, 0);
+ EXPECT_EQ(jni.releasedStrings, 0);
+}
+
+TEST(JavaExceptionAllocationTest, diagnosticAllocationFailureRetainsOriginal) {
+ gluten::TestJniEnvironment jni;
+ auto functions = *jni.env()->functions;
+ functions.GetStringUTFChars = [](JNIEnv*, jstring, jboolean*) -> const char*
{
+ // The throwable is already captured; fail the native diagnostic string,
not NewGlobalRef.
+ failNextAllocation = true;
+ return "original Java exception";
+ };
+ jni.env()->functions = &functions;
Review Comment:
Added a file-local, nonallocating scope guard in 07417444. It restores the
original JNI function-table pointer before the stack-local replacement is
destroyed, both on normal exit and during exception unwinding. The
allocation-failure test and its injection point are unchanged; focused checks
confirmed restoration on both paths without allocating.
--
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]