Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python projects #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions projects/black/decorated_fuzz_format_filecontents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/python3
# Copyright 2023 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 sys
import atheris

import black

from py_io_capture import decorate_module, dump_records, DUMP_FILE_NAME
import atexit
black = decorate_module(black)
atexit.register(dump_records, DUMP_FILE_NAME)

def TestOneInput(data):
fdp = atheris.FuzzedDataProvider(data)
try:
black.format_file_contents(fdp.ConsumeUnicodeNoSurrogates(sys.maxsize),
mode=black.Mode(),
fast=fdp.ConsumeBool())
except black.InvalidInput:
pass
except black.NothingChanged:
pass
except AssertionError:
pass
except KeyError:
pass


def main():
atheris.instrument_all()
atheris.Setup(sys.argv, TestOneInput)
atheris.Fuzz()


if __name__ == "__main__":
main()
44 changes: 44 additions & 0 deletions projects/black/decorated_fuzz_lib2to3_parse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/python3
# Copyright 2023 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 sys
import atheris

import black

from py_io_capture import decorate_module, dump_records, DUMP_FILE_NAME
import atexit
black = decorate_module(black)
atexit.register(dump_records, DUMP_FILE_NAME)

def TestOneInput(data):
fdp = atheris.FuzzedDataProvider(data)
try:
black.lib2to3_parse(fdp.ConsumeUnicodeNoSurrogates(sys.maxsize))
except black.InvalidInput:
pass
except AssertionError:
pass
except KeyError:
pass


def main():
atheris.instrument_all()
atheris.Setup(sys.argv, TestOneInput)
atheris.Fuzz()


if __name__ == "__main__":
main()
49 changes: 49 additions & 0 deletions projects/black/decorated_fuzz_raw_format_filecontents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/python3
# Copyright 2023 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 sys
import atheris

import black

from py_io_capture import decorate_module, dump_records, DUMP_FILE_NAME
import atexit
black = decorate_module(black)
atexit.register(dump_records, DUMP_FILE_NAME)

def TestOneInput(data):
if len(data) < 50:
return

fdp = atheris.FuzzedDataProvider(data)
try:
black.format_file_contents(fdp.ConsumeUnicodeNoSurrogates(sys.maxsize),
mode=black.Mode(),
fast=False)
except black.InvalidInput:
pass
except black.NothingChanged:
pass
except AssertionError:
pass


def main():
atheris.instrument_all()
atheris.Setup(sys.argv, TestOneInput)
atheris.Fuzz()


if __name__ == "__main__":
main()
97 changes: 97 additions & 0 deletions projects/bottleneck/decorated_fuzz_bn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/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.
"""Scalar equivalene checker"""

import os
import sys
import atheris

import numpy as np
from numpy.testing import assert_array_almost_equal
import bottleneck as bn

from py_io_capture import decorate_module, dump_records, DUMP_FILE_NAME
import atexit
bn = decorate_module(bn)
atexit.register(dump_records, DUMP_FILE_NAME)

def gen_random_array(data, fdp = None):
if fdp is None:
fdp = atheris.FuzzedDataProvider(data)
l1 = list()
for i in range(fdp.ConsumeIntInRange(5, 3000)):
l1.append(fdp.ConsumeIntInRange(1,10000))
a = np.array(l1)
return a

def TestOneInput(data):
"""Tests scalar equivalence and also move operations"""
fdp = atheris.FuzzedDataProvider(data)
a = gen_random_array(data, fdp)

func_pairs = [
(bn.nansum, bn.slow.nansum),
(bn.nanmean, bn.slow.nanmean),
(bn.nanstd, bn.slow.nanstd),
(bn.nanvar, bn.slow.nanvar),
(bn.nanmin, bn.slow.nanmin),
(bn.median, bn.slow.median),
(bn.nanmedian, bn.slow.nanmedian),
(bn.ss, bn.slow.ss),
(bn.nanargmin, bn.slow.nanargmin),
(bn.nanargmax, bn.slow.nanargmax),
(bn.anynan, bn.slow.anynan),
(bn.allnan, bn.slow.allnan),
]

idx = 0
for func0, func1 in func_pairs:
idx = idx + 1
actual = func0(a)
desired = func1(a)
assert_array_almost_equal(
actual,
desired,
err_msg="Failed scalar equivalence"
)


# Test move operations
window = fdp.ConsumeIntInRange(2, 50)
min_count = fdp.ConsumeIntInRange(1, window)
actual = bn.move_median(
a,
window=window,
min_count = fdp.ConsumeIntInRange(1,100)
)
desired = bn.slow.move_median(
a,
window=window,
min_count=fdp.ConsumeIntInRange(1, 100)
)
assert_array_almost_equal(
actual,
desired,
err_msg="Failed move operation"
)


def main():
atheris.instrument_all()
atheris.Setup(sys.argv, TestOneInput, enable_python_coverage=True)
atheris.Fuzz()

if __name__ == "__main__":
main()
46 changes: 46 additions & 0 deletions projects/bz2file/decorated_fuzz_bz2file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/python3
# Copyright 2023 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 sys
import atheris

import bz2file

from py_io_capture import decorate_module, dump_records, DUMP_FILE_NAME
import atexit
bz2file = decorate_module(bz2file)
atexit.register(dump_records, DUMP_FILE_NAME)

def TestOneInput(data):
fdp = atheris.FuzzedDataProvider(data)
bzfile_path = '/tmp/random_file.txt'
with open(bzfile_path, 'wb') as f:
f.write(fdp.ConsumeBytes(sys.maxsize))

try:
with bz2file.open(bzfile_path) as target_file:
target_file.seek(fdp.ConsumeIntInRange(-1, 100))
target_file.read(size=fdp.ConsumeIntInRange(-1, 100))
except (ValueError,EOFError,OSError):
pass


def main():
atheris.instrument_all()
atheris.Setup(sys.argv, TestOneInput)
atheris.Fuzz()


if __name__ == "__main__":
main()
49 changes: 49 additions & 0 deletions projects/cffi/decorated_fuzz_recompiler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/python3
# Copyright 2023 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 sys
import atheris

from cffi import FFI, VerificationError, FFIError, CDefError
from cffi import recompiler

from py_io_capture import decorate_module, dump_records, DUMP_FILE_NAME
import atexit
cffi = decorate_module(cffi)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if there is no import cffi, this decoration will case error. We can either decorate recompiler or add import cffi to the top

atexit.register(dump_records, DUMP_FILE_NAME)

def TestOneInput(data):
fdp = atheris.FuzzedDataProvider(data)
ffi = FFI()
try:
ffi.cdef(fdp.ConsumeUnicodeNoSurrogates(sys.maxsize))
recomp = recompiler.Recompiler(ffi, 'fuzzmod')

# Call functions on the recompiler object.
recomp.collect_type_table()
recomp.collect_step_tables()
except (VerificationError, FFIError, CDefError) as e:
pass
except AssertionError:
pass


def main():
atheris.instrument_all()
atheris.Setup(sys.argv, TestOneInput, enable_python_coverage=True)
atheris.Fuzz()


if __name__ == "__main__":
main()
47 changes: 47 additions & 0 deletions projects/chardet/decorated_fuzz_detector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/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 sys
import atheris
import chardet

from py_io_capture import decorate_module, dump_records, DUMP_FILE_NAME
import atexit
chardet = decorate_module(chardet)
atexit.register(dump_records, DUMP_FILE_NAME)

def TestOneInput(data):
fdp = atheris.FuzzedDataProvider(data)

# General detection.
chardet.detect(data)

# Incremental feeding>
detector = chardet.universaldetector.UniversalDetector()
for b in data:
detector.feed(b)
if detector.done:
break
detector.close()
res = detector.result


def main():
atheris.instrument_all()
atheris.Setup(sys.argv, TestOneInput, enable_python_coverage=True)
atheris.Fuzz()


if __name__ == "__main__":
main()
Loading