--- parser3/src/main/pa_request.C 2025/05/25 20:55:23 1.433 +++ parser3/src/main/pa_request.C 2026/09/28 02:09:17 1.451 @@ -1,7 +1,7 @@ /** @file Parser: request class main part. @see compile.C and execute.C. - Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com) + Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com) Authors: Konstantin Morshnev , Alexandr Petrosian */ @@ -35,7 +35,7 @@ #include "pa_vdate.h" #include "pa_varray.h" -volatile const char * IDENT_PA_REQUEST_C="$Id: pa_request.C,v 1.433 2025/05/25 20:55:23 moko Exp $" IDENT_PA_REQUEST_H IDENT_PA_REQUEST_CHARSETS_H IDENT_PA_REQUEST_INFO_H IDENT_PA_VCONSOLE_H; +volatile const char * IDENT_PA_REQUEST_C="$Id: pa_request.C,v 1.451 2026/09/28 02:09:17 moko Exp $" IDENT_PA_REQUEST_H IDENT_PA_REQUEST_CHARSETS_H IDENT_PA_REQUEST_INFO_H IDENT_PA_VCONSOLE_H; // consts @@ -44,10 +44,19 @@ volatile const char * IDENT_PA_REQUEST_C /// content type of response when no $MAIN:defaults.content-type defined const char* DEFAULT_CONTENT_TYPE="text/html"; -const uint LOOP_LIMIT=100000; -const uint ARRAY_LIMIT=1000000; -const uint EXECUTE_RECURSION_LIMIT=1000; -const uint HTTPD_TIMEOUT=4; +// One recursion level uses ~0.85KB of stack under linux/gcc, ~1KB under win32 and ~2KB under win64. +// MSVC win64 exception handling requires an extra ~10KB !!! for each throw, and execute() catches and +// rethrows on every level to provide a stack trace. +// So 1000 levels need ~1MB under linux (default 8MB stack is enough) and ~2+10=12MB under win64. +// Worse, win64 keeps the unwound frames until the catch block returns, so a handler running away +// in its turn starts on top of them and needs one more such budget. The stack is set to 40MB in +// the MSVC project settings to allow nested three runaways. +const int EXECUTE_RECURSION_LIMIT=1000; +const int EXECUTE_RECURSION_RESERVE=100; + +const int LOOP_LIMIT=100000; +const int ARRAY_LIMIT=1000000; +const int HTTPD_TIMEOUT=4; const size_t FILE_SIZE_LIMIT=512*1024*1024; // defines for globals @@ -57,10 +66,6 @@ const size_t FILE_SIZE_LIMIT=512*1024*10 #define USE_METHOD_NAME "use" #define AUTOUSE_METHOD_NAME "autouse" -#define EXCEPTION_TYPE_PART_NAME "type" -#define EXCEPTION_SOURCE_PART_NAME "source" -#define EXCEPTION_COMMENT_PART_NAME "comment" - #define ORIGIN_KEY "origin" // globals @@ -70,11 +75,6 @@ const String auto_method_name(AUTO_METHO static const String use_method_name(USE_METHOD_NAME); static const String autouse_method_name(AUTOUSE_METHOD_NAME); -const String::Body exception_type_part_name(EXCEPTION_TYPE_PART_NAME); -const String::Body exception_source_part_name(EXCEPTION_SOURCE_PART_NAME); -const String::Body exception_comment_part_name(EXCEPTION_COMMENT_PART_NAME); -const String::Body exception_handled_part_name(EXCEPTION_HANDLED_PART_NAME); - static const String origin_key(ORIGIN_KEY); int pa_loop_limit=LOOP_LIMIT; @@ -138,9 +138,18 @@ const String content_disposition_filenam VStateless_class& VClassMAIN_create(); // +void Request::recursion_limit_reached() { + if(execute_recursion_limit>pa_execute_recursion_limit) + throw Recursion_limit_exception(); + + execute_recursion_limit+=EXECUTE_RECURSION_RESERVE; + throw Exception(PARSER_RUNTIME, 0, "call canceled - endless recursion detected"); +} + Request::Request(SAPI_Info& asapi_info, Request_info& arequest_info, String::Language adefault_lang): // private anti_endless_execute_recursion(0), + execute_recursion_limit(pa_execute_recursion_limit), // public allow_class_replace(false), @@ -175,8 +184,8 @@ Request::Request(SAPI_Info& asapi_info, pa_register_thread_request(*this); // file_no=0 => unknown - file_list+="UNKNOWN"; - file_list+="-body of process-"; // pseudo_file_no__process + file_list+=String::Body("UNKNOWN"); + file_list+=String::Body("-body of process-"); // pseudo_file_no__process // maybe expire old caches cache_managers->maybe_expire(); @@ -250,12 +259,11 @@ VStateless_class& Request::get_class_ref } bool Request::add_class(const char* atype, VStateless_class *aclass){ - if(!allow_class_replace){ - if(!classes().put_dont_replace(atype, aclass)) - return true; - if(strcmp(atype, VARRAY_TYPE)) - return false; - } + if(!classes().put_dont_replace(String::Body(atype), aclass)) + return true; + if(!allow_class_replace && strcmp(atype, VARRAY_TYPE)) + return false; + classes().remove(String::Body(atype)); classes().put(atype, aclass); return true; } @@ -352,14 +360,16 @@ void Request::configure_admin(VStateless pa_execute_recursion_limit=EXECUTE_RECURSION_LIMIT; CONF_OPTION(limits, recursion_limit_name, { pa_execute_recursion_limit=option->as_int(); - if(pa_execute_recursion_limit==0) pa_execute_recursion_limit=INT_MAX; + if(pa_execute_recursion_limit<=0) pa_execute_recursion_limit=INT_MAX; + pa_execute_recursion_limit=min(pa_execute_recursion_limit, INT_MAX-EXECUTE_RECURSION_RESERVE); }, "LIMITS.%s must be int"); + execute_recursion_limit=pa_execute_recursion_limit; pa_file_size_limit=FILE_SIZE_LIMIT; CONF_OPTION(limits, file_size_limit_name, { double limit=option->as_double(); if(limit >= (double)SSIZE_MAX) - throw Exception(PARSER_RUNTIME, 0, "$MAIN:LIMITS.%s must be less than %.15g", file_size_limit_name.cstr(), (double)SSIZE_MAX); + throw Exception(PARSER_RUNTIME, 0, "$MAIN:LIMITS.%s must be less than %s", file_size_limit_name.cstr(), pa_itoa(SSIZE_MAX)); pa_file_size_limit=(size_t)limit; if(pa_file_size_limit==0) pa_file_size_limit=SSIZE_MAX; @@ -393,7 +403,7 @@ void Request::configure_admin(VStateless methoded_array().configure_admin(*this); } -const char* Request::get_exception_cstr(const Exception& e, Request::Exception_details& details) { +const char* Request::get_exception_cstr(const Exception& e, VException& details) { #define PA_URI_FORMAT "%s: " #define PA_COMMENT_TYPE_FORMAT "%s [%s]" @@ -431,7 +441,7 @@ const char* Request::get_exception_cstr( return result; } -Table &Request::Exception_trace::table(Request &r){ +Table &Request::Exception_trace::table(Request &r, const String* problem_source, size_t bottom, size_t top){ // $stack[^table::create{name file lineno colno}] Table::columns_type stack_trace_columns(new ArrayString); *stack_trace_columns+=new String("name"); @@ -440,8 +450,8 @@ Table &Request::Exception_trace::table(R *stack_trace_columns+=new String("colno"); Table& stack_trace=*new Table(stack_trace_columns); - if(!is_empty()/*signed!*/) - for(size_t i=bottom_index(); iget_table()) @@ -529,8 +536,11 @@ void Request::core(const char* config_fi output_result(body_value->as_vfile(flang, &charsets), header_only, as_attachment); } catch(const Exception& e) { // request handling problem + // OK, as native execution stack has unwound + anti_endless_execute_recursion=0; + // we're returning not result, but error explanation - Request::Exception_details details=get_details(e); + VException& details=*new VException(*this, e); const char* exception_cstr=get_exception_cstr(e, details); // reset language to default @@ -547,10 +557,11 @@ void Request::core(const char* config_fi if(const Method *method=main_class.get_method(*new String(UNHANDLED_EXCEPTION_METHOD_NAME))) { // preparing parameters to @unhandled_exception[exception;stack] - Table& stack_trace=exception_trace.table(*this); + Table& stack_trace=exception_trace.table(*this, details.problem_source); + details.expire_trace(); exception_trace.clear(); // forget all about previous life, in case there would be error inside of this method, error handled would not be mislead by old stack contents (see extract_origin) - Value *params[]={&details.vhash, new VTable(&stack_trace)}; + Value *params[]={&details, new VTable(&stack_trace)}; METHOD_FRAME_ACTION(*method, 0 /*no caller*/, main_class, { frame.store_params(params, 2); call(frame); @@ -559,12 +570,12 @@ void Request::core(const char* config_fi } // conditionally log it - Value* vhandled=details.vhash.hash().get(exception_handled_part_name); + Value* vhandled=details.handled(); if(!vhandled || !vhandled->as_bool()) { SAPI::log(sapi_info, "%s", exception_cstr); } } catch(const Exception& e) { // exception in @unhandled_exception - Request::Exception_details details=get_details(e); + VException& details=*new VException(*this, e); // logging both initial and new exceptions SAPI::log(sapi_info, "%s", exception_cstr); SAPI::log(sapi_info, "Exception in @unhandled_exception at %s", get_exception_cstr(e, details)); @@ -699,7 +710,7 @@ void Request::use_buf(VStateless_class& ArrayClass& cclasses=compile(&aclass, source, main_alias, file_no, line_no_offset); VString* vfilespec= - new VString(*new String(file_list[file_no], String::L_TAINTED)); + new VString(file_list[file_no]); for(size_t i=0; i= '0' && *p <= '9'){ const char *n=p; while(*p>='0' && *p<='9') p++; - r.start = pa_atoul(pa_strdup(n, p-n)); + r.start = pa_atoul(pa_strdup(n, p-n)); // ((uint64_t)-1) is treated as UNSET } while(*p==' ' || *p=='\t') p++; @@ -819,7 +826,7 @@ static void parse_range(const String* s, if(*p >= '0' && *p <= '9'){ const char *n=p; while(*p>='0' && *p<='9') p++; - r.end = pa_atoul(pa_strdup(n, p-n)); + r.end = pa_atoul(pa_strdup(n, p-n)); // ((uint64_t)-1) is treated as UNSET } while(*p==' ' || *p=='\t') p++; @@ -877,6 +884,8 @@ static void output_pieces(Request& r, bo return SAPI::send_error(r.sapi_info, "", "416"); if(rg.start == UNSET && rg.end != UNSET){ + if(rg.end == 0) + return SAPI::send_error(r.sapi_info, "", "416"); if(rg.end > content_length) rg.end = content_length; rg.start = content_length - rg.end; @@ -896,7 +905,7 @@ static void output_pieces(Request& r, bo part_length = rg.end-rg.start+1; char buf[MAX_STRING]; - snprintf(buf, MAX_STRING, "bytes %.15g-%.15g/%.15g", (double)rg.start, (double)rg.end, (double)content_length); + snprintf(buf, MAX_STRING, "bytes %s-%s/%s", pa_uitoa(rg.start), pa_uitoa(rg.end), pa_uitoa(content_length)); SAPI::add_header_attribute(r.sapi_info, HTTP_STATUS, "206"); SAPI::add_header_attribute(r.sapi_info, "content-range", buf); } else { @@ -1030,47 +1039,83 @@ const String& Request::transcode(const x } #endif -Request::Exception_details Request::get_details(const Exception& e) { - const String* problem_source=e.problem_source(); - VHash& vhash=*new VHash; HashStringValue& hash=vhash.hash(); - Operation::Origin origin={0, 0, 0}; +VException::VException(Request& r, const Exception& e): + fexception(e), fhandled(&VBool::get(false)), + ftrace_bottom(r.exception_trace.bottom_index()), ftrace_top(r.exception_trace.top_index()), + origin(Operation::Origin::create(0, 0, 0)), problem_source(e.problem_source()) { + Request::Exception_trace& exception_trace=r.exception_trace; if(!exception_trace.is_empty()) { - Trace bottom=exception_trace.bottom_value(); + Request::Trace bottom=exception_trace.bottom_value(); origin=bottom.origin(); - if(!problem_source) { // we don't know who trigged the bug + if(!problem_source) // we don't know who trigged the bug problem_source=bottom.name(); // we usually know source of next-from-throw-point exception did that - exception_trace.set_bottom_index(exception_trace.bottom_index()+1); - } else if (bottom.name()==problem_source) { // it is that same guy? - exception_trace.set_bottom_index(exception_trace.bottom_index()+1); // throw away that trace - } else { - // stack top contains not us, leaving intact to help ^throw - } } + if(origin.file_no) + ffile=r.file_list[origin.file_no]; +} - // $.type - if(const char* type=e.type(true)) - hash.put(exception_type_part_name, new VString(*new String(type))); - - // $.source - if(problem_source) - hash.put(exception_source_part_name, new VString(*new String(*problem_source, String::L_TAINTED))); - - // $.file $.lineno $.colno - if(origin.file_no){ - hash.put("file", new VString(*new String(file_list[origin.file_no], String::L_TAINTED))); - hash.put("lineno", new VInt(1+origin.line)); - hash.put("colno", new VInt(1+origin.col)); - } +Value* VException::handled() { + return fhash ? fhash->get(Symbols::HANDLED_SYMBOL) : fhandled; +} - // $.comment - if(const char* comment=e.comment(true)) - hash.put(exception_comment_part_name, new VString(*new String(comment, String::L_TAINTED))); +Table& VException::stack(Request& r) { + if(ftrace_top==size_t(-1)) + throw Exception(PARSER_RUNTIME, 0, "exception stack used outside of context"); + return r.exception_trace.table(r, problem_source, ftrace_bottom, ftrace_top); +} + +Value* VException::get_element(const String& name) { + if(!fhash) { + if(SYMBOLS_EQ(name,HANDLED_SYMBOL)) + return fhandled; + if(SYMBOLS_EQ(name,TYPE_SYMBOL)) + return fexception.type(true) ? new VString(*new String(fexception.type())) : 0; + if(SYMBOLS_EQ(name,SOURCE_SYMBOL)) + return problem_source ? new VString(*problem_source) : 0; + if(SYMBOLS_EQ(name,COMMENT_SYMBOL)) + return fexception.comment(true) ? new VString(fexception.comment()) : 0; + if(SYMBOLS_EQ(name,FILE_SYMBOL)) + return origin.file_no ? new VString(ffile) : 0; + if(SYMBOLS_EQ(name,LINENO_SYMBOL)) + return origin.file_no ? new VInt(1+origin.line) : 0; + if(SYMBOLS_EQ(name,COLNO_SYMBOL)) + return origin.file_no ? new VInt(1+origin.col) : 0; + if(SYMBOLS_EQ(name,FIELDS_SYMBOL)) + return this; + } + // Computed field names need not be interned: use ordinary hash lookup. + return VHashReference::get_element(name); +} + +const VJunction* VException::put_element(const String& name, Value* value) { + if(!fhash && SYMBOLS_EQ(name,HANDLED_SYMBOL)) { + fhandled=value; + return 0; + } + return VHashReference::put_element(name, value); +} + +HashStringValue& VException::hash() { + if(fhash) + return *fhash; + HashStringValue& result=*new HashStringValue; - // $.handled(0) - hash.put(exception_handled_part_name, &VBool::get(false)); + if(const char* type=fexception.type(true)) + result.put(Symbols::TYPE_SYMBOL, new VString(*new String(type))); + if(problem_source) + result.put(Symbols::SOURCE_SYMBOL, new VString(*problem_source)); + if(origin.file_no) { + result.put(Symbols::FILE_SYMBOL, new VString(ffile)); + result.put(Symbols::LINENO_SYMBOL, new VInt(1+origin.line)); + result.put(Symbols::COLNO_SYMBOL, new VInt(1+origin.col)); + } + if(const char* comment=fexception.comment(true)) + result.put(Symbols::COMMENT_SYMBOL, new VString(comment)); + result.put(Symbols::HANDLED_SYMBOL, fhandled); - return Request::Exception_details(origin, problem_source, vhash); + fhash=&result; + return result; } Temp_value_element::Temp_value_element(Request& arequest, Value& awhere, const String& aname, Value* awhat) :