2015-08-03 20:23:44 +00:00
|
|
|
// Copyright 2015 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// Only build when go-fuzz is in use
|
2021-08-25 14:35:30 +00:00
|
|
|
//go:build gofuzz
|
2015-07-29 20:32:02 +00:00
|
|
|
|
2015-08-03 20:23:44 +00:00
|
|
|
package promql
|
|
|
|
|
2019-08-12 18:47:57 +00:00
|
|
|
import (
|
2022-06-08 08:47:52 +00:00
|
|
|
"errors"
|
2019-08-12 18:47:57 +00:00
|
|
|
"io"
|
|
|
|
|
2023-11-29 18:19:48 +00:00
|
|
|
"github.com/prometheus/prometheus/model/labels"
|
2021-11-08 14:23:17 +00:00
|
|
|
"github.com/prometheus/prometheus/model/textparse"
|
2020-02-03 20:02:45 +00:00
|
|
|
"github.com/prometheus/prometheus/promql/parser"
|
2019-08-12 18:47:57 +00:00
|
|
|
)
|
2017-07-07 08:29:38 +00:00
|
|
|
|
2015-08-03 20:23:44 +00:00
|
|
|
// PromQL parser fuzzing instrumentation for use with
|
|
|
|
// https://github.com/dvyukov/go-fuzz.
|
|
|
|
//
|
|
|
|
// Fuzz each parser by building appropriately instrumented parser, ex.
|
|
|
|
// FuzzParseMetric and execute it with it's
|
|
|
|
//
|
|
|
|
// go-fuzz-build -func FuzzParseMetric -o FuzzParseMetric.zip github.com/prometheus/prometheus/promql
|
|
|
|
//
|
|
|
|
// And then run the tests with the appropriate inputs
|
|
|
|
//
|
2020-02-03 18:23:07 +00:00
|
|
|
// go-fuzz -bin FuzzParseMetric.zip -workdir fuzz-data/ParseMetric
|
2015-08-03 20:23:44 +00:00
|
|
|
//
|
2020-02-03 18:23:07 +00:00
|
|
|
// Further input samples should go in the folders fuzz-data/ParseMetric/corpus.
|
2015-08-03 20:23:44 +00:00
|
|
|
//
|
2019-08-12 18:47:57 +00:00
|
|
|
// Repeat for FuzzParseOpenMetric, FuzzParseMetricSelector and FuzzParseExpr.
|
2015-07-29 20:32:02 +00:00
|
|
|
|
2015-08-03 20:23:44 +00:00
|
|
|
// Tuning which value is returned from Fuzz*-functions has a strong influence
|
|
|
|
// on how quick the fuzzer converges on "interesting" cases. At least try
|
|
|
|
// switching between fuzzMeh (= included in corpus, but not a priority) and
|
|
|
|
// fuzzDiscard (=don't use this input for re-building later inputs) when
|
|
|
|
// experimenting.
|
2015-07-29 20:32:02 +00:00
|
|
|
const (
|
2015-08-03 20:23:44 +00:00
|
|
|
fuzzInteresting = 1
|
|
|
|
fuzzMeh = 0
|
|
|
|
fuzzDiscard = -1
|
2020-05-31 07:42:56 +00:00
|
|
|
|
|
|
|
// Input size above which we know that Prometheus would consume too much
|
|
|
|
// memory. The recommended way to deal with it is check input size.
|
|
|
|
// https://google.github.io/oss-fuzz/getting-started/new-project-guide/#input-size
|
|
|
|
maxInputSize = 10240
|
2015-07-29 20:32:02 +00:00
|
|
|
)
|
|
|
|
|
2023-11-29 18:19:48 +00:00
|
|
|
// Use package-scope symbol table to avoid memory allocation on every fuzzing operation.
|
|
|
|
var symbolTable = labels.NewSymbolTable()
|
|
|
|
|
2019-08-12 18:47:57 +00:00
|
|
|
func fuzzParseMetricWithContentType(in []byte, contentType string) int {
|
2024-10-18 15:12:31 +00:00
|
|
|
p, warning := textparse.New(in, contentType, "", false, false, symbolTable)
|
|
|
|
if p == nil || warning != nil {
|
2022-02-08 09:57:56 +00:00
|
|
|
// An invalid content type is being passed, which should not happen
|
|
|
|
// in this context.
|
|
|
|
panic(warning)
|
|
|
|
}
|
|
|
|
|
2019-08-12 18:47:57 +00:00
|
|
|
var err error
|
|
|
|
for {
|
|
|
|
_, err = p.Next()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2022-06-08 08:47:52 +00:00
|
|
|
if errors.Is(err, io.EOF) {
|
2019-08-12 18:47:57 +00:00
|
|
|
err = nil
|
2017-07-07 08:29:38 +00:00
|
|
|
}
|
|
|
|
|
2019-08-12 18:47:57 +00:00
|
|
|
if err == nil {
|
2015-08-03 20:23:44 +00:00
|
|
|
return fuzzInteresting
|
|
|
|
}
|
2015-07-29 20:32:02 +00:00
|
|
|
|
2016-05-10 09:46:03 +00:00
|
|
|
return fuzzMeh
|
2015-07-29 20:32:02 +00:00
|
|
|
}
|
|
|
|
|
2019-08-12 18:47:57 +00:00
|
|
|
// Fuzz the metric parser.
|
|
|
|
//
|
|
|
|
// Note that this is not the parser for the text-based exposition-format; that
|
|
|
|
// lives in github.com/prometheus/client_golang/text.
|
|
|
|
func FuzzParseMetric(in []byte) int {
|
2024-10-18 15:12:31 +00:00
|
|
|
return fuzzParseMetricWithContentType(in, "text/plain")
|
2019-08-12 18:47:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func FuzzParseOpenMetric(in []byte) int {
|
|
|
|
return fuzzParseMetricWithContentType(in, "application/openmetrics-text")
|
|
|
|
}
|
|
|
|
|
2015-08-03 20:23:44 +00:00
|
|
|
// Fuzz the metric selector parser.
|
2015-07-29 20:32:02 +00:00
|
|
|
func FuzzParseMetricSelector(in []byte) int {
|
2020-05-31 07:42:56 +00:00
|
|
|
if len(in) > maxInputSize {
|
|
|
|
return fuzzMeh
|
|
|
|
}
|
2020-02-03 17:00:41 +00:00
|
|
|
_, err := parser.ParseMetricSelector(string(in))
|
2015-08-03 20:23:44 +00:00
|
|
|
if err == nil {
|
|
|
|
return fuzzInteresting
|
|
|
|
}
|
2015-07-29 20:32:02 +00:00
|
|
|
|
2016-05-10 09:46:03 +00:00
|
|
|
return fuzzMeh
|
2015-07-29 20:32:02 +00:00
|
|
|
}
|
|
|
|
|
2015-08-03 20:23:44 +00:00
|
|
|
// Fuzz the expression parser.
|
2015-07-29 20:32:02 +00:00
|
|
|
func FuzzParseExpr(in []byte) int {
|
2020-05-31 07:42:56 +00:00
|
|
|
if len(in) > maxInputSize {
|
|
|
|
return fuzzMeh
|
|
|
|
}
|
2020-02-03 17:00:41 +00:00
|
|
|
_, err := parser.ParseExpr(string(in))
|
2015-08-03 20:23:44 +00:00
|
|
|
if err == nil {
|
|
|
|
return fuzzInteresting
|
|
|
|
}
|
2015-07-29 20:32:02 +00:00
|
|
|
|
2016-05-10 09:46:03 +00:00
|
|
|
return fuzzMeh
|
2015-07-29 20:32:02 +00:00
|
|
|
}
|