How to use matchesAny method of report Package

Best Syzkaller code snippet using report.matchesAny

report.go

Source:report.go Github

copy

Full Screen

...155 if rep == nil {156 return nil157 }158 rep.Title = sanitizeTitle(replaceTable(dynamicTitleReplacement, rep.Title))159 rep.Suppressed = matchesAny(rep.Output, wrap.suppressions)160 if bytes.Contains(rep.Output, gceConsoleHangup) {161 rep.Corrupted = true162 }163 rep.Type = extractReportType(rep)164 if match := reportFrameRe.FindStringSubmatch(rep.Title); match != nil {165 rep.Frame = match[1]166 }167 if pos := bytes.Index(rep.Report, []byte(VMDiagnosisStart)); pos != -1 {168 rep.Report = rep.Report[:pos]169 }170 rep.SkipPos = len(output)171 if pos := bytes.IndexByte(output[rep.StartPos:], '\n'); pos != -1 {172 rep.SkipPos = rep.StartPos + pos173 }174 if rep.EndPos < rep.SkipPos {175 // This generally should not happen.176 // But openbsd does some hacks with /r/n which may lead to off-by-one EndPos.177 rep.EndPos = rep.SkipPos178 }179 return rep180}181func extractReportType(rep *Report) Type {182 // Type/frame extraction logic should be integrated with oops types.183 // But for now we do this more ad-hoc analysis here to at least isolate184 // the rest of the code base from report parsing.185 if rep.Title == unexpectedKernelReboot {186 return UnexpectedReboot187 }188 if strings.HasPrefix(rep.Title, memoryLeakPrefix) {189 return MemoryLeak190 }191 if strings.HasPrefix(rep.Title, dataRacePrefix) {192 return DataRace193 }194 if strings.HasPrefix(rep.Title, "INFO: rcu detected stall") ||195 strings.HasPrefix(rep.Title, "INFO: task hung") ||196 strings.HasPrefix(rep.Title, "BUG: soft lockup") {197 return Hang198 }199 return Unknown200}201func IsSuppressed(reporter Reporter, output []byte) bool {202 return matchesAny(output, reporter.(*reporterWrapper).suppressions) ||203 bytes.Contains(output, gceConsoleHangup)204}205// ParseAll returns all successive reports in output.206func ParseAll(reporter Reporter, output []byte) (reports []*Report) {207 for {208 rep := reporter.Parse(output)209 if rep == nil {210 return211 }212 reports = append(reports, rep)213 output = output[rep.SkipPos:]214 }215}216// GCE console connection sometimes fails with this message.217// The message frequently happens right after a kernel panic.218// So if we see it in output where we recognized a crash, we mark the report as corrupted219// because the crash message is usually truncated (maybe we don't even have the title line).220// If we see it in no output/lost connection reports then we mark them as suppressed instead221// because the crash itself may have been caused by the console connection error.222var gceConsoleHangup = []byte("serialport: VM disconnected.")223type replacement struct {224 match *regexp.Regexp225 replacement string226}227func replaceTable(replacements []replacement, str string) string {228 for _, repl := range replacements {229 str = repl.match.ReplaceAllString(str, repl.replacement)230 }231 return str232}233var dynamicTitleReplacement = []replacement{234 {235 // Executor PIDs are not interesting.236 regexp.MustCompile(`syz-executor\.?[0-9]+((/|:)[0-9]+)?`),237 "syz-executor",238 },239 {240 // syzkaller binaries are coming from repro.241 regexp.MustCompile(`syzkaller[0-9]+((/|:)[0-9]+)?`),242 "syzkaller",243 },244 {245 // Replace that everything looks like an address with "ADDR",246 // addresses in descriptions can't be good regardless of the oops regexps.247 regexp.MustCompile(`([^a-zA-Z])(?:0x)?[0-9a-f]{6,}`),248 "${1}ADDR",249 },250 {251 // Replace that everything looks like a decimal number with "NUM".252 regexp.MustCompile(`([^a-zA-Z])[0-9]{5,}`),253 "${1}NUM",254 },255 {256 // Replace that everything looks like a file line number with "LINE".257 regexp.MustCompile(`(:[0-9]+)+`),258 ":LINE",259 },260 {261 // Replace all raw references to runctions (e.g. "ip6_fragment+0x1052/0x2d80")262 // with just function name ("ip6_fragment"). Offsets and sizes are not stable.263 regexp.MustCompile(`([a-zA-Z][a-zA-Z0-9_.]+)\+0x[0-9a-z]+/0x[0-9a-z]+`),264 "${1}",265 },266 {267 // CPU numbers are not interesting.268 regexp.MustCompile(`CPU#[0-9]+`),269 "CPU",270 },271}272func sanitizeTitle(title string) string {273 const maxTitleLen = 120 // Corrupted/intermixed lines can be very long.274 res := make([]byte, 0, len(title))275 prev := byte(' ')276 for i := 0; i < len(title) && i < maxTitleLen; i++ {277 ch := title[i]278 switch {279 case ch == '\t':280 ch = ' '281 case ch < 0x20 || ch >= 0x7f:282 continue283 }284 if ch == ' ' && prev == ' ' {285 continue286 }287 res = append(res, ch)288 prev = ch289 }290 return strings.TrimSpace(string(res))291}292type oops struct {293 header []byte294 formats []oopsFormat295 suppressions []*regexp.Regexp296}297type oopsFormat struct {298 title *regexp.Regexp299 // If title is matched but report is not, the report is considered corrupted.300 report *regexp.Regexp301 // Format string to create report title.302 // Strings captured by title (or by report if present) are passed as input.303 // If stack is not nil, extracted function name is passed as an additional last argument.304 fmt string305 // If not nil, a function name is extracted from the report and passed to fmt.306 // If not nil but frame extraction fails, the report is considered corrupted.307 stack *stackFmt308 noStackTrace bool309 corrupted bool310}311type stackFmt struct {312 // parts describe how guilty stack frame must be extracted from the report.313 // parts are matched consecutively potentially capturing frames.314 // parts can be of 3 types:315 // - non-capturing regexp, matched against report and advances current position316 // - capturing regexp, same as above, but also yields a frame317 // - special value parseStackTrace means that a stack trace must be parsed318 // starting from current position319 parts []*regexp.Regexp320 // If parts2 is present it is tried when parts matching fails.321 parts2 []*regexp.Regexp322 // Skip these functions in stack traces (matched as substring).323 skip []string324 // Custom frame extractor (optional).325 // Accepts set of all frames, returns guilty frame and corruption reason.326 extractor frameExtractor327}328type frameExtractor func(frames []string) (frame string, corrupted string)329var parseStackTrace *regexp.Regexp330func compile(re string) *regexp.Regexp {331 re = strings.Replace(re, "{{ADDR}}", "0x[0-9a-f]+", -1)332 re = strings.Replace(re, "{{PC}}", "\\[\\<?(?:0x)?[0-9a-f]+\\>?\\]", -1)333 re = strings.Replace(re, "{{FUNC}}", "([a-zA-Z0-9_]+)(?:\\.|\\+)", -1)334 re = strings.Replace(re, "{{SRC}}", "([a-zA-Z0-9-_/.]+\\.[a-z]+:[0-9]+)", -1)335 return regexp.MustCompile(re)336}337func containsCrash(output []byte, oopses []*oops, ignores []*regexp.Regexp) bool {338 for pos := 0; pos < len(output); {339 next := bytes.IndexByte(output[pos:], '\n')340 if next != -1 {341 next += pos342 } else {343 next = len(output)344 }345 for _, oops := range oopses {346 if matchOops(output[pos:next], oops, ignores) {347 return true348 }349 }350 pos = next + 1351 }352 return false353}354func matchOops(line []byte, oops *oops, ignores []*regexp.Regexp) bool {355 match := bytes.Index(line, oops.header)356 if match == -1 {357 return false358 }359 if matchesAny(line, oops.suppressions) {360 return false361 }362 if matchesAny(line, ignores) {363 return false364 }365 return true366}367func extractDescription(output []byte, oops *oops, params *stackParams) (368 desc string, corrupted string, format oopsFormat) {369 startPos := len(output)370 matchedTitle := false371 for _, f := range oops.formats {372 match := f.title.FindSubmatchIndex(output)373 if match == nil || match[0] > startPos {374 continue375 }376 if match[0] == startPos && desc != "" {377 continue378 }379 if match[0] < startPos {380 desc = ""381 format = oopsFormat{}382 startPos = match[0]383 }384 matchedTitle = true385 if f.report != nil {386 match = f.report.FindSubmatchIndex(output)387 if match == nil {388 continue389 }390 }391 var args []interface{}392 for i := 2; i < len(match); i += 2 {393 args = append(args, string(output[match[i]:match[i+1]]))394 }395 corrupted = ""396 if f.stack != nil {397 frame := ""398 frame, corrupted = extractStackFrame(params, f.stack, output[match[0]:])399 if frame == "" {400 frame = "corrupted"401 if corrupted == "" {402 corrupted = "extracted no stack frame"403 }404 }405 args = append(args, frame)406 }407 desc = fmt.Sprintf(f.fmt, args...)408 format = f409 }410 if desc == "" {411 // If we are here and matchedTitle is set, it means that we've matched412 // a title of an oops but not full report regexp or stack trace,413 // which means the report was corrupted.414 if matchedTitle {415 corrupted = "matched title but not report regexp"416 }417 pos := bytes.Index(output, oops.header)418 if pos == -1 {419 return420 }421 end := bytes.IndexByte(output[pos:], '\n')422 if end == -1 {423 end = len(output)424 } else {425 end += pos426 }427 desc = string(output[pos:end])428 }429 if corrupted == "" && format.corrupted {430 corrupted = "report format is marked as corrupted"431 }432 return433}434type stackParams struct {435 // stackStartRes matches start of stack traces.436 stackStartRes []*regexp.Regexp437 // frameRes match different formats of lines containing kernel frames (capture function name).438 frameRes []*regexp.Regexp439 // skipPatterns match functions that must be unconditionally skipped.440 skipPatterns []string441 // If we looked at any lines that match corruptedLines during report analysis,442 // then the report is marked as corrupted.443 corruptedLines []*regexp.Regexp444}445func extractStackFrame(params *stackParams, stack *stackFmt, output []byte) (string, string) {446 skip := append([]string{}, params.skipPatterns...)447 skip = append(skip, stack.skip...)448 var skipRe *regexp.Regexp449 if len(skip) != 0 {450 skipRe = regexp.MustCompile(strings.Join(skip, "|"))451 }452 extractor := stack.extractor453 if extractor == nil {454 extractor = func(frames []string) (string, string) {455 return frames[0], ""456 }457 }458 frame, corrupted := extractStackFrameImpl(params, output, skipRe, stack.parts, extractor)459 if frame != "" || len(stack.parts2) == 0 {460 return frame, corrupted461 }462 return extractStackFrameImpl(params, output, skipRe, stack.parts2, extractor)463}464func extractStackFrameImpl(params *stackParams, output []byte, skipRe *regexp.Regexp,465 parts []*regexp.Regexp, extractor frameExtractor) (string, string) {466 s := bufio.NewScanner(bytes.NewReader(output))467 var frames []string468nextPart:469 for _, part := range parts {470 if part == parseStackTrace {471 for s.Scan() {472 ln := bytes.Trim(s.Bytes(), "\r")473 if matchesAny(ln, params.corruptedLines) {474 break nextPart475 }476 if matchesAny(ln, params.stackStartRes) {477 continue nextPart478 }479 var match [][]byte480 for _, re := range params.frameRes {481 match = re.FindSubmatch(ln)482 if match != nil {483 break484 }485 }486 frames = appendStackFrame(frames, match, skipRe)487 }488 } else {489 for s.Scan() {490 ln := bytes.Trim(s.Bytes(), "\r")491 if matchesAny(ln, params.corruptedLines) {492 break nextPart493 }494 match := part.FindSubmatch(ln)495 if match == nil {496 continue497 }498 frames = appendStackFrame(frames, match, skipRe)499 break500 }501 }502 }503 if len(frames) == 0 {504 return "", corruptedNoFrames505 }506 return extractor(frames)507}508func appendStackFrame(frames []string, match [][]byte, skipRe *regexp.Regexp) []string {509 if len(match) < 2 {510 return frames511 }512 for _, frame := range match[1:] {513 if frame != nil && (skipRe == nil || !skipRe.Match(frame)) {514 frames = append(frames, string(frame))515 break516 }517 }518 return frames519}520func simpleLineParser(output []byte, oopses []*oops, params *stackParams, ignores []*regexp.Regexp) *Report {521 rep := &Report{522 Output: output,523 }524 var oops *oops525 for pos := 0; pos < len(output); {526 next := bytes.IndexByte(output[pos:], '\n')527 if next != -1 {528 next += pos529 } else {530 next = len(output)531 }532 line := output[pos:next]533 for _, oops1 := range oopses {534 if matchOops(line, oops1, ignores) {535 oops = oops1536 rep.StartPos = pos537 rep.EndPos = next538 break539 }540 }541 if oops != nil {542 break543 }544 pos = next + 1545 }546 if oops == nil {547 return nil548 }549 title, corrupted, _ := extractDescription(output[rep.StartPos:], oops, params)550 rep.Title = title551 rep.Report = output[rep.StartPos:]552 rep.Corrupted = corrupted != ""553 rep.CorruptedReason = corrupted554 return rep555}556func matchesAny(line []byte, res []*regexp.Regexp) bool {557 for _, re := range res {558 if re.Match(line) {559 return true560 }561 }562 return false563}564// replace replaces [start:end] in where with what, inplace.565func replace(where []byte, start, end int, what []byte) []byte {566 if len(what) >= end-start {567 where = append(where, what[end-start:]...)568 copy(where[start+len(what):], where[end:])569 copy(where[start:], what)570 } else {...

Full Screen

Full Screen

main.go

Source:main.go Github

copy

Full Screen

...122 func(path string, info os.FileInfo, err error) error {123 if err != nil {124 return err125 }126 matches := matchesAny(path, rule.rFiles)127 if !matches {128 return nil129 }130 inputData, err := ioutil.ReadFile(path)131 ctx := Context{132 Path: path,133 InputData: inputData,134 Rule: rule,135 Results: &run.Results,136 }137 for _, r := range rule.rMatch {138 matches := r.FindAllSubmatch(inputData, -1)139 for _, match := range matches {140 searchString := ""141 if len(match) == 1 {142 searchString = string(match[0])143 } else if len(match) > 1 {144 searchString = string(match[1])145 }146 if len(rule.rIncludes) > 0 {147 matches := matchesAny(searchString, rule.rIncludes)148 if !matches {149 continue150 }151 }152 if len(rule.rExcludes) > 0 {153 matches := matchesAny(searchString, rule.rExcludes)154 if matches {155 continue156 }157 }158 run.Results = append(run.Results, createResult(&ctx, searchString))159 }160 }161 return nil162 })163 if err != nil {164 os.Exit(-1)165 }166 }167 if *humanize {168 HumanWrite(report, outFile)169 } else {170 _ = report.PrettyWrite(outFile)171 }172}173func matchesAny(data string, regexes []*regexp.Regexp) bool {174 fileValid := false175 for _, r := range regexes {176 fileValid = fileValid || r.MatchString(data)177 }178 return fileValid179}180func compileRegexes(regexes []string) []*regexp.Regexp {181 var out []*regexp.Regexp182 for _, fileRegex := range regexes {183 regex, err := regexp.Compile(fileRegex)184 if err == nil {185 out = append(out, regex)186 }187 }...

Full Screen

Full Screen

matchesAny

Using AI Code Generation

copy

Full Screen

1import (2type report struct {3}4func (r report) matchesAny(regexps []*regexp.Regexp) bool {5 for _, re := range regexps {6 if re.MatchString(r.name) {7 }8 }9}10func main() {11 regexps = append(regexps, regexp.MustCompile("test1"))12 regexps = append(regexps, regexp.MustCompile("test2"))13 regexps = append(regexps, regexp.MustCompile("test3"))14 r := report{name: "test1"}15 fmt.Println(r.matchesAny(regexps))16}17import (18type report struct {19}20func (r report) matchesAny(regexps []*regexp.Regexp) bool {21 for _, re := range regexps {22 if re.FindString(r.name) != "" {23 }24 }25}26func main() {27 regexps = append(regexps, regexp.MustCompile("test1"))28 regexps = append(regexps, regexp.MustCompile("test2"))29 regexps = append(regexps, regexp.MustCompile("test3"))30 r := report{name: "test1"}31 fmt.Println(r.matchesAny(regexps))32}33import (34type report struct {35}36func (r report) matchesAny(regexps []*regexp.Regexp) bool {37 for _, re := range regexps {38 if re.FindStringIndex(r.name) != nil {39 }40 }41}42func main() {43 regexps = append(regexps, regexp.MustCompile("test1"))44 regexps = append(regexps, regexp.MustCompile("test2"))45 regexps = append(regexps, regexp.MustCompile("test3"))46 r := report{name: "test1"}47 fmt.Println(r.matchesAny(regexps))48}

Full Screen

Full Screen

matchesAny

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 var match = pattern.FindStringIndex(text)4 fmt.Println(match)5 fmt.Println(pattern.FindString(text))6 fmt.Println(pattern.FindStringSubmatch(text))7 fmt.Println(pattern.FindStringSubmatchIndex(text))8 fmt.Println(pattern.FindAllString(text, -1))9 fmt.Println(pattern.FindAllStringSubmatch(text, -1))10 fmt.Println(pattern.FindAllStringSubmatchIndex(text, -1))11 fmt.Println(pattern.MatchString(text))12}13import (14func main() {15 var patterns = []string{16 }17 var pattern = regexp.MustCompile(`(?i)(` + strings.Join(patterns, `|`) + `)`)18 fmt.Println(pattern.MatchString(text))19}20import (21func main() {22 var pattern = regexp.MustCompile(`quick`)23 var result = pattern.ReplaceAllString(text, "slow")24 fmt.Println(result)25}26import (27func main() {

Full Screen

Full Screen

matchesAny

Using AI Code Generation

copy

Full Screen

1import (2type Report struct {3}4func (r *Report) matchesAny(re *regexp.Regexp) bool {5}6func main() {7 r := &Report{}8 re := regexp.MustCompile("foo")9 fmt.Println(r.matchesAny(re))10}11import (12type Report struct {13}14func (r *Report) matchesAny(re *regexp.Regexp) bool {15}16func main() {17 r := &Report{}18 re := regexp.MustCompile("foo")19 fmt.Println(r.matchesAny(re))20}21import (22type Report struct {23}24func (r *Report) matchesAny(re *regexp.Regexp) bool {25}26func main() {27 r := &Report{}28 re := regexp.MustCompile("foo")29 fmt.Println(r.matchesAny(re))30}31import (32type Report struct {33}34func (r *Report) matchesAny(re *regexp.Regexp) bool {35}36func main() {37 r := &Report{}38 re := regexp.MustCompile("foo")39 fmt.Println(r.matchesAny(re))40}41import (42type Report struct {43}44func (r *Report) matchesAny(re *regexp.Regexp) bool {45}46func main() {47 r := &Report{}48 re := regexp.MustCompile("foo")49 fmt.Println(r.matchesAny(re))50}51import (52type Report struct {53}54func (r *Report) matchesAny(re *regexp.Regexp) bool {55}56func main() {57 r := &Report{}58 re := regexp.MustCompile("foo")59 fmt.Println(r.matchesAny(re))60}

Full Screen

Full Screen

matchesAny

Using AI Code Generation

copy

Full Screen

1import (2func main() {3}4import (5func main() {6}7import (8func main() {9}10import (11func main() {12}

Full Screen

Full Screen

matchesAny

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 var report = NewReport("2014-01-01", "2014-02-01", "2014-03-01", "2014-04-01")4 fmt.Println(report.MatchesAny("2014-01-01", "2014-02-01", "2014-03-01"))5 fmt.Println(report.MatchesAny("2014-01-01", "2014-02-01", "2014-03-01", "2014-04-01"))6 fmt.Println(report.MatchesAny("2014-01-01", "2014-02-01", "2014-03-01", "2014-04-01", "2014-05-01"))7}8func (r *Report) MatchesAny(dates ...string) bool {9 for _, date := range dates {10 if r.Matches(date) {11 }12 }13}

Full Screen

Full Screen

matchesAny

Using AI Code Generation

copy

Full Screen

1import (2type report struct {3}4func (r *report) Visit(node ast.Node) ast.Visitor {5 if fn, ok := node.(*ast.FuncDecl); ok {6 }7}8func (r *report) matchesAny(fn *ast.FuncDecl, patterns []string) bool {9 if fn.Recv != nil {10 for _, field := range fn.Recv.List {11 if r.matchesAnyType(field.Type, patterns) {12 }13 }14 }15 if r.matchesAnyName(fn.Name, patterns) {16 }17 for _, field := range fn.Type.Params.List {18 if r.matchesAnyType(field.Type, patterns) {19 }20 }21 for _, field := range fn.Type.Results.List {22 if r.matchesAnyType(field.Type, patterns) {23 }24 }25}26func (r *report) matchesAnyName(name *ast.Ident, patterns []string) bool {27 for _, pattern := range patterns {28 if name.Name == pattern {29 }30 }31}32func (r *report) matchesAnyType(typ ast.Expr, patterns []string) bool {33 switch typ := typ.(type) {34 return r.matchesAnyName(typ, patterns)35 return r.matchesAnyType(typ.X, patterns)36 return r.matchesAnyType(typ.Elt, patterns)37 return r.matchesAnyType(typ.Key, patterns) || r.matchesAnyType(typ.Value, patterns)38 return r.matchesAnyType(typ.Value, patterns)39 for _, field := range typ.Params.List {

Full Screen

Full Screen

matchesAny

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 var regex = regexp.MustCompile("report|something")4 var match = regex.MatchString(report)5 fmt.Println(match)6}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful