blob: ed28e7c7b1725a20410352fe5a1b4840d676a745 [file] [log] [blame]
Dmytro Shteflyuk60417982026-03-14 02:12:29 -04001#
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__)
22require 'thrift'
23$:.unshift File.dirname(__FILE__) + "/gen-rb"
24require 'fuzz_test_constants'
25
26require 'coverage'
27Coverage.start(branches: true) unless Coverage.respond_to?(:running?) && Coverage.running?
28require 'ruzzy'
29# Ruzzy.enable_branch_coverage_hooks
30
31def 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 }
44end
45
46def 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
52end
53
54def 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
60end
61
62def 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
72end
73
74def 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
95end