ssadb

package
v1.3.5-alpha0828 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2024 License: AGPL-3.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NameMatch int = 1
	KeyMatch      = 1 << 1
	BothMatch     = NameMatch | KeyMatch
)

type MatchMode int

View Source
const (
	ExactCompare int = iota
	GlobCompare
	RegexpCompare
)

Variables

View Source
var Programs = make(map[string]*schema.SSAProgram)
View Source
var SSAProjectTables = []any{

	&IrCode{},
	&IrIndex{},

	&IrSource{},

	&IrType{},

	&IrProgram{},
	&IrOffset{},

	&AuditNode{},
	&AuditEdge{},
}

Functions

func AllPrograms

func AllPrograms(db *gorm.DB) []string

func AllSSAPrograms

func AllSSAPrograms() []*schema.SSAProgram

func CheckAndSwitchDB

func CheckAndSwitchDB(name string)

func DeleteProgram

func DeleteProgram(db *gorm.DB, program string)

func DeleteSSAProgram

func DeleteSSAProgram(name string) error

func ExactSearchVariable added in v1.3.3

func ExactSearchVariable(DB *gorm.DB, mod int, value string) chan int64

func GetDB added in v1.3.3

func GetDB() *gorm.DB

func GetDBInProgram

func GetDBInProgram(program string) *gorm.DB

func GetEditorByFileName

func GetEditorByFileName(name string) (*memedit.MemEditor, error)

func GetIrSourceFromHash added in v1.3.3

func GetIrSourceFromHash(hash string) (*memedit.MemEditor, error)

GetIrSourceFromHash fetch editor from cache by hash(md5)

func GetSSAIndexCost

func GetSSAIndexCost() time.Duration

func GetSSAProgram

func GetSSAProgram(name string) *schema.SSAProgram

func GetSSASaveTypeCost

func GetSSASaveTypeCost() time.Duration

func GetSSASourceCodeCost

func GetSSASourceCodeCost() time.Duration

func GetType

func GetType(id int) (int, string, string, error)

func GetValueBeforeEndOffset

func GetValueBeforeEndOffset(db *gorm.DB, rng memedit.RangeIf) (int64, error)

func GlobSearchVariable added in v1.3.3

func GlobSearchVariable(DB *gorm.DB, mod int, value string) chan int64

func NewIrSourceFs

func NewIrSourceFs() *irSourceFS

func RegexpSearchVariable added in v1.3.3

func RegexpSearchVariable(DB *gorm.DB, mod int, value string) chan int64

func SaveFile

func SaveFile(filename, content string, folderPath []string) string

func SaveFolder

func SaveFolder(folderName string, folderPaths []string) error

func SaveIrIndex

func SaveIrIndex(idx *IrIndex)

func SaveIrOffset

func SaveIrOffset(idx *IrOffset)

func SaveSSAProgram

func SaveSSAProgram(name, desc, language string) error

func SaveType

func SaveType(kind int, str string, extra string) int

func SearchVariable

func SearchVariable(db *gorm.DB, compareMode, matchMod int, value string) chan int64

func UpdateProgram

func UpdateProgram(prog *IrProgram)

func YieldAuditNodeByRuleName

func YieldAuditNodeByRuleName(db *gorm.DB, ruleName string) chan *AuditNode

func YieldAuditNodeByRuntimeId

func YieldAuditNodeByRuntimeId(db *gorm.DB, runtimeId string) chan *AuditNode

func YieldIrCodesProgramName added in v1.3.3

func YieldIrCodesProgramName(db *gorm.DB, ctx context.Context, program string) chan *IrCode

Types

type AuditEdge

type AuditEdge struct {
	gorm.Model

	RuntimeId   string `json:"runtime_id" gorm:"index"`
	RuleName    string `json:"rule_name" gorm:"index"`
	RuleId      int64  `json:"rule_id" gorm:"index"`
	ProgramName string `json:"program_name" gorm:"index"`

	FromNode int64
	ToNode   int64
	EdgeType AuditEdgeType

	AnalysisStep  int64
	AnalysisLabel string
}

type AuditEdgeType

type AuditEdgeType string
const (
	EdgeType_DependsOn AuditEdgeType = "depends_on"
	EdgeType_EffectsOn AuditEdgeType = "effects_on"

	// EdgeType_Predecessor 记录审计过程
	EdgeType_Predecessor AuditEdgeType = "predecessor"
)

type AuditNode

type AuditNode struct {
	gorm.Model

	RuntimeId   string `json:"runtime_id" gorm:"index"`
	RuleName    string `json:"rule_name" gorm:"index"`
	RuleId      int64  `json:"rule_id" gorm:"index"`
	ProgramName string `json:"program_name" gorm:"index"`
	IsEntryNode bool   `json:"is_entry_node"`

	SsaId      int64  `json:"ssa_id"`
	ConstValue string `json:"const_value"`
}

func (*AuditNode) CreateDependsOnEdge

func (n *AuditNode) CreateDependsOnEdge(to int64) *AuditEdge

func (*AuditNode) CreateEffectsOnEdge

func (n *AuditNode) CreateEffectsOnEdge(to int64) *AuditEdge

func (*AuditNode) CreatePredecessorEdge

func (n *AuditNode) CreatePredecessorEdge(to int64, step int64, label string) *AuditEdge

type Int64Map

type Int64Map []item[int64]

func (*Int64Map) Append added in v1.3.3

func (m *Int64Map) Append(key, value int64)

func (Int64Map) ForEach added in v1.3.3

func (m Int64Map) ForEach(fn func(key, value int64))

func (*Int64Map) Scan

func (m *Int64Map) Scan(value any) error

func (Int64Map) Value

func (m Int64Map) Value() (driver.Value, error)

type Int64Slice

type Int64Slice []int64

Int64Slice 是一个自定义类型,用于处理 []int64 的序列化和反序列化

func (*Int64Slice) Scan

func (us *Int64Slice) Scan(value interface{}) error

Scan 实现了 sql.Scanner 接口,允许从数据库读取值时将其转换回 Int64Slice 类型

func (Int64Slice) Value

func (us Int64Slice) Value() (driver.Value, error)

Value 实现了 driver.Valuer 接口,允许将 Int64Slice 转换为一个适合存储在数据库中的形式

type IrCode

type IrCode struct {
	gorm.Model

	ProgramName string `json:"program_name" gorm:"index"`
	Version     string `json:"package_version" gorm:"index"`

	// source code
	SourceCodeStartOffset int64  `json:"source_code_start_offset"`
	SourceCodeEndOffset   int64  `json:"source_code_end_offset"`
	SourceCodeHash        string `json:"source_code_hash"` // default md5

	// opcode
	Opcode     int64  `json:"opcode"`
	OpcodeName string `json:"opcode_name"`

	// just for binary and unary operator
	OpcodeOperator string `json:"opcode_operator"`

	// basic info
	Name             string `json:"name"`
	VerboseName      string `json:"verbose_name"`
	ShortVerboseName string `json:"short_verbose_name"`
	String           string `json:"string"`

	// any IrCode in one block inner one  function
	CurrentBlock    int64 `json:"current_block"`
	CurrentFunction int64 `json:"current_function"`

	// FunctionDefs
	IsFunction     bool       `json:"is_function"`
	FormalArgs     Int64Slice `json:"formal_args" gorm:"type:text"`
	FreeValues     Int64Slice `json:"free_values" gorm:"type:text"`
	MemberCallArgs Int64Slice `json:"formal_member_call_args" gorm:"type:text"`

	SideEffects      Int64Slice `json:"side_effects" gorm:"type:text"`
	IsVariadic       bool       `json:"is_variadic"`
	ReturnCodes      Int64Slice `json:"return_codes" gorm:"type:text"`
	IsExternal       bool       `json:"is_external"`
	CodeBlocks       Int64Slice `json:"code_blocks" gorm:"type:text"`
	EnterBlock       int64      `json:"enter_block"`
	ExitBlock        int64      `json:"exit_block"`
	DeferBlock       int64      `json:"defer_block"`
	ChildrenFunction Int64Slice `json:"children_function" gorm:"type:text"`
	ParentFunction   int64      `json:"parent_function"`

	// block
	IsBlock   bool       `json:"is_block"`
	PredBlock Int64Slice `json:"pred_block" gorm:"type:text"`
	SuccBlock Int64Slice `json:"succ_block" gorm:"type:text"`
	Phis      Int64Slice `json:"phis_in_block" gorm:"type:text"`

	// Use-Def Chains Relation
	Defs  Int64Slice `json:"defs" gorm:"type:text"`
	Users Int64Slice `json:"users" gorm:"type:text"`
	// this is user is call and method is this IR self
	CalledBy Int64Slice `json:"is_called_by" gorm:"type:text"`

	// OOP Supporting
	IsObject       bool
	ObjectMembers  Int64Map `json:"object_members" gorm:"type:text"`
	IsObjectMember bool
	ObjectParent   int64 `json:"object_parent"`
	ObjectKey      int64 `json:"object_key"`

	// Maskable
	MaskedCodes Int64Slice `json:"masked_codes" gorm:"type:text"`
	IsMasked    bool       `json:"is_masked"`

	// Variable
	Variable StringSlice `json:"variable" gorm:"type:text"`

	// compile hash means: hash[ (file-content)+(program-name)+(package-name)+(program-index) ]
	ProgramCompileHash string `json:"program_compile_hash" gorm:"index"`

	// type
	TypeID int `json:"type_id"`

	// reference
	Point   int64      `json:"point" gorm:"type:text"`
	Pointer Int64Slice `json:"pointer" gorm:"type:text"`

	// not important information
	ExtraInformation string `json:"extra_information"`
}

func GetIrByVariable

func GetIrByVariable(db *gorm.DB, program, name string) []*IrCode

func GetIrCodeById

func GetIrCodeById(db *gorm.DB, id int64) *IrCode

func RequireIrCode

func RequireIrCode(db *gorm.DB, program string) (uint, *IrCode)

func (*IrCode) GetExtraInfo added in v1.3.3

func (r *IrCode) GetExtraInfo() map[string]any

func (*IrCode) GetIdInt added in v1.3.3

func (r *IrCode) GetIdInt() int

func (*IrCode) GetIdInt64 added in v1.3.3

func (r *IrCode) GetIdInt64() int64

func (*IrCode) GetSourceCode added in v1.3.3

func (r *IrCode) GetSourceCode(db *gorm.DB) string

func (*IrCode) GetSourceCodeContext added in v1.3.3

func (r *IrCode) GetSourceCodeContext(db *gorm.DB, n int) string

func (*IrCode) GetStartAndEndPositions added in v1.3.3

func (r *IrCode) GetStartAndEndPositions(db *gorm.DB) (*memedit.MemEditor, memedit.PositionIf, memedit.PositionIf, error)

func (*IrCode) IsEmptySourceCodeHash added in v1.3.3

func (r *IrCode) IsEmptySourceCodeHash() bool

func (*IrCode) SetExtraInfo added in v1.3.3

func (r *IrCode) SetExtraInfo(params map[string]any)

func (*IrCode) Show added in v1.3.3

func (i *IrCode) Show()

func (*IrCode) VerboseString added in v1.3.3

func (i *IrCode) VerboseString() string

type IrIndex

type IrIndex struct {
	gorm.Model

	ProgramName string `json:"program_name" gorm:"index"`

	// class
	ClassName string `json:"class_name" gorm:"index"`

	// variable
	VariableName string `json:"variable_name" gorm:"index"`
	VersionID    int64  `json:"version_id" gorm:"index"`
	// member call
	FieldName string `json:"field_name" gorm:"index"`

	// scope
	ScopeName string `json:"scope_name" gorm:"index"`

	// value
	ValueID int64 `json:"value_id" gorm:"index"`
}

func CreateIndex

func CreateIndex() *IrIndex

func GetScope

func GetScope(programName, scopeName string) ([]IrIndex, error)

func GetVariableByValue

func GetVariableByValue(valueID int64) []*IrIndex

type IrOffset

type IrOffset struct {
	gorm.Model

	ProgramName string `json:"program_name" gorm:"index"`
	// offset
	FileHash    string `json:"file_hash" gorm:"index"`
	StartOffset int64  `json:"start_offset" gorm:"index"`
	EndOffset   int64  `json:"end_offset" gorm:"index"`
	//variable
	VariableName string `json:"variable_name"` // this id set when have variable
	// value
	ValueID int64 `json:"value_id"` // this id will set
}

func CreateOffset

func CreateOffset(rng memedit.RangeIf) *IrOffset

func GetOffsetByVariable

func GetOffsetByVariable(name string, valueID int64) []*IrOffset

func (*IrOffset) GetStartAndEndPositions

func (r *IrOffset) GetStartAndEndPositions() (*memedit.MemEditor, memedit.PositionIf, memedit.PositionIf, error)

type IrProgram

type IrProgram struct {
	gorm.Model

	ProgramName string `json:"program_name" gorm:"index"`
	Version     string `json:"package_version" gorm:"index"`

	// Language: yak, java, php, js, etc
	// if the program contains many language,
	// use comma to separate them.
	// e.g. "yak,java,php"
	Language string `json:"language" gorm:"index"`

	// application / library
	ProgramKind          string      `json:"program_kind" gorm:"index"`
	ChildApplicationName StringSlice `json:"child_application_name" gorm:"type:text"`
	// up-stream program is the program that this program depends on
	UpStream StringSlice `json:"up_stream_programs" gorm:"type:text"`
	// down-stream program is the program that depends on this program
	DownStream StringSlice `json:"down_stream_programs" gorm:"type:text"`

	// this  program  contain this file
	FileList StringMap `json:"file_list" gorm:"type:text"`

	// program extra file: *.properties, *.xml, *.json, etc
	ExtraFile StringMap `json:"extra_file" gorm:"type:text"`
}

func CreateProgram

func CreateProgram(name, kind, version string, childName []string) *IrProgram

func GetLibrary

func GetLibrary(name, version string) (*IrProgram, error)

func GetProgram

func GetProgram(name, kind string) (*IrProgram, error)

type IrSource added in v1.3.3

type IrSource struct {
	ProgramName    string `json:"program_name" gorm:"index"`
	SourceCodeHash string `json:"source_code_hash" gorm:"index"` // default md5

	// file path
	FolderPath string `json:"folder_path"`
	FileName   string `json:"file_name"`

	// file content
	QuotedCode string `json:"quoted_code"`
	IsBigFile  bool   `json:"is_big_file"` // if set this flag, the source code is too big, QuotedCode contain this file path
}

func GetIrSourceByPath

func GetIrSourceByPath(path string) ([]*IrSource, error)

func GetIrSourceByPathAndName

func GetIrSourceByPathAndName(path, name string) (*IrSource, error)

type IrType

type IrType struct {
	gorm.Model
	Kind             int    `json:"kind"`
	String           string `json:"string"`
	ExtraInformation string `json:"extra_information"`
	Hash             string `json:"hash" gorm:"unique_index"`
}

func (*IrType) BeforeCreate

func (t *IrType) BeforeCreate() error

func (*IrType) BeforeSave

func (t *IrType) BeforeSave() error

func (*IrType) BeforeUpdate

func (t *IrType) BeforeUpdate() error

func (*IrType) CalcHash

func (t *IrType) CalcHash() string

type StringMap

type StringMap map[string]string

func (*StringMap) Scan

func (m *StringMap) Scan(value any) error

func (StringMap) Value

func (m StringMap) Value() (driver.Value, error)

type StringSlice

type StringSlice []string

func (*StringSlice) Scan

func (ss *StringSlice) Scan(value interface{}) error

func (StringSlice) Value

func (us StringSlice) Value() (driver.Value, error)

Jump to

Keyboard shortcuts

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