Philippe Antoine | 65ea752 | 2021-03-15 09:34:58 +0100 | [diff] [blame^] | 1 | // +build gofuzz |
| 2 | |
| 3 | /* |
| 4 | * Licensed to the Apache Software Foundation (ASF) under one |
| 5 | * or more contributor license agreements. See the NOTICE file |
| 6 | * distributed with this work for additional information |
| 7 | * regarding copyright ownership. The ASF licenses this file |
| 8 | * to you under the Apache License, Version 2.0 (the |
| 9 | * "License"); you may not use this file except in compliance |
| 10 | * with the License. You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, |
| 15 | * software distributed under the License is distributed on an |
| 16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | * KIND, either express or implied. See the License for the |
| 18 | * specific language governing permissions and limitations |
| 19 | * under the License. |
| 20 | */ |
| 21 | |
| 22 | package fuzz |
| 23 | |
| 24 | import ( |
| 25 | "context" |
| 26 | "fmt" |
| 27 | "shared" |
| 28 | "strconv" |
| 29 | "tutorial" |
| 30 | |
| 31 | "github.com/apache/thrift/lib/go/thrift" |
| 32 | ) |
| 33 | |
| 34 | const nbFuzzedProtocols = 2 |
| 35 | |
| 36 | func fuzzChooseProtocol(d byte, t thrift.TTransport) thrift.TProtocol { |
| 37 | switch d % nbFuzzedProtocols { |
| 38 | default: |
| 39 | fallthrough |
| 40 | case 0: |
| 41 | return thrift.NewTBinaryProtocolFactoryConf(nil).GetProtocol(t) |
| 42 | case 1: |
| 43 | return thrift.NewTCompactProtocolFactoryConf(nil).GetProtocol(t) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func Fuzz(data []byte) int { |
| 48 | if len(data) < 2 { |
| 49 | return 0 |
| 50 | } |
| 51 | inputTransport := thrift.NewTMemoryBuffer() |
| 52 | inputTransport.Buffer.Write(data[2:]) |
| 53 | outputTransport := thrift.NewTMemoryBuffer() |
| 54 | outputProtocol := fuzzChooseProtocol(data[0], outputTransport) |
| 55 | inputProtocol := fuzzChooseProtocol(data[1], inputTransport) |
| 56 | ctx := thrift.SetResponseHelper( |
| 57 | context.Background(), |
| 58 | thrift.TResponseHelper{ |
| 59 | THeaderResponseHelper: thrift.NewTHeaderResponseHelper(outputProtocol), |
| 60 | }, |
| 61 | ) |
| 62 | handler := NewCalculatorHandler() |
| 63 | processor := tutorial.NewCalculatorProcessor(handler) |
| 64 | ok := true |
| 65 | var err error |
| 66 | for ok { |
| 67 | ok, err = processor.Process(ctx, inputProtocol, outputProtocol) |
| 68 | if err != nil { |
| 69 | // Handle parse error |
| 70 | return 0 |
| 71 | } |
| 72 | res := make([]byte, 1024) |
| 73 | n, err := outputTransport.Buffer.Read(res) |
| 74 | fmt.Printf("lol %d %s %v\n", n, err, res) |
| 75 | } |
| 76 | return 1 |
| 77 | } |
| 78 | |
| 79 | type CalculatorHandler struct { |
| 80 | log map[int]*shared.SharedStruct |
| 81 | } |
| 82 | |
| 83 | func NewCalculatorHandler() *CalculatorHandler { |
| 84 | return &CalculatorHandler{log: make(map[int]*shared.SharedStruct)} |
| 85 | } |
| 86 | |
| 87 | func (p *CalculatorHandler) Ping(ctx context.Context) (err error) { |
| 88 | fmt.Print("ping()\n") |
| 89 | return nil |
| 90 | } |
| 91 | |
| 92 | func (p *CalculatorHandler) Add(ctx context.Context, num1 int32, num2 int32) (retval17 int32, err error) { |
| 93 | fmt.Print("add(", num1, ",", num2, ")\n") |
| 94 | return num1 + num2, nil |
| 95 | } |
| 96 | |
| 97 | func (p *CalculatorHandler) Calculate(ctx context.Context, logid int32, w *tutorial.Work) (val int32, err error) { |
| 98 | fmt.Print("calculate(", logid, ", {", w.Op, ",", w.Num1, ",", w.Num2, "})\n") |
| 99 | switch w.Op { |
| 100 | case tutorial.Operation_ADD: |
| 101 | val = w.Num1 + w.Num2 |
| 102 | break |
| 103 | case tutorial.Operation_SUBTRACT: |
| 104 | val = w.Num1 - w.Num2 |
| 105 | break |
| 106 | case tutorial.Operation_MULTIPLY: |
| 107 | val = w.Num1 * w.Num2 |
| 108 | break |
| 109 | case tutorial.Operation_DIVIDE: |
| 110 | if w.Num2 == 0 { |
| 111 | ouch := tutorial.NewInvalidOperation() |
| 112 | ouch.WhatOp = int32(w.Op) |
| 113 | ouch.Why = "Cannot divide by 0" |
| 114 | err = ouch |
| 115 | return |
| 116 | } |
| 117 | val = w.Num1 / w.Num2 |
| 118 | break |
| 119 | default: |
| 120 | ouch := tutorial.NewInvalidOperation() |
| 121 | ouch.WhatOp = int32(w.Op) |
| 122 | ouch.Why = "Unknown operation" |
| 123 | err = ouch |
| 124 | return |
| 125 | } |
| 126 | entry := shared.NewSharedStruct() |
| 127 | entry.Key = logid |
| 128 | entry.Value = strconv.Itoa(int(val)) |
| 129 | k := int(logid) |
| 130 | p.log[k] = entry |
| 131 | return val, err |
| 132 | } |
| 133 | |
| 134 | func (p *CalculatorHandler) GetStruct(ctx context.Context, key int32) (*shared.SharedStruct, error) { |
| 135 | fmt.Print("getStruct(", key, ")\n") |
| 136 | v, _ := p.log[int(key)] |
| 137 | return v, nil |
| 138 | } |
| 139 | |
| 140 | func (p *CalculatorHandler) Zip(ctx context.Context) (err error) { |
| 141 | fmt.Print("zip()\n") |
| 142 | return nil |
| 143 | } |