Copilot commented on code in PR #13560:
URL: https://github.com/apache/gluten/pull/13560#discussion_r4542503228


##########
cpp/core/jni/JniError.h:
##########
@@ -28,11 +28,25 @@
 #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);                                                        
                  \
+    if (!env->ExceptionCheck() &&                                              
                  \
+        env->ThrowNew(gluten::getJniErrorState()->glutenExceptionClass(), 
e.what()) != JNI_OK) { \
+      LOG(ERROR) << "Unable to throw GlutenException at JNI boundary";         
                  \
+    }                                                                          
                  \
+    return fallback_expr;                                                      
                  \
+  }                                                                            
                  \

Review Comment:
   `e.throwToJava(env)` is invoked unconditionally. If a newer JNI exception is 
already pending when unwinding reaches this boundary, calling `Throw` can 
overwrite that pending exception. To match the documented behavior of 
preserving newer pending Java exceptions, guard the restoration call (e.g., 
only restore if `!env->ExceptionCheck()`), and otherwise leave the pending 
exception untouched.



##########
cpp/core/jni/JniError.h:
##########
@@ -28,11 +28,25 @@
 #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);                                                        
                  \
+    if (!env->ExceptionCheck() &&                                              
                  \
+        env->ThrowNew(gluten::getJniErrorState()->glutenExceptionClass(), 
e.what()) != JNI_OK) { \
+      LOG(ERROR) << "Unable to throw GlutenException at JNI boundary";         
                  \
+    }                                                                          
                  \
+    return fallback_expr;                                                      
                  \
+  }                                                                            
                  \
+  catch (const std::exception& e) {                                            
                  \
+    if (!env->ExceptionCheck()) {                                              
                  \
+      gluten::JavaException::throwNestedToJava(env, e);                        
                  \
+    }                                                                          
                  \
+    if (!env->ExceptionCheck() &&                                              
                  \
+        env->ThrowNew(gluten::getJniErrorState()->glutenExceptionClass(), 
e.what()) != JNI_OK) { \
+      LOG(ERROR) << "Unable to throw GlutenException at JNI boundary";         
                  \
+    }                                                                          
                  \
+    return fallback_expr;                                                      
                  \
   }

Review Comment:
   `JNI_METHOD_END` now references `gluten::JavaException`, but this header 
doesn’t show any include that guarantees the type is complete at the macro 
expansion site. Catching `const gluten::JavaException&` requires a complete 
type, so translation units that include `JniError.h` without including 
`jni/JavaException.h` first may fail to compile. Make `JniError.h` include 
`jni/JavaException.h` (preferred), rather than relying on include order.



##########
cpp/core/tests/JavaExceptionAllocationTest.cc:
##########
@@ -0,0 +1,112 @@
+/*
+ * 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;
+
+struct RestoreJniFunctions {
+  JNIEnv* env;
+  const JNINativeInterface_* original;
+
+  ~RestoreJniFunctions() {
+    env->functions = original;
+  }
+};
+} // namespace
+
+// 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);
+}

Review Comment:
   This test binary overrides global scalar `operator new/delete` but not 
`operator new[]/delete[]` (and not aligned/nothrow overloads). That can make 
allocation-failure injection incomplete (allocations using other overloads 
won’t be intercepted) and can create allocator-mismatch risk if any code in 
this binary uses `new[]`. Consider providing the corresponding `new[]/delete[]` 
(and, if needed by the toolchain, aligned overloads) in the same file so the 
failure injection is consistent within the executable.



-- 
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]

Reply via email to