Skip to content

Commit

Permalink
Use pointer for arg key in cache key (#553)
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraaga authored Dec 27, 2022
1 parent a40315b commit 8bad405
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
7 changes: 6 additions & 1 deletion internal/corazawaf/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ package corazawaf

import (
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"sync"
"unsafe"

"github.com/corazawaf/coraza/v3/internal/corazarules"
"github.com/corazawaf/coraza/v3/macro"
Expand Down Expand Up @@ -303,8 +305,11 @@ func (r *Rule) transformArg(arg types.MatchData, argIdx int, cache map[transform
arg, errs := r.executeTransformations(arg.Value())
return []string{arg}, errs
default:
// NOTE: See comment on transformationKey struct to understand this hacky code
argKey := arg.Key()
argKeyPtr := (*reflect.StringHeader)(unsafe.Pointer(&argKey)).Data
key := transformationKey{
argKey: arg.Key(),
argKey: argKeyPtr,
argIndex: argIdx,
argVariable: arg.Variable(),
transformationsID: r.transformationsID,
Expand Down
9 changes: 8 additions & 1 deletion internal/corazawaf/rulegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,14 @@ func NewRuleGroup() RuleGroup {
}

type transformationKey struct {
argKey string
// TODO(anuraaga): This is a big hack to support performance on TinyGo. TinyGo
// cannot efficiently compute a hashcode for a struct if it has embedded non-fixed
// size fields, for example string as we'd prefer to use here. A pointer is usable,
// and it works for us since we know that the arg key string is populated once per
// transaction phase and we would never have different string pointers with the same
// content, or more problematically same pointer for different content, as the strings
// will be alive throughout the phase.
argKey uintptr
argIndex int
argVariable variables.RuleVariable
transformationsID int
Expand Down

0 comments on commit 8bad405

Please sign in to comment.