LucaCappelletti94 commented on code in PR #2646:
URL: 
https://github.com/apache/datafusion-sqlparser-rs/pull/2646#discussion_r4299059396


##########
fuzz/build.rs:
##########
@@ -0,0 +1,34 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+fn main() {
+    #[cfg(feature = "clickhouse")]
+    {
+        let dir = "vendor/clickhouse-lexer";
+        println!("cargo:rerun-if-changed={dir}");
+        cc::Build::new()
+            .cpp(true)

Review Comment:
   You should pin the compiler to clang++. When `c++` is GCC (the default on 
Ubuntu), `cargo +nightly fuzz build --features clickhouse` fails with 
`reference to 'std' is ambiguous` at `Lexer.cpp:526`.
   
   ```suggestion
           cc::Build::new()
               // LexerStandalone.h declares a `std` namespace that GCC's 
predeclared one makes ambiguous.
               .compiler("clang++")
               .cpp(true)
   ```
   
   But this is only relevant if we go for git submodules instead of a crate.



##########
fuzz/fuzz_targets/fuzz_clickhouse_lexes.rs:
##########
@@ -0,0 +1,80 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#![no_main]
+
+use libfuzzer_sys::fuzz_target;
+use sqlparser::dialect::ClickHouseDialect;
+use sqlparser::tokenizer::Tokenizer;
+use std::ffi::{c_char, c_int, c_uchar, c_void};
+
+const COMMIT: &str = include_str!("../vendor/clickhouse-lexer/COMMIT");
+
+// C ABI of the vendored lexer, see 
fuzz/vendor/clickhouse-lexer/Parsers/clickhouse_lexer.h.
+extern "C" {
+    static clickhouse_lexer_size: usize;
+    fn clickhouse_lexer_create(
+        ptr: *mut c_void,
+        begin: *const c_char,
+        end: *const c_char,
+        max_query_size: usize,
+    );
+    fn clickhouse_lexer_next_token(
+        ptr: *mut c_void,
+        out_token_begin: *mut *const c_char,
+        out_token_end: *mut *const c_char,
+    ) -> c_uchar;
+    fn clickhouse_lexer_token_is_error(token: c_uchar) -> c_int;
+    fn clickhouse_lexer_token_is_end(token: c_uchar) -> c_int;
+}
+
+/// Whether ClickHouse's lexer reads `sql` to the end without an error token.

Review Comment:
   With the lone `$` check, the doc comment should name it as it is hardly 
obvious.
   
   ```suggestion
   /// Whether ClickHouse's lexer reads `sql` to the end without an error token 
or a lone `$`.
   ```



##########
fuzz/fuzz_targets/fuzz_clickhouse_lexes.rs:
##########
@@ -0,0 +1,80 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#![no_main]
+
+use libfuzzer_sys::fuzz_target;
+use sqlparser::dialect::ClickHouseDialect;
+use sqlparser::tokenizer::Tokenizer;
+use std::ffi::{c_char, c_int, c_uchar, c_void};
+
+const COMMIT: &str = include_str!("../vendor/clickhouse-lexer/COMMIT");
+
+// C ABI of the vendored lexer, see 
fuzz/vendor/clickhouse-lexer/Parsers/clickhouse_lexer.h.
+extern "C" {
+    static clickhouse_lexer_size: usize;
+    fn clickhouse_lexer_create(
+        ptr: *mut c_void,
+        begin: *const c_char,
+        end: *const c_char,
+        max_query_size: usize,
+    );
+    fn clickhouse_lexer_next_token(
+        ptr: *mut c_void,
+        out_token_begin: *mut *const c_char,
+        out_token_end: *mut *const c_char,
+    ) -> c_uchar;
+    fn clickhouse_lexer_token_is_error(token: c_uchar) -> c_int;
+    fn clickhouse_lexer_token_is_end(token: c_uchar) -> c_int;
+}
+
+/// Whether ClickHouse's lexer reads `sql` to the end without an error token.
+fn clickhouse_lexes(sql: &str) -> bool {
+    // SAFETY: the symbol is a plain `size_t` the C++ side initializes 
statically.
+    let size = unsafe { clickhouse_lexer_size };
+    // u64 slots keep the pointer fields of the C++ Lexer aligned.
+    let mut lexer = vec![0u64; size.div_ceil(8)];
+    let ptr = lexer.as_mut_ptr().cast::<c_void>();
+    let begin = sql.as_ptr().cast::<c_char>();
+    // SAFETY: `lexer` holds `clickhouse_lexer_size` bytes and `sql` outlives 
every call below.
+    unsafe {
+        clickhouse_lexer_create(ptr, begin, begin.add(sql.len()), 0);
+        loop {
+            let mut token_begin = std::ptr::null();
+            let mut token_end = std::ptr::null();
+            let token = clickhouse_lexer_next_token(ptr, &mut token_begin, 
&mut token_end);
+            if clickhouse_lexer_token_is_error(token) != 0 {
+                return false;
+            }
+            if clickhouse_lexer_token_is_end(token) != 0 {
+                return true;
+            }

Review Comment:
   You should count a lone `$` as rejected. ClickHouse lexes it as 
`DollarSign`, which only its JSONPath parser consumes, so no statement holding 
one parses.
   
   Furthermore, ClickHouse 26.9 rejects `SELECT $$`, `SELECT [$]`, `SELECT 1 AS 
$` and `SELECT * FROM t WHERE x = $`, yet the target reports `SELECT $$` as a 
tokenizer bug.
   
   I tested an 8-minute `-fork=6` run, and 9190 of the 46504 crashes were this 
case. A one-byte `$` token is always `DollarSign`, and that held for all 46504 
crashes.
   
   ```suggestion
               if clickhouse_lexer_token_is_end(token) != 0 {
                   return true;
               }
               // A lone `$` lexes as DollarSign, which no ClickHouse SQL 
statement accepts.
               if token_end.offset_from(token_begin) == 1 && *token_begin == 
b'$' as c_char {
                   return false;
               }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to