chat : reject JSON nested more than 128 levels before walking it (#30242)
* chat : reject JSON nested more than 128 levels before walking it The schema builder, the jinja engine and the decision code walk request JSON with recursion. A request nested deep enough overflows the stack of the HTTP thread and llama-server dies (SIGBUS on macOS, SIGSEGV on Linux): for a tool schema from a few hundred levels on macOS and about 10000 on Linux. Check the depth first and answer 400 past 128 levels: the chat inputs in common_chat_templates_apply (tools, tool-call arguments, chat_template_kwargs, json_schema), the json_schema of /completion, and the whole /v1/systemone body. A gemma4 tool result that is too deep stays text. * fix the root, not the call side --------- Co-authored-by: Xuan Son Nguyen <son@huggingface.co>
This commit is contained in:
1 parent
74bca39500
commit
b3fa69955e
5 files changed
+135
-15
No files matched your search
@@ -10,6 +10,8 @@
|
||||
#include "../tools/server/server-chat.h"
|
||||
#include "chat-auto-parser.h"
|
||||
#include "chat.h"
|
||||
#include "json-schema-to-grammar.h"
|
||||
#include "json-schema.h"
|
||||
#include "common.h"
|
||||
#include "ggml.h"
|
||||
#include "log.h"
|
||||
@@ -8106,6 +8108,58 @@ static void test_chat_session() {
|
||||
}
|
||||
}
|
||||
|
||||
static void test_chat_json_depth() {
|
||||
LOG_DBG("%s\n", __func__);
|
||||
const auto arrays = [](size_t n) {
|
||||
return std::string(n, '[') + std::string(n, ']');
|
||||
};
|
||||
// a schema of n nested array types
|
||||
const auto schema = [](size_t n) {
|
||||
std::string s;
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
s += R"({"type": "array", "items": )";
|
||||
}
|
||||
return s + R"({"type": "string"})" + std::string(n, '}');
|
||||
};
|
||||
const auto rejected = [](const std::function<void()> & fn) {
|
||||
try {
|
||||
fn();
|
||||
} catch (const std::invalid_argument & e) {
|
||||
return std::string(e.what()).find("nested more than 128 levels deep") != std::string::npos;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
// the schema builder stops at 128 levels, the root schema is one of them
|
||||
assert_equals(false, rejected([&] { common_chat_schema_from_json(json::parse(schema(127))); }));
|
||||
assert_equals(true, rejected([&] { common_chat_schema_from_json(json::parse(schema(128))); }));
|
||||
assert_equals(true, rejected([&] { json_schema_to_grammar(json::parse(schema(1000))); }));
|
||||
|
||||
// the jinja engine and the schema builder reject the inputs before they recurse too deep
|
||||
auto tmpls = read_templates("models/templates/Qwen3.5-4B.jinja");
|
||||
const size_t deep = 1000;
|
||||
|
||||
common_chat_templates_inputs tool_schema;
|
||||
tool_schema.messages = { message_user };
|
||||
tool_schema.tools = { { "f", "", R"({"type": "object", "properties": {"x": )" + schema(deep) + "}}" } };
|
||||
assert_equals(true, rejected([&] { common_chat_templates_apply(tmpls.get(), tool_schema); }));
|
||||
|
||||
common_chat_templates_inputs tool_args;
|
||||
tool_args.messages = { message_user, simple_assist_msg("", "", "special_function", R"({"arg1": )" + arrays(deep) + "}") };
|
||||
tool_args.tools = { special_function_tool };
|
||||
assert_equals(true, rejected([&] { common_chat_templates_apply(tmpls.get(), tool_args); }));
|
||||
|
||||
common_chat_templates_inputs kwargs;
|
||||
kwargs.messages = { message_user };
|
||||
kwargs.chat_template_kwargs["deep"] = arrays(deep);
|
||||
assert_equals(true, rejected([&] { common_chat_templates_apply(tmpls.get(), kwargs); }));
|
||||
|
||||
common_chat_templates_inputs json_schema;
|
||||
json_schema.messages = { message_user };
|
||||
json_schema.json_schema = schema(deep);
|
||||
assert_equals(true, rejected([&] { common_chat_templates_apply(tmpls.get(), json_schema); }));
|
||||
}
|
||||
|
||||
static void test_reasoning_budget_tokens_per_request() {
|
||||
LOG_DBG("%s\n", __func__);
|
||||
// Use Qwen3 template which has <think>...</think> reasoning markers.
|
||||
@@ -8355,6 +8409,7 @@ int main(int argc, char ** argv) {
|
||||
test_reasoning_effort_caps();
|
||||
test_prompt_parts();
|
||||
test_chat_session();
|
||||
test_chat_json_depth();
|
||||
test_reasoning_budget_tokens_per_request();
|
||||
test_reasoning_budget_message_per_request();
|
||||
test_template_output_peg_parsers(detailed_debug);
|
||||
|
||||
Reference in new issue
Block a user