Coverage for /pythoncovmergedfiles/medio/medio/src/irsb_fuzzer.py: 55%

51 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-09-25 06:15 +0000

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# Copyright 2023 Google LLC 

14# 

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

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

17# You may obtain a copy of the License at 

18# 

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

20# 

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

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

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

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

25# limitations under the License. 

26# 

27################################################################################ 

28import sys 

29from enum import IntEnum 

30 

31import atheris 

32 

33with atheris.instrument_imports(include=["pyvex"]): 

34 import pyvex 

35 

36# Additional imports 

37import archinfo 

38from contextlib import contextmanager 

39from io import StringIO 

40 

41from enhanced_fdp import EnhancedFuzzedDataProvider 

42 

43 

44@contextmanager 

45def nostdout(): 

46 saved_stdout = sys.stdout 

47 saved_stderr = sys.stderr 

48 sys.stdout = StringIO() 

49 sys.stderr = StringIO() 

50 yield 

51 sys.stdout = saved_stdout 

52 sys.stderr = saved_stderr 

53 

54 

55# Save all available architectures off 

56available_archs = [tup[3]() for tup in archinfo.arch.arch_id_map if len(tup) >= 3] 

57 

58 

59class SupportedOptLevels(IntEnum): 

60 StrictUnopt = -1 

61 Unopt = 0 

62 Opt = 1 

63 StrictOpt = 2 

64 

65 

66def consume_random_arch(fdp: atheris.FuzzedDataProvider) -> archinfo.Arch: 

67 return fdp.PickValueInList(available_archs) 

68 

69 

70def TestOneInput(data: bytes): 

71 fdp = EnhancedFuzzedDataProvider(data) 

72 

73 arch = consume_random_arch(fdp) 

74 

75 try: 

76 with nostdout(): 

77 data = fdp.ConsumeRandomBytes() 

78 

79 irsb = pyvex.lift( 

80 data, 

81 fdp.ConsumeInt(arch.bits), 

82 arch, 

83 max_bytes=fdp.ConsumeIntInRange(0, len(data)), 

84 max_inst=fdp.ConsumeInt(16), 

85 bytes_offset=fdp.ConsumeIntInRange(0, len(data)), 

86 opt_level=fdp.PickValueInEnum(SupportedOptLevels) 

87 ) 

88 irsb.pp() 

89 except pyvex.PyVEXError: 

90 return -1 

91 except OverflowError: 

92 return -1 

93 

94 

95def main(): 

96 atheris.Setup(sys.argv, TestOneInput) 

97 atheris.Fuzz() 

98 

99 

100if __name__ == "__main__": 

101 main()