Best Syzkaller code snippet using compiler.genType
gen.go
Source:gen.go
...34 }35 if len(res.Values) == 0 {36 res.Values = []uint64{0}37 }38 res.Type = comp.genType(base, "", prog.DirIn, false)39 return res40}41func (comp *compiler) genSyscalls() []*prog.Syscall {42 var calls []*prog.Syscall43 for _, decl := range comp.desc.Nodes {44 if n, ok := decl.(*ast.Call); ok && n.NR != ^uint64(0) {45 calls = append(calls, comp.genSyscall(n))46 }47 }48 sort.Slice(calls, func(i, j int) bool {49 return calls[i].Name < calls[j].Name50 })51 for i, c := range calls {52 c.ID = i53 }54 return calls55}56func (comp *compiler) genSyscall(n *ast.Call) *prog.Syscall {57 var ret prog.Type58 if n.Ret != nil {59 ret = comp.genType(n.Ret, "ret", prog.DirOut, true)60 }61 return &prog.Syscall{62 Name: n.Name.Name,63 CallName: n.CallName,64 NR: n.NR,65 Args: comp.genFieldArray(n.Args, prog.DirIn, true),66 Ret: ret,67 }68}69func (comp *compiler) genStructDescs(syscalls []*prog.Syscall) []*prog.KeyedStruct {70 // Calculate struct/union/array sizes, add padding to structs and detach71 // StructDesc's from StructType's. StructType's can be recursive so it's72 // not possible to write them out inline as other types. To break the73 // recursion detach them, and write StructDesc's out as separate array74 // of KeyedStruct's. prog package will reattach them during init.75 padded := make(map[interface{}]bool)76 detach := make(map[**prog.StructDesc]bool)77 var structs []*prog.KeyedStruct78 var rec func(t prog.Type)79 checkStruct := func(key prog.StructKey, descp **prog.StructDesc) bool {80 detach[descp] = true81 desc := *descp82 if padded[desc] {83 return false84 }85 padded[desc] = true86 for _, f := range desc.Fields {87 rec(f)88 if !f.Varlen() && f.Size() == sizeUnassigned {89 // An inner struct is not padded yet.90 // Leave this struct for next iteration.91 delete(padded, desc)92 return false93 }94 }95 if comp.used[key.Name] {96 structs = append(structs, &prog.KeyedStruct{97 Key: key,98 Desc: desc,99 })100 }101 return true102 }103 rec = func(t0 prog.Type) {104 switch t := t0.(type) {105 case *prog.PtrType:106 rec(t.Type)107 case *prog.ArrayType:108 if padded[t] {109 return110 }111 rec(t.Type)112 if !t.Type.Varlen() && t.Type.Size() == sizeUnassigned {113 // An inner struct is not padded yet.114 // Leave this array for next iteration.115 return116 }117 padded[t] = true118 t.TypeSize = 0119 if t.Kind == prog.ArrayRangeLen && t.RangeBegin == t.RangeEnd && !t.Type.Varlen() {120 t.TypeSize = t.RangeBegin * t.Type.Size()121 }122 case *prog.StructType:123 if !checkStruct(t.Key, &t.StructDesc) {124 return125 }126 // Add paddings, calculate size, mark bitfields.127 varlen := false128 for _, f := range t.Fields {129 if f.Varlen() {130 varlen = true131 }132 }133 comp.markBitfields(t.Fields)134 packed, alignAttr := comp.parseStructAttrs(comp.structNodes[t.StructDesc])135 t.Fields = comp.addAlignment(t.Fields, varlen, packed, alignAttr)136 t.AlignAttr = alignAttr137 t.TypeSize = 0138 if !varlen {139 for _, f := range t.Fields {140 if !f.BitfieldMiddle() {141 t.TypeSize += f.Size()142 }143 }144 }145 case *prog.UnionType:146 if !checkStruct(t.Key, &t.StructDesc) {147 return148 }149 t.TypeSize = 0150 varlen := comp.parseUnionAttrs(comp.structNodes[t.StructDesc])151 if !varlen {152 for _, fld := range t.Fields {153 if t.TypeSize < fld.Size() {154 t.TypeSize = fld.Size()155 }156 }157 }158 }159 }160 // We have to do this in the loop until we pad nothing new161 // due to recursive structs.162 for {163 start := len(padded)164 for _, c := range syscalls {165 for _, a := range c.Args {166 rec(a)167 }168 if c.Ret != nil {169 rec(c.Ret)170 }171 }172 if start == len(padded) {173 break174 }175 }176 // Detach StructDesc's from StructType's. prog will reattach them again.177 for descp := range detach {178 *descp = nil179 }180 sort.Slice(structs, func(i, j int) bool {181 si, sj := structs[i], structs[j]182 if si.Key.Name != sj.Key.Name {183 return si.Key.Name < sj.Key.Name184 }185 return si.Key.Dir < sj.Key.Dir186 })187 return structs188}189func (comp *compiler) genStructDesc(res *prog.StructDesc, n *ast.Struct, dir prog.Dir) {190 // Leave node for genStructDescs to calculate size/padding.191 comp.structNodes[res] = n192 *res = prog.StructDesc{193 TypeCommon: genCommon(n.Name.Name, "", sizeUnassigned, dir, false),194 Fields: comp.genFieldArray(n.Fields, dir, false),195 }196}197func (comp *compiler) isStructVarlen(name string) bool {198 if varlen, ok := comp.structVarlen[name]; ok {199 return varlen200 }201 s := comp.structs[name]202 if s.IsUnion && comp.parseUnionAttrs(s) {203 comp.structVarlen[name] = true204 return true205 }206 comp.structVarlen[name] = false // to not hang on recursive types207 varlen := false208 for _, fld := range s.Fields {209 if comp.isVarlen(fld.Type) {210 varlen = true211 break212 }213 }214 comp.structVarlen[name] = varlen215 return varlen216}217func (comp *compiler) markBitfields(fields []prog.Type) {218 var bfOffset uint64219 for i, f := range fields {220 if f.BitfieldLength() == 0 {221 continue222 }223 off, middle := bfOffset, true224 bfOffset += f.BitfieldLength()225 if i == len(fields)-1 || // Last bitfield in a group, if last field of the struct...226 fields[i+1].BitfieldLength() == 0 || // or next field is not a bitfield...227 f.Size() != fields[i+1].Size() || // or next field is of different size...228 bfOffset+fields[i+1].BitfieldLength() > f.Size()*8 { // or next field does not fit into the current group.229 middle, bfOffset = false, 0230 }231 setBitfieldOffset(f, off, middle)232 }233}234func setBitfieldOffset(t0 prog.Type, offset uint64, middle bool) {235 switch t := t0.(type) {236 case *prog.IntType:237 t.BitfieldOff, t.BitfieldMdl = offset, middle238 case *prog.ConstType:239 t.BitfieldOff, t.BitfieldMdl = offset, middle240 case *prog.LenType:241 t.BitfieldOff, t.BitfieldMdl = offset, middle242 case *prog.FlagsType:243 t.BitfieldOff, t.BitfieldMdl = offset, middle244 case *prog.ProcType:245 t.BitfieldOff, t.BitfieldMdl = offset, middle246 default:247 panic(fmt.Sprintf("type %#v can't be a bitfield", t))248 }249}250func (comp *compiler) addAlignment(fields []prog.Type, varlen, packed bool, alignAttr uint64) []prog.Type {251 var newFields []prog.Type252 if packed {253 // If a struct is packed, statically sized and has explicitly set alignment,254 // add a padding at the end.255 newFields = fields256 if !varlen && alignAttr != 0 {257 size := uint64(0)258 for _, f := range fields {259 size += f.Size()260 }261 if tail := size % alignAttr; tail != 0 {262 newFields = append(newFields, genPad(alignAttr-tail))263 }264 }265 return newFields266 }267 var align, off uint64268 for i, f := range fields {269 if i > 0 && !fields[i-1].BitfieldMiddle() {270 a := comp.typeAlign(f)271 if align < a {272 align = a273 }274 // Append padding if the last field is not a bitfield or it's the last bitfield in a set.275 if off%a != 0 {276 pad := a - off%a277 off += pad278 newFields = append(newFields, genPad(pad))279 }280 }281 newFields = append(newFields, f)282 if !f.BitfieldMiddle() && (i != len(fields)-1 || !f.Varlen()) {283 // Increase offset if the current field is not a bitfield284 // or it's the last bitfield in a set, except when it's285 // the last field in a struct and has variable length.286 off += f.Size()287 }288 }289 if alignAttr != 0 {290 align = alignAttr291 }292 if align != 0 && off%align != 0 && !varlen {293 pad := align - off%align294 off += pad295 newFields = append(newFields, genPad(pad))296 }297 return newFields298}299func (comp *compiler) typeAlign(t0 prog.Type) uint64 {300 switch t0.(type) {301 case *prog.IntType, *prog.ConstType, *prog.LenType, *prog.FlagsType, *prog.ProcType,302 *prog.CsumType, *prog.PtrType, *prog.VmaType, *prog.ResourceType:303 return t0.Size()304 case *prog.BufferType:305 return 1306 }307 switch t := t0.(type) {308 case *prog.ArrayType:309 return comp.typeAlign(t.Type)310 case *prog.StructType:311 packed, alignAttr := comp.parseStructAttrs(comp.structNodes[t.StructDesc])312 if alignAttr != 0 {313 return alignAttr // overrided by user attribute314 }315 if packed {316 return 1317 }318 align := uint64(0)319 for _, f := range t.Fields {320 if a := comp.typeAlign(f); align < a {321 align = a322 }323 }324 return align325 case *prog.UnionType:326 align := uint64(0)327 for _, f := range t.Fields {328 if a := comp.typeAlign(f); align < a {329 align = a330 }331 }332 return align333 default:334 panic(fmt.Sprintf("unknown type: %#v", t))335 }336}337func genPad(size uint64) prog.Type {338 return &prog.ConstType{339 IntTypeCommon: genIntCommon(genCommon("pad", "", size, prog.DirIn, false), 0, false),340 IsPad: true,341 }342}343func (comp *compiler) genField(f *ast.Field, dir prog.Dir, isArg bool) prog.Type {344 return comp.genType(f.Type, f.Name.Name, dir, isArg)345}346func (comp *compiler) genFieldArray(fields []*ast.Field, dir prog.Dir, isArg bool) []prog.Type {347 var res []prog.Type348 for _, f := range fields {349 res = append(res, comp.genField(f, dir, isArg))350 }351 return res352}353func (comp *compiler) genType(t *ast.Type, field string, dir prog.Dir, isArg bool) prog.Type {354 desc, args, base := comp.getArgsBase(t, field, dir, isArg)355 return desc.Gen(comp, t, args, base)356}357func genCommon(name, field string, size uint64, dir prog.Dir, opt bool) prog.TypeCommon {358 return prog.TypeCommon{359 TypeName: name,360 TypeSize: size,361 FldName: field,362 ArgDir: dir,363 IsOptional: opt,364 }365}366func genIntCommon(com prog.TypeCommon, bitLen uint64, bigEndian bool) prog.IntTypeCommon {367 return prog.IntTypeCommon{...
codegen.go
Source:codegen.go
...47 }48 case ast.GoVarDecl:49 {50 c.write("var ", d.Name)51 c.genType(d.Type)52 c.toInit = append(c.toInit, d)53 }54 case ast.GoFuncDecl:55 c.genFuncDecl(d)56 default:57 panic("got unknow GoDecl in codegen")58 }59}60func (c *Codegen) genFuncDecl(d ast.GoFuncDecl) {61 c.write("func ", d.Name, "(")62 i := 063 for par, typ := range d.Params {64 if i > 0 {65 c.sb.WriteString(", ")66 }67 c.write(par, " ")68 c.genType(typ)69 i++70 }71 c.sb.WriteRune(')')72 if len(d.Returns) > 0 {73 c.sb.WriteRune(' ')74 if len(d.Returns) == 1 {75 c.genType(d.Returns[0])76 } else {77 c.sb.WriteRune('(')78 for i, ret := range d.Returns {79 if i > 0 {80 c.sb.WriteString(", ")81 }82 c.genType(ret)83 }84 c.sb.WriteRune(')')85 }86 }87 if d.Body == nil {88 c.sb.WriteString(" {}")89 } else {90 c.sb.WriteString(" {\n")91 c.withTab(func() {92 c.genExpr(*d.Body)93 })94 c.sb.WriteString("\n}\n\n")95 }96}97func (c *Codegen) genStruct(d ast.GoStruct) {98 c.write("type ", d.Name, " struct {")99 c.withTab(func() {100 for name, field := range d.Fields {101 c.sb.WriteRune('\n')102 c.writeTab(name, " ")103 c.genType(field)104 }105 })106 c.sb.WriteString("\n}\n\n")107}108func (c *Codegen) genInterface(d ast.GoInterface) {109 c.write("type ", d.Name, " interface {")110 c.withTab(func() {111 for _, method := range d.Methods {112 c.sb.WriteRune('\n')113 c.writeTab(method.Name, "(")114 for i, arg := range method.Args {115 if i > 0 {116 c.sb.WriteString(", ")117 }118 c.genType(arg)119 }120 c.sb.WriteString(")")121 if len(method.Ret) == 1 {122 c.sb.WriteRune(' ')123 c.genType(method.Ret[0])124 }125 if len(method.Ret) > 1 {126 c.sb.WriteString(" (")127 for i, ret := range method.Ret {128 if i > 0 {129 c.sb.WriteString(", ")130 }131 c.genType(ret)132 }133 c.sb.WriteRune(')')134 }135 }136 })137 c.sb.WriteString("\n}\n\n")138}139func (c *Codegen) genExpr(exp ast.GoExpr) {140 c.writePos(exp.GetPos())141 switch e := exp.(type) {142 case ast.GoConst:143 c.sb.WriteString(e.V)144 case ast.GoVar:145 {146 if e.Package != "" {147 c.write(e.Package, ".")148 }149 c.write(e.Name)150 }151 case ast.GoFunc:152 {153 c.sb.WriteString("func (")154 i := 0155 for v, ty := range e.Args {156 if i > 0 {157 c.sb.WriteString(", ")158 }159 c.write(v, " ")160 c.genType(ty)161 i++162 }163 c.sb.WriteRune(')')164 if len(e.Returns) > 0 {165 c.sb.WriteRune(' ')166 if len(e.Returns) == 1 {167 c.genType(e.Returns[0])168 } else {169 c.sb.WriteRune('(')170 for i, ty := range e.Returns {171 if i > 0 {172 c.sb.WriteString(", ")173 }174 c.genType(ty)175 }176 c.sb.WriteRune(')')177 }178 }179 c.write(" {\n")180 c.withTab(func() {181 c.sb.WriteString(c.tab)182 c.genExpr(e.Body)183 })184 c.write("\n", c.tab, "}")185 }186 case ast.GoCall:187 {188 c.genExpr(e.Fn)189 c.sb.WriteRune('(')190 for i, arg := range e.Args {191 if i > 0 {192 c.sb.WriteString(", ")193 }194 c.genExpr(arg)195 }196 c.sb.WriteRune(')')197 }198 case ast.GoReturn:199 {200 c.sb.WriteString("return ")201 c.genExpr(e.Exp)202 }203 case ast.GoIf:204 {205 c.sb.WriteString("if ")206 c.genExpr(e.Cond)207 c.sb.WriteString(" {\n")208 c.withTab(func() {209 c.sb.WriteString(c.tab)210 c.genExpr(e.Then)211 })212 c.write("\n", c.tab, "} else {\n")213 c.withTab(func() {214 c.sb.WriteString(c.tab)215 c.genExpr(e.Else)216 })217 c.write("\n", c.tab, "}")218 }219 case ast.GoVarDef:220 {221 c.write("var ", e.Name, " ")222 c.genType(e.Type)223 }224 case ast.GoLet:225 {226 c.write(e.Binder, " := ")227 c.genExpr(e.BindExpr)228 }229 case ast.GoSetvar:230 {231 c.write(e.Name, " = ")232 c.genExpr(e.Exp)233 }234 case ast.GoStmts:235 for i, exp := range e.Exps {236 if i > 0 {237 c.write("\n", c.tab)238 }239 c.genExpr(exp)240 }241 case ast.GoUnit:242 c.sb.WriteString("nil")243 case ast.GoNil:244 c.sb.WriteString("nil")245 case ast.GoWhile:246 {247 c.sb.WriteString("for ")248 c.genExpr(e.Cond)249 c.withTab(func() {250 c.write(" {\n", c.tab)251 for i, exp := range e.Exps {252 if i > 0 {253 c.write("\n", c.tab)254 }255 c.genExpr(exp)256 }257 })258 }259 default:260 panic("unknow GoExpr in codegen")261 }262}263func (c *Codegen) genType(typ ast.GoType) {264 switch t := typ.(type) {265 case ast.GoTConst:266 {267 if t.Package != "" {268 c.write(t.Package, ".")269 }270 c.write(t.Name)271 }272 case ast.GoTFunc:273 {274 c.sb.WriteString("func(")275 c.genType(t.Arg)276 c.sb.WriteString(") ")277 c.genType(t.Ret)278 }279 }280}281func (c *Codegen) write(strs ...string) {282 for _, s := range strs {283 c.sb.WriteString(s)284 }285}286func (c *Codegen) writeTab(strs ...string) {287 c.sb.WriteString(c.tab)288 c.write(strs...)289}290func (c *Codegen) writePos(pos data.Pos) {291 c.write("/*line :", strconv.Itoa(pos.Line), ":", strconv.Itoa(pos.Col), "*/")...
genType
Using AI Code Generation
1import (2func main() {3 fmt.Println("Enter your name: ")4 fmt.Scanln(&name)5 fmt.Println("Hello", name)6}7import (8func main() {9 fmt.Println("Enter your name: ")10 fmt.Scanln(&name)11 fmt.Println("Hello", name)12}13import (14func main() {15 fmt.Println("Enter your name: ")16 fmt.Scanln(&name)17 fmt.Println("Hello", name)18}19import (20func main() {21 fmt.Println("Enter your name: ")22 fmt.Scanln(&name)23 fmt.Println("Hello", name)24}25import (26func main() {27 fmt.Println("Enter your name: ")28 fmt.Scanln(&name)29 fmt.Println("Hello", name)30}31import (32func main() {33 fmt.Println("Enter your name: ")34 fmt.Scanln(&name)35 fmt.Println("Hello", name)36}37import (38func main() {39 fmt.Println("Enter your name: ")40 fmt.Scanln(&name)41 fmt.Println("Hello", name)42}43import (44func main() {45 fmt.Println("Enter your name: ")46 fmt.Scanln(&name)47 fmt.Println("Hello", name)48}49import (50func main() {51 fmt.Println("Enter your name: ")52 fmt.Scanln(&name)53 fmt.Println("Hello", name)54}
genType
Using AI Code Generation
1import (2type Person struct {3}4func main() {5 p := Person{name: "Abhishek", age: 27}6 fmt.Println("Type of p: ", reflect.TypeOf(p))7 fmt.Println("Value of p: ", reflect.ValueOf(p))8}9Value of p: {Abhishek 27}
genType
Using AI Code Generation
1import (2type Foo struct {3}4func main() {5 var t reflect.Type = reflect.TypeOf(Foo{})6 fmt.Println(t)7 fmt.Println(t.Name())8}
genType
Using AI Code Generation
1import (2func main() {3 fmt.Println(reflect.TypeOf(a))4}5import (6func main() {7 fmt.Println(reflect.TypeOf(a))8}9import (10func main() {11 fmt.Println(reflect.TypeOf(a))12}13import (14func main() {15 fmt.Println(reflect.TypeOf(a))16}17import (18func main() {19 fmt.Println(reflect.TypeOf(a))20}21import (22func main() {23 fmt.Println(reflect.TypeOf(a))24}25import (26func main() {27 fmt.Println(reflect.TypeOf(a))28}29import (30func main() {31 fmt.Println(reflect.TypeOf(a))32}33import (34func main() {
genType
Using AI Code Generation
1import (2func main() {3 comp := new(compiler)4 comp.genType("Go")5 fmt.Println(comp)6}7import (8func main() {9 comp := new(compiler)10 comp.genType("Go")11 fmt.Println(comp)12}13import (14func main() {15 comp := new(compiler)16 comp.genType("Go")17 fmt.Println(comp)18}19import (20func main() {21 comp := new(compiler)22 comp.genType("Go")23 fmt.Println(comp)24}25import (26func main() {27 comp := new(compiler)28 comp.genType("Go")29 fmt.Println(comp)30}31import (32func main() {33 comp := new(compiler)34 comp.genType("Go")35 fmt.Println(comp)36}37import (38func main() {39 comp := new(compiler)40 comp.genType("Go")41 fmt.Println(comp)42}43import (44func main() {45 comp := new(compiler)46 comp.genType("Go")47 fmt.Println(comp)48}49import (
genType
Using AI Code Generation
1import java.io.*;2public class genType {3 public static void main(String args[]) {4 try {5 compiler c = new compiler(new BufferedReader(new FileReader(args[0])));6 c.parse();7 c.printParseTree(" ");8 c.printCode(args[1]);9 } catch (Exception e) {10 System.out.println("Exception occurred during parsing");11 }12 }13}14import java.io.*;15public class genType {16 public static void main(String args[]) {17 try {18 compiler c = new compiler(new BufferedReader(new FileReader(args[0])));19 c.parse();20 c.printParseTree(" ");21 c.printCode(args[1]);22 } catch (Exception e) {23 System.out.println("Exception occurred during parsing");24 }25 }26}27import java.io.*;28public class genType {29 public static void main(String args[]) {30 try {31 compiler c = new compiler(new BufferedReader(new FileReader(args[0])));32 c.parse();33 c.printParseTree(" ");34 c.printCode(args[1]);35 } catch (Exception e) {36 System.out.println("Exception occurred during parsing");37 }38 }39}40import java.io.*;41public class genType {42 public static void main(String args[]) {43 try {44 compiler c = new compiler(new BufferedReader(new FileReader(args[0])));
genType
Using AI Code Generation
1import (2func main() {3 c.GenType("int")4 c.GenType("float")5 c.GenType("string")6 fmt.Println(c)7}8import (9func main() {10 c.GenType("int")11 c.GenVar("i")12 c.GenType("float")13 c.GenVar("f")14 c.GenType("string")15 c.GenVar("s")16 fmt.Println(c)17}18import (19func main() {20 c.GenType("int")21 c.GenVar("i")22 c.GenType("float")23 c.GenVar("f")24 c.GenType("string")25 c.GenVar("s")26 c.GenFunc("main")27 fmt.Println(c)28}29import (30func main() {31 c.GenType("int")32 c.GenVar("i")33 c.GenType("float")34 c.GenVar("f")35 c.GenType("string")36 c.GenVar("s")37 c.GenFunc("main")38 c.GenReturn("0")39 fmt.Println(c)40}41import (42func main() {43 c.GenType("int")44 c.GenVar("i")45 c.GenType("float")46 c.GenVar("f")47 c.GenType("string")48 c.GenVar("s")49 c.GenFunc("main")50 c.GenReturn("0")51 fmt.Println(c)52}53import (
genType
Using AI Code Generation
1func (c *compiler) genFunc(f *ast.FuncDecl) string {2 c.genFuncDecl(f)3}4func (c *compiler) genFuncDecl(f *ast.FuncDecl) {5 c.genFuncBody()6}7func (c *compiler) genFuncBody() {8 c.locals = make(map[string]int)9 c.labels = make(map[string]int)10 c.breaks = make(map[int]string)11 c.continues = make(map[int]string)12 c.gotos = make(map[string]int)13 c.switches = make(map[int]string)14 c.cases = make(map[int]string)15 c.caseValues = make(map[int]int64)16 c.caseExprs = make(map[int]ast.Expr)17 c.defaults = make(map[int]string)18 c.params = make(map[string]int)19 c.results = make(map[string]int)
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!!