How to use genFieldArray method of compiler Package

Best Syzkaller code snippet using compiler.genFieldArray

gen.go

Source:gen.go Github

copy

Full Screen

...58 return &prog.Syscall{59 Name: n.Name.Name,60 CallName: n.CallName,61 NR: n.NR,62 Args: comp.genFieldArray(n.Args, prog.DirIn, true),63 Ret: ret,64 }65}66func (comp *compiler) genStructDescs(syscalls []*prog.Syscall) []*prog.KeyedStruct {67 // Calculate struct/union/array sizes, add padding to structs and detach68 // StructDesc's from StructType's. StructType's can be recursive so it's69 // not possible to write them out inline as other types. To break the70 // recursion detach them, and write StructDesc's out as separate array71 // of KeyedStruct's. prog package will reattach them during init.72 padded := make(map[interface{}]bool)73 detach := make(map[**prog.StructDesc]bool)74 var structs []*prog.KeyedStruct75 var rec func(t prog.Type)76 checkStruct := func(key prog.StructKey, descp **prog.StructDesc) bool {77 detach[descp] = true78 desc := *descp79 if padded[desc] {80 return false81 }82 padded[desc] = true83 for _, f := range desc.Fields {84 rec(f)85 if !f.Varlen() && f.Size() == sizeUnassigned {86 // An inner struct is not padded yet.87 // Leave this struct for next iteration.88 delete(padded, desc)89 return false90 }91 }92 if comp.used[key.Name] {93 structs = append(structs, &prog.KeyedStruct{94 Key: key,95 Desc: desc,96 })97 }98 return true99 }100 rec = func(t0 prog.Type) {101 switch t := t0.(type) {102 case *prog.PtrType:103 rec(t.Type)104 case *prog.ArrayType:105 if padded[t] {106 return107 }108 rec(t.Type)109 if !t.Type.Varlen() && t.Type.Size() == sizeUnassigned {110 // An inner struct is not padded yet.111 // Leave this array for next iteration.112 return113 }114 padded[t] = true115 t.TypeSize = 0116 if t.Kind == prog.ArrayRangeLen && t.RangeBegin == t.RangeEnd && !t.Type.Varlen() {117 t.TypeSize = t.RangeBegin * t.Type.Size()118 }119 case *prog.StructType:120 if !checkStruct(t.Key, &t.StructDesc) {121 return122 }123 structNode := comp.structNodes[t.StructDesc]124 // Add paddings, calculate size, mark bitfields.125 varlen := false126 for _, f := range t.Fields {127 if f.Varlen() {128 varlen = true129 }130 }131 comp.markBitfields(t.Fields)132 packed, sizeAttr, alignAttr := comp.parseStructAttrs(structNode)133 t.Fields = comp.addAlignment(t.Fields, varlen, packed, alignAttr)134 t.AlignAttr = alignAttr135 t.TypeSize = 0136 if !varlen {137 for _, f := range t.Fields {138 if !f.BitfieldMiddle() {139 t.TypeSize += f.Size()140 }141 }142 if sizeAttr != sizeUnassigned {143 if t.TypeSize > sizeAttr {144 comp.error(structNode.Pos, "struct %v has size attribute %v"+145 " which is less than struct size %v",146 structNode.Name.Name, sizeAttr, t.TypeSize)147 }148 if pad := sizeAttr - t.TypeSize; pad != 0 {149 t.Fields = append(t.Fields, genPad(pad))150 }151 t.TypeSize = sizeAttr152 }153 }154 case *prog.UnionType:155 if !checkStruct(t.Key, &t.StructDesc) {156 return157 }158 structNode := comp.structNodes[t.StructDesc]159 varlen, sizeAttr := comp.parseUnionAttrs(structNode)160 t.TypeSize = 0161 if !varlen {162 for _, fld := range t.Fields {163 sz := fld.Size()164 if sizeAttr != sizeUnassigned && sz > sizeAttr {165 comp.error(structNode.Pos, "union %v has size attribute %v"+166 " which is less than field %v size %v",167 structNode.Name.Name, sizeAttr, fld.Name(), sz)168 }169 if t.TypeSize < sz {170 t.TypeSize = sz171 }172 }173 if sizeAttr != sizeUnassigned {174 t.TypeSize = sizeAttr175 }176 }177 }178 }179 // We have to do this in the loop until we pad nothing new180 // due to recursive structs.181 for {182 start := len(padded)183 for _, c := range syscalls {184 for _, a := range c.Args {185 rec(a)186 }187 if c.Ret != nil {188 rec(c.Ret)189 }190 }191 if start == len(padded) {192 break193 }194 }195 // Detach StructDesc's from StructType's. prog will reattach them again.196 for descp := range detach {197 *descp = nil198 }199 sort.Slice(structs, func(i, j int) bool {200 si, sj := structs[i], structs[j]201 if si.Key.Name != sj.Key.Name {202 return si.Key.Name < sj.Key.Name203 }204 return si.Key.Dir < sj.Key.Dir205 })206 return structs207}208func (comp *compiler) genStructDesc(res *prog.StructDesc, n *ast.Struct, dir prog.Dir, varlen bool) {209 // Leave node for genStructDescs to calculate size/padding.210 comp.structNodes[res] = n211 common := genCommon(n.Name.Name, "", sizeUnassigned, dir, false)212 common.IsVarlen = varlen213 *res = prog.StructDesc{214 TypeCommon: common,215 Fields: comp.genFieldArray(n.Fields, dir, false),216 }217}218func (comp *compiler) markBitfields(fields []prog.Type) {219 var bfOffset uint64220 for i, f := range fields {221 if f.BitfieldLength() == 0 {222 continue223 }224 off, middle := bfOffset, true225 bfOffset += f.BitfieldLength()226 if i == len(fields)-1 || // Last bitfield in a group, if last field of the struct...227 fields[i+1].BitfieldLength() == 0 || // or next field is not a bitfield...228 f.Size() != fields[i+1].Size() || // or next field is of different size...229 bfOffset+fields[i+1].BitfieldLength() > f.Size()*8 { // or next field does not fit into the current group.230 middle, bfOffset = false, 0231 }232 setBitfieldOffset(f, off, middle)233 }234}235func setBitfieldOffset(t0 prog.Type, offset uint64, middle bool) {236 switch t := t0.(type) {237 case *prog.IntType:238 t.BitfieldOff, t.BitfieldMdl = offset, middle239 case *prog.ConstType:240 t.BitfieldOff, t.BitfieldMdl = offset, middle241 case *prog.LenType:242 t.BitfieldOff, t.BitfieldMdl = offset, middle243 case *prog.FlagsType:244 t.BitfieldOff, t.BitfieldMdl = offset, middle245 case *prog.ProcType:246 t.BitfieldOff, t.BitfieldMdl = offset, middle247 default:248 panic(fmt.Sprintf("type %#v can't be a bitfield", t))249 }250}251func (comp *compiler) addAlignment(fields []prog.Type, varlen, packed bool, alignAttr uint64) []prog.Type {252 var newFields []prog.Type253 if packed {254 // If a struct is packed, statically sized and has explicitly set alignment,255 // add a padding at the end.256 newFields = fields257 if !varlen && alignAttr != 0 {258 size := uint64(0)259 for _, f := range fields {260 if !f.BitfieldMiddle() {261 size += f.Size()262 }263 }264 if tail := size % alignAttr; tail != 0 {265 newFields = append(newFields, genPad(alignAttr-tail))266 }267 }268 return newFields269 }270 var align, off uint64271 for i, f := range fields {272 if i == 0 || !fields[i-1].BitfieldMiddle() {273 a := comp.typeAlign(f)274 if align < a {275 align = a276 }277 // Append padding if the last field is not a bitfield or it's the last bitfield in a set.278 if off%a != 0 {279 pad := a - off%a280 off += pad281 newFields = append(newFields, genPad(pad))282 }283 }284 newFields = append(newFields, f)285 if !f.BitfieldMiddle() && (i != len(fields)-1 || !f.Varlen()) {286 // Increase offset if the current field is not a bitfield287 // or it's the last bitfield in a set, except when it's288 // the last field in a struct and has variable length.289 off += f.Size()290 }291 }292 if alignAttr != 0 {293 align = alignAttr294 }295 if align != 0 && off%align != 0 && !varlen {296 pad := align - off%align297 off += pad298 newFields = append(newFields, genPad(pad))299 }300 return newFields301}302func (comp *compiler) typeAlign(t0 prog.Type) uint64 {303 switch t0.(type) {304 case *prog.IntType, *prog.ConstType, *prog.LenType, *prog.FlagsType, *prog.ProcType,305 *prog.CsumType, *prog.PtrType, *prog.VmaType, *prog.ResourceType:306 return t0.Size()307 case *prog.BufferType:308 return 1309 }310 switch t := t0.(type) {311 case *prog.ArrayType:312 return comp.typeAlign(t.Type)313 case *prog.StructType:314 packed, _, alignAttr := comp.parseStructAttrs(comp.structNodes[t.StructDesc])315 if alignAttr != 0 {316 return alignAttr // overrided by user attribute317 }318 if packed {319 return 1320 }321 align := uint64(0)322 for _, f := range t.Fields {323 if a := comp.typeAlign(f); align < a {324 align = a325 }326 }327 return align328 case *prog.UnionType:329 align := uint64(0)330 for _, f := range t.Fields {331 if a := comp.typeAlign(f); align < a {332 align = a333 }334 }335 return align336 default:337 panic(fmt.Sprintf("unknown type: %#v", t))338 }339}340func genPad(size uint64) prog.Type {341 return &prog.ConstType{342 IntTypeCommon: genIntCommon(genCommon("pad", "", size, prog.DirIn, false), 0, false),343 IsPad: true,344 }345}346func (comp *compiler) genField(f *ast.Field, dir prog.Dir, isArg bool) prog.Type {347 return comp.genType(f.Type, f.Name.Name, dir, isArg)348}349func (comp *compiler) genFieldArray(fields []*ast.Field, dir prog.Dir, isArg bool) []prog.Type {350 var res []prog.Type351 for _, f := range fields {352 res = append(res, comp.genField(f, dir, isArg))353 }354 return res355}356func (comp *compiler) genType(t *ast.Type, field string, dir prog.Dir, isArg bool) prog.Type {357 desc, args, base := comp.getArgsBase(t, field, dir, isArg)358 if desc.Gen == nil {359 panic(fmt.Sprintf("no gen for %v %#v", field, t))360 }361 base.IsVarlen = desc.Varlen != nil && desc.Varlen(comp, t, args)362 return desc.Gen(comp, t, args, base)363}...

Full Screen

Full Screen

genFieldArray

Using AI Code Generation

copy

Full Screen

1import (2type compiler struct {3}4func (c *compiler) genFieldArray(v reflect.Value) []string {5 for i := 0; i < v.NumField(); i++ {6 fields = append(fields, v.Type().Field(i).Name)7 }8}9type myStruct struct {10}11func main() {12 c := compiler{}13 s := myStruct{Field1: "Hello", Field2: "World"}14 fmt.Println(c.genFieldArray(reflect.ValueOf(s)))15}

Full Screen

Full Screen

genFieldArray

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 p := program.NewProgram()4 p.AddSource("main.c", `int main() {5 int a[10];6 return 0;7 }`)8 err := p.ParseAST()9 if err != nil {10 panic(err)11 }12 err = p.ResolveTypes()13 if err != nil {14 panic(err)15 }16 fmt.Println(p.GenFieldArray())17}18import "unsafe"19func main() {20}

Full Screen

Full Screen

genFieldArray

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 var s1 struct {4 }5 fmt.Println(reflect.TypeOf(s1).Field(0).Name)6}7import (8type struct {9}10func main() {11 var s1 struct {12 }13 fmt.Println(reflect.TypeOf(s1).Field(0).Name)14}15import (16type s1 struct {17}18func main() {19 var s1 struct {20 }21 fmt.Println(reflect.TypeOf(s1).Field(0).Name)22}23import (24type s1 struct {25}26func main() {27 fmt.Println(reflect.TypeOf(s1).Field(0).Name)28}29import (30type s1 struct {31}32func main() {33 fmt.Println(reflect.TypeOf(s1).Field(0).Name)34}35import (36type s1 struct {37}38func main() {39 fmt.Println(reflect.TypeOf(s1).Field(0).Name)40}41import (42type s1 struct {43}44func main() {45 fmt.Println(reflect.TypeOf(s1).Field(0).Name)46}

Full Screen

Full Screen

genFieldArray

Using AI Code Generation

copy

Full Screen

1func main() {2 c := compiler.New()3 c.GenFieldArray()4}5func main() {6 c := compiler.New()7 c.GenMethodArray()8}9func main() {10 c := compiler.New()11 c.GenInterfaceArray()12}13func main() {14 c := compiler.New()15 c.GenTypeArray()16}17func main() {18 c := compiler.New()19 c.GenFuncArray()20}21func main() {22 c := compiler.New()23 c.GenConstArray()24}25func main() {26 c := compiler.New()27 c.GenVarArray()28}29func main() {30 c := compiler.New()31 c.GenFieldArray()32}33func main() {34 c := compiler.New()35 c.GenTypeArray()36}

Full Screen

Full Screen

genFieldArray

Using AI Code Generation

copy

Full Screen

1import (2type sample struct {3}4func main() {5 s := sample{6 }7 v := reflect.ValueOf(s)8 t := v.Type()9 c := compiler{}10 f := c.genFieldArray(t)11 fmt.Println(f)12}

Full Screen

Full Screen

genFieldArray

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 c.genFieldArray()4}5type compiler struct {6}7func (c *compiler) genFieldArray() {8 s := student{}9 t := reflect.TypeOf(s)10 for i := 0; i < t.NumField(); i++ {11 f := field{}12 field := t.Field(i)13 c.fields = append(c.fields, f)14 }15 fmt.Println(c.fields)16}17type field struct {18}19type student struct {20}21[{name string json:"name"} {age int json:"age"} {marks float64 json:"marks"}]

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Syzkaller automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful