sfvm

package
v1.3.4-alpha11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 8, 2024 License: AGPL-3.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CompareStringAnyMode  int = 0
	CompareStringHaveMode     = 1
)
View Source
const (
	RecursiveConfig_NULL     RecursiveConfigKey = ""
	RecursiveConfig_Depth                       = "depth"
	RecursiveConfig_DepthMin                    = "depth_min"
	RecursiveConfig_DepthMax                    = "depth_max"
	RecursiveConfig_Exclude                     = "exclude"
	RecursiveConfig_Until                       = "until"
)
View Source
const (
	NameMatch int = 1
	KeyMatch      = 1 << 1
	BothMatch     = NameMatch | KeyMatch
)

type MatchMode int

Variables

View Source
var CriticalError = utils.Error("CriticalError(BUG)")

Functions

func MatchModeString

func MatchModeString(mode int) string

func RegisterNativeCall

func RegisterNativeCall(name string, f NativeCallFunc)

Types

type AnalysisContext

type AnalysisContext struct {
	Step  int
	Label string
}

func NewDefaultAnalysisContext

func NewDefaultAnalysisContext() *AnalysisContext

type AnalysisContextOption

type AnalysisContextOption func(*AnalysisContext)

func WithAnalysisContext_Label

func WithAnalysisContext_Label(label string) AnalysisContextOption

func WithAnalysisContext_Step

func WithAnalysisContext_Step(step int) AnalysisContextOption

type Config added in v1.3.3

type Config struct {
	StrictMatch bool
	FailFast    bool
	// contains filtered or unexported fields
}

func NewConfig added in v1.3.3

func NewConfig(opts ...Option) *Config

type GlobEx added in v1.3.3

type GlobEx struct {
	Origin glob.Glob
	Rule   string
}

func (*GlobEx) Match added in v1.3.3

func (g *GlobEx) Match(d string) bool

func (*GlobEx) String added in v1.3.3

func (g *GlobEx) String() string

type IterContext

type IterContext struct {
	// contains filtered or unexported fields
}

type NativeCallFunc

type NativeCallFunc func(v ValueOperator, frame *SFFrame) (bool, ValueOperator, error)

func GetNativeCall

func GetNativeCall(name string) (NativeCallFunc, error)

type Option added in v1.3.3

type Option func(*Config)

func WithEnableDebug added in v1.3.3

func WithEnableDebug(b ...bool) Option

func WithFailFast

func WithFailFast(b ...bool) Option

func WithInitialContextVars added in v1.3.3

func WithInitialContextVars(o *omap.OrderedMap[string, ValueOperator]) Option

func WithResultCaptured added in v1.3.3

func WithResultCaptured(c ResultCapturedCallback) Option

func WithStrictMatch

func WithStrictMatch(b bool) Option

type RecursiveConfigItem added in v1.3.3

type RecursiveConfigItem struct {
	Key            RecursiveConfigKey
	Value          string
	SyntaxFlowRule bool
}

type RecursiveConfigKey added in v1.3.3

type RecursiveConfigKey string

func FormatRecursiveConfigKey added in v1.3.3

func FormatRecursiveConfigKey(i string) RecursiveConfigKey

type ResultCapturedCallback added in v1.3.3

type ResultCapturedCallback func(name string, results ValueOperator) error

type SFFrame

type SFFrame struct {
	Text  string
	Codes []*SFI
	// contains filtered or unexported fields
}

func NewSFFrame

func NewSFFrame(vars *omap.OrderedMap[string, ValueOperator], text string, codes []*SFI) *SFFrame

func (*SFFrame) Feed

func (frame *SFFrame) Feed(i ValueOperator) (*SFFrameResult, error)

func (*SFFrame) Flush

func (s *SFFrame) Flush()

func (*SFFrame) GetSymbolTable

func (s *SFFrame) GetSymbolTable() *omap.OrderedMap[string, ValueOperator]

func (*SFFrame) Show

func (f *SFFrame) Show()

func (*SFFrame) ToLeft

func (s *SFFrame) ToLeft() bool

func (*SFFrame) ToRight

func (s *SFFrame) ToRight() bool

func (*SFFrame) WithPredecessorContext

func (s *SFFrame) WithPredecessorContext(label string) AnalysisContextOption

type SFFrameResult

type SFFrameResult struct {
	// base info
	Rule string
	// additional info
	Description *omap.OrderedMap[string, string]
	CheckParams []string
	Errors      []string
	// value
	SymbolTable      *omap.OrderedMap[string, ValueOperator]
	AlertSymbolTable map[string]ValueOperator
	AlertMsgTable    map[string]string
}

func NewSFResult

func NewSFResult(rule string) *SFFrameResult

func (*SFFrameResult) Copy

func (s *SFFrameResult) Copy() *SFFrameResult

func (*SFFrameResult) Show

func (s *SFFrameResult) Show()

func (*SFFrameResult) String

func (s *SFFrameResult) String() string

type SFI

type SFI struct {
	OpCode           SFVMOpCode
	UnaryInt         int
	UnaryStr         string
	Desc             string
	Values           []string
	SyntaxFlowConfig []*RecursiveConfigItem
	// contains filtered or unexported fields
}

func (*SFI) IsIterOpcode

func (s *SFI) IsIterOpcode() bool

func (*SFI) String

func (s *SFI) String() string

func (*SFI) ValueByIndex

func (s *SFI) ValueByIndex(i int) string

type SFVMOpCode

type SFVMOpCode int
const (
	OpPass SFVMOpCode = iota

	// enter/exit statement
	OpEnterStatement
	OpExitStatement

	// duplicate the top of stack
	OpDuplicate

	// OpPushSearchExact can push data from origin
	OpPushSearchExact
	OpPushSearchGlob
	OpPushSearchRegexp

	// OpRecursive... can fetch origin value (not program) push data from origin
	OpRecursiveSearchExact
	OpRecursiveSearchGlob
	OpRecursiveSearchRegexp

	// handle function call
	OpGetCall
	OpGetCallArgs
	OpGetAllCallArgs

	// use def chain
	OpGetUsers
	OpGetBottomUsers
	OpGetDefs
	OpGetTopDefs

	// ListOperation
	OpListIndex

	// => variable
	OpNewRef
	OpUpdateRef

	// OpPushNumber and OpPushString and OpPushBool can push literal into stack
	OpPushNumber
	OpPushString
	OpPushBool
	OpPop

	// Condition
	// use the []bool  && []Value of stack top, push result into stack
	OpCondition
	OpCompareOpcode
	OpCompareString

	/*
		Binary Operator
		Fetch TWO in STACK, calc result, push result into stack
	*/
	OpEq
	OpNotEq
	OpGt
	OpGtEq
	OpLt
	OpLtEq
	OpLogicAnd
	OpLogicOr
	OpLogicBang

	/*
		Unary Operator: Fetch ONE in STACK, calc result, push result into stack
	*/
	OpReMatch
	OpGlobMatch
	OpNot

	OpAlert // echo variable

	// OpCheckParams check the params in vm context
	// if not match, record error
	// matched, use 'then expr' (if exists)
	OpCheckParams

	// OpAddDescription add description to current context
	OpAddDescription

	// OpCreateIter will create iterator for current context
	// the context contains origin values(list) and channel for elements
	OpCreateIter
	// OpIterNext will get next value from iterator
	// if the channel from iter context has a next element, push into stack and execute filter
	// if not, exit
	OpIterNext
	OpIterEnd

	OpCheckStackTop // check the top of stack, if empty, push input into stack

	// OpFilterExprEnter will assert the top of stack, make sure a input
	OpFilterExprEnter
	OpFilterExprExit

	OpMergeRef
	OpRemoveRef

	OpNativeCall
)

type SyntaxFlowVirtualMachine

type SyntaxFlowVirtualMachine struct {
	// contains filtered or unexported fields
}

func NewSyntaxFlowVirtualMachine

func NewSyntaxFlowVirtualMachine(opts ...Option) *SyntaxFlowVirtualMachine

func (*SyntaxFlowVirtualMachine) Compile

func (s *SyntaxFlowVirtualMachine) Compile(text string) (frame *SFFrame, ret error)

func (*SyntaxFlowVirtualMachine) Debug

func (*SyntaxFlowVirtualMachine) Feed

func (*SyntaxFlowVirtualMachine) ForEachFrame added in v1.3.3

func (s *SyntaxFlowVirtualMachine) ForEachFrame(h func(frame *SFFrame))

func (*SyntaxFlowVirtualMachine) Show added in v1.3.3

func (s *SyntaxFlowVirtualMachine) Show()

func (*SyntaxFlowVirtualMachine) Snapshot

type SyntaxFlowVisitor

type SyntaxFlowVisitor struct {
	// contains filtered or unexported fields
}

func NewSyntaxFlowVisitor

func NewSyntaxFlowVisitor() *SyntaxFlowVisitor

func (*SyntaxFlowVisitor) CreateFrame

func (v *SyntaxFlowVisitor) CreateFrame(vars *omap.OrderedMap[string, ValueOperator]) *SFFrame

func (*SyntaxFlowVisitor) EmitAddDescription

func (y *SyntaxFlowVisitor) EmitAddDescription(key string, value string)

func (*SyntaxFlowVisitor) EmitAlert

func (y *SyntaxFlowVisitor) EmitAlert(ref string, value string)

func (*SyntaxFlowVisitor) EmitCheckParam

func (y *SyntaxFlowVisitor) EmitCheckParam(ref string, then string, elseString string)

func (*SyntaxFlowVisitor) EmitCheckStackTop

func (y *SyntaxFlowVisitor) EmitCheckStackTop()

func (*SyntaxFlowVisitor) EmitCompareOpcode

func (v *SyntaxFlowVisitor) EmitCompareOpcode(i []string)

func (*SyntaxFlowVisitor) EmitCompareString

func (v *SyntaxFlowVisitor) EmitCompareString(i []string, mode int)

func (*SyntaxFlowVisitor) EmitCondition

func (v *SyntaxFlowVisitor) EmitCondition()

func (*SyntaxFlowVisitor) EmitCreateIterator

func (v *SyntaxFlowVisitor) EmitCreateIterator() *IterContext

func (*SyntaxFlowVisitor) EmitDuplicate added in v1.3.3

func (v *SyntaxFlowVisitor) EmitDuplicate()

func (*SyntaxFlowVisitor) EmitEnterStatement added in v1.3.3

func (y *SyntaxFlowVisitor) EmitEnterStatement()

func (*SyntaxFlowVisitor) EmitEqual

func (v *SyntaxFlowVisitor) EmitEqual(i any)

func (*SyntaxFlowVisitor) EmitExitStatement added in v1.3.3

func (y *SyntaxFlowVisitor) EmitExitStatement()

func (*SyntaxFlowVisitor) EmitFilterExprEnter

func (y *SyntaxFlowVisitor) EmitFilterExprEnter() *SFI

func (*SyntaxFlowVisitor) EmitFilterExprExit

func (y *SyntaxFlowVisitor) EmitFilterExprExit(c *SFI)

func (*SyntaxFlowVisitor) EmitGetBottomUsers added in v1.3.3

func (v *SyntaxFlowVisitor) EmitGetBottomUsers(config ...*RecursiveConfigItem)

func (*SyntaxFlowVisitor) EmitGetCall added in v1.3.3

func (v *SyntaxFlowVisitor) EmitGetCall()

func (*SyntaxFlowVisitor) EmitGetDefs added in v1.3.3

func (v *SyntaxFlowVisitor) EmitGetDefs()

func (*SyntaxFlowVisitor) EmitGetTopDefs added in v1.3.3

func (v *SyntaxFlowVisitor) EmitGetTopDefs(config ...*RecursiveConfigItem)

func (*SyntaxFlowVisitor) EmitGetUsers added in v1.3.3

func (v *SyntaxFlowVisitor) EmitGetUsers()

func (*SyntaxFlowVisitor) EmitIterEnd

func (v *SyntaxFlowVisitor) EmitIterEnd(i *IterContext)

func (*SyntaxFlowVisitor) EmitListIndex added in v1.3.3

func (y *SyntaxFlowVisitor) EmitListIndex(i int)

func (*SyntaxFlowVisitor) EmitMergeRef

func (y *SyntaxFlowVisitor) EmitMergeRef(i string)

func (*SyntaxFlowVisitor) EmitNativaCall

func (y *SyntaxFlowVisitor) EmitNativaCall(i string, results ...any)

func (*SyntaxFlowVisitor) EmitNewRef

func (y *SyntaxFlowVisitor) EmitNewRef(i string)

func (*SyntaxFlowVisitor) EmitNextIterator

func (v *SyntaxFlowVisitor) EmitNextIterator(i *IterContext)

func (*SyntaxFlowVisitor) EmitOperator

func (y *SyntaxFlowVisitor) EmitOperator(i string)

func (*SyntaxFlowVisitor) EmitPass added in v1.3.3

func (v *SyntaxFlowVisitor) EmitPass()

func (*SyntaxFlowVisitor) EmitPop

func (y *SyntaxFlowVisitor) EmitPop()

func (*SyntaxFlowVisitor) EmitPushAllCallArgs added in v1.3.3

func (v *SyntaxFlowVisitor) EmitPushAllCallArgs()

func (*SyntaxFlowVisitor) EmitPushCallArgs added in v1.3.3

func (v *SyntaxFlowVisitor) EmitPushCallArgs(i int)

func (*SyntaxFlowVisitor) EmitPushGlob

func (v *SyntaxFlowVisitor) EmitPushGlob(i string)

func (*SyntaxFlowVisitor) EmitPushLiteral

func (v *SyntaxFlowVisitor) EmitPushLiteral(i any)

func (*SyntaxFlowVisitor) EmitRecursiveSearchExact

func (v *SyntaxFlowVisitor) EmitRecursiveSearchExact(mod int, i string) *SFI

func (*SyntaxFlowVisitor) EmitRecursiveSearchGlob

func (v *SyntaxFlowVisitor) EmitRecursiveSearchGlob(mod int, i string) *SFI

func (*SyntaxFlowVisitor) EmitRecursiveSearchRegexp

func (v *SyntaxFlowVisitor) EmitRecursiveSearchRegexp(mod int, i string) *SFI

func (*SyntaxFlowVisitor) EmitRegexpMatch

func (y *SyntaxFlowVisitor) EmitRegexpMatch(i string)

func (*SyntaxFlowVisitor) EmitRemoveRef

func (y *SyntaxFlowVisitor) EmitRemoveRef(i string)

func (*SyntaxFlowVisitor) EmitSearchExact added in v1.3.3

func (v *SyntaxFlowVisitor) EmitSearchExact(mod int, i string) *SFI

func (*SyntaxFlowVisitor) EmitSearchGlob added in v1.3.3

func (v *SyntaxFlowVisitor) EmitSearchGlob(mod int, i string) *SFI

func (*SyntaxFlowVisitor) EmitSearchRegexp added in v1.3.3

func (v *SyntaxFlowVisitor) EmitSearchRegexp(mod int, i string) *SFI

func (*SyntaxFlowVisitor) EmitUpdate

func (y *SyntaxFlowVisitor) EmitUpdate(i string)

func (*SyntaxFlowVisitor) FormatStringOrGlob

func (y *SyntaxFlowVisitor) FormatStringOrGlob(text string) (string, bool)

func (*SyntaxFlowVisitor) Show

func (v *SyntaxFlowVisitor) Show()

func (*SyntaxFlowVisitor) VisitActualParam added in v1.3.3

func (y *SyntaxFlowVisitor) VisitActualParam(i sf.IActualParamContext) error

func (*SyntaxFlowVisitor) VisitAlertStatement

func (y *SyntaxFlowVisitor) VisitAlertStatement(raw sf.IAlertStatementContext)

func (*SyntaxFlowVisitor) VisitChainFilter

func (y *SyntaxFlowVisitor) VisitChainFilter(raw sf.IChainFilterContext) interface{}

func (*SyntaxFlowVisitor) VisitCheckStatement

func (y *SyntaxFlowVisitor) VisitCheckStatement(raw sf.ICheckStatementContext) interface{}

func (*SyntaxFlowVisitor) VisitConditionExpression

func (y *SyntaxFlowVisitor) VisitConditionExpression(raw sf.IConditionExpressionContext) any

func (*SyntaxFlowVisitor) VisitDescriptionStatement

func (y *SyntaxFlowVisitor) VisitDescriptionStatement(raw sf.IDescriptionStatementContext) interface{}

func (*SyntaxFlowVisitor) VisitFilterExpr

func (y *SyntaxFlowVisitor) VisitFilterExpr(raw sf.IFilterExprContext) error

func (*SyntaxFlowVisitor) VisitFilterItem

func (y *SyntaxFlowVisitor) VisitFilterItem(raw sf.IFilterItemContext) error

func (*SyntaxFlowVisitor) VisitFilterItemFirst

func (y *SyntaxFlowVisitor) VisitFilterItemFirst(raw sf.IFilterItemFirstContext) error

func (*SyntaxFlowVisitor) VisitFilterStatement

func (y *SyntaxFlowVisitor) VisitFilterStatement(raw sf.IFilterStatementContext)

func (*SyntaxFlowVisitor) VisitFlow

func (y *SyntaxFlowVisitor) VisitFlow(raw sf.IFlowContext)

func (*SyntaxFlowVisitor) VisitNameFilter added in v1.3.3

func (y *SyntaxFlowVisitor) VisitNameFilter(isMember bool, i sf.INameFilterContext) error

func (*SyntaxFlowVisitor) VisitNumberLiteral

func (y *SyntaxFlowVisitor) VisitNumberLiteral(raw sf.INumberLiteralContext) int

func (*SyntaxFlowVisitor) VisitRecursiveConfig added in v1.3.3

func (v *SyntaxFlowVisitor) VisitRecursiveConfig(i *sf.ConfigContext) []*RecursiveConfigItem

func (*SyntaxFlowVisitor) VisitRecursiveNameFilter

func (y *SyntaxFlowVisitor) VisitRecursiveNameFilter(recursive bool, isMember bool, i sf.INameFilterContext) error

func (*SyntaxFlowVisitor) VisitRefVariable

func (y *SyntaxFlowVisitor) VisitRefVariable(raw sf.IRefVariableContext) string

func (*SyntaxFlowVisitor) VisitStatement

func (y *SyntaxFlowVisitor) VisitStatement(raw sf.IStatementContext)

func (*SyntaxFlowVisitor) VisitStringLiteral

func (y *SyntaxFlowVisitor) VisitStringLiteral(raw sf.IStringLiteralContext) (string, bool)

func (*SyntaxFlowVisitor) VisitStringLiteralWithoutStarGroup

func (y *SyntaxFlowVisitor) VisitStringLiteralWithoutStarGroup(raw sf.IStringLiteralWithoutStarGroupContext) []string

type ValueList added in v1.3.3

type ValueList struct {
	// contains filtered or unexported fields
}

func (*ValueList) AppendPredecessor

func (v *ValueList) AppendPredecessor(value ValueOperator, opts ...AnalysisContextOption) error

func (*ValueList) ExactMatch added in v1.3.3

func (v *ValueList) ExactMatch(mod int, s string) (bool, ValueOperator, error)

func (*ValueList) ForEach added in v1.3.3

func (v *ValueList) ForEach(h func(i any))

func (*ValueList) GetAllCallActualParams added in v1.3.3

func (v *ValueList) GetAllCallActualParams() (ValueOperator, error)

func (*ValueList) GetCallActualParams added in v1.3.3

func (v *ValueList) GetCallActualParams(i int) (ValueOperator, error)

func (*ValueList) GetCalled added in v1.3.3

func (v *ValueList) GetCalled() (ValueOperator, error)

func (*ValueList) GetFields

func (v *ValueList) GetFields() (ValueOperator, error)

func (*ValueList) GetOpcode

func (v *ValueList) GetOpcode() string

func (*ValueList) GetSyntaxFlowBottomUse added in v1.3.3

func (v *ValueList) GetSyntaxFlowBottomUse(config ...*RecursiveConfigItem) (ValueOperator, error)

func (*ValueList) GetSyntaxFlowDef added in v1.3.3

func (v *ValueList) GetSyntaxFlowDef() (ValueOperator, error)

func (*ValueList) GetSyntaxFlowTopDef added in v1.3.3

func (v *ValueList) GetSyntaxFlowTopDef(config ...*RecursiveConfigItem) (ValueOperator, error)

func (*ValueList) GetSyntaxFlowUse added in v1.3.3

func (v *ValueList) GetSyntaxFlowUse() (ValueOperator, error)

func (*ValueList) GlobMatch added in v1.3.3

func (v *ValueList) GlobMatch(mod int, s ssa.Glob) (bool, ValueOperator, error)

func (*ValueList) IsList added in v1.3.3

func (v *ValueList) IsList() bool

func (*ValueList) IsMap added in v1.3.3

func (v *ValueList) IsMap() bool

func (*ValueList) ListIndex added in v1.3.3

func (v *ValueList) ListIndex(i int) (ValueOperator, error)

func (*ValueList) Merge

func (v *ValueList) Merge(values ...ValueOperator) (ValueOperator, error)

func (*ValueList) Recursive added in v1.3.3

func (v *ValueList) Recursive(f func(operator ValueOperator) error) error

func (*ValueList) RegexpMatch added in v1.3.3

func (v *ValueList) RegexpMatch(mod int, regexp *regexp.Regexp) (bool, ValueOperator, error)

func (*ValueList) Remove

func (v *ValueList) Remove(values ...ValueOperator) (ValueOperator, error)

func (*ValueList) String added in v1.3.3

func (v *ValueList) String() string

type ValueOperator added in v1.3.3

type ValueOperator interface {
	String() string
	IsMap() bool
	IsList() bool
	GetOpcode() string

	// Recursive will execute with handler for every list or map
	Recursive(func(ValueOperator) error) error

	// ExactMatch return ops, for OpPushSearchExact
	ExactMatch(int, string) (bool, ValueOperator, error)
	// GlobMatch return opts, for OpPushSearchGlob
	GlobMatch(int, ssa.Glob) (bool, ValueOperator, error)
	// RegexpMatch for OpPushSearchRegexp
	RegexpMatch(int, *regexp.Regexp) (bool, ValueOperator, error)

	// GetCallActualParams for OpGetCallArgs
	GetCalled() (ValueOperator, error)
	GetCallActualParams(int) (ValueOperator, error)
	GetAllCallActualParams() (ValueOperator, error)
	GetFields() (ValueOperator, error)

	// GetTopDef and GetBottomUse is for OpBottomUse
	// use and def
	GetSyntaxFlowUse() (ValueOperator, error)
	GetSyntaxFlowDef() (ValueOperator, error)
	// top and bottom
	GetSyntaxFlowTopDef(...*RecursiveConfigItem) (ValueOperator, error)
	GetSyntaxFlowBottomUse(...*RecursiveConfigItem) (ValueOperator, error)

	// ListIndex for OpListIndex, like a[1] a must be list...
	ListIndex(i int) (ValueOperator, error)

	Merge(...ValueOperator) (ValueOperator, error)
	Remove(...ValueOperator) (ValueOperator, error)

	AppendPredecessor(ValueOperator, ...AnalysisContextOption) error
}

func AutoValue added in v1.3.3

func AutoValue(i any) ValueOperator

func MergeValues added in v1.3.3

func MergeValues(values ...ValueOperator) ValueOperator

func NewEmptyValues

func NewEmptyValues() ValueOperator

func NewValues added in v1.3.3

func NewValues(values []ValueOperator) ValueOperator

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL