Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pointer for arg key in cache key #553

Merged
merged 2 commits into from
Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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