How to use genResource method of proggen Package

Best Syzkaller code snippet using proggen.genResource

proggen.go

Source:proggen.go Github

copy

Full Screen

...152 return syzType.DefaultArg(dir)153 case *prog.ProcType:154 return ctx.parseProc(a, dir, traceArg)155 case *prog.ResourceType:156 return ctx.genResource(a, dir, traceArg)157 case *prog.PtrType:158 return ctx.genPtr(a, dir, traceArg)159 case *prog.BufferType:160 return ctx.genBuffer(a, dir, traceArg)161 case *prog.StructType:162 return ctx.genStruct(a, dir, traceArg)163 case *prog.ArrayType:164 return ctx.genArray(a, dir, traceArg)165 case *prog.UnionType:166 return ctx.genUnionArg(a, dir, traceArg)167 case *prog.VmaType:168 return ctx.genVma(a, dir, traceArg)169 default:170 log.Fatalf("unsupported type: %#v", syzType)171 }172 return nil173}174func (ctx *context) genVma(syzType *prog.VmaType, dir prog.Dir, _ parser.IrType) prog.Arg {175 npages := uint64(1)176 if syzType.RangeBegin != 0 || syzType.RangeEnd != 0 {177 npages = syzType.RangeEnd178 }179 return prog.MakeVmaPointerArg(syzType, dir, ctx.builder.AllocateVMA(npages), npages)180}181func (ctx *context) genArray(syzType *prog.ArrayType, dir prog.Dir, traceType parser.IrType) prog.Arg {182 var args []prog.Arg183 switch a := traceType.(type) {184 case *parser.GroupType:185 for i := 0; i < len(a.Elems); i++ {186 args = append(args, ctx.genArg(syzType.Elem, dir, a.Elems[i]))187 }188 default:189 log.Fatalf("unsupported type for array: %#v", traceType)190 }191 return prog.MakeGroupArg(syzType, dir, args)192}193func (ctx *context) genStruct(syzType *prog.StructType, dir prog.Dir, traceType parser.IrType) prog.Arg {194 var args []prog.Arg195 switch a := traceType.(type) {196 case *parser.GroupType:197 j := 0198 if ret, recursed := ctx.recurseStructs(syzType, dir, a); recursed {199 return ret200 }201 for i := range syzType.Fields {202 if prog.IsPad(syzType.Fields[i].Type) {203 args = append(args, syzType.Fields[i].DefaultArg(dir))204 continue205 }206 // If the last n fields of a struct are zero or NULL, strace will occasionally omit those values207 // this creates a mismatch in the number of elements in the ir type and in208 // our descriptions. We generate default values for omitted fields209 if j >= len(a.Elems) {210 args = append(args, syzType.Fields[i].DefaultArg(dir))211 } else {212 args = append(args, ctx.genArg(syzType.Fields[i].Type, dir, a.Elems[j]))213 }214 j++215 }216 case *parser.BufferType:217 // We could have a case like the following:218 // ioctl(3, 35111, {ifr_name="\x6c\x6f", ifr_hwaddr=00:00:00:00:00:00}) = 0219 // if_hwaddr gets parsed as a BufferType but our syscall descriptions have it as a struct type220 return syzType.DefaultArg(dir)221 default:222 log.Fatalf("unsupported type for struct: %#v", a)223 }224 return prog.MakeGroupArg(syzType, dir, args)225}226// recurseStructs handles cases where syzType corresponds to struct descriptions like227// sockaddr_storage_in6 {228// addr sockaddr_in6229// } [size[SOCKADDR_STORAGE_SIZE], align_ptr]230// which need to be recursively generated. It returns true if we needed to recurse231// along with the generated argument and false otherwise.232func (ctx *context) recurseStructs(syzType *prog.StructType, dir prog.Dir, traceType *parser.GroupType) (prog.Arg, bool) {233 // only consider structs with one non-padded field234 numFields := 0235 for _, field := range syzType.Fields {236 if prog.IsPad(field.Type) {237 continue238 }239 numFields++240 }241 if numFields != 1 {242 return nil, false243 }244 // the strace group type needs to have more one field (a mismatch)245 if len(traceType.Elems) == 1 {246 return nil, false247 }248 // first field needs to be a struct249 switch t := syzType.Fields[0].Type.(type) {250 case *prog.StructType:251 var args []prog.Arg252 // first element and traceType should have the same number of elements253 if len(t.Fields) != len(traceType.Elems) {254 return nil, false255 }256 args = append(args, ctx.genStruct(t, dir, traceType))257 for _, field := range syzType.Fields[1:] {258 args = append(args, field.DefaultArg(dir))259 }260 return prog.MakeGroupArg(syzType, dir, args), true261 }262 return nil, false263}264func (ctx *context) genUnionArg(syzType *prog.UnionType, dir prog.Dir, straceType parser.IrType) prog.Arg {265 if straceType == nil {266 log.Logf(1, "generating union arg. straceType is nil")267 return syzType.DefaultArg(dir)268 }269 log.Logf(4, "generating union arg: %s %#v", syzType.TypeName, straceType)270 // Unions are super annoying because they sometimes need to be handled case by case271 // We might need to lookinto a matching algorithm to identify the union type that most closely272 // matches our strace type.273 switch syzType.TypeName {274 case "sockaddr_storage":275 return ctx.genSockaddrStorage(syzType, dir, straceType)276 case "sockaddr_nl":277 return ctx.genSockaddrNetlink(syzType, dir, straceType)278 case "ifr_ifru":279 return ctx.genIfrIfru(syzType, dir, straceType)280 }281 return prog.MakeUnionArg(syzType, dir, ctx.genArg(syzType.Fields[0].Type, dir, straceType), 0)282}283func (ctx *context) genBuffer(syzType *prog.BufferType, dir prog.Dir, traceType parser.IrType) prog.Arg {284 if dir == prog.DirOut {285 if !syzType.Varlen() {286 return prog.MakeOutDataArg(syzType, dir, syzType.Size())287 }288 switch a := traceType.(type) {289 case *parser.BufferType:290 return prog.MakeOutDataArg(syzType, dir, uint64(len(a.Val)))291 default:292 switch syzType.Kind {293 case prog.BufferBlobRand:294 size := rand.Intn(256)295 return prog.MakeOutDataArg(syzType, dir, uint64(size))296 case prog.BufferBlobRange:297 max := rand.Intn(int(syzType.RangeEnd) - int(syzType.RangeBegin) + 1)298 size := max + int(syzType.RangeBegin)299 return prog.MakeOutDataArg(syzType, dir, uint64(size))300 default:301 log.Fatalf("unexpected buffer type kind: %v. call %v arg %#v", syzType.Kind, ctx.currentSyzCall, traceType)302 }303 }304 }305 var bufVal []byte306 switch a := traceType.(type) {307 case *parser.BufferType:308 bufVal = []byte(a.Val)309 case parser.Constant:310 val := a.Val()311 bArr := make([]byte, 8)312 binary.LittleEndian.PutUint64(bArr, val)313 bufVal = bArr314 default:315 log.Fatalf("unsupported type for buffer: %#v", traceType)316 }317 // strace always drops the null byte for buffer types but we only need to add it back for filenames and strings318 switch syzType.Kind {319 case prog.BufferFilename, prog.BufferString:320 bufVal = append(bufVal, '\x00')321 }322 if !syzType.Varlen() {323 size := syzType.Size()324 for uint64(len(bufVal)) < size {325 bufVal = append(bufVal, 0)326 }327 bufVal = bufVal[:size]328 }329 return prog.MakeDataArg(syzType, dir, bufVal)330}331func (ctx *context) genPtr(syzType *prog.PtrType, dir prog.Dir, traceType parser.IrType) prog.Arg {332 switch a := traceType.(type) {333 case parser.Constant:334 if a.Val() == 0 {335 return prog.MakeSpecialPointerArg(syzType, dir, 0)336 }337 // Likely have a type of the form bind(3, 0xfffffffff, [3]);338 res := syzType.Elem.DefaultArg(syzType.ElemDir)339 return ctx.addr(syzType, dir, res.Size(), res)340 default:341 res := ctx.genArg(syzType.Elem, syzType.ElemDir, a)342 return ctx.addr(syzType, dir, res.Size(), res)343 }344}345func (ctx *context) genConst(syzType prog.Type, dir prog.Dir, traceType parser.IrType) prog.Arg {346 switch a := traceType.(type) {347 case parser.Constant:348 return prog.MakeConstArg(syzType, dir, a.Val())349 case *parser.GroupType:350 // Sometimes strace represents a pointer to int as [0] which gets parsed351 // as Array([0], len=1). A good example is ioctl(3, FIONBIO, [1]). We may also have an union int type that352 // is a represented as a struct in strace e.g.353 // sigev_value={sival_int=-2123636944, sival_ptr=0x7ffd816bdf30}354 // For now we choose the first option355 if len(a.Elems) == 0 {356 log.Logf(2, "parsing const type, got array type with len 0")357 return syzType.DefaultArg(dir)358 }359 return ctx.genConst(syzType, dir, a.Elems[0])360 case *parser.BufferType:361 // strace decodes some arguments as hex strings because those values are network ordered362 // e.g. sin_port or sin_addr fields of sockaddr_in.363 // network order is big endian byte order so if the len of byte array is 1, 2, 4, or 8 then364 // it is a good chance that we are decoding one of those fields. If it isn't, then most likely365 // we have an error i.e. a sockaddr_un struct passed to a connect call with an inet file descriptor366 var val uint64367 toUint64 := binary.LittleEndian.Uint64368 toUint32 := binary.LittleEndian.Uint32369 toUint16 := binary.LittleEndian.Uint16370 if syzType.Format() == prog.FormatBigEndian {371 toUint64 = binary.BigEndian.Uint64372 toUint32 = binary.BigEndian.Uint32373 toUint16 = binary.BigEndian.Uint16374 }375 switch len(a.Val) {376 case 8:377 val = toUint64([]byte(a.Val))378 case 4:379 val = uint64(toUint32([]byte(a.Val)))380 case 2:381 val = uint64(toUint16([]byte(a.Val)))382 case 1:383 val = uint64(a.Val[0])384 default:385 return syzType.DefaultArg(dir)386 }387 return prog.MakeConstArg(syzType, dir, val)388 default:389 log.Fatalf("unsupported type for const: %#v", traceType)390 }391 return nil392}393func (ctx *context) genResource(syzType *prog.ResourceType, dir prog.Dir, traceType parser.IrType) prog.Arg {394 if dir == prog.DirOut {395 log.Logf(2, "resource returned by call argument: %s", traceType.String())396 res := prog.MakeResultArg(syzType, dir, nil, syzType.Default())397 ctx.returnCache.cache(syzType, traceType, res)398 return res399 }400 switch a := traceType.(type) {401 case parser.Constant:402 val := a.Val()403 if arg := ctx.returnCache.get(syzType, traceType); arg != nil {404 res := prog.MakeResultArg(syzType, dir, arg.(*prog.ResultArg), syzType.Default())405 return res406 }407 res := prog.MakeResultArg(syzType, dir, nil, val)...

Full Screen

Full Screen

genResource

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, 世界")4 os.Exit(0)5}6import (7func main() {8 fmt.Println("Hello, 世界")9 os.Exit(0)10}11import (12func main() {13 fmt.Println("Hello, 世界")14 os.Exit(0)15}16import (17func main() {18 fmt.Println("Hello, 世界")19 os.Exit(0)20}21import (22func main() {23 fmt.Println("Hello, 世界")24 os.Exit(0)25}26import (27func main() {28 fmt.Println("Hello, 世界")29 os.Exit(0)30}31import (32func main() {33 fmt.Println("Hello, 世界")34 os.Exit(0)35}36import (37func main() {38 fmt.Println("Hello, 世界")39 os.Exit(

Full Screen

Full Screen

genResource

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 p := gen.ProgGen{}4 p.GenResource()5 fmt.Println("Done")6}7import (8type ProgGen struct {9}10func (p *ProgGen) GenResource() {11 fmt.Println("Generating resource")12}

Full Screen

Full Screen

genResource

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 pg.Init()4 pg.GenResource()5}6import (7type ProgGen struct {8}9func (pg *ProgGen) Init() {10}11func (pg *ProgGen) GenResource() {12 fmt.Println(pg.Resource)13}14 /usr/local/go/src/github.com/ymcvalu/ProgGen (from $GOROOT)15 /home/ymcvalu/go/src/github.com/ymcvalu/ProgGen (from $GOPATH)16 /usr/local/go/src/github.com/ymcvalu/ProgGen (from $GOROOT)

Full Screen

Full Screen

genResource

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, playground")4 resource = proggen.genResource()5 fmt.Println("resource is ", resource)6 resourceFile, err := os.Create("resource.txt")7 if err != nil {8 fmt.Println("Error creating resource file")9 }10 resourceFile.WriteString(resource)11 resourceFile.Close()12}13import (14type proggen struct {15}16func (pg proggen) genResource() string {17 rand.Seed(time.Now().UTC().UnixNano())18 randNum = rand.Intn(100)19 if randNum%2 == 0 {20 } else {21 }22}23func main() {24 fmt.Println("Hello, playground")25 resource = proggen.genResource()26 fmt.Println("resource is ", resource)27}28cmd := exec.Command("ls", "-l")29out, err := cmd.Output()30import (

Full Screen

Full Screen

genResource

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 gen := proggen.NewGenResource()4 gen.GenResource("test")5 gen.GenResource("test", "test2")6 gen.GenResource("test", "test2", "testpackage")7 gen.GenResource("test", "test2", "testpackage", "testfile")8 gen.GenResource("test", "test2", "testpackage", "testfile", "testpath")9}10import "github.com/darkowlzz/Go-Gen-Resource"11var (12 Test2 = proggen.Resource{Data: []byte{0x74, 0x65, 0x73, 0x74}, Name: "test2", Package: "testpackage", Size: 4}

Full Screen

Full Screen

genResource

Using AI Code Generation

copy

Full Screen

1import java.io.*;2import java.util.*;3import java.awt.*;4import java.awt.event.*;5import javax.swing.*;6import javax.swing.event.*;7import java.util.regex.*;8import javax.swing.text.*;9import java.text.*;10import java.lang.*;11import java.lang.reflect.*;12import java.net.*;13import java.util.zip.*;14import java.util.jar.*;15import java.awt.print.*;16import java.beans.*;17import java.util.prefs.*;18import java.util.logging.*;19import java.util.logging.Logger;20import java.util.logging.Level;21import java.util.logging.Handler;22import java.util.logging.FileHandler;23import java.util.logging.SimpleFormatter;24import java.util.logging.Formatter;25import java.util.logging.LogRecord;26import java.util.logging.ConsoleHandler;27import java.util.logging.XMLFormatter;28import java.util.logging.ErrorManager;29import java.util.logging.Filter;30import java.util.logging.MemoryHandler;31import java.util.logging.StreamHandler;32import java.util.logging.SocketHandler;33import java.util.logging.Level;

Full Screen

Full Screen

genResource

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 vm := otto.New()4 underscore.Enable(vm)5 f, err := os.Open("proggen.js")6 if err != nil {7 log.Fatal(err)8 }9 defer f.Close()10 if _, err := vm.Run(f); err != nil {11 log.Fatal(err)12 }13 result, err := vm.Run("genResource('Hello World')")14 if err != nil {15 log.Fatal(err)16 }17 f2, err := os.Create("test.res")18 if err != nil {19 log.Fatal(err)20 }21 f2.WriteString(result.String())22 f2.Close()23}

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful