How to use matchesAnyString method of report Package

Best Syzkaller code snippet using report.matchesAnyString

report.go

Source:report.go Github

copy

Full Screen

...216func (reporter *Reporter) isInteresting(rep *Report) bool {217 if len(reporter.interests) == 0 {218 return true219 }220 if matchesAnyString(rep.Title, reporter.interests) ||221 matchesAnyString(rep.guiltyFile, reporter.interests) {222 return true223 }224 for _, title := range rep.AltTitles {225 if matchesAnyString(title, reporter.interests) {226 return true227 }228 }229 for _, recipient := range rep.Recipients {230 if matchesAnyString(recipient.Address.Address, reporter.interests) {231 return true232 }233 }234 return false235}236func extractReportType(rep *Report) Type {237 // Type/frame extraction logic should be integrated with oops types.238 // But for now we do this more ad-hoc analysis here to at least isolate239 // the rest of the code base from report parsing.240 if rep.Title == unexpectedKernelReboot {241 return UnexpectedReboot242 }243 if strings.HasPrefix(rep.Title, memoryLeakPrefix) {244 return MemoryLeak245 }246 if strings.HasPrefix(rep.Title, dataRacePrefix) {247 return DataRace248 }249 if strings.HasPrefix(rep.Title, "INFO: rcu detected stall") ||250 strings.HasPrefix(rep.Title, "INFO: task hung") ||251 strings.HasPrefix(rep.Title, "BUG: soft lockup") ||252 strings.HasPrefix(rep.Title, "INFO: task can't die") {253 return Hang254 }255 return Unknown256}257func IsSuppressed(reporter *Reporter, output []byte) bool {258 return matchesAny(output, reporter.suppressions) ||259 bytes.Contains(output, gceConsoleHangup)260}261// ParseAll returns all successive reports in output.262func ParseAll(reporter *Reporter, output []byte) (reports []*Report) {263 skipPos := 0264 for {265 rep := reporter.ParseFrom(output, skipPos)266 if rep == nil {267 return268 }269 reports = append(reports, rep)270 skipPos = rep.SkipPos271 }272}273// GCE console connection sometimes fails with this message.274// The message frequently happens right after a kernel panic.275// So if we see it in output where we recognized a crash, we mark the report as corrupted276// because the crash message is usually truncated (maybe we don't even have the title line).277// If we see it in no output/lost connection reports then we mark them as suppressed instead278// because the crash itself may have been caused by the console connection error.279var gceConsoleHangup = []byte("serialport: VM disconnected.")280type replacement struct {281 match *regexp.Regexp282 replacement string283}284func replaceTable(replacements []replacement, str string) string {285 for _, repl := range replacements {286 for stop := false; !stop; {287 newStr := repl.match.ReplaceAllString(str, repl.replacement)288 stop = newStr == str289 str = newStr290 }291 }292 return str293}294var dynamicTitleReplacement = []replacement{295 {296 // Executor PIDs are not interesting.297 regexp.MustCompile(`syz-executor\.?[0-9]+((/|:)[0-9]+)?`),298 "syz-executor",299 },300 {301 // Executor process IDs are dynamic and are not interesting.302 regexp.MustCompile(`syzkaller[0-9]+((/|:)[0-9]+)?`),303 "syzkaller",304 },305 {306 // Replace that everything looks like an address with "ADDR",307 // addresses in descriptions can't be good regardless of the oops regexps.308 regexp.MustCompile(`([^a-zA-Z0])(?:0x)?[0-9a-f]{6,}`),309 "${1}ADDR",310 },311 {312 // Replace IP addresses.313 regexp.MustCompile(`([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})`),314 "IP",315 },316 {317 // Replace that everything looks like a file line number with "LINE".318 regexp.MustCompile(`(\.\w+)(:[0-9]+)+`),319 "${1}:LINE",320 },321 {322 // Replace all raw references to runctions (e.g. "ip6_fragment+0x1052/0x2d80")323 // with just function name ("ip6_fragment"). Offsets and sizes are not stable.324 regexp.MustCompile(`([a-zA-Z][a-zA-Z0-9_.]+)\+0x[0-9a-z]+/0x[0-9a-z]+`),325 "${1}",326 },327 {328 // CPU numbers are not interesting.329 regexp.MustCompile(`CPU#[0-9]+`),330 "CPU",331 },332 {333 // Replace with "NUM" everything that looks like a decimal number and has not334 // been replaced yet. It might require multiple replacement executions as the335 // matching substrings may overlap (e.g. "0,1,2").336 regexp.MustCompile(`(\W)(\d+)(\W|$)`),337 "${1}NUM${3}",338 },339}340func sanitizeTitle(title string) string {341 const maxTitleLen = 120 // Corrupted/intermixed lines can be very long.342 res := make([]byte, 0, len(title))343 prev := byte(' ')344 for i := 0; i < len(title) && i < maxTitleLen; i++ {345 ch := title[i]346 switch {347 case ch == '\t':348 ch = ' '349 case ch < 0x20 || ch >= 0x7f:350 continue351 }352 if ch == ' ' && prev == ' ' {353 continue354 }355 res = append(res, ch)356 prev = ch357 }358 return strings.TrimSpace(string(res))359}360type oops struct {361 header []byte362 formats []oopsFormat363 suppressions []*regexp.Regexp364}365type oopsFormat struct {366 title *regexp.Regexp367 // If title is matched but report is not, the report is considered corrupted.368 report *regexp.Regexp369 // Format string to create report title.370 // Strings captured by title (or by report if present) are passed as input.371 // If stack is not nil, extracted function name is passed as an additional last argument.372 fmt string373 // Alternative titles used for better crash deduplication.374 // Format is the same as for fmt.375 alt []string376 // If not nil, a function name is extracted from the report and passed to fmt.377 // If not nil but frame extraction fails, the report is considered corrupted.378 stack *stackFmt379 // Disable stack report corruption checking as it would expect one of stackStartRes to be380 // present, but this format does not comply with that.381 noStackTrace bool382 corrupted bool383}384type stackFmt struct {385 // parts describe how guilty stack frame must be extracted from the report.386 // parts are matched consecutively potentially capturing frames.387 // parts can be of 3 types:388 // - non-capturing regexp, matched against report and advances current position389 // - capturing regexp, same as above, but also yields a frame390 // - special value parseStackTrace means that a stack trace must be parsed391 // starting from current position392 parts []*regexp.Regexp393 // If parts2 is present it is tried when parts matching fails.394 parts2 []*regexp.Regexp395 // Skip these functions in stack traces (matched as substring).396 skip []string397 // Custom frame extractor (optional).398 // Accepts set of all frames, returns guilty frame and corruption reason.399 extractor frameExtractor400}401type frameExtractor func(frames []string) string402var parseStackTrace *regexp.Regexp403func compile(re string) *regexp.Regexp {404 re = strings.Replace(re, "{{ADDR}}", "0x[0-9a-f]+", -1)405 re = strings.Replace(re, "{{PC}}", "\\[\\<?(?:0x)?[0-9a-f]+\\>?\\]", -1)406 re = strings.Replace(re, "{{FUNC}}", "([a-zA-Z0-9_]+)(?:\\.|\\+)", -1)407 re = strings.Replace(re, "{{SRC}}", "([a-zA-Z0-9-_/.]+\\.[a-z]+:[0-9]+)", -1)408 return regexp.MustCompile(re)409}410func containsCrash(output []byte, oopses []*oops, ignores []*regexp.Regexp) bool {411 for pos := 0; pos < len(output); {412 next := bytes.IndexByte(output[pos:], '\n')413 if next != -1 {414 next += pos415 } else {416 next = len(output)417 }418 for _, oops := range oopses {419 if matchOops(output[pos:next], oops, ignores) {420 return true421 }422 }423 pos = next + 1424 }425 return false426}427func matchOops(line []byte, oops *oops, ignores []*regexp.Regexp) bool {428 match := bytes.Index(line, oops.header)429 if match == -1 {430 return false431 }432 if matchesAny(line, oops.suppressions) {433 return false434 }435 if matchesAny(line, ignores) {436 return false437 }438 return true439}440func extractDescription(output []byte, oops *oops, params *stackParams) (441 desc, corrupted string, altTitles []string, format oopsFormat) {442 startPos := len(output)443 matchedTitle := false444 for _, f := range oops.formats {445 match := f.title.FindSubmatchIndex(output)446 if match == nil || match[0] > startPos {447 continue448 }449 if match[0] == startPos && desc != "" {450 continue451 }452 if match[0] < startPos {453 desc = ""454 format = oopsFormat{}455 startPos = match[0]456 }457 matchedTitle = true458 if f.report != nil {459 match = f.report.FindSubmatchIndex(output)460 if match == nil {461 continue462 }463 }464 var args []interface{}465 for i := 2; i < len(match); i += 2 {466 args = append(args, string(output[match[i]:match[i+1]]))467 }468 corrupted = ""469 if f.stack != nil {470 frames, ok := extractStackFrame(params, f.stack, output[match[0]:])471 if !ok {472 corrupted = corruptedNoFrames473 }474 for _, frame := range frames {475 args = append(args, frame)476 }477 }478 desc = fmt.Sprintf(f.fmt, args...)479 for _, alt := range f.alt {480 altTitles = append(altTitles, fmt.Sprintf(alt, args...))481 }482 format = f483 }484 if desc == "" {485 // If we are here and matchedTitle is set, it means that we've matched486 // a title of an oops but not full report regexp or stack trace,487 // which means the report was corrupted.488 if matchedTitle {489 corrupted = "matched title but not report regexp"490 }491 pos := bytes.Index(output, oops.header)492 if pos == -1 {493 return494 }495 end := bytes.IndexByte(output[pos:], '\n')496 if end == -1 {497 end = len(output)498 } else {499 end += pos500 }501 desc = string(output[pos:end])502 }503 if corrupted == "" && format.corrupted {504 corrupted = "report format is marked as corrupted"505 }506 return507}508type stackParams struct {509 // stackStartRes matches start of stack traces.510 stackStartRes []*regexp.Regexp511 // frameRes match different formats of lines containing kernel frames (capture function name).512 frameRes []*regexp.Regexp513 // skipPatterns match functions that must be unconditionally skipped.514 skipPatterns []string515 // If we looked at any lines that match corruptedLines during report analysis,516 // then the report is marked as corrupted.517 corruptedLines []*regexp.Regexp518 // Prefixes that need to be removed from frames.519 // E.g. syscall prefixes as different arches have different prefixes.520 stripFramePrefixes []string521}522func extractStackFrame(params *stackParams, stack *stackFmt, output []byte) ([]string, bool) {523 skip := append([]string{}, params.skipPatterns...)524 skip = append(skip, stack.skip...)525 var skipRe *regexp.Regexp526 if len(skip) != 0 {527 skipRe = regexp.MustCompile(strings.Join(skip, "|"))528 }529 extractor := func(frames []string) string {530 if len(frames) == 0 {531 return ""532 }533 if stack.extractor == nil {534 return frames[0]535 }536 return stack.extractor(frames)537 }538 frames, ok := extractStackFrameImpl(params, output, skipRe, stack.parts, extractor)539 if ok || len(stack.parts2) == 0 {540 return frames, ok541 }542 return extractStackFrameImpl(params, output, skipRe, stack.parts2, extractor)543}544func extractStackFrameImpl(params *stackParams, output []byte, skipRe *regexp.Regexp,545 parts []*regexp.Regexp, extractor frameExtractor) ([]string, bool) {546 s := bufio.NewScanner(bytes.NewReader(output))547 var frames, results []string548 ok := true549 numStackTraces := 0550nextPart:551 for partIdx := 0; ; partIdx++ {552 if partIdx == len(parts) || parts[partIdx] == parseStackTrace && numStackTraces > 0 {553 keyFrame := extractor(frames)554 if keyFrame == "" {555 keyFrame, ok = "corrupted", false556 }557 results = append(results, keyFrame)558 frames = nil559 }560 if partIdx == len(parts) {561 break562 }563 part := parts[partIdx]564 if part == parseStackTrace {565 numStackTraces++566 for s.Scan() {567 ln := s.Bytes()568 if matchesAny(ln, params.corruptedLines) {569 ok = false570 continue nextPart571 }572 if matchesAny(ln, params.stackStartRes) {573 continue nextPart574 }575 if partIdx != len(parts)-1 {576 match := parts[partIdx+1].FindSubmatch(ln)577 if match != nil {578 frames = appendStackFrame(frames, match, params, skipRe)579 partIdx++580 continue nextPart581 }582 }583 var match [][]byte584 for _, re := range params.frameRes {585 match = re.FindSubmatch(ln)586 if match != nil {587 break588 }589 }590 frames = appendStackFrame(frames, match, params, skipRe)591 }592 } else {593 for s.Scan() {594 ln := s.Bytes()595 if matchesAny(ln, params.corruptedLines) {596 ok = false597 continue nextPart598 }599 match := part.FindSubmatch(ln)600 if match == nil {601 continue602 }603 frames = appendStackFrame(frames, match, params, skipRe)604 break605 }606 }607 }608 return results, ok609}610func appendStackFrame(frames []string, match [][]byte, params *stackParams, skipRe *regexp.Regexp) []string {611 if len(match) < 2 {612 return frames613 }614 for _, frame := range match[1:] {615 if frame != nil && (skipRe == nil || !skipRe.Match(frame)) {616 frameName := string(frame)617 for _, prefix := range params.stripFramePrefixes {618 frameName = strings.TrimPrefix(frameName, prefix)619 }620 frames = append(frames, frameName)621 break622 }623 }624 return frames625}626func simpleLineParser(output []byte, oopses []*oops, params *stackParams, ignores []*regexp.Regexp) *Report {627 rep := &Report{628 Output: output,629 }630 var oops *oops631 for pos := 0; pos < len(output); {632 next := bytes.IndexByte(output[pos:], '\n')633 if next != -1 {634 next += pos635 } else {636 next = len(output)637 }638 line := output[pos:next]639 for _, oops1 := range oopses {640 if matchOops(line, oops1, ignores) {641 oops = oops1642 rep.StartPos = pos643 rep.EndPos = next644 break645 }646 }647 if oops != nil {648 break649 }650 pos = next + 1651 }652 if oops == nil {653 return nil654 }655 title, corrupted, altTitles, _ := extractDescription(output[rep.StartPos:], oops, params)656 rep.Title = title657 rep.AltTitles = altTitles658 rep.Report = output[rep.StartPos:]659 rep.Corrupted = corrupted != ""660 rep.CorruptedReason = corrupted661 return rep662}663func matchesAny(line []byte, res []*regexp.Regexp) bool {664 for _, re := range res {665 if re.Match(line) {666 return true667 }668 }669 return false670}671func matchesAnyString(str string, res []*regexp.Regexp) bool {672 for _, re := range res {673 if re.MatchString(str) {674 return true675 }676 }677 return false678}679// replace replaces [start:end] in where with what, inplace.680func replace(where []byte, start, end int, what []byte) []byte {681 if len(what) >= end-start {682 where = append(where, what[end-start:]...)683 copy(where[start+len(what):], where[end:])684 copy(where[start:], what)685 } else {...

Full Screen

Full Screen

matchesAnyString

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 r := regexp.MustCompile("p([a-z]+)ch")4 fmt.Println(r.MatchString("peach"))5 fmt.Println(r.FindString("peach punch"))6 fmt.Println(r.FindStringIndex("peach punch"))7 fmt.Println(r.FindStringSubmatch("peach punch"))8 fmt.Println(r.FindStringSubmatchIndex("peach punch"))9 fmt.Println(r.FindAllString("peach punch pinch", -1))10 fmt.Println(r.FindAllStringSubmatchIndex("peach punch pinch", -1))11 fmt.Println(r.FindAllString("peach punch pinch", 2))12 fmt.Println(r.Match([]byte("peach")))13 r = regexp.MustCompile("p([a-z]+)ch")14 fmt.Println(r)15 fmt.Println(r.ReplaceAllString("a peach", "<fruit>"))16 in := []byte("a peach")17 out := r.ReplaceAllFunc(in, bytes.ToUpper)18 fmt.Println(string(out))19}20import (21func main() {22 r := regexp.MustCompile("foo.")

Full Screen

Full Screen

matchesAnyString

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f := func(s string) string {4 v, _ := strconv.ParseFloat(s, 32)5 return strconv.FormatFloat(v*2, 'f', 2, 32)6 }7 if ok, _ := regexp.Match(pat, []byte(searchIn)); ok {8 fmt.Println("Match Found!")9 }10 re, _ := regexp.Compile(pat)11 str := re.ReplaceAllStringFunc(searchIn, f)12 fmt.Println(str)13}14Related Posts: GoLang | regexp.Compile() Method15GoLang | regexp.MatchString() Method16GoLang | regexp.Match() Method17GoLang | regexp.MatchReader() Method18GoLang | regexp.MatchReader() Method19GoLang | regexp.MatchString() Method20GoLang | regexp.QuoteMeta() Method21GoLang | regexp.CompilePOSIX() Method22GoLang | regexp.FindString() Method23GoLang | regexp.FindStringSubmatch() Method24GoLang | regexp.FindStringSubmatchIndex() Method25GoLang | regexp.FindStringIndex() Method26GoLang | regexp.FindAllString() Method27GoLang | regexp.FindAllStringSubmatchIndex() Method28GoLang | regexp.FindAllStringIndex() Method29GoLang | regexp.FindAllStringSubmatch() Method30GoLang | regexp.FindAll() Method31GoLang | regexp.FindReaderIndex() Method32GoLang | regexp.FindReaderSubmatchIndex() Method33GoLang | regexp.FindReaderSubmatch() Method34GoLang | regexp.FindReader() Method35GoLang | regexp.Find() Method36GoLang | regexp.FindAllIndex() Method37GoLang | regexp.FindAllSubmatchIndex() Method38GoLang | regexp.FindAllSubmatch() Method

Full Screen

Full Screen

matchesAnyString

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 report := NewReport("some text")4 if report.matchesAnyString("some") {5 fmt.Println("Report contains some")6 }7 if report.matchesAnyString("text") {8 fmt.Println("Report contains text")9 }10 if report.matchesAnyString("some text") {11 fmt.Println("Report contains some text")12 }13 if report.matchesAnyString("other") {14 fmt.Println("Report contains other")15 }16}

Full Screen

Full Screen

matchesAnyString

Using AI Code Generation

copy

Full Screen

1func main() {2 r.matchesAnyString("hello", []string{"hi", "hello", "howdy"})3}4type report struct {5 matchesAnyString func(string, []string) bool6}7import "testing"8func TestReport(t *testing.T) {9 r.matchesAnyString = func(s string, ss []string) bool {10 }11 if !r.matchesAnyString("hello", []string{"hi", "hello", "howdy"}) {12 t.Error("expected true")13 }14}15func (p *Person) FullName() string {16}17func TestFullName(t *testing.T) {18 p := Person{19 }20 if p.FullName() != "John Doe" {21 t.Error("Expected 'John Doe', got ", p.FullName())22 }23}

Full Screen

Full Screen

matchesAnyString

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 re := regexp.MustCompile(`[a-z]`)4 result := re.FindAllString(str, -1)5 fmt.Println(result)6}7import (8func main() {9 re := regexp.MustCompile(`[a-z]`)10 result := re.FindAllString(str, -1)11 fmt.Println(result)12}13import (14func main() {15 re := regexp.MustCompile(`[a-z]`)16 result := re.FindAllStringIndex(str, -1)17 fmt.Println(result)18}19import (20func main() {21 re := regexp.MustCompile(`[a-z]`)22 result := re.FindAllStringSubmatch(str, -1)23 fmt.Println(result)24}25import (

Full Screen

Full Screen

matchesAnyString

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 s := []string{"cat", "dog", "mouse", "cow", "bird"}4 r, _ := regexp.Compile("^c")5 report := NewReport(s, r)6 fmt.Println(report.MatchesAnyString())7}8import (9type Report struct {10}11func NewReport(s []string, r *regexp.Regexp) *Report {12 return &Report{Strings: s, Regexp: r}13}14func (r *Report) MatchesAnyString() bool {15 for _, s := range r.Strings {16 if r.Regexp.MatchString(s) {17 }18 }19}

Full Screen

Full Screen

matchesAnyString

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 report = report{content: "This is a sample report"}4 regex = regexp.MustCompile("sample")5 fmt.Println(report.matchesAnyString(regex))6}7import (8type report struct {9}10func (r report) matchesAnyString(regex *regexp.Regexp) bool {11 return regex.MatchString(r.content)12}

Full Screen

Full Screen

matchesAnyString

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 keywords := []string{"error", "exception", "fail"}4 if matchesAnyString(report, keywords) {5 fmt.Println("The report contains a problem")6 } else {7 fmt.Println("The report is fine")8 }9}10func matchesAnyString(text string, keywords []string) bool {11 re := regexp.MustCompile("(?i)" + strings.Join(keywords, "|"))12 return re.MatchString(text)13}14import (15func main() {16 keywords := []string{"error", "exception", "fail"}17 if matchesAnyString(report, keywords) {18 fmt.Println("The report contains a problem")19 } else {20 fmt.Println("The report is fine")21 }22}23func matchesAnyString(text string

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