server : escape the special tokens without std::regex (#30243)
The d1-omni and kev prompts escape "<|name|>" in the request text with std::regex_replace. libstdc++ recurses for each char of the name, so a state of "<|" and 30000 letters overflows the stack of the HTTP thread and llama-server dies with SIGSEGV. server_decision_escape_special_tokens() writes the same text with a loop. test-server-component compares it with the regex on every string of up to 6 chars of "<|>a-".
This commit is contained in:
1 parent
ff026f8c0a
commit
04f7226a2c
3 files changed
+100
-6
No files matched your search
@@ -5,7 +5,6 @@
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <cmath>
|
||||
#include <regex>
|
||||
#include <stdexcept>
|
||||
|
||||
static const char * decision_question_type_name(server_decision_question_type type) {
|
||||
@@ -449,17 +448,43 @@ static std::string decision_kev_render(const json & val, int indent = 0) {
|
||||
return val.dump();
|
||||
}
|
||||
|
||||
// same result as std::regex_replace with <\|([A-Za-z0-9_]+)\|>
|
||||
// std::regex is not used: libstdc++ recurses for each char, a long input overflows the stack
|
||||
std::string server_decision_escape_special_tokens(const std::string & text) {
|
||||
const auto is_name = [](char c) {
|
||||
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_';
|
||||
};
|
||||
std::string out;
|
||||
size_t n_done = 0; // the chars of text that are in out
|
||||
for (size_t i = text.find("<|"); i != std::string::npos; i = text.find("<|", std::max(i + 1, n_done))) {
|
||||
size_t j = i + 2;
|
||||
while (j < text.size() && is_name(text[j])) {
|
||||
j++;
|
||||
}
|
||||
if (j > i + 2 && text.compare(j, 2, "|>") == 0) {
|
||||
out.append(text, n_done, i - n_done);
|
||||
out += "<\xC2\xA6";
|
||||
out.append(text, i + 2, j - i - 2);
|
||||
out += "\xC2\xA6>";
|
||||
n_done = j + 2;
|
||||
}
|
||||
}
|
||||
if (n_done == 0) {
|
||||
return text;
|
||||
}
|
||||
out.append(text, n_done, std::string::npos);
|
||||
return out;
|
||||
}
|
||||
|
||||
// kev text input: special tokens written in the text must not be parsed as such
|
||||
static std::string decision_kev_text(const json & val) {
|
||||
static const std::regex re_special("<\\|([A-Za-z0-9_]+)\\|>");
|
||||
return std::regex_replace(decision_kev_render(val), re_special, "<\xC2\xA6$1\xC2\xA6>");
|
||||
return server_decision_escape_special_tokens(decision_kev_render(val));
|
||||
}
|
||||
|
||||
// lfm2-d1-omni: special tokens written in the input must not be parsed as such, in keys too (d1-omni prompt.py: escape)
|
||||
static json decision_d1omni_escape(const json & val) {
|
||||
static const std::regex re_special("<\\|([A-Za-z0-9_]+)\\|>");
|
||||
if (val.is_string()) {
|
||||
return std::regex_replace(val.get<std::string>(), re_special, "<\xC2\xA6$1\xC2\xA6>");
|
||||
return server_decision_escape_special_tokens(val.get<std::string>());
|
||||
}
|
||||
if (val.is_array()) {
|
||||
json out = json::array();
|
||||
@@ -471,7 +496,7 @@ static json decision_d1omni_escape(const json & val) {
|
||||
if (val.is_object()) {
|
||||
json out = json::object();
|
||||
for (const auto & [key, item] : val.items()) {
|
||||
out[std::regex_replace(key, re_special, "<\xC2\xA6$1\xC2\xA6>")] = decision_d1omni_escape(item);
|
||||
out[server_decision_escape_special_tokens(key)] = decision_d1omni_escape(item);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user