| Dmytro Shteflyuk | 6041798 | 2026-03-14 02:12:29 -0400 | [diff] [blame^] | 1 | # |
| 2 | # Licensed to the Apache Software Foundation (ASF) under one |
| 3 | # or more contributor license agreements. See the NOTICE file |
| 4 | # distributed with this work for additional information |
| 5 | # regarding copyright ownership. The ASF licenses this file |
| 6 | # to you under the Apache License, Version 2.0 (the |
| 7 | # "License"); you may not use this file except in compliance |
| 8 | # with the License. You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, |
| 13 | # software distributed under the License is distributed on an |
| 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | # KIND, either express or implied. See the License for the |
| 16 | # specific language governing permissions and limitations |
| 17 | # under the License. |
| 18 | # |
| 19 | |
| 20 | $:.unshift File.expand_path('../../lib', __dir__) |
| 21 | $:.unshift File.expand_path('../../ext', __dir__) |
| 22 | require 'thrift' |
| 23 | $:.unshift File.dirname(__FILE__) + "/gen-rb" |
| 24 | require 'fuzz_test_constants' |
| 25 | |
| 26 | require 'coverage' |
| 27 | Coverage.start(branches: true) unless Coverage.respond_to?(:running?) && Coverage.running? |
| 28 | require 'ruzzy' |
| 29 | # Ruzzy.enable_branch_coverage_hooks |
| 30 | |
| 31 | def ignorable_fuzz_exception?(error) |
| 32 | return true if error.is_a?(Thrift::ProtocolException) || |
| 33 | error.is_a?(EOFError) || |
| 34 | error.is_a?(Encoding::UndefinedConversionError) |
| 35 | |
| 36 | [ |
| 37 | /don't know what (?:c)?type/, |
| 38 | /Too many fields for union/, |
| 39 | /too big to convert to '(?:int|long)'/, |
| 40 | /bignum too big to convert into 'long'/, |
| 41 | /negative array size/, |
| 42 | /Union fields are not set/ |
| 43 | ].any? { |pattern| error.message =~ pattern } |
| 44 | end |
| 45 | |
| 46 | def read_fuzz_test(protocol, read_message_begin) |
| 47 | protocol.read_message_begin if read_message_begin |
| 48 | obj = Fuzz::FuzzTest.new |
| 49 | obj.read(protocol) |
| 50 | protocol.read_message_end if read_message_begin |
| 51 | obj |
| 52 | end |
| 53 | |
| 54 | def write_fuzz_test(protocol, obj, write_message_begin) |
| 55 | if write_message_begin |
| 56 | protocol.write_message_begin('fuzz', Thrift::MessageTypes::CALL, 0) |
| 57 | end |
| 58 | obj.write(protocol) |
| 59 | protocol.write_message_end if write_message_begin |
| 60 | end |
| 61 | |
| 62 | def create_parser_fuzzer(protocol_factory_class, read_message_begin: false) |
| 63 | lambda do |data| |
| 64 | transport = Thrift::MemoryBufferTransport.new(data) |
| 65 | protocol = protocol_factory_class.new.get_protocol(transport) |
| 66 | read_fuzz_test(protocol, read_message_begin) |
| 67 | 0 |
| 68 | rescue StandardError => e |
| 69 | # We're looking for memory corruption, not Ruby exceptions |
| 70 | raise unless ignorable_fuzz_exception?(e) |
| 71 | end |
| 72 | end |
| 73 | |
| 74 | def create_roundtrip_fuzzer(protocol_factory_class, read_message_begin: false) |
| 75 | lambda do |data| |
| 76 | transport = Thrift::MemoryBufferTransport.new(data) |
| 77 | protocol = protocol_factory_class.new.get_protocol(transport) |
| 78 | obj = read_fuzz_test(protocol, read_message_begin) |
| 79 | |
| 80 | serialized_data = +"" |
| 81 | transport2 = Thrift::MemoryBufferTransport.new(serialized_data) |
| 82 | protocol2 = protocol_factory_class.new.get_protocol(transport2) |
| 83 | write_fuzz_test(protocol2, obj, read_message_begin) |
| 84 | |
| 85 | transport3 = Thrift::MemoryBufferTransport.new(serialized_data) |
| 86 | protocol3 = protocol_factory_class.new.get_protocol(transport3) |
| 87 | deserialized_obj = read_fuzz_test(protocol3, read_message_begin) |
| 88 | |
| 89 | raise "Roundtrip mismatch" unless obj == deserialized_obj |
| 90 | 0 |
| 91 | rescue StandardError => e |
| 92 | # We're looking for memory corruption, not Ruby exceptions |
| 93 | raise unless ignorable_fuzz_exception?(e) |
| 94 | end |
| 95 | end |