github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/tools/syz-trace2syz/proggen/proggen_test.go (about)

     1  // Copyright 2018 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  //go:build !codeanalysis
     5  
     6  package proggen
     7  
     8  import (
     9  	"bytes"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/google/syzkaller/prog"
    14  	_ "github.com/google/syzkaller/sys"
    15  	"github.com/google/syzkaller/sys/targets"
    16  	"github.com/google/syzkaller/tools/syz-trace2syz/parser"
    17  )
    18  
    19  func TestParse(t *testing.T) {
    20  	type Test struct {
    21  		input  string
    22  		output string
    23  	}
    24  	tests := []Test{
    25  		{`
    26  open("file", 66) = 3
    27  write(3, "somedata", 8) = 8
    28  `, `
    29  r0 = open(&(0x7f0000000000)='file\x00', 0x42, 0x0)
    30  write(r0, &(0x7f0000000040)='somedata', 0x8)
    31  `,
    32  		}, {`
    33  pipe([5,6]) = 0
    34  write(6, "\xff\xff\xfe\xff", 4) = 4
    35  `, `
    36  pipe(&(0x7f0000000000)={0xffffffffffffffff, <r0=>0xffffffffffffffff})
    37  write(r0, &(0x7f0000000040)="fffffeff", 0x4)
    38  `,
    39  		}, {`
    40  pipe({0x0, 0x1}) = 0
    41  shmget(0x0, 0x1, 0x2, 0x3) = 0
    42  `, `
    43  pipe(&(0x7f0000000000))
    44  shmget(0x0, 0x1, 0x2, &(0x7f0000001000/0x1)=nil)
    45  `,
    46  		}, {`
    47  socket(29, 3, 1) = 3
    48  getsockopt(-1, 132, 119, 0x200005c0, [14]) = -1 EBADF (Bad file descriptor)
    49  `, `
    50  socket$can_raw(0x1d, 0x3, 0x1)
    51  getsockopt$inet_sctp6_SCTP_RESET_STREAMS(0xffffffffffffffff, 0x84, 0x77, &(0x7f0000000000), &(0x7f0000000040)=0x8)
    52  `,
    53  		}, {`
    54  inotify_init() = 2
    55  open("tmp", 66) = 3
    56  inotify_add_watch(3, "\x2e", 0xfff) = 3
    57  write(3, "temp", 4) = 4
    58  inotify_rm_watch(2, 3) = 0
    59  `, `
    60  r0 = inotify_init()
    61  r1 = open(&(0x7f0000000000)='tmp\x00', 0x42, 0x0)
    62  r2 = inotify_add_watch(r1, &(0x7f0000000040)='.\x00', 0xfff)
    63  write(r1, &(0x7f0000000080)='temp', 0x4)
    64  inotify_rm_watch(r0, r2)
    65  `,
    66  		}, {`
    67  socket(1, 1, 0) = 3
    68  socket(1, 1 | 2048, 0) = 3
    69  socket(1, 1 | 524288, 0) = 3
    70  socket(1, 1 | 524288, 0) = 3
    71  `, `
    72  socket$unix(0x1, 0x1, 0x0)
    73  socket$unix(0x1, 0x801, 0x0)
    74  socket$unix(0x1, 0x80001, 0x0)
    75  socket$unix(0x1, 0x80001, 0x0)
    76  `,
    77  		}, {`
    78  open("temp", 1) = 3
    79  connect(3, {sa_family=2, sin_port=37957, sin_addr=0x0}, 16) = -1
    80  `, `
    81  r0 = open(&(0x7f0000000000)='temp\x00', 0x1, 0x0)
    82  connect(r0, &(0x7f0000000040)=@in={0x2, 0x9445}, 0x80)
    83  `,
    84  		}, {`
    85  open("temp", 1) = 3
    86  connect(3, {sa_family=1, sun_path="temp"}, 110) = -1
    87  `, `
    88  r0 = open(&(0x7f0000000000)='temp\x00', 0x1, 0x0)
    89  connect(r0, &(0x7f0000000040)=@un=@file={0x1, 'temp\x00'}, 0x80)
    90  `,
    91  		}, {`
    92  open("temp", 1) = 3
    93  bind(5, {sa_family=16, nl_pid=0x2, nl_groups=00000003}, 12)  = -1
    94  `, `
    95  open(&(0x7f0000000000)='temp\x00', 0x1, 0x0)
    96  bind(0x5, &(0x7f0000000040)=@nl=@proc={0x10, 0x2, 0x3}, 0x80)
    97  `,
    98  		}, {`
    99  socket(17, 3, 768)  = 3
   100  ioctl(3, 35111, {ifr_name="\x6c\x6f", ifr_hwaddr=00:00:00:00:00:00}) = 0
   101  `, `
   102  r0 = socket$packet(0x11, 0x3, 0x300)
   103  ioctl$sock_ifreq(r0, 0x8927, &(0x7f0000000000)={'lo\x00'})
   104  `,
   105  		}, {`
   106  socket(1, 1, 0) = 3
   107  connect(3, {sa_family=1, sun_path="temp"}, 110) = -1 ENOENT (Bad file descriptor)
   108  `, `
   109  r0 = socket$unix(0x1, 0x1, 0x0)
   110  connect$unix(r0, &(0x7f0000000000)=@file={0x1, 'temp\x00'}, 0x6e)
   111  `,
   112  		}, {`
   113  socket(1, 1, 0) = 3
   114  `, `
   115  socket$unix(0x1, 0x1, 0x0)
   116  `,
   117  		}, {`
   118  socket(2, 1, 0) = 5
   119  ioctl(5, 21537, [1]) = 0
   120  `, `
   121  r0 = socket$inet_tcp(0x2, 0x1, 0x0)
   122  ioctl$int_in(r0, 0x5421, &(0x7f0000000000)=0x1)
   123  `,
   124  		}, {`
   125  socket(2, 1, 0) = 3
   126  setsockopt(3, 1, 2, [1], 4) = 0
   127  `, `
   128  r0 = socket$inet_tcp(0x2, 0x1, 0x0)
   129  setsockopt$sock_int(r0, 0x1, 0x2, &(0x7f0000000000)=0x1, 0x4)
   130  `,
   131  		}, {`
   132  9795  socket(17, 3, 768)  = 3
   133  9795  ioctl(3, 35123, {ifr_name="\x6c\x6f", }) = 0
   134  `, `
   135  r0 = socket$packet(0x11, 0x3, 0x300)
   136  ioctl$ifreq_SIOCGIFINDEX_batadv_hard(r0, 0x8933, &(0x7f0000000000)={'lo\x00'})
   137  `,
   138  		}, {`
   139  open("temp", 1) = 3
   140  connect(3, {sa_family=2, sin_port=17812, sin_addr=0x0}, 16) = -1
   141  `, `
   142  r0 = open(&(0x7f0000000000)='temp\x00', 0x1, 0x0)
   143  connect(r0, &(0x7f0000000040)=@in={0x2, 0x4594}, 0x80)
   144  `,
   145  		}, {`
   146  ioprio_get(1, 0) = 4
   147  `, `
   148  ioprio_get$pid(0x1, 0x0)
   149  `,
   150  		}, {`
   151  socket(17, 2, 768) = 3
   152  `, `
   153  socket$packet(0x11, 0x2, 0x300)
   154  `,
   155  		}, {`
   156  socket(2, 1, 0) = 3
   157  connect(3, {sa_family=2, sin_port=17812, sin_addr=0x0}, 16) = 0
   158  `, `
   159  r0 = socket$inet_tcp(0x2, 0x1, 0x0)
   160  connect$inet(r0, &(0x7f0000000000)={0x2, 0x4594}, 0x10)
   161  `,
   162  		}, {`
   163  open("\x2f\x64\x65\x76\x2f\x73\x6e\x64\x2f\x73\x65\x71", 0) = 3
   164  fsetxattr(3, "\x73\x65\x63\x75\x72\x69\x74\x79\x2e\x73\x65\x6c\x69\x6e\x75\x78","\x73\x79\x73", 4, 0) = 0
   165  `, `
   166  r0 = openat$sndseq(0xffffffffffffff9c, &(0x7f0000000000), 0x0)
   167  fsetxattr(r0, &(0x7f0000000040)=@known='security.selinux\x00', &(0x7f0000000080)='sys\x00', 0x4, 0x0)
   168  `,
   169  		}, {`
   170  socket(0x2, 0x1, 0) = 3
   171  connect(3, {sa_family=0x2, sin_port="\x1f\x90", sin_addr="\x7f\x00\x00\x01"}, 16) = -1
   172  `, `
   173  r0 = socket$inet_tcp(0x2, 0x1, 0x0)
   174  connect$inet(r0, &(0x7f0000000000)={0x2, 0x1f90, @rand_addr=0x7f000001}, 0x10)
   175  `,
   176  		}, {`
   177  socket(0x2, 0x1, 0) = 3
   178  connect(3, {sa_family=0x2, sin_port="\x1f\x90", sin_addr="\x00\x00\x00\x00\x7f\x00\x00\x01"}, 16) = -1
   179  `, `
   180  r0 = socket$inet_tcp(0x2, 0x1, 0x0)
   181  connect$inet(r0, &(0x7f0000000000)={0x2, 0x1f90, @rand_addr=0x7f000001}, 0x10)
   182  `,
   183  		}, {`
   184  socket(0x2, 0x1, 0) = 3
   185  connect(3, {sa_family=0x2, sin_port="\x1f\x90", sin_addr="\x00"}, 16) = -1
   186  `, `
   187  r0 = socket$inet_tcp(0x2, 0x1, 0x0)
   188  connect$inet(r0, &(0x7f0000000000)={0x2, 0x1f90}, 0x10)
   189  `,
   190  		}, {`
   191  socket(0x2, 0x1, 0) = 3
   192  connect(3, {sa_family=0x2, sin_port="\x1f\x90", sin_addr="\x00"}, 16) = -1
   193  `, `
   194  r0 = socket$inet_tcp(0x2, 0x1, 0x0)
   195  connect$inet(r0, &(0x7f0000000000)={0x2, 0x1f90}, 0x10)
   196  `,
   197  		}, {`
   198  connect(-1, {sa_family=0xa, sin6_port="\x30\x39",` +
   199  			`sin6_flowinfo="\x07\x5b\xcd\x7a",` +
   200  			`sin6_addr="\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01",` +
   201  			`sin6_scope_id=4207869677}, 28) = -1
   202  `, `
   203  connect(0xffffffffffffffff, &(0x7f0000000000)=` +
   204  			`@in6={0xa, 0x3039, 0x75bcd7a, @rand_addr='\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01',` +
   205  			` 0xfacefeed}, 0x80)
   206  `,
   207  		}, {`
   208  connect(-1, {sa_family=0xa, sin6_port="\x30\x39",` +
   209  			` sin6_flowinfo="\x07\x5b\xcd\x7a",` +
   210  			` sin6_addr="\x00\x12\x00\x34\x00\x56\x00\x78\x00\x90\x00\xab\x00\xcd\x00\xef",` +
   211  			` sin6_scope_id=4207869677}, 28) = -1
   212  `, `
   213  connect(0xffffffffffffffff, &(0x7f0000000000)=` +
   214  			`@in6={0xa, 0x3039, 0x75bcd7a, @rand_addr='\x00\x12\x004\x00V\x00x\x00\x90\x00\xab\x00\xcd\x00\xef',` +
   215  			` 0xfacefeed}, 0x80)
   216  `,
   217  		}, {`
   218  socket(0xa, 0x2, 0) = 3
   219  sendto(3, "", 0, 0, {sa_family=0xa, sin6_port="\x4e\x24", sin6_flowinfo="\x00\x00\x00\x00",` +
   220  			` sin6_addr="\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",` +
   221  			` sin6_scope_id=0}, 28) = -1
   222  `, `
   223  r0 = socket$inet6_udp(0xa, 0x2, 0x0)
   224  sendto$inet6(r0, &(0x7f0000000000), 0x0, 0x0, &(0x7f0000000040)={0xa, 0x4e24}, 0x1c)
   225  `,
   226  		}, {`
   227  open("\x2f\x64\x65\x76\x2f\x7a\x65\x72\x6f", "1") = 3
   228  `, `
   229  openat$zero(0xffffffffffffff9c, &(0x7f0000000000), 0x31, 0x0)
   230  `,
   231  		}, {`
   232  open("\x2f\x64\x65\x76\x2f\x6c\x6f\x6f\x70\x30", 0) = 3
   233  `, `
   234  syz_open_dev$loop(&(0x7f0000000000)='/dev/loop0\x00', 0x0, 0x0)
   235  `,
   236  		}, {`
   237  open("\x2f\x64\x65\x76\x2f\x6c\x6f\x6f\x70\x31", 0) = 3
   238  `, `
   239  syz_open_dev$loop(&(0x7f0000000000)='/dev/loop1\x00', 0x1, 0x0)
   240  `,
   241  		}, {`
   242  open("\x2f\x64\x65\x76\x2f\x62\x75\x73\x2f\x75\x73\x62\x2f\x30\x30\x31\x2f\x30\x30\x31", 0) = 3
   243  `, `
   244  syz_open_dev$usbfs(&(0x7f0000000000)='/dev/bus/usb/001/001\x00', 0xb, 0x0)
   245  `,
   246  		}, {`
   247  openat(0xffffffffffffff9c, "\x2f\x64\x65\x76\x2f\x7a\x65\x72\x6f", 0x31, 0) = 3
   248  `, `
   249  openat$zero(0xffffffffffffff9c, &(0x7f0000000000), 0x31, 0x0)
   250  `}, {`
   251  socket(0xa, 0x1, 0) = 3
   252  setsockopt(3, 0x29, 0x2a, {gr_interface=0, gr_group={sa_family=0xa, sin6_port="\x00\x00", sin6_flowinfo=` +
   253  			`"\x00\x00\x00\x00", sin6_addr="\xff\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01",` +
   254  			` sin6_scope_id=0}}, 136) = 0
   255  `, `
   256  r0 = socket$inet6_tcp(0xa, 0x1, 0x0)
   257  setsockopt$inet6_MCAST_JOIN_GROUP(r0, 0x29, 0x2a, ` +
   258  			`&(0x7f0000000000)={0x0, {{0xa, 0x0, 0x0, @rand_addr='\xff\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01'}}}, 0x88)`,
   259  		}, {
   260  			`
   261  openat(-100, "\x2f\x64\x65\x76\x2f\x72\x74\x63\x30", 0) = 3
   262  ioctl(3, 0x4028700f, {enabled=0, pending=0, time={tm_sec=0, tm_min=0, tm_hour=0, tm_mday=0, tm_mon=65536,` +
   263  				`tm_year=20865, tm_wday=0, tm_yday=0, tm_isdst=0}}) = -1 EINVAL (Invalid argument)`,
   264  			`
   265  r0 = openat$rtc(0xffffffffffffff9c, &(0x7f0000000000), 0x0, 0x0)
   266  ioctl$RTC_WKALM_SET(r0, 0x4028700f, &(0x7f0000000040)={0x0, 0x0, {0x0, 0x0, 0x0, 0x0, 0x10000, 0x5181}})`,
   267  		},
   268  	}
   269  	target, err := prog.GetTarget(targets.Linux, targets.AMD64)
   270  	if err != nil {
   271  		t.Fatal(err)
   272  	}
   273  	target.ConstMap = make(map[string]uint64)
   274  	for _, c := range target.Consts {
   275  		target.ConstMap[c.Name] = c.Value
   276  	}
   277  	for _, test := range tests {
   278  		input := strings.TrimSpace(test.input)
   279  		tree, err := parser.ParseData([]byte(input))
   280  		if err != nil {
   281  			t.Fatal(err)
   282  		}
   283  		p := genProg(tree.TraceMap[tree.RootPid], target)
   284  		if p == nil {
   285  			t.Fatalf("failed to parse trace")
   286  		}
   287  		got := string(bytes.TrimSpace(p.Serialize()))
   288  		want := strings.TrimSpace(test.output)
   289  		if want != got {
   290  			t.Errorf("input:\n%v\n\nwant:\n%v\n\ngot:\n%v", input, want, got)
   291  		}
   292  	}
   293  }