-
Notifications
You must be signed in to change notification settings - Fork 958
fix(ios): Add SAFE_SHERPA_ONNX_OR macro to prevent FFI invalid pointe… #2682
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1166,82 +1166,86 @@ struct SherpaOnnxOfflineTts { | |
| std::unique_ptr<sherpa_onnx::OfflineTts> impl; | ||
| }; | ||
|
|
||
| // iOS安全指针检查宏:防止FFI传递的无效指针导致崩溃 | ||
| // 检查指针是否为null或指向低地址区域(通常是无效指针) | ||
| #define SAFE_SHERPA_ONNX_OR(x, y) ((x && (uintptr_t)(x) >= 0x1000) ? x : y) | ||
|
Comment on lines
+1169
to
+1171
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major 🧩 Analysis chainAddress the comment language and consider logging invalid pointers. The macro implementation has several concerns:
Apply this diff to improve the implementation: -// iOS安全指针检查宏:防止FFI传递的无效指针导致崩溃
-// 检查指针是否为null或指向低地址区域(通常是无效指针)
+// Safety macro for iOS: Prevents crashes from invalid pointers passed via FFI
+// Checks if pointer is null or points to low-address region (typically invalid pointers)
+// Note: 0x1000 threshold is a heuristic and may not catch all invalid pointers
-#define SAFE_SHERPA_ONNX_OR(x, y) ((x && (uintptr_t)(x) >= 0x1000) ? x : y)
+#define SAFE_SHERPA_ONNX_OR(x, y) \
+ (((x) && (uintptr_t)(x) >= 0x1000) ? (x) : \
+ (SHERPA_ONNX_LOGE("Invalid pointer detected at %s:%d, using fallback", __FILE__, __LINE__), (y)))Additionally, consider whether this should be conditionally compiled for iOS only: +#ifdef __APPLE__
#define SAFE_SHERPA_ONNX_OR(x, y) ((x && (uintptr_t)(x) >= 0x1000) ? x : y)
+#else
+#define SAFE_SHERPA_ONNX_OR(x, y) SHERPA_ONNX_OR(x, y)
+#endif🏁 Script executed: #!/bin/bash
# Find macro definition and its comment
rg -n "SAFE_SHERPA_ONNX_OR" -C2 sherpa-onnx/c-api/c-api.cc
# Check other uses of the original macro
rg -n "SHERPA_ONNX_OR" -C2 sherpa-onnx/c-api/c-api.cc
# Inspect GetOfflineTtsConfig to see scope of SAFE_SHERPA_ONNX_OR usage
rg -n "GetOfflineTtsConfig" -C5 sherpa-onnx/c-api/c-api.ccLength of output: 28746 Improve SAFE_SHERPA_ONNX_OR macro robustness
|
||
|
|
||
| static sherpa_onnx::OfflineTtsConfig GetOfflineTtsConfig( | ||
| const SherpaOnnxOfflineTtsConfig *config) { | ||
| sherpa_onnx::OfflineTtsConfig tts_config; | ||
|
|
||
| // vits | ||
| tts_config.model.vits.model = SHERPA_ONNX_OR(config->model.vits.model, ""); | ||
| tts_config.model.vits.model = SAFE_SHERPA_ONNX_OR(config->model.vits.model, ""); | ||
| tts_config.model.vits.lexicon = | ||
| SHERPA_ONNX_OR(config->model.vits.lexicon, ""); | ||
| tts_config.model.vits.tokens = SHERPA_ONNX_OR(config->model.vits.tokens, ""); | ||
| SAFE_SHERPA_ONNX_OR(config->model.vits.lexicon, ""); | ||
| tts_config.model.vits.tokens = SAFE_SHERPA_ONNX_OR(config->model.vits.tokens, ""); | ||
| tts_config.model.vits.data_dir = | ||
| SHERPA_ONNX_OR(config->model.vits.data_dir, ""); | ||
| SAFE_SHERPA_ONNX_OR(config->model.vits.data_dir, ""); | ||
| tts_config.model.vits.noise_scale = | ||
| SHERPA_ONNX_OR(config->model.vits.noise_scale, 0.667); | ||
| tts_config.model.vits.noise_scale_w = | ||
| SHERPA_ONNX_OR(config->model.vits.noise_scale_w, 0.8); | ||
| tts_config.model.vits.length_scale = | ||
| SHERPA_ONNX_OR(config->model.vits.length_scale, 1.0); | ||
| tts_config.model.vits.dict_dir = | ||
| SHERPA_ONNX_OR(config->model.vits.dict_dir, ""); | ||
| SAFE_SHERPA_ONNX_OR(config->model.vits.dict_dir, ""); | ||
|
|
||
| // matcha | ||
| tts_config.model.matcha.acoustic_model = | ||
| SHERPA_ONNX_OR(config->model.matcha.acoustic_model, ""); | ||
| SAFE_SHERPA_ONNX_OR(config->model.matcha.acoustic_model, ""); | ||
| tts_config.model.matcha.vocoder = | ||
| SHERPA_ONNX_OR(config->model.matcha.vocoder, ""); | ||
| SAFE_SHERPA_ONNX_OR(config->model.matcha.vocoder, ""); | ||
| tts_config.model.matcha.lexicon = | ||
| SHERPA_ONNX_OR(config->model.matcha.lexicon, ""); | ||
| SAFE_SHERPA_ONNX_OR(config->model.matcha.lexicon, ""); | ||
| tts_config.model.matcha.tokens = | ||
| SHERPA_ONNX_OR(config->model.matcha.tokens, ""); | ||
| SAFE_SHERPA_ONNX_OR(config->model.matcha.tokens, ""); | ||
| tts_config.model.matcha.data_dir = | ||
| SHERPA_ONNX_OR(config->model.matcha.data_dir, ""); | ||
| SAFE_SHERPA_ONNX_OR(config->model.matcha.data_dir, ""); | ||
| tts_config.model.matcha.noise_scale = | ||
| SHERPA_ONNX_OR(config->model.matcha.noise_scale, 0.667); | ||
| tts_config.model.matcha.length_scale = | ||
| SHERPA_ONNX_OR(config->model.matcha.length_scale, 1.0); | ||
| tts_config.model.matcha.dict_dir = | ||
| SHERPA_ONNX_OR(config->model.matcha.dict_dir, ""); | ||
| SAFE_SHERPA_ONNX_OR(config->model.matcha.dict_dir, ""); | ||
|
|
||
| // kokoro | ||
| tts_config.model.kokoro.model = | ||
| SHERPA_ONNX_OR(config->model.kokoro.model, ""); | ||
| SAFE_SHERPA_ONNX_OR(config->model.kokoro.model, ""); | ||
| tts_config.model.kokoro.voices = | ||
| SHERPA_ONNX_OR(config->model.kokoro.voices, ""); | ||
| SAFE_SHERPA_ONNX_OR(config->model.kokoro.voices, ""); | ||
| tts_config.model.kokoro.tokens = | ||
| SHERPA_ONNX_OR(config->model.kokoro.tokens, ""); | ||
| SAFE_SHERPA_ONNX_OR(config->model.kokoro.tokens, ""); | ||
| tts_config.model.kokoro.data_dir = | ||
| SHERPA_ONNX_OR(config->model.kokoro.data_dir, ""); | ||
| SAFE_SHERPA_ONNX_OR(config->model.kokoro.data_dir, ""); | ||
| tts_config.model.kokoro.length_scale = | ||
| SHERPA_ONNX_OR(config->model.kokoro.length_scale, 1.0); | ||
| tts_config.model.kokoro.dict_dir = | ||
| SHERPA_ONNX_OR(config->model.kokoro.dict_dir, ""); | ||
| SAFE_SHERPA_ONNX_OR(config->model.kokoro.dict_dir, ""); | ||
| tts_config.model.kokoro.lexicon = | ||
| SHERPA_ONNX_OR(config->model.kokoro.lexicon, ""); | ||
| tts_config.model.kokoro.lang = SHERPA_ONNX_OR(config->model.kokoro.lang, ""); | ||
| SAFE_SHERPA_ONNX_OR(config->model.kokoro.lexicon, ""); | ||
| tts_config.model.kokoro.lang = SAFE_SHERPA_ONNX_OR(config->model.kokoro.lang, ""); | ||
|
|
||
| // kitten | ||
| tts_config.model.kitten.model = | ||
| SHERPA_ONNX_OR(config->model.kitten.model, ""); | ||
| SAFE_SHERPA_ONNX_OR(config->model.kitten.model, ""); | ||
| tts_config.model.kitten.voices = | ||
| SHERPA_ONNX_OR(config->model.kitten.voices, ""); | ||
| SAFE_SHERPA_ONNX_OR(config->model.kitten.voices, ""); | ||
| tts_config.model.kitten.tokens = | ||
| SHERPA_ONNX_OR(config->model.kitten.tokens, ""); | ||
| SAFE_SHERPA_ONNX_OR(config->model.kitten.tokens, ""); | ||
| tts_config.model.kitten.data_dir = | ||
| SHERPA_ONNX_OR(config->model.kitten.data_dir, ""); | ||
| SAFE_SHERPA_ONNX_OR(config->model.kitten.data_dir, ""); | ||
| tts_config.model.kitten.length_scale = | ||
| SHERPA_ONNX_OR(config->model.kitten.length_scale, 1.0); | ||
|
|
||
| tts_config.model.num_threads = SHERPA_ONNX_OR(config->model.num_threads, 1); | ||
| tts_config.model.debug = config->model.debug; | ||
| tts_config.model.provider = SHERPA_ONNX_OR(config->model.provider, "cpu"); | ||
| tts_config.model.provider = SAFE_SHERPA_ONNX_OR(config->model.provider, "cpu"); | ||
| if (tts_config.model.provider.empty()) { | ||
| tts_config.model.provider = "cpu"; | ||
| } | ||
|
|
||
| tts_config.rule_fsts = SHERPA_ONNX_OR(config->rule_fsts, ""); | ||
| tts_config.rule_fars = SHERPA_ONNX_OR(config->rule_fars, ""); | ||
| tts_config.rule_fsts = SAFE_SHERPA_ONNX_OR(config->rule_fsts, ""); | ||
| tts_config.rule_fars = SAFE_SHERPA_ONNX_OR(config->rule_fars, ""); | ||
|
Comment on lines
+1178
to
+1248
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major 🧩 Analysis chainVerify that other config functions don't have the same vulnerability. The Run the following script to identify other locations that may need similar protection: Based on the results, consider either:
🏁 Script executed: #!/bin/bash
# Description: Find all usages of SHERPA_ONNX_OR with string/pointer parameters in config functions
# Search for SHERPA_ONNX_OR calls with string-like parameters
rg -n 'SHERPA_ONNX_OR\(config->[^,]+\.(model|lexicon|tokens|data_dir|dict_dir|provider|encoder|decoder|vocoder|voices|lang|graph|rule_fsts|rule_fars|hotwords_file)[^)]*\)' --type cpp -C 2
echo "---"
echo "Summary: Review the above usages to determine if they should also use SAFE_SHERPA_ONNX_OR"Length of output: 23186 Wrap all string‐pointer config fields with SAFE_SHERPA_ONNX_OR 🤖 Prompt for AI Agents |
||
| tts_config.max_num_sentences = SHERPA_ONNX_OR(config->max_num_sentences, 1); | ||
| tts_config.silence_scale = SHERPA_ONNX_OR(config->silence_scale, 0.2); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
请问这个无效指针是哪里来的?
如果不为空,为什么还要额外判断?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.