From 00089cf2e32d52a601a124b3bf85f5b9ee76f533 Mon Sep 17 00:00:00 2001 From: DavidKorczynski Date: Tue, 30 Aug 2022 18:02:38 +0100 Subject: [PATCH] pyparsing: initial integration (#8266) --- projects/pyparsing/Dockerfile | 21 ++++++++ projects/pyparsing/build.sh | 21 ++++++++ projects/pyparsing/fuzz_parse.py | 84 ++++++++++++++++++++++++++++++++ projects/pyparsing/project.yaml | 11 +++++ 4 files changed, 137 insertions(+) create mode 100644 projects/pyparsing/Dockerfile create mode 100755 projects/pyparsing/build.sh create mode 100644 projects/pyparsing/fuzz_parse.py create mode 100644 projects/pyparsing/project.yaml diff --git a/projects/pyparsing/Dockerfile b/projects/pyparsing/Dockerfile new file mode 100644 index 000000000..fa1ab41e2 --- /dev/null +++ b/projects/pyparsing/Dockerfile @@ -0,0 +1,21 @@ +# Copyright 2022 Google LLC +# +# Licensed 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. +# +################################################################################ + +FROM gcr.io/oss-fuzz-base/base-builder-python +RUN apt-get update && apt-get install -y make autoconf automake libtool +RUN git clone --depth 1 https://github.com/pyparsing/pyparsing pyparsing +WORKDIR pyparsing +COPY build.sh *.py $SRC/ diff --git a/projects/pyparsing/build.sh b/projects/pyparsing/build.sh new file mode 100755 index 000000000..1974eb7eb --- /dev/null +++ b/projects/pyparsing/build.sh @@ -0,0 +1,21 @@ +#!/bin/bash -eu +# Copyright 2022 Google LLC +# +# Licensed 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. +# +################################################################################ + +pip3 install . +for fuzzer in $(find $SRC -name 'fuzz_*.py'); do + compile_python_fuzzer $fuzzer +done diff --git a/projects/pyparsing/fuzz_parse.py b/projects/pyparsing/fuzz_parse.py new file mode 100644 index 000000000..885056300 --- /dev/null +++ b/projects/pyparsing/fuzz_parse.py @@ -0,0 +1,84 @@ +#!/usr/bin/python3 +# Copyright 2022 Google LLC +# +# Licensed 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. + +import os +import sys +import atheris + +from pyparsing import ( + Literal, + Word, + ZeroOrMore, + Group, + Dict, + Suppress, + ParseException, +) + +def dict_parse_generator(fdp): + """Generate random parser""" + curr = Literal("f") + special_chars="[]{}/|\<>:;-=" + op_count = fdp.ConsumeIntInRange(2, 15) + for i in range(op_count): + operation = fdp.ConsumeIntInRange(0,4) + if operation == 0: + l1 = Literal(fdp.PickValueInList(list(special_chars))) + l2 = Literal(fdp.PickValueInList(list(special_chars))) + word = Word(fdp.ConsumeUnicodeNoSurrogates(10)) + curr = Group(Dict(ZeroOrMore(word)) + curr) + elif operation == 1: + word1 = Word(fdp.ConsumeUnicodeNoSurrogates(10)) + word2 = Word(fdp.ConsumeUnicodeNoSurrogates(10)) + curr = Group(Dict(OneOrMore(word1, word2)) + curr) + elif operation == 2: + curr = curr + Word(fdp.ConsumeUnicodeNoSurrogates(10)) + elif operation == 3: + curr = curr + Suppress(fdp.ConsumeUnicodeNoSurrogates(2)) + else: + word1 = Word(fdp.ConsumeUnicodeNoSurrogates(10)) + word2 = Word(fdp.ConsumeUnicodeNoSurrogates(10)) + curr = Group(Dict(OneOrMore(word1, word2)) + curr) + return Dict(curr) + + +def TestOneInput(data): + fdp = atheris.FuzzedDataProvider(data) + try: + bnf = dict_parse_generator(fdp) + except: + return + + try: + tokens = bnf.parseString(fdp.ConsumeUnicodeNoSurrogates(1024)) + except ParseException: + pass + except TypeError as e: + # Catch the TypeError exception from here: + # https://github.com/pyparsing/pyparsing/blob/d93930308f7fe79f2290812074f62a472c83db59/pyparsing/core.py#L5674 + if "could not extract dict values from parsed results" in str(e): + pass + else: + raise e + + +def main(): + atheris.instrument_all() + atheris.Setup(sys.argv, TestOneInput, enable_python_coverage=True) + atheris.Fuzz() + + +if __name__ == "__main__": + main() diff --git a/projects/pyparsing/project.yaml b/projects/pyparsing/project.yaml new file mode 100644 index 000000000..1e0694bf6 --- /dev/null +++ b/projects/pyparsing/project.yaml @@ -0,0 +1,11 @@ +homepage: "https://github.com/pyparsing/pyparsing" +language: python +main_repo: "https://github.com/pyparsing/pyparsing" +fuzzing_engines: +- libfuzzer +sanitizers: +- address +- undefined +vendor_ccs: +- david@adalogics.com +- adam@adalogics.com