Skip to content

Make the lexer's string literal buffer thread_local - #6

Open
alepacheco wants to merge 1 commit into
envoyproxy:mainfrom
alepacheco:fix/lexer-thread-local-string-buffer
Open

alepacheco wants to merge 1 commit into
envoyproxy:mainfrom
alepacheco:fix/lexer-thread-local-string-buffer

Conversation

@alepacheco

Copy link
Copy Markdown

SQLParser::parse is a static function with no state of its own, but the lexer accumulates single-quoted string literals in strbuf, a process-wide static std::stringstream in flex_lexer.l. Two threads parsing statements that contain string literals at the same time reset, append to and read from the same stream, which corrupts it and the heap.

Envoy calls the parser from every worker thread: contrib/common/sqlutils parses the queries seen by the mysql_proxy and postgres_proxy filters, so two workers handling queries with string literals at the same moment race on this buffer.

This makes the buffer thread_local, as upstream hyrise/sql-parser already has it, and regenerates flex_lexer.cpp and flex_lexer.h with flex 2.6.4 (apart from the strbuf declaration, only #line numbers change).

Testing:

  • New ConcurrentStringLiteralTest in test/sql_tests.cpp parses queries with distinct literals from four threads and checks that each thread gets its own literal back. The test binary is now linked with -pthread.
  • make test passes (SQL tests, memory leak check, grammar conflict check).
  • With the library and tests built with -fsanitize=thread, ThreadSanitizer reports a data race on strbuf in the new test before this change; after it the suite runs clean.

Once this is merged I can send the sql-parser bump to Envoy.

An AI tool was used to help find this bug and draft the fix; I reviewed, tested and understand the change.

SQLParser::parse is a static function with no shared state apart from the
lexer's buffer for single-quoted string literals, which was a process-wide
static std::stringstream. Two threads parsing statements that contain string
literals at the same time wrote to and read from the same stringstream,
corrupting it and the heap.

Make the buffer thread_local, as upstream hyrise/sql-parser does, regenerate
flex_lexer.cpp with flex 2.6.4, and add a test that parses queries with
distinct string literals from several threads and checks that each thread
gets its own literal back. The test binary is now linked with -pthread.

Signed-off-by: Alejandro Pacheco <alepacheco99@gmail.com>
@alepacheco

Copy link
Copy Markdown
Author

Hi @ggreenway (you reviewed and merged #5 here) and @cpakulski (owner of contrib/common/sqlutils in Envoy, which calls this parser): could one of you take a look at this when you have a moment?

It is a small change in flex_lexer.l (the string literal buffer becomes thread_local, as upstream hyrise/sql-parser has it), the regenerated lexer and a concurrency test; it merges cleanly on main. Once it is in I will send the matching sql-parser bump to Envoy, as envoyproxy/envoy#46156 did for #5. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant