How to use parseAttrs method of compiler Package

Best Syzkaller code snippet using compiler.parseAttrs

gen.go

Source:gen.go Github

copy

Full Screen

...105 if n.Ret != nil {106 ret = comp.genType(n.Ret, comp.ptrSize)107 }108 var attrs prog.SyscallAttrs109 descAttrs := comp.parseAttrs(callAttrs, n, n.Attrs)110 for desc, val := range descAttrs {111 fld := reflect.ValueOf(&attrs).Elem().FieldByName(desc.Name)112 if desc.HasArg {113 fld.SetUint(val)114 } else {115 fld.SetBool(val != 0)116 }117 }118 return &prog.Syscall{119 Name: n.Name.Name,120 CallName: n.CallName,121 NR: n.NR,122 MissingArgs: len(argSizes) - len(n.Args),123 Args: comp.genFieldArray(n.Args, argSizes),124 Ret: ret,125 Attrs: attrs,126 }127}128type typeProxy struct {129 typ prog.Type130 id string131 ref prog.Ref132 locations []*prog.Type133}134func (comp *compiler) generateTypes(syscalls []*prog.Syscall) []prog.Type {135 // Replace all Type's in the descriptions with Ref's136 // and prepare a sorted array of corresponding real types.137 proxies := make(map[string]*typeProxy)138 prog.ForeachTypePost(syscalls, func(typ prog.Type, ctx prog.TypeCtx) {139 if _, ok := typ.(prog.Ref); ok {140 return141 }142 if !typ.Varlen() && typ.Size() == sizeUnassigned {143 panic("unassigned size")144 }145 id := typ.Name()146 switch typ.(type) {147 case *prog.StructType, *prog.UnionType:148 // There types can be uniquely identified with the name.149 default:150 buf := new(bytes.Buffer)151 serializer.Write(buf, typ)152 id = buf.String()153 }154 proxy := proxies[id]155 if proxy == nil {156 proxy = &typeProxy{157 typ: typ,158 id: id,159 ref: prog.Ref(len(proxies)),160 }161 proxies[id] = proxy162 }163 *ctx.Ptr = proxy.ref164 proxy.locations = append(proxy.locations, ctx.Ptr)165 })166 array := make([]*typeProxy, 0, len(proxies))167 for _, proxy := range proxies {168 array = append(array, proxy)169 }170 sort.Slice(array, func(i, j int) bool {171 return array[i].id < array[j].id172 })173 types := make([]prog.Type, len(array))174 for i, proxy := range array {175 types[i] = proxy.typ176 for _, loc := range proxy.locations {177 *loc = prog.Ref(i)178 }179 }180 return types181}182func (comp *compiler) layoutTypes(syscalls []*prog.Syscall) {183 // Calculate struct/union/array sizes, add padding to structs, mark bitfields.184 padded := make(map[prog.Type]bool)185 prog.ForeachTypePost(syscalls, func(typ prog.Type, _ prog.TypeCtx) {186 comp.layoutType(typ, padded)187 })188}189func (comp *compiler) layoutType(typ prog.Type, padded map[prog.Type]bool) {190 if padded[typ] {191 return192 }193 switch t := typ.(type) {194 case *prog.ArrayType:195 comp.layoutType(t.Elem, padded)196 comp.layoutArray(t)197 case *prog.StructType:198 for _, f := range t.Fields {199 comp.layoutType(f.Type, padded)200 }201 comp.layoutStruct(t)202 case *prog.UnionType:203 for _, f := range t.Fields {204 comp.layoutType(f.Type, padded)205 }206 comp.layoutUnion(t)207 default:208 return209 }210 if !typ.Varlen() && typ.Size() == sizeUnassigned {211 panic("size unassigned")212 }213 padded[typ] = true214}215func (comp *compiler) layoutArray(t *prog.ArrayType) {216 t.TypeSize = 0217 if t.Kind == prog.ArrayRangeLen && t.RangeBegin == t.RangeEnd && !t.Elem.Varlen() {218 t.TypeSize = t.RangeBegin * t.Elem.Size()219 }220}221func (comp *compiler) layoutUnion(t *prog.UnionType) {222 structNode := comp.structs[t.TypeName]223 attrs := comp.parseAttrs(unionAttrs, structNode, structNode.Attrs)224 t.TypeSize = 0225 if attrs[attrVarlen] != 0 {226 return227 }228 sizeAttr, hasSize := attrs[attrSize]229 for i, fld := range t.Fields {230 sz := fld.Size()231 if hasSize && sz > sizeAttr {232 comp.error(structNode.Fields[i].Pos, "union %v has size attribute %v"+233 " which is less than field %v size %v",234 structNode.Name.Name, sizeAttr, fld.Type.Name(), sz)235 }236 if t.TypeSize < sz {237 t.TypeSize = sz238 }239 }240 if hasSize {241 t.TypeSize = sizeAttr242 }243}244func (comp *compiler) layoutStruct(t *prog.StructType) {245 // Add paddings, calculate size, mark bitfields.246 structNode := comp.structs[t.TypeName]247 varlen := false248 for _, f := range t.Fields {249 if f.Varlen() {250 varlen = true251 }252 }253 attrs := comp.parseAttrs(structAttrs, structNode, structNode.Attrs)254 t.AlignAttr = attrs[attrAlign]255 comp.layoutStructFields(t, varlen, attrs[attrPacked] != 0)256 t.TypeSize = 0257 if !varlen {258 for _, f := range t.Fields {259 t.TypeSize += f.Size()260 }261 sizeAttr, hasSize := attrs[attrSize]262 if hasSize {263 if t.TypeSize > sizeAttr {264 comp.error(structNode.Attrs[0].Pos, "struct %v has size attribute %v"+265 " which is less than struct size %v",266 structNode.Name.Name, sizeAttr, t.TypeSize)267 }268 if pad := sizeAttr - t.TypeSize; pad != 0 {269 t.Fields = append(t.Fields, genPad(pad))270 }271 t.TypeSize = sizeAttr272 }273 }274}275func (comp *compiler) layoutStructFields(t *prog.StructType, varlen, packed bool) {276 var newFields []prog.Field277 var structAlign, byteOffset, bitOffset uint64278 for i, field := range t.Fields {279 f := field.Type280 fieldAlign := uint64(1)281 if !packed {282 fieldAlign = f.Alignment()283 if structAlign < fieldAlign {284 structAlign = fieldAlign285 }286 }287 fullBitOffset := byteOffset*8 + bitOffset288 var fieldOffset uint64289 if f.IsBitfield() {290 unitAlign := f.UnitSize()291 if packed {292 unitAlign = 1293 }294 fieldOffset = rounddown(fullBitOffset/8, unitAlign)295 unitBits := f.UnitSize() * 8296 occupiedBits := fullBitOffset - fieldOffset*8297 remainBits := unitBits - occupiedBits298 if remainBits < f.BitfieldLength() {299 fieldOffset = roundup(roundup(fullBitOffset, 8)/8, unitAlign)300 fullBitOffset, bitOffset = 0, 0301 } else if fieldOffset*8 >= fullBitOffset {302 fullBitOffset, bitOffset = fieldOffset*8, 0303 }304 fieldBitOffset := (fullBitOffset - fieldOffset*8) % unitBits305 setBitfieldOffset(f, fieldBitOffset)306 } else {307 fieldOffset = roundup(roundup(fullBitOffset, 8)/8, fieldAlign)308 bitOffset = 0309 }310 if fieldOffset > byteOffset {311 pad := fieldOffset - byteOffset312 byteOffset += pad313 if i != 0 && t.Fields[i-1].IsBitfield() {314 setBitfieldTypeSize(t.Fields[i-1].Type, pad)315 if bitOffset >= 8*pad {316 // The padding is due to bitfields, so consume the bitOffset.317 bitOffset -= 8 * pad318 } else if bitOffset >= 8 {319 // Unclear is this is a bug or not and what to do in this case.320 // But since we don't have any descriptions that trigger this,321 // let's just guard with the panic.322 panic(fmt.Sprintf("bad bitOffset: %v.%v pad=%v bitOffset=%v",323 t.Name(), field.Name, pad, bitOffset))324 }325 } else {326 newFields = append(newFields, genPad(pad))327 }328 }329 if f.IsBitfield() {330 if byteOffset > fieldOffset {331 unitOffset := byteOffset - fieldOffset332 setBitfieldUnitOffset(f, unitOffset)333 }334 }335 newFields = append(newFields, field)336 if f.IsBitfield() {337 bitOffset += f.BitfieldLength()338 } else if !f.Varlen() {339 // Increase offset if the current field except when it's340 // the last field in a struct and has variable length.341 byteOffset += f.Size()342 }343 }344 if bitOffset != 0 {345 pad := roundup(bitOffset, 8) / 8346 byteOffset += pad347 i := len(t.Fields)348 if i != 0 && t.Fields[i-1].IsBitfield() {349 setBitfieldTypeSize(t.Fields[i-1].Type, pad)350 } else {351 newFields = append(newFields, genPad(pad))352 }353 }354 if t.AlignAttr != 0 {355 structAlign = t.AlignAttr356 }357 if !varlen && structAlign != 0 && byteOffset%structAlign != 0 {358 pad := structAlign - byteOffset%structAlign359 newFields = append(newFields, genPad(pad))360 }361 t.Fields = newFields362}363func roundup(v, a uint64) uint64 {364 return rounddown(v+a-1, a)365}366func rounddown(v, a uint64) uint64 {367 if (a & (a - 1)) != 0 {368 panic(fmt.Sprintf("rounddown(%v)", a))369 }370 return v & ^(a - 1)371}372func bitfieldFields(t0 prog.Type) (*uint64, *uint64, *uint64) {373 switch t := t0.(type) {374 case *prog.IntType:375 return &t.TypeSize, &t.BitfieldOff, &t.BitfieldUnitOff376 case *prog.ConstType:377 return &t.TypeSize, &t.BitfieldOff, &t.BitfieldUnitOff378 case *prog.LenType:379 return &t.TypeSize, &t.BitfieldOff, &t.BitfieldUnitOff380 case *prog.FlagsType:381 return &t.TypeSize, &t.BitfieldOff, &t.BitfieldUnitOff382 case *prog.ProcType:383 return &t.TypeSize, &t.BitfieldOff, &t.BitfieldUnitOff384 default:385 panic(fmt.Sprintf("type %#v can't be a bitfield", t))386 }387}388func setBitfieldTypeSize(t prog.Type, v uint64) {389 p, _, _ := bitfieldFields(t)390 *p = v391}392func setBitfieldOffset(t prog.Type, v uint64) {393 _, p, _ := bitfieldFields(t)394 *p = v395}396func setBitfieldUnitOffset(t prog.Type, v uint64) {397 _, _, p := bitfieldFields(t)398 *p = v399}400func genPad(size uint64) prog.Field {401 return prog.Field{402 Type: &prog.ConstType{403 IntTypeCommon: genIntCommon(genCommon("pad", size, false), 0, false),404 IsPad: true,405 },406 }407}408func (comp *compiler) genFieldArray(fields []*ast.Field, argSizes []uint64) []prog.Field {409 var res []prog.Field410 for i, f := range fields {411 res = append(res, comp.genField(f, argSizes[i]))412 }413 return res414}415func (comp *compiler) genFieldDir(f *ast.Field) (prog.Dir, bool) {416 attrs := comp.parseAttrs(fieldAttrs, f, f.Attrs)417 switch {418 case attrs[attrIn] != 0:419 return prog.DirIn, true420 case attrs[attrOut] != 0:421 return prog.DirOut, true422 case attrs[attrInOut] != 0:423 return prog.DirInOut, true424 default:425 return prog.DirIn, false426 }427}428func (comp *compiler) genField(f *ast.Field, argSize uint64) prog.Field {429 dir, hasDir := comp.genFieldDir(f)430 return prog.Field{...

Full Screen

Full Screen

parseAttrs

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fset := token.NewFileSet()4 f, err := parser.ParseFile(fset, "1.go", nil, parser.ParseComments)5 if err != nil {6 panic(err)7 }8 ast.Inspect(f, func(n ast.Node) bool {9 switch x := n.(type) {10 for _, spec := range x.Specs {11 switch spec := spec.(type) {12 for _, name := range spec.Names {13 fmt.Println(name)14 }15 }16 }17 }18 })19}20import (

Full Screen

Full Screen

parseAttrs

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {4 c := compiler{r}5 fmt.Fprintf(w, "Hello, %s", c.parseAttrs())6 })7 http.ListenAndServe(":8080", nil)8}9type compiler struct {10}11func (c *compiler) parseAttrs() string {12}13import (14func main() {15 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {16 c := compiler{r}17 fmt.Fprintf(w, "Hello, %s", c.parseAttrs())18 })19 http.ListenAndServe(":8080", nil)20}21type compiler struct {22}23func (c *compiler) parseAttrs() string {24}25import (26func main() {27 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {28 c := compiler{r}29 fmt.Fprintf(w, "Hello, %s", c.parseAttrs())30 })31 http.ListenAndServe(":8080", nil)32}33type compiler struct {34}35func (c *compiler) parseAttrs() string {36}37import (38func main() {39 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {40 c := compiler{r}41 fmt.Fprintf(w, "Hello, %s", c.parseAttrs())42 })43 http.ListenAndServe(":8080", nil)44}45type compiler struct {46}47func (c *compiler) parseAttrs() string {48}

Full Screen

Full Screen

parseAttrs

Using AI Code Generation

copy

Full Screen

1import (2type constantPoolInfo struct {3 info interface{}4}5type fieldInfo struct {6}7type methodInfo struct {8}9type attributeInfo struct {10}11type classFile struct {12}13func main() {14 file, err := os.Open("C:\\Users\\Akhil\\Desktop\\Test\\Test.class")15 if err != nil {16 fmt.Println(err)17 }18 defer file.Close()19 reader := bufio.NewReader(file)20 cf := classFile{}21 binary.Read(reader, binary.BigEndian, &cf.magic)22 binary.Read(reader, binary.BigEndian, &cf.minorVersion)23 binary.Read(reader, binary.BigEndian, &cf.majorVersion)24 binary.Read(reader, binary.BigEndian, &cf.constantPoolCount)25 cf.constantPool = make([]constantPoolInfo, cf.constantPoolCount - 1)26 for i := 0; i < int(cf.constantPoolCount - 1); i++ {27 binary.Read(reader, binary.BigEndian, &tag)28 switch tag {

Full Screen

Full Screen

parseAttrs

Using AI Code Generation

copy

Full Screen

1import (2type ClassFile struct {3 ConstantPool []interface{}4 Fields []interface{}5 Methods []interface{}6 Attributes []interface{}7}8type ConstantUtf8Info struct {9}10type ConstantIntegerInfo struct {11}12type ConstantFloatInfo struct {13}14type ConstantLongInfo struct {15}16type ConstantDoubleInfo struct {17}18type ConstantClassInfo struct {19}20type ConstantStringInfo struct {21}22type ConstantFieldrefInfo struct {23}24type ConstantMethodrefInfo struct {25}26type ConstantInterfaceMethodrefInfo struct {27}28type ConstantNameAndTypeInfo struct {29}30type ConstantMethodHandleInfo struct {31}32type ConstantMethodTypeInfo struct {33}34type ConstantInvokeDynamicInfo struct {35}36type ConstantPoolInfo struct {37 Info interface{}38}39type ConstantValueAttribute struct {40}41type CodeAttribute struct {42 ExceptionTable []interface{}43 Attributes []interface{}44}45type ExceptionsAttribute struct {46}47type InnerClassesAttribute struct {48 Classes []interface{}49}50type EnclosingMethodAttribute struct {

Full Screen

Full Screen

parseAttrs

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 compiler.ParseAttrs("compiler.go")4 fmt.Println(compiler)5}6import (7func main() {8 compiler.ParseAttrs("compiler.go")9 fmt.Println(compiler)10}

Full Screen

Full Screen

parseAttrs

Using AI Code Generation

copy

Full Screen

1import java.io.*;2import java.util.*;3import java.util.zip.*;4public class Compiler{5 String classFileName;6 String className;7 String superClassName;8 int accessFlags;9 int interfaceCount;10 String[] interfaceNames;11 int fieldCount;12 String[] fieldNames;13 String[] fieldTypes;14 int[] fieldAccessFlags;15 int methodCount;16 String[] methodNames;17 String[] methodTypes;18 int[] methodAccessFlags;19 int attributeCount;20 String[] attributeNames;21 int[] attributeLengths;22 String[] attributeValues;23 int constantPoolCount;24 int[] constantPoolTags;25 String[] constantPoolValues;26 int[] constantPoolSize;27 int[] constantPoolIndex;28 int[] constantPoolIndex2;29 int[] constantPoolIndex3;30 int[] constantPoolIndex4;31 int[] constantPoolIndex5;32 int[] constantPoolIndex6;33 int[] constantPoolIndex7;34 int[] constantPoolIndex8;

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