From 1a06df878e484c19bfddc4d806cb7374fded6b37 Mon Sep 17 00:00:00 2001 From: yeyunpeng2020 Date: Mon, 19 Jul 2021 11:54:48 +0800 Subject: [PATCH] fix jni bug --- mindspore/lite/java/native/CMakeLists.txt | 1 + .../lite/java/native/common/jni_utils.cpp | 49 +++++++++++++++++++ mindspore/lite/java/native/common/jni_utils.h | 23 +++++++++ mindspore/lite/java/native/runtime/model.cpp | 11 +++-- 4 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 mindspore/lite/java/native/common/jni_utils.cpp create mode 100644 mindspore/lite/java/native/common/jni_utils.h diff --git a/mindspore/lite/java/native/CMakeLists.txt b/mindspore/lite/java/native/CMakeLists.txt index f2b3990cafe..af1a706b24b 100644 --- a/mindspore/lite/java/native/CMakeLists.txt +++ b/mindspore/lite/java/native/CMakeLists.txt @@ -88,6 +88,7 @@ set(JNI_SRC ${CMAKE_CURRENT_SOURCE_DIR}/runtime/ms_config.cpp ${CMAKE_CURRENT_SOURCE_DIR}/runtime/ms_tensor.cpp ${CMAKE_CURRENT_SOURCE_DIR}/runtime/lite_session.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/common/jni_utils.cpp ) set(LITE_SO_NAME mindspore-lite) diff --git a/mindspore/lite/java/native/common/jni_utils.cpp b/mindspore/lite/java/native/common/jni_utils.cpp new file mode 100644 index 00000000000..f9c6ec7cb29 --- /dev/null +++ b/mindspore/lite/java/native/common/jni_utils.cpp @@ -0,0 +1,49 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * Licensed 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 "common/jni_utils.h" +#include +#include +#include +#include +#include "common/ms_log.h" + +std::string RealPath(const char *path) { + if (path == nullptr) { + MS_LOGE("path is nullptr"); + return ""; + } + if ((strlen(path)) >= PATH_MAX) { + MS_LOGE("path is too long"); + return ""; + } + auto resolved_path = std::make_unique(PATH_MAX); + if (resolved_path == nullptr) { + MS_LOGE("new resolved_path failed"); + return ""; + } +#ifdef _WIN32 + char *real_path = _fullpath(resolved_path.get(), path, 1024); +#else + char *real_path = realpath(path, resolved_path.get()); +#endif + if (real_path == nullptr || strlen(real_path) == 0) { + MS_LOGE("file path is not valid : %s", path); + return ""; + } + std::string res = resolved_path.get(); + return res; +} diff --git a/mindspore/lite/java/native/common/jni_utils.h b/mindspore/lite/java/native/common/jni_utils.h new file mode 100644 index 00000000000..493d0d69179 --- /dev/null +++ b/mindspore/lite/java/native/common/jni_utils.h @@ -0,0 +1,23 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * Licensed 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. + */ + +#ifndef MINDSPORE_LITE_JAVA_SRC_COMMON_JNI_UTILS_H +#define MINDSPORE_LITE_JAVA_SRC_COMMON_JNI_UTILS_H +#include + +std::string RealPath(const char *path); + +#endif // MINDSPORE_LITE_JAVA_SRC_COMMON_JNI_UTILS_H diff --git a/mindspore/lite/java/native/runtime/model.cpp b/mindspore/lite/java/native/runtime/model.cpp index df085c06ac1..5273a36b55f 100644 --- a/mindspore/lite/java/native/runtime/model.cpp +++ b/mindspore/lite/java/native/runtime/model.cpp @@ -18,6 +18,7 @@ #include #include "common/ms_log.h" #include "include/model.h" +#include "common/jni_utils.h" extern "C" JNIEXPORT jlong JNICALL Java_com_mindspore_lite_Model_loadModel(JNIEnv *env, jobject thiz, jobject buffer) { if (buffer == nullptr) { @@ -37,19 +38,19 @@ extern "C" JNIEXPORT jlong JNICALL Java_com_mindspore_lite_Model_loadModel(JNIEn extern "C" JNIEXPORT jlong JNICALL Java_com_mindspore_lite_Model_loadModelByPath(JNIEnv *env, jobject thiz, jstring model_path) { - auto model_path_char = env->GetStringUTFChars(model_path, JNI_FALSE); - if (nullptr == model_path_char) { + auto model_path_char = RealPath(env->GetStringUTFChars(model_path, JNI_FALSE)); + if (model_path_char.empty()) { MS_LOGE("model_path_char is nullptr"); return reinterpret_cast(nullptr); } std::ifstream ifs(model_path_char); if (!ifs.good()) { - MS_LOGE("file: %s is not exist", model_path_char); + MS_LOGE("file: %s is not exist", model_path_char.c_str()); return reinterpret_cast(nullptr); } if (!ifs.is_open()) { - MS_LOGE("file: %s open failed", model_path_char); + MS_LOGE("file: %s open failed", model_path_char.c_str()); return reinterpret_cast(nullptr); } @@ -57,7 +58,7 @@ extern "C" JNIEXPORT jlong JNICALL Java_com_mindspore_lite_Model_loadModelByPath auto size = ifs.tellg(); auto buf = new (std::nothrow) char[size]; if (buf == nullptr) { - MS_LOGE("malloc buf failed, file: %s", model_path_char); + MS_LOGE("malloc buf failed, file: %s", model_path_char.c_str()); ifs.close(); return reinterpret_cast(nullptr); }