github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/prog/parse_test.go (about)

     1  // Copyright 2015 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  package prog
     5  
     6  import (
     7  	"testing"
     8  )
     9  
    10  func TestParseSingle(t *testing.T) {
    11  	t.Parallel()
    12  	target, err := GetTarget("linux", "amd64")
    13  	if err != nil {
    14  		t.Fatal(err)
    15  	}
    16  	const execLog = `getpid()
    17  gettid()	
    18  `
    19  	entries := target.ParseLog([]byte(execLog))
    20  	if len(entries) != 1 {
    21  		t.Fatalf("got %v programs, want 1", len(entries))
    22  	}
    23  	ent := entries[0]
    24  	if ent.Start != 0 {
    25  		t.Fatalf("start offset %v, want 0", ent.Start)
    26  	}
    27  	if ent.End != len(execLog) {
    28  		t.Fatalf("end offset %v, want %v", ent.End, len(execLog))
    29  	}
    30  	if ent.Proc != 0 {
    31  		t.Fatalf("proc %v, want 0", ent.Proc)
    32  	}
    33  	if ent.P.RequiredFeatures().FaultInjection {
    34  		t.Fatalf("fault injection enabled")
    35  	}
    36  	want := "getpid-gettid"
    37  	got := ent.P.String()
    38  	if got != want {
    39  		t.Fatalf("bad program: %s, want %s", got, want)
    40  	}
    41  }
    42  
    43  func TestParseMulti(t *testing.T) {
    44  	t.Parallel()
    45  	target, err := GetTarget("linux", "amd64")
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  	entries := target.ParseLog([]byte(execLog))
    50  	if len(entries) != 5 {
    51  		for i, ent := range entries {
    52  			t.Logf("program #%v: %v", i, ent.P)
    53  		}
    54  		t.Fatalf("got %v programs, want 5", len(entries))
    55  	}
    56  	off := 0
    57  	for _, ent := range entries {
    58  		if off > ent.Start || ent.Start > ent.End || ent.End > len(execLog) {
    59  			t.Fatalf("bad offsets")
    60  		}
    61  	}
    62  	if entries[0].Proc != 0 ||
    63  		entries[1].Proc != 1 ||
    64  		entries[2].Proc != 2 ||
    65  		entries[3].Proc != 33 ||
    66  		entries[4].Proc != 9 {
    67  		t.Fatalf("bad procs")
    68  	}
    69  	for i, ent := range entries {
    70  		if ent.P.RequiredFeatures().FaultInjection {
    71  			t.Fatalf("prog %v has fault injection enabled", i)
    72  		}
    73  	}
    74  	if s := entries[0].P.String(); s != "getpid-gettid" {
    75  		t.Fatalf("bad program 0: %s", s)
    76  	}
    77  	if s := entries[1].P.String(); s != "getpid-gettid-munlockall" {
    78  		t.Fatalf("bad program 0: %s", s)
    79  	}
    80  	if s := entries[2].P.String(); s != "getpid-gettid" {
    81  		t.Fatalf("bad program 1: %s", s)
    82  	}
    83  	if s := entries[3].P.String(); s != "gettid-getpid" {
    84  		t.Fatalf("bad program 2: %s", s)
    85  	}
    86  	if s := entries[4].P.String(); s != "munlockall" {
    87  		t.Fatalf("bad program 3: %s", s)
    88  	}
    89  }
    90  
    91  const execLog = `
    92  getpid()
    93  gettid()
    94  2015/12/21 12:18:05 executing program 1:
    95  getpid()
    96  [ 2351.935478] Modules linked in:
    97  gettid()
    98  munlockall()
    99  2015/12/21 12:18:05 executing program 2:
   100  [ 2351.935478] Modules linked in:
   101  getpid()
   102  gettid()
   103  2015/12/21 12:18:05 executing program 33:
   104  gettid()
   105  getpid()
   106  [ 2351.935478] Modules linked in:
   107  2015/12/21 12:18:05 executing program 9:
   108  munlockall()
   109  `
   110  
   111  func TestParseFault(t *testing.T) {
   112  	t.Parallel()
   113  	target, err := GetTarget("linux", "amd64")
   114  	if err != nil {
   115  		t.Fatal(err)
   116  	}
   117  	const execLog = `2015/12/21 12:18:05 executing program 1 (fault-call:1 fault-nth:55):
   118  gettid()
   119  getpid()
   120  `
   121  	entries := target.ParseLog([]byte(execLog))
   122  	if len(entries) != 1 {
   123  		t.Fatalf("got %v programs, want 1", len(entries))
   124  	}
   125  	ent := entries[0]
   126  	faultCall := ent.P.Calls[1]
   127  	normalCall := ent.P.Calls[0]
   128  	if faultCall.Props.FailNth != 56 {
   129  		// We want 56 (not 55!) because the number is now not 0-based.
   130  		t.Fatalf("fault nth on the 2nd call: got %v, want 56", faultCall.Props.FailNth)
   131  	}
   132  	if normalCall.Props.FailNth != 0 {
   133  		t.Fatalf("fault nth on the 1st call: got %v, want 0", normalCall.Props.FailNth)
   134  	}
   135  	want := "gettid-getpid"
   136  	got := ent.P.String()
   137  	if got != want {
   138  		t.Fatalf("bad program: %s, want %s", got, want)
   139  	}
   140  }