How to use genCommon method of compiler Package

Best Syzkaller code snippet using compiler.genCommon

gen.go

Source:gen.go Github

copy

Full Screen

...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{368 TypeCommon: com,369 BigEndian: bigEndian,370 BitfieldLen: bitLen,371 }...

Full Screen

Full Screen

genCommon

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, playground")4 c.genCommon()5}6import (7func main() {8 fmt.Println("Hello, playground")9 c.genCommon()10}11import (12func main() {13 fmt.Println("Hello, playground")14 c.genCommon()15}16import (17func main() {18 fmt.Println("Hello, playground")19 c.genCommon()20}21import (22func main() {23 fmt.Println("Hello, playground")24 c.genCommon()25}26import (27func main() {28 fmt.Println("Hello, playground")29 c.genCommon()30}31import (32func main() {33 fmt.Println("Hello, playground")34 c.genCommon()35}36import (37func main() {38 fmt.Println("Hello, playground")39 c.genCommon()40}41import (42func main() {43 fmt.Println("Hello, playground")44 c.genCommon()45}46import (47func main() {48 fmt.Println("Hello, playground")49 c.genCommon()50}

Full Screen

Full Screen

genCommon

Using AI Code Generation

copy

Full Screen

1func main() {2 compiler := new(Compiler)3 compiler.genCommon()4}5type Compiler struct {6}7func (c *Compiler) genCommon() {8}

Full Screen

Full Screen

genCommon

Using AI Code Generation

copy

Full Screen

1import (2func main() {3import "fmt"4func main() {5 fmt.Println("Hello World")6}7 c := compiler.New()8 c.SetCode(code)9 c.SetOutput("2.exe")10 c.SetCompiler("go")11 err := c.Compile()12 if err != nil {13 fmt.Println(err)14 }15}16import (17type Compiler struct {18}19func New() *Compiler {20 return &Compiler{}21}22func (c *Compiler) SetCode(code string) {23}24func (c *Compiler) SetOutput(output string) {25}26func (c *Compiler) SetCompiler(compiler string) {27}28func (c *Compiler) Compile() error {29 if c.Compiler == "go" {30 return c.genCommon()31 }32 return errors.New("Unknown language")33}34func (c *Compiler) genCommon() error {35 dir, err := ioutil.TempDir("", "compiler")36 if err != nil {37 }38 defer os.RemoveAll(dir)39 c.Output = filepath.Join(dir, c.Output)40 err = ioutil.WriteFile(c.Output+".go", []byte(c.Code), 0644)41 if err != nil {42 }43 cmd := exec.Command("go", "build", "-o", c.Output, c.Output+".go")44 err = cmd.Run()45 if err != nil {46 return errors.New(out.String())47 }48}49import (50func TestCompiler(t *testing.T) {51import "fmt"52func main() {53 fmt.Println("Hello World")54}55 c := New()56 c.SetCode(code)57 c.SetOutput("2.exe")58 c.SetCompiler("go")59 err := c.Compile()60 if err != nil {61 t.Error(err)62 }63}

Full Screen

Full Screen

genCommon

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f, err := os.Create("common.asm")4 if err != nil {5 fmt.Println(err)6 }7 defer f.Close()8 _, err = f.WriteString(`9 if err != nil {10 fmt.Println(err)11 f.Close()12 }13}

Full Screen

Full Screen

genCommon

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 compiler := new(Compiler)4 code := compiler.genCommon("func1", "func2")5 file, err := os.Create("common.txt")6 if err != nil {7 fmt.Println("Error while creating file")8 }9 file.WriteString(code)10 file.Close()11 codeFromFile, err := ioutil.ReadFile("common.txt")12 if err != nil {13 fmt.Println("Error while reading file")14 }15 fmt.Println("The common code is:")16 fmt.Println(string(codeFromFile))17}18type Compiler struct {19}20func (compiler *Compiler) genCommon(func1, func2 string) string {21 code := "function " + func1 + "(){\n"22 code += "}\n\n"23 code += "function " + func2 + "(){\n"24 code += "}\n\n"25 code += "function main(){\n"26 code += "\t" + func1 + "();\n"27 code += "\t" + func2 + "();\n"28 code += "}\n"29}30function func1(){31}32function func2(){33}34function main(){35 func1();36 func2();37}

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