Repository navigation
Make the lexer's string literal buffer thread_local - #6
Open
alepacheco wants to merge 1 commit into
Open
alepacheco wants to merge 1 commit into
alepacheco wants to merge 1 commit into
Conversation
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>
Author
|
Hi @ggreenway (you reviewed and merged #5 here) and @cpakulski (owner of It is a small change in |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
SQLParser::parseis a static function with no state of its own, but the lexer accumulates single-quoted string literals instrbuf, a process-widestatic std::stringstreaminflex_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/sqlutilsparses the queries seen by themysql_proxyandpostgres_proxyfilters, 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 regeneratesflex_lexer.cppandflex_lexer.hwith flex 2.6.4 (apart from thestrbufdeclaration, only#linenumbers change).Testing:
ConcurrentStringLiteralTestintest/sql_tests.cppparses 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 testpasses (SQL tests, memory leak check, grammar conflict check).-fsanitize=thread, ThreadSanitizer reports a data race onstrbufin 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.