Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[enhancement](jni)append exception check for jni code. #42507

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions be/src/util/doris_metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,8 @@ void DorisMetrics::initialize(bool init_system_metrics, const std::set<std::stri
}
}

void DorisMetrics::init_jvm_metrics(JNIEnv* env) {
_jvm_metrics.reset(new JvmMetrics(&_metric_registry, env));
void DorisMetrics::init_jvm_metrics() {
_jvm_metrics.reset(new JvmMetrics(&_metric_registry));
}

void DorisMetrics::_update() {
Expand Down
2 changes: 1 addition & 1 deletion be/src/util/doris_metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ class DorisMetrics {
SystemMetrics* system_metrics() { return _system_metrics.get(); }
MetricEntity* server_entity() { return _server_metric_entity.get(); }
JvmMetrics* jvm_metrics() { return _jvm_metrics.get(); }
void init_jvm_metrics(JNIEnv* env);
void init_jvm_metrics();

private:
// Don't allow constructor
Expand Down
21 changes: 16 additions & 5 deletions be/src/util/jni-util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ Status JniUtil::GetJniExceptionMsg(JNIEnv* env, bool log_stack, const string& pr
}

jobject JniUtil::convert_to_java_map(JNIEnv* env, const std::map<std::string, std::string>& map) {
//TODO: ADD EXCEPTION CHECK.
jclass hashmap_class = env->FindClass("java/util/HashMap");
jmethodID hashmap_constructor = env->GetMethodID(hashmap_class, "<init>", "(I)V");
jobject hashmap_object = env->NewObject(hashmap_class, hashmap_constructor, map.size());
Expand Down Expand Up @@ -399,16 +400,26 @@ std::map<std::string, std::string> JniUtil::convert_to_cpp_map(JNIEnv* env, jobj

Status JniUtil::GetGlobalClassRef(JNIEnv* env, const char* class_str, jclass* class_ref) {
*class_ref = NULL;
jclass local_cl = env->FindClass(class_str);
RETURN_ERROR_IF_EXC(env);
JNI_CALL_METHOD_CHECK_EXCEPTION_DELETE_REF(jclass, local_cl, env, FindClass(class_str));
RETURN_IF_ERROR(LocalToGlobalRef(env, local_cl, reinterpret_cast<jobject*>(class_ref)));
env->DeleteLocalRef(local_cl);
RETURN_ERROR_IF_EXC(env);
return Status::OK();
}

Status JniUtil::LocalToGlobalRef(JNIEnv* env, jobject local_ref, jobject* global_ref) {
*global_ref = env->NewGlobalRef(local_ref);
// NewGlobalRef:
// Returns a global reference to the given obj.
//
//May return NULL if:
// obj refers to null
// the system has run out of memory
// obj was a weak global reference and has already been garbage collected
if (*global_ref == NULL) {
return Status::InternalError(
"LocalToGlobalRef fail,global ref is NULL,maybe the system has run out of memory.");
}

//NewGlobalRef not throw exception,maybe we just need check NULL.
RETURN_ERROR_IF_EXC(env);
return Status::OK();
}
Expand Down Expand Up @@ -592,7 +603,7 @@ Status JniUtil::Init() {
}
RETURN_IF_ERROR(init_jni_scanner_loader(env));
jvm_inited_ = true;
DorisMetrics::instance()->init_jvm_metrics(env);
DorisMetrics::instance()->init_jvm_metrics();
return Status::OK();
}

Expand Down
22 changes: 18 additions & 4 deletions be/src/util/jni-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include "common/status.h"
#include "jni_md.h"
#include "util/defer_op.h"
#include "util/thrift_util.h"

#ifdef USE_HADOOP_HDFS
Expand All @@ -38,12 +39,21 @@ extern "C" JNIEnv* getJNIEnv(void);
namespace doris {
class JniUtil;

#define RETURN_ERROR_IF_EXC(env) \
do { \
jthrowable exc = (env)->ExceptionOccurred(); \
if (exc != nullptr) return JniUtil::GetJniExceptionMsg(env); \
#define RETURN_ERROR_IF_EXC(env) \
do { \
if (env->ExceptionCheck()) [[unlikely]] \
return JniUtil::GetJniExceptionMsg(env); \
} while (false)

#define JNI_CALL_METHOD_CHECK_EXCEPTION_DELETE_REF(type, result, env, func) \
type result = env->func; \
DEFER(env->DeleteLocalRef(result)); \
RETURN_ERROR_IF_EXC(env)

#define JNI_CALL_METHOD_CHECK_EXCEPTION(type, result, env, func) \
type result = env->func; \
RETURN_ERROR_IF_EXC(env)

class JniUtil {
public:
static Status Init() WARN_UNUSED_RESULT;
Expand All @@ -65,6 +75,10 @@ class JniUtil {
return Status::OK();
}

//jclass is generally a local reference.
//Method ID and field ID values are forever.
//If you want to use the jclass across multiple threads or multiple calls into the JNI code you need
// to create a global reference to it with GetGlobalClassRef().
static Status GetGlobalClassRef(JNIEnv* env, const char* class_str,
jclass* class_ref) WARN_UNUSED_RESULT;

Expand Down
Loading
Loading