Coverage for /pythoncovmergedfiles/medio/medio/src/ujson_fuzzer.py: 41%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

27 statements  

1###### Coverage stub 

2import atexit 

3import coverage 

4cov = coverage.coverage(data_file='.coverage', cover_pylib=True) 

5cov.start() 

6# Register an exist handler that will print coverage 

7def exit_handler(): 

8 cov.stop() 

9 cov.save() 

10atexit.register(exit_handler) 

11####### End of coverage stub 

12#!/usr/bin/python3 

13 

14# Copyright 2020 Google LLC 

15# 

16# Licensed under the Apache License, Version 2.0 (the "License"); 

17# you may not use this file except in compliance with the License. 

18# You may obtain a copy of the License at 

19# 

20# http://www.apache.org/licenses/LICENSE-2.0 

21# 

22# Unless required by applicable law or agreed to in writing, software 

23# distributed under the License is distributed on an "AS IS" BASIS, 

24# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

25# See the License for the specific language governing permissions and 

26# limitations under the License. 

27"""This fuzzer is an example of native extension fuzzing with coverage. 

28 

29This requires that the fuzzer be built with coverage: 

30see build_install_ujson.sh. 

31The fuzzer should then be executed under ASAN. 

32 

33As an example, this is the run command under the author's machine: 

34 LD_PRELOAD="/usr/lib/llvm-9/lib/clang/9.0.1/lib/linux/libclang_rt.asan-x86_64.so 

35 $(python3 -c "import atheris; print(atheris.path())")" python3 

36 ./ujson_fuzzer.py -detect_leaks=0 

37 

38This fuzzer is provided mainly as an example for how to deal with native 

39coverage. 

40""" 

41 

42import sys 

43import atheris 

44import ujson 

45 

46 

47def TestOneInput(input_bytes): 

48 fdp = atheris.FuzzedDataProvider(input_bytes) 

49 original = fdp.ConsumeUnicode(sys.maxsize) 

50 

51 try: 

52 ujson_data = ujson.loads(original) 

53 except ValueError: 

54 return 

55 

56 # We make sure there's no error in encoding, but we don't actually compare 

57 # (encoded == original) because it's not entirely preserving. For example, 

58 # it does not preserve whitespace. 

59 encoded = ujson.dumps(ujson_data) 

60 del encoded 

61 

62 

63def main(): 

64 # Since everything interesting in this fuzzer is in native code, we can 

65 # disable Python coverage to improve performance and reduce coverage noise. 

66 atheris.Setup(sys.argv, TestOneInput, enable_python_coverage=False) 

67 atheris.Fuzz() 

68 

69 

70if __name__ == "__main__": 

71 main()