github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/tools/syz-trace2syz/proggen/generate_unions.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  	"github.com/google/syzkaller/pkg/log"
    10  	"github.com/google/syzkaller/prog"
    11  	"github.com/google/syzkaller/tools/syz-trace2syz/parser"
    12  )
    13  
    14  func (ctx *context) genSockaddrStorage(syzType *prog.UnionType, dir prog.Dir, straceType parser.IrType) prog.Arg {
    15  	field2Opt := make(map[string]int)
    16  	for i, field := range syzType.Fields {
    17  		field2Opt[field.Name] = i
    18  	}
    19  	idx := 0
    20  	switch strType := straceType.(type) {
    21  	case *parser.GroupType:
    22  		socketFamily, ok := strType.Elems[0].(parser.Constant)
    23  		if !ok {
    24  			log.Fatalf("failed to identify socket family when generating sockaddr stroage union. "+
    25  				"expected constant got: %#v", strType.Elems[0])
    26  		}
    27  		switch socketFamily.Val() {
    28  		case ctx.target.ConstMap["AF_INET6"]:
    29  			idx = field2Opt["in6"]
    30  		case ctx.target.ConstMap["AF_INET"]:
    31  			idx = field2Opt["in"]
    32  		case ctx.target.ConstMap["AF_UNIX"]:
    33  			idx = field2Opt["un"]
    34  		case ctx.target.ConstMap["AF_UNSPEC"]:
    35  			idx = field2Opt["nl"]
    36  		case ctx.target.ConstMap["AF_NETLINK"]:
    37  			idx = field2Opt["nl"]
    38  		case ctx.target.ConstMap["AF_NFC"]:
    39  			idx = field2Opt["nfc"]
    40  		case ctx.target.ConstMap["AF_PACKET"]:
    41  			idx = field2Opt["ll"]
    42  		}
    43  
    44  	default:
    45  		log.Fatalf("unable to parse sockaddr_storage. Unsupported type: %#v", strType)
    46  	}
    47  	return prog.MakeUnionArg(syzType, dir, ctx.genArg(syzType.Fields[idx].Type, dir, straceType), idx)
    48  }
    49  
    50  func (ctx *context) genSockaddrNetlink(syzType *prog.UnionType, dir prog.Dir, straceType parser.IrType) prog.Arg {
    51  	var idx = 2
    52  	field2Opt := make(map[string]int)
    53  	for i, field := range syzType.Fields {
    54  		field2Opt[field.Name] = i
    55  	}
    56  	switch a := straceType.(type) {
    57  	case *parser.GroupType:
    58  		if len(a.Elems) > 2 {
    59  			switch b := a.Elems[1].(type) {
    60  			case parser.Constant:
    61  				pid := b.Val()
    62  				if pid > 0 {
    63  					// User
    64  					idx = field2Opt["proc"]
    65  				} else if pid == 0 {
    66  					// Kernel
    67  					idx = field2Opt["kern"]
    68  				} else {
    69  					// Unspec
    70  					idx = field2Opt["unspec"]
    71  				}
    72  			default:
    73  				log.Fatalf("unable to parse netlink addr struct. Unsupported type: %#v", a)
    74  			}
    75  		}
    76  	}
    77  	return prog.MakeUnionArg(syzType, dir, ctx.genArg(syzType.Fields[idx].Type, dir, straceType), idx)
    78  }
    79  
    80  func (ctx *context) genIfrIfru(syzType *prog.UnionType, dir prog.Dir, straceType parser.IrType) prog.Arg {
    81  	idx := 0
    82  	switch straceType.(type) {
    83  	case parser.Constant:
    84  		idx = 2
    85  	}
    86  	return prog.MakeUnionArg(syzType, dir, ctx.genArg(syzType.Fields[idx].Type, dir, straceType), idx)
    87  }