forked from huawei/mindspore2022
fix jni bug
This commit is contained in:
parent
eb5d50d3e1
commit
1a06df878e
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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 <cstring>
|
||||
#include <cstdlib>
|
||||
#include <climits>
|
||||
#include <memory>
|
||||
#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<char[]>(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;
|
||||
}
|
||||
|
|
@ -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 <string>
|
||||
|
||||
std::string RealPath(const char *path);
|
||||
|
||||
#endif // MINDSPORE_LITE_JAVA_SRC_COMMON_JNI_UTILS_H
|
||||
|
|
@ -18,6 +18,7 @@
|
|||
#include <fstream>
|
||||
#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<jlong>(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<jlong>(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<jlong>(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<jlong>(nullptr);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue