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


##########
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 executable deliberately targets the scalar allocations for carrier 
state and diagnostic formatting, rather than simulating failure of every 
allocation overload. Both regressions verify that the armed hook is consumed at 
the intended stage.
   
   Leaving the other overloads at their defaults does not create the stated 
mismatch: [default array allocation/deallocation forwards to the corresponding 
scalar operators](https://eel.is/c++draft/new.delete.array), while aligned 
allocation and deallocation remain paired default implementations. No 
aligned-allocation behavior is being tested here. Adding replacements for all 
overloads would broaden the fault injector without addressing a demonstrated 
defect in these cases.



##########
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:
   The complete type is already available through `JniError.h` -> `JniCommon.h` 
-> `JavaException.h`, before this macro is defined. No caller-supplied include 
order is required. This repeats the [previously checked missing-include 
finding](https://github.com/apache/gluten/pull/13560#discussion_r4537292045); 
the include chain remains unchanged.



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