github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/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  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestParseSingle(t *testing.T) {
    13  	t.Parallel()
    14  	target, err := GetTarget("linux", "amd64")
    15  	if err != nil {
    16  		t.Fatal(err)
    17  	}
    18  	const execLog = `getpid()
    19  gettid()	
    20  `
    21  	entries := target.ParseLog([]byte(execLog), NonStrict)
    22  	if len(entries) != 1 {
    23  		t.Fatalf("got %v programs, want 1", len(entries))
    24  	}
    25  	ent := entries[0]
    26  	if ent.Start != 0 {
    27  		t.Fatalf("start offset %v, want 0", ent.Start)
    28  	}
    29  	if ent.End != len(execLog) {
    30  		t.Fatalf("end offset %v, want %v", ent.End, len(execLog))
    31  	}
    32  	if ent.Proc != 0 {
    33  		t.Fatalf("proc %v, want 0", ent.Proc)
    34  	}
    35  	if ent.P.RequiredFeatures().FaultInjection {
    36  		t.Fatalf("fault injection enabled")
    37  	}
    38  	want := "getpid-gettid"
    39  	got := ent.P.String()
    40  	if got != want {
    41  		t.Fatalf("bad program: %s, want %s", got, want)
    42  	}
    43  }
    44  
    45  func TestParseMulti(t *testing.T) {
    46  	t.Parallel()
    47  	target, err := GetTarget("linux", "amd64")
    48  	if err != nil {
    49  		t.Fatal(err)
    50  	}
    51  	entries := target.ParseLog([]byte(execLogNew), NonStrict)
    52  	validateProgs(t, entries, len(execLogNew))
    53  	if entries[0].ID != -1 ||
    54  		entries[1].ID != 70 ||
    55  		entries[2].ID != 75 ||
    56  		entries[3].ID != 80 ||
    57  		entries[4].ID != 85 {
    58  		t.Fatalf("bad IDs")
    59  	}
    60  }
    61  
    62  func TestParseMultiLegacy(t *testing.T) {
    63  	t.Parallel()
    64  	target, err := GetTarget("linux", "amd64")
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  	entries := target.ParseLog([]byte(execLogOld), NonStrict)
    69  	validateProgs(t, entries, len(execLogOld))
    70  	for _, ent := range entries {
    71  		assert.Equal(t, -1, ent.ID)
    72  	}
    73  }
    74  
    75  func validateProgs(t *testing.T, entries []*LogEntry, logLen int) {
    76  	for i, ent := range entries {
    77  		t.Logf("program #%v: %v", i, ent.P)
    78  	}
    79  	if len(entries) != 5 {
    80  		t.Fatalf("got %v programs, want 5", len(entries))
    81  	}
    82  	off := 0
    83  	for _, ent := range entries {
    84  		if off > ent.Start || ent.Start > ent.End || ent.End > logLen {
    85  			t.Fatalf("bad offsets")
    86  		}
    87  		off = ent.End
    88  	}
    89  	if entries[0].Proc != 0 ||
    90  		entries[1].Proc != 1 ||
    91  		entries[2].Proc != 2 ||
    92  		entries[3].Proc != 33 ||
    93  		entries[4].Proc != 9 {
    94  		t.Fatalf("bad procs")
    95  	}
    96  	for i, ent := range entries {
    97  		if ent.P.RequiredFeatures().FaultInjection {
    98  			t.Fatalf("prog %v has fault injection enabled", i)
    99  		}
   100  	}
   101  	if s := entries[0].P.String(); s != "getpid-gettid" {
   102  		t.Fatalf("bad program 0: %s", s)
   103  	}
   104  	if s := entries[1].P.String(); s != "getpid-gettid-munlockall" {
   105  		t.Fatalf("bad program 0: %s", s)
   106  	}
   107  	if s := entries[2].P.String(); s != "getpid-gettid" {
   108  		t.Fatalf("bad program 1: %s", s)
   109  	}
   110  	if s := entries[3].P.String(); s != "gettid-getpid" {
   111  		t.Fatalf("bad program 2: %s", s)
   112  	}
   113  	if s := entries[4].P.String(); s != "munlockall" {
   114  		t.Fatalf("bad program 3: %s", s)
   115  	}
   116  }
   117  
   118  const execLogNew = `
   119  getpid()
   120  gettid()
   121  15.133581935s ago: executing program 1 (id=70):
   122  getpid()
   123  [ 2351.935478] Modules linked in:
   124  gettid()
   125  munlockall()
   126  14.133581935s ago: executing program 2 (id=75):
   127  [ 2351.935478] Modules linked in:
   128  getpid()
   129  gettid()
   130  13.133581935s ago: executing program 33 (id=80):
   131  gettid()
   132  getpid()
   133  [ 2351.935478] Modules linked in:
   134  12.133581935s ago: executing program 9 (id=85):
   135  munlockall()
   136  `
   137  
   138  // Logs before the introduction of rpcserver.LastExecuting.
   139  const execLogOld = `
   140  getpid()
   141  gettid()
   142  2015/12/21 12:18:05 executing program 1:
   143  getpid()
   144  [ 2351.935478] Modules linked in:
   145  gettid()
   146  munlockall()
   147  2015/12/21 12:18:05 executing program 2:
   148  [ 2351.935478] Modules linked in:
   149  getpid()
   150  gettid()
   151  2015/12/21 12:18:05 executing program 33:
   152  gettid()
   153  getpid()
   154  [ 2351.935478] Modules linked in:
   155  2015/12/21 12:18:05 executing program 9:
   156  munlockall()
   157  `
   158  
   159  func TestParseFault(t *testing.T) {
   160  	t.Parallel()
   161  	target, err := GetTarget("linux", "amd64")
   162  	if err != nil {
   163  		t.Fatal(err)
   164  	}
   165  	const execLog = `2015/12/21 12:18:05 executing program 1 (fault-call:1 fault-nth:55):
   166  gettid()
   167  getpid()
   168  `
   169  	entries := target.ParseLog([]byte(execLog), NonStrict)
   170  	if len(entries) != 1 {
   171  		t.Fatalf("got %v programs, want 1", len(entries))
   172  	}
   173  	ent := entries[0]
   174  	faultCall := ent.P.Calls[1]
   175  	normalCall := ent.P.Calls[0]
   176  	if faultCall.Props.FailNth != 56 {
   177  		// We want 56 (not 55!) because the number is now not 0-based.
   178  		t.Fatalf("fault nth on the 2nd call: got %v, want 56", faultCall.Props.FailNth)
   179  	}
   180  	if normalCall.Props.FailNth != 0 {
   181  		t.Fatalf("fault nth on the 1st call: got %v, want 0", normalCall.Props.FailNth)
   182  	}
   183  	want := "gettid-getpid"
   184  	got := ent.P.String()
   185  	if got != want {
   186  		t.Fatalf("bad program: %s, want %s", got, want)
   187  	}
   188  }