Skip to content

Commit

Permalink
module/apmsql: fuzz signature logic
Browse files Browse the repository at this point in the history
And fix existing fuzz tests.
  • Loading branch information
axw committed Jul 4, 2018
1 parent e8af3f6 commit 8c8e283
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
11 changes: 8 additions & 3 deletions gofuzz.go
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/elastic/apm-agent-go/stacktrace"
"github.com/elastic/apm-server/processor"
error_processor "github.com/elastic/apm-server/processor/error"
metric_processor "github.com/elastic/apm-server/processor/metric"
transaction_processor "github.com/elastic/apm-server/processor/transaction"
)

Expand Down Expand Up @@ -129,9 +130,6 @@ func Fuzz(data []byte) int {
return 0
}
for _, s := range t.Spans {
if s == nil {
continue
}
span := tx.StartSpan(s.Name, s.Type, nil)
span.Timestamp = tx.Timestamp.Add(time.Duration(s.Start * float64(time.Millisecond)))
if s.Context != nil && s.Context.Database != nil {
Expand Down Expand Up @@ -232,6 +230,13 @@ func (t *gofuzzTransport) SendErrors(ctx context.Context, payload *model.ErrorsP
return nil
}

func (t *gofuzzTransport) SendMetrics(ctx context.Context, payload *model.MetricsPayload) error {
t.writer.Reset()
payload.MarshalFastJSON(&t.writer)
t.process(metric_processor.NewProcessor())
return nil
}

func (t *gofuzzTransport) SendTransactions(ctx context.Context, payload *model.TransactionsPayload) error {
t.writer.Reset()
payload.MarshalFastJSON(&t.writer)
Expand Down
10 changes: 5 additions & 5 deletions model/gofuzz.go
Expand Up @@ -13,11 +13,11 @@ import (

func Fuzz(data []byte) int {
type Payload struct {
Service *Service `json:"service"`
Process *Process `json:"process,omitempty"`
System *System `json:"system,omitempty"`
Errors []*Error `json:"errors"`
Transactions []*Transaction `json:"transactions"`
Service *Service `json:"service"`
Process *Process `json:"process,omitempty"`
System *System `json:"system,omitempty"`
Errors []*Error `json:"errors"`
Transactions []Transaction `json:"transactions"`
}

var payload Payload
Expand Down
30 changes: 30 additions & 0 deletions module/apmsql/gofuzz_signature.go
@@ -0,0 +1,30 @@
// +build gofuzz

package apmsql

import "strings"

func Fuzz(data []byte) int {
sql := string(data)
sig := genericQuerySignature(sql)
if sig == "" {
return -1
}
prefixes := [...]string{
"CALL ",
"DELETE FROM ",
"INSERT INTO ",
"REPLACE INTO ",
"SELECT FROM ",
"UPDATE ",
}
for _, p := range prefixes {
if strings.HasPrefix(sig, p) {
// Give priority to input that is parsed
// successfully, and doesn't just result
// in the fallback.
return 1
}
}
return 0
}

0 comments on commit 8c8e283

Please sign in to comment.