1717#include < Common/CurrentMemoryTracker.h>
1818#include < Core/Settings.h>
1919
20+ #include < csetjmp>
2021#include < cstddef>
2122#include < cstdio>
2223#include < cstdlib>
24+ #include < cstring>
2325#include < exception>
2426#include < string>
2527#include < typeinfo>
@@ -282,16 +284,60 @@ const SettingFieldTimezone & Settings::operator[](SettingsTimezone) const
282284// / Exceptions.
283285// /
284286// / The build uses `-fignore-exceptions`: `throw`, `try` and `catch` still compile, but no landing
285- // / pads or unwind tables are emitted, so nothing can be caught. That is sound here because an
286- // / exception can only mean a bug or a resource limit - a syntax error is reported by
287- // / `tryParseQuery` returning null, and `src/Parsers` contains no `catch` at all, which a style
288- // / check enforces. Defining `__cxa_throw` here keeps libc++abi's exception machinery out of the
289- // / bundle entirely; the exception object is constructed and then we stop.
287+ // / pads or unwind tables are emitted, so nothing can be caught, and defining `__cxa_throw` here
288+ // / keeps libc++abi's exception machinery out of the bundle entirely.
289+ // /
290+ // / A syntax error does not come this way - `tryParseQuery` returns null and fills in the message,
291+ // / and `src/Parsers` contains no `catch` at all, which a style check enforces. But a handful of
292+ // / checks in the parser still report an invalid query by throwing (`Frame start cannot be
293+ // / UNBOUNDED FOLLOWING`, for one), and stopping the module on an ordinary user mistake is not
294+ // / acceptable. So `ch_check` and `ch_format` arm a `setjmp` boundary and a throw returns to it,
295+ // / with the message, as a parse failure.
296+ // /
297+ // / The unwinding this replaces would have run destructors; `longjmp` does not, so a throw leaks
298+ // / whatever the parser allocated below the boundary. The alternative is `-fwasm-exceptions`, which
299+ // / costs 262 KB and an engine implementing the exception-handling proposal - too much to pay for
300+ // / tidiness on a path that a browser hits only on an invalid query.
301+ // /
302+ // / Recovery is for `DB::Exception` and nothing else. The object arriving at `__cxa_throw` is
303+ // / untyped, so its dynamic type has to be established from the `type_info` argument before it can
304+ // / be read as anything; a `std::bad_alloc` from `operator new` or a `Poco` exception is an
305+ // / unrelated object, and reading it through a `Poco::Exception` pointer would be undefined. For
306+ // / those only the type name can be reported, and the module stops. The comparison is exact rather
307+ // / than a `dynamic_cast` - there is no RTTI hierarchy walk available here - so a hypothetical class
308+ // / derived from `DB::Exception` also lands in the second case, which is the safe direction.
290309// / ---------------------------------------------------------------------------------------------
291310
311+ namespace
312+ {
313+
314+ jmp_buf recovery_point;
315+ bool recovery_armed = false ;
316+
317+ // / Not a `std::string`: filling this in must not allocate, or a throw from the allocation would
318+ // / re-enter `__cxa_throw`.
319+ char recovery_message[1024 ];
320+
321+ }
322+
292323extern " C"
293324{
294325
326+ jmp_buf * chParserRecoveryPoint ()
327+ {
328+ return &recovery_point;
329+ }
330+
331+ void chParserArmRecovery (bool armed)
332+ {
333+ recovery_armed = armed;
334+ }
335+
336+ const char * chParserRecoveryMessage ()
337+ {
338+ return recovery_message;
339+ }
340+
295341void * __cxa_allocate_exception (size_t size) noexcept
296342{
297343 // / The exception object is constructed in place here, so the storage has to be aligned for any
@@ -306,17 +352,33 @@ void __cxa_free_exception(void *) noexcept
306352
307353[[noreturn]] void __cxa_throw (void * thrown, void * type_info, void (*)(void *))
308354{
309- // / Only `DB::Exception` may be read as such: the thrown object is untyped here, and everything
310- // / else - `std::bad_alloc` from `operator new`, a `Poco` exception from a `Poco::Net` address
311- // / parser - would be a reinterpretation of an unrelated object. For those, the type name is all
312- // / that can be reported safely. This is not a `dynamic_cast`, so a hypothetical class derived
313- // / from `DB::Exception` also falls into the second case rather than into undefined behavior.
314355 const auto * thrown_type = static_cast <const std::type_info *>(type_info);
315- const char * message = thrown_type && *thrown_type == typeid (DB ::Exception)
316- ? static_cast <const DB ::Exception *>(thrown)->what ()
317- : (thrown_type ? thrown_type->name () : " unknown exception" );
318356
319- std::fprintf (stderr, " ClickHouse parser: unrecoverable error: %s\n " , message);
357+ if (thrown_type && *thrown_type == typeid (DB ::Exception))
358+ {
359+ // / `DB::Exception` derives from `Poco::Exception`, whose `what()` is the message.
360+ const char * message = static_cast <const DB ::Exception *>(thrown)->what ();
361+
362+ if (recovery_armed)
363+ {
364+ // / Disarm first: this is the only path that can be re-entered.
365+ recovery_armed = false ;
366+
367+ size_t length = std::strlen (message);
368+ if (length > sizeof (recovery_message) - 1 )
369+ length = sizeof (recovery_message) - 1 ;
370+ std::memcpy (recovery_message, message, length);
371+ recovery_message[length] = 0 ;
372+
373+ longjmp (recovery_point, 1 );
374+ }
375+
376+ std::fprintf (stderr, " ClickHouse parser: unrecoverable error: %s\n " , message);
377+ std::abort ();
378+ }
379+
380+ // / The object cannot be read, so the type name is all there is to report.
381+ std::fprintf (stderr, " ClickHouse parser: unrecoverable error of type %s\n " , thrown_type ? thrown_type->name () : " unknown" );
320382 std::abort ();
321383}
322384
0 commit comments