From f17d2c721e6d44726bb97c29046fec14e9b0a2d0 Mon Sep 17 00:00:00 2001 From: "NAIWENXIE\\Naiwen" Date: Thu, 12 Dec 2024 23:55:39 +0000 Subject: [PATCH 1/2] Fix crash caused by ggml_backend_load_all when launching on AndroidActivity. Details: Calling ggml_backend_load_all during initialization in the AndroidActivity project leads to a crash with the error: terminating with uncaught exception of type std::__ndk1::__fs::filesystem::filesystem_error: filesystem error: in directory_iterator::directory_iterator(...): Permission denied [./]. This issue occurs because AndroidActivity restricts file access due to sandboxing. Reproduction: In the example folder, the LlamaAndroid project can reproduce the crash by calling ggml_backend_load_all first in Java_android_llama_cpp_LLamaAndroid_backend_1init. --- ggml/src/ggml-backend-reg.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ggml/src/ggml-backend-reg.cpp b/ggml/src/ggml-backend-reg.cpp index 2e7340145eed2..ebd90c9d81bd2 100644 --- a/ggml/src/ggml-backend-reg.cpp +++ b/ggml/src/ggml-backend-reg.cpp @@ -473,7 +473,8 @@ static ggml_backend_reg_t ggml_backend_load_best(const char * name, bool silent, if (!fs::exists(search_path)) { continue; } - for (const auto & entry : fs::directory_iterator(search_path)) { + for (const auto & entry : fs::directory_iterator(search_path, + std::filesystem::directory_options::skip_permission_denied)) { if (entry.is_regular_file()) { std::string filename = entry.path().filename().string(); std::string ext = entry.path().extension().string(); From 1f032a95773a85f13143f4915116d1bb5120a9c1 Mon Sep 17 00:00:00 2001 From: Diego Devesa Date: Fri, 13 Dec 2024 01:07:03 +0100 Subject: [PATCH 2/2] Update ggml/src/ggml-backend-reg.cpp --- ggml/src/ggml-backend-reg.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ggml/src/ggml-backend-reg.cpp b/ggml/src/ggml-backend-reg.cpp index ebd90c9d81bd2..b2eded903cf91 100644 --- a/ggml/src/ggml-backend-reg.cpp +++ b/ggml/src/ggml-backend-reg.cpp @@ -473,8 +473,8 @@ static ggml_backend_reg_t ggml_backend_load_best(const char * name, bool silent, if (!fs::exists(search_path)) { continue; } - for (const auto & entry : fs::directory_iterator(search_path, - std::filesystem::directory_options::skip_permission_denied)) { + fs::directory_iterator dir_it(search_path, fs::directory_options::skip_permission_denied); + for (const auto & entry : dir_it) { if (entry.is_regular_file()) { std::string filename = entry.path().filename().string(); std::string ext = entry.path().extension().string();