github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/configs/legacy_promql/fuzz.go (about) 1 // Copyright 2015 The Prometheus Authors 2 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // you may not use this file except in compliance with the License. 4 // You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 // Only build when go-fuzz is in use 15 //go:build gofuzz 16 // +build gofuzz 17 18 package promql 19 20 import "github.com/prometheus/prometheus/pkg/textparse" 21 22 // PromQL parser fuzzing instrumentation for use with 23 // https://github.com/dvyukov/go-fuzz. 24 // 25 // Fuzz each parser by building appropriately instrumented parser, ex. 26 // FuzzParseMetric and execute it with it's 27 // 28 // go-fuzz-build -func FuzzParseMetric -o FuzzParseMetric.zip github.com/prometheus/prometheus/promql 29 // 30 // And then run the tests with the appropriate inputs 31 // 32 // go-fuzz -bin FuzzParseMetric.zip -workdir fuzz-data/ParseMetric 33 // 34 // Further input samples should go in the folders fuzz-data/ParseMetric/corpus. 35 // 36 // Repeat for ParseMetricSeletion, ParseExpr and ParseStmt. 37 38 // Tuning which value is returned from Fuzz*-functions has a strong influence 39 // on how quick the fuzzer converges on "interesting" cases. At least try 40 // switching between fuzzMeh (= included in corpus, but not a priority) and 41 // fuzzDiscard (=don't use this input for re-building later inputs) when 42 // experimenting. 43 const ( 44 fuzzInteresting = 1 45 fuzzMeh = 0 46 fuzzDiscard = -1 47 ) 48 49 // Fuzz the metric parser. 50 // 51 // Note that his is not the parser for the text-based exposition-format; that 52 // lives in github.com/prometheus/client_golang/text. 53 func FuzzParseMetric(in []byte) int { 54 p := textparse.New(in) 55 for p.Next() { 56 } 57 58 if p.Err() == nil { 59 return fuzzInteresting 60 } 61 62 return fuzzMeh 63 } 64 65 // Fuzz the metric selector parser. 66 func FuzzParseMetricSelector(in []byte) int { 67 _, err := ParseMetricSelector(string(in)) 68 if err == nil { 69 return fuzzInteresting 70 } 71 72 return fuzzMeh 73 } 74 75 // Fuzz the expression parser. 76 func FuzzParseExpr(in []byte) int { 77 _, err := ParseExpr(string(in)) 78 if err == nil { 79 return fuzzInteresting 80 } 81 82 return fuzzMeh 83 } 84 85 // Fuzz the parser. 86 func FuzzParseStmts(in []byte) int { 87 _, err := ParseStmts(string(in)) 88 if err == nil { 89 return fuzzInteresting 90 } 91 92 return fuzzMeh 93 }