Best Syzkaller code snippet using compiler.checkVarlens
check.go
Source:check.go  
...21	comp.checkUsed()22	comp.checkRecursion()23	comp.checkLenTargets()24	comp.checkConstructors()25	comp.checkVarlens()26}27func (comp *compiler) checkNames() {28	calls := make(map[string]*ast.Call)29	for _, decl := range comp.desc.Nodes {30		switch decl.(type) {31		case *ast.Resource, *ast.Struct:32			pos, typ, name := decl.Info()33			if reservedName[name] {34				comp.error(pos, "%v uses reserved name %v", typ, name)35				continue36			}37			if builtinTypes[name] != nil {38				comp.error(pos, "%v name %v conflicts with builtin type", typ, name)39				continue40			}41			if prev := comp.resources[name]; prev != nil {42				comp.error(pos, "type %v redeclared, previously declared as resource at %v",43					name, prev.Pos)44				continue45			}46			if prev := comp.structs[name]; prev != nil {47				_, typ, _ := prev.Info()48				comp.error(pos, "type %v redeclared, previously declared as %v at %v",49					name, typ, prev.Pos)50				continue51			}52			if res, ok := decl.(*ast.Resource); ok {53				comp.resources[name] = res54			} else if str, ok := decl.(*ast.Struct); ok {55				comp.structs[name] = str56			}57		case *ast.IntFlags:58			n := decl.(*ast.IntFlags)59			name := n.Name.Name60			if reservedName[name] {61				comp.error(n.Pos, "flags uses reserved name %v", name)62				continue63			}64			if prev := comp.intFlags[name]; prev != nil {65				comp.error(n.Pos, "flags %v redeclared, previously declared at %v",66					name, prev.Pos)67				continue68			}69			comp.intFlags[name] = n70		case *ast.StrFlags:71			n := decl.(*ast.StrFlags)72			name := n.Name.Name73			if reservedName[name] {74				comp.error(n.Pos, "string flags uses reserved name %v", name)75				continue76			}77			if prev := comp.strFlags[name]; prev != nil {78				comp.error(n.Pos, "string flags %v redeclared, previously declared at %v",79					name, prev.Pos)80				continue81			}82			comp.strFlags[name] = n83		case *ast.Call:84			c := decl.(*ast.Call)85			name := c.Name.Name86			if prev := calls[name]; prev != nil {87				comp.error(c.Pos, "syscall %v redeclared, previously declared at %v",88					name, prev.Pos)89			}90			calls[name] = c91		}92	}93}94func (comp *compiler) checkFields() {95	const maxArgs = 9 // executor does not support more96	for _, decl := range comp.desc.Nodes {97		switch n := decl.(type) {98		case *ast.Struct:99			_, typ, name := n.Info()100			fields := make(map[string]bool)101			for _, f := range n.Fields {102				fn := f.Name.Name103				if fn == "parent" {104					comp.error(f.Pos, "reserved field name %v in %v %v", fn, typ, name)105				}106				if fields[fn] {107					comp.error(f.Pos, "duplicate field %v in %v %v", fn, typ, name)108				}109				fields[fn] = true110			}111			if !n.IsUnion && len(n.Fields) < 1 {112				comp.error(n.Pos, "struct %v has no fields, need at least 1 field", name)113			}114			if n.IsUnion && len(n.Fields) < 2 {115				comp.error(n.Pos, "union %v has only %v field, need at least 2 fields",116					name, len(n.Fields))117			}118		case *ast.Call:119			name := n.Name.Name120			args := make(map[string]bool)121			for _, a := range n.Args {122				an := a.Name.Name123				if an == "parent" {124					comp.error(a.Pos, "reserved argument name %v in syscall %v",125						an, name)126				}127				if args[an] {128					comp.error(a.Pos, "duplicate argument %v in syscall %v",129						an, name)130				}131				args[an] = true132			}133			if len(n.Args) > maxArgs {134				comp.error(n.Pos, "syscall %v has %v arguments, allowed maximum is %v",135					name, len(n.Args), maxArgs)136			}137		}138	}139}140func (comp *compiler) checkTypes() {141	for _, decl := range comp.desc.Nodes {142		switch n := decl.(type) {143		case *ast.Resource:144			comp.checkType(n.Base, false, false, false, true)145		case *ast.Struct:146			for _, f := range n.Fields {147				comp.checkType(f.Type, false, false, !n.IsUnion, false)148			}149			comp.checkStruct(n)150		case *ast.Call:151			for _, a := range n.Args {152				comp.checkType(a.Type, true, false, false, false)153			}154			if n.Ret != nil {155				comp.checkType(n.Ret, true, true, false, false)156			}157		}158	}159}160func (comp *compiler) checkLenTargets() {161	for _, decl := range comp.desc.Nodes {162		switch n := decl.(type) {163		case *ast.Call:164			for _, arg := range n.Args {165				comp.checkLenType(arg.Type, arg.Name.Name, n.Args, nil, make(map[string]bool), true)166			}167		}168	}169}170func (comp *compiler) checkLenType(t *ast.Type, name string, fields []*ast.Field,171	parents []*ast.Struct, checked map[string]bool, isArg bool) {172	desc := comp.getTypeDesc(t)173	if desc == typeStruct {174		s := comp.structs[t.Ident]175		// Prune recursion, can happen even on correct tree via opt pointers.176		if checked[s.Name.Name] {177			return178		}179		checked[s.Name.Name] = true180		parents = append(parents, s)181		for _, fld := range s.Fields {182			comp.checkLenType(fld.Type, fld.Name.Name, s.Fields, parents, checked, false)183		}184		return185	}186	_, args, _ := comp.getArgsBase(t, "", prog.DirIn, isArg)187	for i, arg := range args {188		argDesc := desc.Args[i]189		if argDesc.Type == typeArgLenTarget {190			comp.checkLenTarget(t, name, arg.Ident, fields, parents)191		} else if argDesc.Type == typeArgType {192			comp.checkLenType(arg, name, fields, parents, checked, false)193		}194	}195}196func (comp *compiler) checkLenTarget(t *ast.Type, name, target string, fields []*ast.Field, parents []*ast.Struct) {197	if target == name {198		comp.error(t.Pos, "%v target %v refer to itself", t.Ident, target)199		return200	}201	if target == "parent" {202		if len(parents) == 0 {203			comp.error(t.Pos, "%v target %v does not exist", t.Ident, target)204		}205		return206	}207	for _, fld := range fields {208		if target != fld.Name.Name {209			continue210		}211		if fld.Type == t {212			comp.error(t.Pos, "%v target %v refer to itself", t.Ident, target)213		}214		return215	}216	for _, parent := range parents {217		if target == parent.Name.Name {218			return219		}220	}221	comp.error(t.Pos, "%v target %v does not exist", t.Ident, target)222}223func (comp *compiler) checkUsed() {224	for _, decl := range comp.desc.Nodes {225		switch n := decl.(type) {226		case *ast.Call:227			if n.NR == ^uint64(0) {228				break229			}230			for _, arg := range n.Args {231				comp.checkUsedType(arg.Type, true)232			}233			if n.Ret != nil {234				comp.checkUsedType(n.Ret, true)235			}236		}237	}238}239func (comp *compiler) checkUsedType(t *ast.Type, isArg bool) {240	if comp.used[t.Ident] {241		return242	}243	desc := comp.getTypeDesc(t)244	if desc == typeResource {245		r := comp.resources[t.Ident]246		for r != nil && !comp.used[r.Name.Name] {247			comp.used[r.Name.Name] = true248			r = comp.resources[r.Base.Ident]249		}250		return251	}252	if desc == typeStruct {253		comp.used[t.Ident] = true254		s := comp.structs[t.Ident]255		for _, fld := range s.Fields {256			comp.checkUsedType(fld.Type, false)257		}258		return259	}260	_, args, _ := comp.getArgsBase(t, "", prog.DirIn, isArg)261	for i, arg := range args {262		if desc.Args[i].Type == typeArgType {263			comp.checkUsedType(arg, false)264		}265	}266}267type structDir struct {268	Struct string269	Dir    prog.Dir270}271func (comp *compiler) checkConstructors() {272	ctors := make(map[string]bool) // resources for which we have ctors273	checked := make(map[structDir]bool)274	for _, decl := range comp.desc.Nodes {275		switch n := decl.(type) {276		case *ast.Call:277			for _, arg := range n.Args {278				comp.checkTypeCtors(arg.Type, prog.DirIn, true, ctors, checked)279			}280			if n.Ret != nil {281				comp.checkTypeCtors(n.Ret, prog.DirOut, true, ctors, checked)282			}283		}284	}285	for _, decl := range comp.desc.Nodes {286		switch n := decl.(type) {287		case *ast.Resource:288			name := n.Name.Name289			if !ctors[name] && comp.used[name] {290				comp.error(n.Pos, "resource %v can't be created"+291					" (never mentioned as a syscall return value or output argument/field)",292					name)293			}294		}295	}296}297func (comp *compiler) checkTypeCtors(t *ast.Type, dir prog.Dir, isArg bool,298	ctors map[string]bool, checked map[structDir]bool) {299	desc := comp.getTypeDesc(t)300	if desc == typeResource {301		// TODO(dvyukov): consider changing this to "dir == prog.DirOut".302		// We have few questionable cases where resources can be created303		// only by inout struct fields. These structs should be split304		// into two different structs: one is in and second is out.305		// But that will require attaching dir to individual fields.306		if dir != prog.DirIn {307			r := comp.resources[t.Ident]308			for r != nil && !ctors[r.Name.Name] {309				ctors[r.Name.Name] = true310				r = comp.resources[r.Base.Ident]311			}312		}313		return314	}315	if desc == typeStruct {316		s := comp.structs[t.Ident]317		name := s.Name.Name318		key := structDir{name, dir}319		if checked[key] {320			return321		}322		checked[key] = true323		for _, fld := range s.Fields {324			comp.checkTypeCtors(fld.Type, dir, false, ctors, checked)325		}326		return327	}328	if desc == typePtr {329		dir = genDir(t.Args[0])330	}331	_, args, _ := comp.getArgsBase(t, "", dir, isArg)332	for i, arg := range args {333		if desc.Args[i].Type == typeArgType {334			comp.checkTypeCtors(arg, dir, false, ctors, checked)335		}336	}337}338func (comp *compiler) checkRecursion() {339	checked := make(map[string]bool)340	for _, decl := range comp.desc.Nodes {341		switch n := decl.(type) {342		case *ast.Resource:343			comp.checkResourceRecursion(n)344		case *ast.Struct:345			var path []pathElem346			comp.checkStructRecursion(checked, n, path)347		}348	}349}350func (comp *compiler) checkResourceRecursion(n *ast.Resource) {351	var seen []string352	for n != nil {353		if arrayContains(seen, n.Name.Name) {354			chain := ""355			for _, r := range seen {356				chain += r + "->"357			}358			chain += n.Name.Name359			comp.error(n.Pos, "recursive resource %v", chain)360			return361		}362		seen = append(seen, n.Name.Name)363		n = comp.resources[n.Base.Ident]364	}365}366type pathElem struct {367	Pos    ast.Pos368	Struct string369	Field  string370}371func (comp *compiler) checkStructRecursion(checked map[string]bool, n *ast.Struct, path []pathElem) {372	name := n.Name.Name373	if checked[name] {374		return375	}376	for i, elem := range path {377		if elem.Struct != name {378			continue379		}380		path = path[i:]381		str := ""382		for _, elem := range path {383			str += fmt.Sprintf("%v.%v -> ", elem.Struct, elem.Field)384		}385		str += name386		comp.error(path[0].Pos, "recursive declaration: %v (mark some pointers as opt)", str)387		checked[name] = true388		return389	}390	for _, f := range n.Fields {391		path = append(path, pathElem{392			Pos:    f.Pos,393			Struct: name,394			Field:  f.Name.Name,395		})396		comp.recurseField(checked, f.Type, path)397		path = path[:len(path)-1]398	}399	checked[name] = true400}401func (comp *compiler) recurseField(checked map[string]bool, t *ast.Type, path []pathElem) {402	desc := comp.getTypeDesc(t)403	if desc == typeStruct {404		comp.checkStructRecursion(checked, comp.structs[t.Ident], path)405		return406	}407	_, args, base := comp.getArgsBase(t, "", prog.DirIn, false)408	if desc == typePtr && base.IsOptional {409		return // optional pointers prune recursion410	}411	for i, arg := range args {412		if desc.Args[i].Type == typeArgType {413			comp.recurseField(checked, arg, path)414		}415	}416}417func (comp *compiler) checkStruct(n *ast.Struct) {418	if n.IsUnion {419		comp.parseUnionAttrs(n)420	} else {421		comp.parseStructAttrs(n)422	}423}424func (comp *compiler) checkType(t *ast.Type, isArg, isRet, isStruct, isResourceBase bool) {425	if unexpected, _, ok := checkTypeKind(t, kindIdent); !ok {426		comp.error(t.Pos, "unexpected %v, expect type", unexpected)427		return428	}429	desc := comp.getTypeDesc(t)430	if desc == nil {431		comp.error(t.Pos, "unknown type %v", t.Ident)432		return433	}434	if t.HasColon {435		if !desc.AllowColon {436			comp.error(t.Pos2, "unexpected ':'")437			return438		}439		if !isStruct {440			comp.error(t.Pos2, "unexpected ':', only struct fields can be bitfields")441			return442		}443	}444	if isRet && (!desc.CanBeArg || desc.CantBeRet) {445		comp.error(t.Pos, "%v can't be syscall return", t.Ident)446		return447	}448	if isArg && !desc.CanBeArg {449		comp.error(t.Pos, "%v can't be syscall argument", t.Ident)450		return451	}452	if isResourceBase && !desc.ResourceBase {453		comp.error(t.Pos, "%v can't be resource base (int types can)", t.Ident)454		return455	}456	args, opt := removeOpt(t)457	if opt && (desc.CantBeOpt || isResourceBase) {458		what := "resource base"459		if desc.CantBeOpt {460			what = t.Ident461		}462		pos := t.Args[len(t.Args)-1].Pos463		comp.error(pos, "%v can't be marked as opt", what)464		return465	}466	addArgs := 0467	needBase := !isArg && desc.NeedBase468	if needBase {469		addArgs++ // last arg must be base type, e.g. const[0, int32]470	}471	if len(args) > len(desc.Args)+addArgs || len(args) < len(desc.Args)-desc.OptArgs+addArgs {472		comp.error(t.Pos, "wrong number of arguments for type %v, expect %v",473			t.Ident, expectedTypeArgs(desc, needBase))474		return475	}476	if needBase {477		base := args[len(args)-1]478		args = args[:len(args)-1]479		comp.checkTypeArg(t, base, typeArgBase)480	}481	err0 := comp.errors482	for i, arg := range args {483		if desc.Args[i].Type == typeArgType {484			comp.checkType(arg, false, isRet, false, false)485		} else {486			comp.checkTypeArg(t, arg, desc.Args[i])487		}488	}489	if err0 != comp.errors {490		return491	}492	if desc.Check != nil {493		_, args, base := comp.getArgsBase(t, "", prog.DirIn, isArg)494		desc.Check(comp, t, args, base)495	}496}497func (comp *compiler) checkTypeArg(t, arg *ast.Type, argDesc namedArg) {498	desc := argDesc.Type499	if len(desc.Names) != 0 {500		if unexpected, _, ok := checkTypeKind(arg, kindIdent); !ok {501			comp.error(arg.Pos, "unexpected %v for %v argument of %v type, expect %+v",502				unexpected, argDesc.Name, t.Ident, desc.Names)503			return504		}505		if !arrayContains(desc.Names, arg.Ident) {506			comp.error(arg.Pos, "unexpected value %v for %v argument of %v type, expect %+v",507				arg.Ident, argDesc.Name, t.Ident, desc.Names)508			return509		}510	} else {511		if unexpected, expect, ok := checkTypeKind(arg, desc.Kind); !ok {512			comp.error(arg.Pos, "unexpected %v for %v argument of %v type, expect %v",513				unexpected, argDesc.Name, t.Ident, expect)514			return515		}516	}517	if !desc.AllowColon && arg.HasColon {518		comp.error(arg.Pos2, "unexpected ':'")519		return520	}521	if desc.Check != nil {522		desc.Check(comp, arg)523	}524}525func expectedTypeArgs(desc *typeDesc, needBase bool) string {526	expect := ""527	for i, arg := range desc.Args {528		if expect != "" {529			expect += ", "530		}531		opt := i >= len(desc.Args)-desc.OptArgs532		if opt {533			expect += "["534		}535		expect += arg.Name536		if opt {537			expect += "]"538		}539	}540	if needBase {541		if expect != "" {542			expect += ", "543		}544		expect += typeArgBase.Name545	}546	if !desc.CantBeOpt {547		if expect != "" {548			expect += ", "549		}550		expect += "[opt]"551	}552	if expect == "" {553		expect = "no arguments"554	}555	return expect556}557func checkTypeKind(t *ast.Type, kind int) (unexpected string, expect string, ok bool) {558	switch {559	case kind == kindAny:560		ok = true561	case t.String != "":562		ok = kind == kindString563		if !ok {564			unexpected = fmt.Sprintf("string %q", t.String)565		}566	case t.Ident != "":567		ok = kind == kindIdent568		if !ok {569			unexpected = fmt.Sprintf("identifier %v", t.Ident)570		}571	default:572		ok = kind == kindInt573		if !ok {574			unexpected = fmt.Sprintf("int %v", t.Value)575		}576	}577	if !ok {578		switch kind {579		case kindString:580			expect = "string"581		case kindIdent:582			expect = "identifier"583		case kindInt:584			expect = "int"585		}586	}587	return588}589func (comp *compiler) checkVarlens() {590	for _, decl := range comp.desc.Nodes {591		switch n := decl.(type) {592		case *ast.Struct:593			comp.checkVarlen(n)594		}595	}596}597func (comp *compiler) isVarlen(t *ast.Type) bool {598	desc, args, base := comp.getArgsBase(t, "", prog.DirIn, false)599	return desc.Varlen != nil && desc.Varlen(comp, t, args, base)600}601func (comp *compiler) checkVarlen(n *ast.Struct) {602	// Non-varlen unions can't have varlen fields.603	// Non-packed structs can't have varlen fields in the middle....checkVarlens
Using AI Code Generation
1import (2func main() {3	compiler := compiler.NewCompiler()4	compiler.CheckVarlens()5	fmt.Println("CheckVarlens method executed successfully")6}checkVarlens
Using AI Code Generation
1import (2func main() {3	c := compiler.NewCompiler()4	c.CheckVarlens()5	fmt.Println("Done")6}checkVarlens
Using AI Code Generation
1import (2func main() {3	compilerObj := compiler.New()4	pretty.Println(compilerObj.CheckVarlens([]string{"abc", "de", "fgh"}))5	fmt.Println(compilerObj.CheckVarlens([]string{"abc", "", "fgh"}))6}checkVarlens
Using AI Code Generation
1import (2func main() {3	compiler := new(Compiler.Compiler)4	compiler.CheckVarlens()5	fmt.Println("Done")6}checkVarlens
Using AI Code Generation
1import (2func main() {3	comp := compiler.NewCompiler()4	vars := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}5	varlens := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}6	comp.CheckVarlens(vars, varlens)7	fmt.Println("Length of the variable list: ", len(comp.Varlist))8	fmt.Println("Length of the variable length list: ", len(comp.Varlens))9}checkVarlens
Using AI Code Generation
1import "fmt"2import "mycompiler"3func main() {4  var k []int = []int{1,2,3,4}5  var l []int = []int{1,2,3,4,5}6  var m [5]int = [5]int{1,2,3,4,5}7  var n [5]int = [5]int{1,2,3,4,5}8  var o map[int]string = map[int]string{1:"one",2:"two"}9  var p map[int]string = map[int]string{1:"one",2:"two",3:"three"}10  var q chan int = make(chan int)11  var r chan int = make(chan int)12  var s func() int = func() int {return 10}13  var t func() int = func() int {return 10}14  var u interface{} = 1015  var v interface{} = 1016  var w interface{} = "hello"17  var x interface{} = "hello"18  var y interface{} = 10.219  var z interface{} = 10.220  var x1 []int = []int{1,2,3,4}21  var y1 []int = []int{1,2,3,4,5}22  var z1 []int = []int{1,2,3,4}23  var x2 [5]int = [5]int{1,2,3,4,5}24  var y2 [5]int = [5]int{1,2,3,4,5}25  var z2 [5]int = [5]int{1,2,3,4,5}26  var x3 map[int]string = map[int]string{1:"one",2:"two"}27  var y3 map[int]string = map[int]string{1:"one",2:"two",3:"three"}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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
