How to use Alignment method of prog Package

Best Syzkaller code snippet using prog.Alignment

types.go

Source:types.go Github

copy

Full Screen

...130 if len(t.Colon) != 0 {131 bitLen = t.Colon[0].Value132 }133 base.TypeSize = size134 base.TypeAlign = getIntAlignment(comp, base)135 return &prog.IntType{136 IntTypeCommon: genIntCommon(base.TypeCommon, bitLen, be),137 Kind: kind,138 RangeBegin: rangeBegin,139 RangeEnd: rangeEnd,140 Align: align,141 }142 },143}144func getIntAlignment(comp *compiler, base prog.IntTypeCommon) uint64 {145 align := base.UnitSize()146 if align == 8 && comp.target.Int64Alignment != 0 {147 align = comp.target.Int64Alignment148 }149 return align150}151var typePtr = &typeDesc{152 Names: []string{"ptr", "ptr64"},153 CanBeArgRet: canBeArg,154 CanBeTypedef: true,155 Args: []namedArg{{Name: "direction", Type: typeArgDir}, {Name: "type", Type: typeArgType}},156 Gen: func(comp *compiler, t *ast.Type, args []*ast.Type, base prog.IntTypeCommon) prog.Type {157 base.TypeSize = comp.ptrSize158 if t.Ident == "ptr64" {159 base.TypeSize = 8160 }161 base.TypeAlign = getIntAlignment(comp, base)162 return &prog.PtrType{163 TypeCommon: base.TypeCommon,164 Elem: comp.genType(args[1], 0),165 ElemDir: genDir(args[0]),166 }167 },168}169var typeVoid = &typeDesc{170 Names: []string{"void"},171 CantBeOpt: true,172 ZeroSize: func(comp *compiler, t *ast.Type, args []*ast.Type) bool {173 return true174 },175 Gen: func(comp *compiler, t *ast.Type, args []*ast.Type, base prog.IntTypeCommon) prog.Type {176 base.TypeSize = 0 // the only type with static size 0177 base.TypeAlign = 1178 return &prog.BufferType{179 TypeCommon: base.TypeCommon,180 Kind: prog.BufferBlobRange,181 RangeBegin: 0,182 RangeEnd: 0,183 }184 },185}186var typeArray = &typeDesc{187 Names: []string{"array"},188 CanBeTypedef: true,189 CantBeOpt: true,190 OptArgs: 1,191 Args: []namedArg{{Name: "type", Type: typeArgType}, {Name: "size", Type: typeArgSizeRange}},192 CheckConsts: func(comp *compiler, t *ast.Type, args []*ast.Type, base prog.IntTypeCommon) {193 if len(args) > 1 && args[1].Value == 0 && (len(args[1].Colon) == 0 || args[1].Colon[0].Value == 0) {194 comp.error(args[1].Pos, "arrays of size 0 are not supported")195 }196 },197 Varlen: func(comp *compiler, t *ast.Type, args []*ast.Type) bool {198 if comp.isZeroSize(args[0]) {199 return false200 }201 if comp.isVarlen(args[0]) {202 return true203 }204 if len(args) > 1 {205 return len(args[1].Colon) != 0 && args[1].Value != args[1].Colon[0].Value206 }207 return true208 },209 ZeroSize: func(comp *compiler, t *ast.Type, args []*ast.Type) bool {210 return comp.isZeroSize(args[0])211 },212 Gen: func(comp *compiler, t *ast.Type, args []*ast.Type, base prog.IntTypeCommon) prog.Type {213 elemType := comp.genType(args[0], 0)214 kind, begin, end := prog.ArrayRandLen, uint64(0), uint64(0)215 if len(args) > 1 {216 kind, begin, end = prog.ArrayRangeLen, args[1].Value, args[1].Value217 if len(args[1].Colon) != 0 {218 end = args[1].Colon[0].Value219 }220 }221 if it, ok := elemType.(*prog.IntType); ok && it.Kind == prog.IntPlain && it.TypeSize == 1 {222 // Special case: buffer is better mutated.223 bufKind := prog.BufferBlobRand224 base.TypeSize = 0225 if kind == prog.ArrayRangeLen {226 bufKind = prog.BufferBlobRange227 if begin == end {228 base.TypeSize = begin * elemType.Size()229 }230 }231 base.TypeAlign = 1232 return &prog.BufferType{233 TypeCommon: base.TypeCommon,234 Kind: bufKind,235 RangeBegin: begin,236 RangeEnd: end,237 }238 }239 // TypeSize is assigned later in layoutArray.240 base.TypeAlign = elemType.Alignment()241 return &prog.ArrayType{242 TypeCommon: base.TypeCommon,243 Elem: elemType,244 Kind: kind,245 RangeBegin: begin,246 RangeEnd: end,247 }248 },249}250var typeLen = &typeDesc{251 Names: []string{"len", "bytesize", "bytesize2", "bytesize4", "bytesize8", "bitsize", "offsetof"},252 CanBeArgRet: canBeArg,253 CantBeOpt: true,254 NeedBase: true,255 Args: []namedArg{{Name: "len target", Type: typeArgLenTarget}},256 Gen: func(comp *compiler, t *ast.Type, args []*ast.Type, base prog.IntTypeCommon) prog.Type {257 var bitSize uint64258 var offset bool259 switch t.Ident {260 case "bytesize":261 bitSize = 8262 case "bytesize2", "bytesize4", "bytesize8":263 byteSize, _ := strconv.ParseUint(t.Ident[8:], 10, 8)264 bitSize = byteSize * 8265 case "bitsize":266 bitSize = 1267 case "offsetof":268 bitSize = 8269 offset = true270 }271 path := []string{args[0].Ident}272 for _, col := range args[0].Colon {273 path = append(path, col.Ident)274 }275 base.TypeAlign = getIntAlignment(comp, base)276 return &prog.LenType{277 IntTypeCommon: base,278 Path: path,279 BitSize: bitSize,280 Offset: offset,281 }282 },283}284var typeConst = &typeDesc{285 Names: []string{"const"},286 CanBeArgRet: canBeArg,287 CanBeTypedef: true,288 CantBeOpt: true,289 NeedBase: true,290 Args: []namedArg{{Name: "value", Type: typeArgInt}},291 CheckConsts: func(comp *compiler, t *ast.Type, args []*ast.Type, base prog.IntTypeCommon) {292 v := args[0].Value293 bitSize := base.TypeBitSize()294 if constOverflowsBase(v, base) {295 comp.error(args[0].Pos, "const val 0x%x does not fit into %v bits", v, bitSize)296 }297 args[0].Value = v & (uint64(1)<<bitSize - 1)298 },299 Gen: func(comp *compiler, t *ast.Type, args []*ast.Type, base prog.IntTypeCommon) prog.Type {300 base.TypeAlign = getIntAlignment(comp, base)301 return &prog.ConstType{302 IntTypeCommon: base,303 Val: args[0].Value,304 }305 },306}307func constOverflowsBase(v uint64, base prog.IntTypeCommon) bool {308 size := base.TypeBitSize()309 if size == 64 {310 return false311 }312 mask := uint64(1)<<size - 1313 v1 := v & mask314 if int64(v1<<(64-size)) < 0 && int64(v) < 0 {315 v1 |= ^mask316 }317 return v1 != v318}319var typeArgLenTarget = &typeArg{320 Kind: kindIdent,321 MaxColon: 10,322}323var typeFlags = &typeDesc{324 Names: []string{"flags"},325 CanBeArgRet: canBeArg,326 CanBeTypedef: true,327 CantBeOpt: true,328 NeedBase: true,329 Args: []namedArg{{Name: "flags", Type: typeArgFlags}},330 CheckConsts: func(comp *compiler, t *ast.Type, args []*ast.Type, base prog.IntTypeCommon) {331 name := args[0].Ident332 if name == "xdp_mmap_offsets" && comp.ptrSize == 4 {333 // TODO(dvyukov): this sucks a lot. It seems that out 32-bit mmap is wrong.334 // The syscall accepts number of pages as int32, but we pass offset in bytes.335 // As the result large XDP consts don't fit into the arg.336 return337 }338 f := comp.intFlags[name]339 for _, val := range f.Values {340 if constOverflowsBase(val.Value, base) {341 comp.error(args[0].Pos, "%v %v=0x%x doesn't fit into %v bits",342 name, val.Ident, val.Value, base.TypeBitSize())343 }344 }345 },346 Gen: func(comp *compiler, t *ast.Type, args []*ast.Type, base prog.IntTypeCommon) prog.Type {347 name := args[0].Ident348 base.TypeName = name349 f := comp.intFlags[name]350 values := genIntArray(f.Values)351 if len(values) == 0 || len(values) == 1 && values[0] == 0 {352 // We can get this if all values are unsupported consts.353 // Also generate const[0] if we have only 1 flags value which is 0,354 // this is the intention in all existing cases (e.g. an enum with types355 // of something, but there is really only 1 type exists).356 return &prog.ConstType{357 IntTypeCommon: base,358 Val: 0,359 }360 }361 sort.Slice(values, func(i, j int) bool {362 return values[i] < values[j]363 })364 base.TypeAlign = getIntAlignment(comp, base)365 return &prog.FlagsType{366 IntTypeCommon: base,367 Vals: values,368 BitMask: isBitmask(values),369 }370 },371}372func isBitmask(values []uint64) bool {373 if values[0] == 0 {374 // 0 can't be part of bitmask, this helps to handle important375 // case like "0, 1" and "0, 1, 2" that would be detected376 // as bitmask otherwise.377 return false378 }379 var combined uint64380 for _, v := range values {381 if v&combined != 0 {382 return false383 }384 combined |= v385 }386 return true387}388var typeArgFlags = &typeArg{389 Kind: kindIdent,390 Check: func(comp *compiler, t *ast.Type) {391 if comp.intFlags[t.Ident] == nil {392 comp.error(t.Pos, "unknown flags %v", t.Ident)393 return394 }395 },396}397var typeVMA = &typeDesc{398 Names: []string{"vma", "vma64"},399 CanBeArgRet: canBeArg,400 OptArgs: 1,401 Args: []namedArg{{Name: "size range", Type: typeArgSizeRange}},402 Gen: func(comp *compiler, t *ast.Type, args []*ast.Type, base prog.IntTypeCommon) prog.Type {403 var begin, end uint64404 if len(args) > 0 {405 begin, end = args[0].Value, args[0].Value406 if len(args[0].Colon) != 0 {407 end = args[0].Colon[0].Value408 }409 }410 base.TypeSize = comp.ptrSize411 if t.Ident == "vma64" {412 base.TypeSize = 8413 }414 base.TypeAlign = getIntAlignment(comp, base)415 return &prog.VmaType{416 TypeCommon: base.TypeCommon,417 RangeBegin: begin,418 RangeEnd: end,419 }420 },421}422var typeCsum = &typeDesc{423 Names: []string{"csum"},424 NeedBase: true,425 CantBeOpt: true,426 OptArgs: 1,427 Args: []namedArg{428 {Name: "csum target", Type: typeArgLenTarget},429 {Name: "kind", Type: typeArgCsumType},430 {Name: "proto", Type: typeArgInt},431 },432 Check: func(comp *compiler, t *ast.Type, args []*ast.Type, base prog.IntTypeCommon) {433 if len(args) > 2 && genCsumKind(args[1]) != prog.CsumPseudo {434 comp.error(args[2].Pos, "only pseudo csum can have proto")435 }436 if len(args[0].Colon) != 0 {437 comp.error(args[0].Colon[0].Pos, "path expressions are not implemented for csum")438 }439 },440 Gen: func(comp *compiler, t *ast.Type, args []*ast.Type, base prog.IntTypeCommon) prog.Type {441 var proto uint64442 if len(args) > 2 {443 proto = args[2].Value444 }445 base.TypeAlign = getIntAlignment(comp, base)446 return &prog.CsumType{447 IntTypeCommon: base,448 Buf: args[0].Ident,449 Kind: genCsumKind(args[1]),450 Protocol: proto,451 }452 },453}454var typeArgCsumType = &typeArg{455 Kind: kindIdent,456 Names: []string{"inet", "pseudo"},457}458func genCsumKind(t *ast.Type) prog.CsumKind {459 switch t.Ident {460 case "inet":461 return prog.CsumInet462 case "pseudo":463 return prog.CsumPseudo464 default:465 panic(fmt.Sprintf("unknown csum kind %q", t.Ident))466 }467}468var typeProc = &typeDesc{469 Names: []string{"proc"},470 CanBeArgRet: canBeArg,471 CanBeTypedef: true,472 NeedBase: true,473 Args: []namedArg{474 {Name: "range start", Type: typeArgInt},475 {Name: "per-proc values", Type: typeArgInt},476 },477 CheckConsts: func(comp *compiler, t *ast.Type, args []*ast.Type, base prog.IntTypeCommon) {478 start := args[0].Value479 perProc := args[1].Value480 if perProc == 0 {481 comp.error(args[1].Pos, "proc per-process values must not be 0")482 return483 }484 size := base.TypeSize * 8485 max := uint64(1) << size486 if size == 64 {487 max = ^uint64(0)488 }489 if start >= max {490 comp.error(args[0].Pos, "values starting from %v overflow base type", start)491 } else if perProc > (max-start)/prog.MaxPids {492 comp.error(args[0].Pos, "values starting from %v with step %v overflow base type for %v procs",493 start, perProc, prog.MaxPids)494 }495 },496 Gen: func(comp *compiler, t *ast.Type, args []*ast.Type, base prog.IntTypeCommon) prog.Type {497 base.TypeAlign = getIntAlignment(comp, base)498 return &prog.ProcType{499 IntTypeCommon: base,500 ValuesStart: args[0].Value,501 ValuesPerProc: args[1].Value,502 }503 },504}505var typeText = &typeDesc{506 Names: []string{"text"},507 CantBeOpt: true,508 Args: []namedArg{{Name: "kind", Type: typeArgTextType}},509 Varlen: func(comp *compiler, t *ast.Type, args []*ast.Type) bool {510 return true511 },512 Gen: func(comp *compiler, t *ast.Type, args []*ast.Type, base prog.IntTypeCommon) prog.Type {513 base.TypeSize = 0514 base.TypeAlign = 1515 return &prog.BufferType{516 TypeCommon: base.TypeCommon,517 Kind: prog.BufferText,518 Text: genTextType(args[0]),519 }520 },521}522var typeArgTextType = &typeArg{523 Kind: kindIdent,524 Names: []string{"target", "x86_real", "x86_16", "x86_32", "x86_64", "arm64"},525}526func genTextType(t *ast.Type) prog.TextKind {527 switch t.Ident {528 case "target":529 return prog.TextTarget530 case "x86_real":531 return prog.TextX86Real532 case "x86_16":533 return prog.TextX86bit16534 case "x86_32":535 return prog.TextX86bit32536 case "x86_64":537 return prog.TextX86bit64538 case "arm64":539 return prog.TextArm64540 default:541 panic(fmt.Sprintf("unknown text type %q", t.Ident))542 }543}544const (545 stringnoz = "stringnoz"546)547var typeString = &typeDesc{548 Names: []string{"string", stringnoz},549 CanBeTypedef: true,550 OptArgs: 2,551 Args: []namedArg{552 {Name: "literal or flags", Type: typeArgStringFlags},553 {Name: "size", Type: typeArgInt},554 },555 Check: func(comp *compiler, t *ast.Type, args []*ast.Type, base prog.IntTypeCommon) {556 if t.Ident == stringnoz && len(args) > 1 {557 comp.error(args[0].Pos, "fixed-size string can't be non-zero-terminated")558 }559 },560 CheckConsts: func(comp *compiler, t *ast.Type, args []*ast.Type, base prog.IntTypeCommon) {561 if len(args) > 1 {562 size := args[1].Value563 vals := comp.genStrings(t, args)564 for _, s := range vals {565 if uint64(len(s)) > size {566 comp.error(args[0].Pos, "string value %q exceeds buffer length %v",567 s, size)568 }569 }570 }571 },572 Varlen: func(comp *compiler, t *ast.Type, args []*ast.Type) bool {573 return comp.stringSize(t, args) == varlenString574 },575 ZeroSize: func(comp *compiler, t *ast.Type, args []*ast.Type) bool {576 return comp.stringSize(t, args) == 0577 },578 Gen: func(comp *compiler, t *ast.Type, args []*ast.Type, base prog.IntTypeCommon) prog.Type {579 base.TypeAlign = 1580 if len(args) > 0 && args[0].Ident == "filename" {581 base.TypeName = "filename"582 base.TypeSize = 0583 if len(args) >= 2 {584 base.TypeSize = args[1].Value585 }586 return &prog.BufferType{587 TypeCommon: base.TypeCommon,588 Kind: prog.BufferFilename,589 NoZ: t.Ident == stringnoz,590 }591 }592 subkind := ""593 if len(args) > 0 && args[0].Ident != "" {594 subkind = args[0].Ident595 }596 vals := comp.genStrings(t, args)597 base.TypeSize = comp.stringSize(t, args)598 if base.TypeSize == varlenString {599 base.TypeSize = 0600 }601 return &prog.BufferType{602 TypeCommon: base.TypeCommon,603 Kind: prog.BufferString,604 SubKind: subkind,605 Values: vals,606 NoZ: t.Ident == stringnoz,607 }608 },609}610func (comp *compiler) genStrings(t *ast.Type, args []*ast.Type) []string {611 var vals []string612 if len(args) > 0 {613 if args[0].HasString {614 vals = append(vals, args[0].String)615 } else {616 vals = genStrArray(comp.strFlags[args[0].Ident].Values)617 }618 }619 if t.Ident == stringnoz {620 return vals621 }622 var size uint64623 if len(args) > 1 {624 size = args[1].Value625 }626 for i, s := range vals {627 s += "\x00"628 for uint64(len(s)) < size {629 s += "\x00"630 }631 vals[i] = s632 }633 return vals634}635const varlenString = ^uint64(0)636// stringSize returns static string size, or varlenString if it is variable length.637func (comp *compiler) stringSize(t *ast.Type, args []*ast.Type) uint64 {638 switch len(args) {639 case 0:640 return varlenString // a random string641 case 1:642 var z uint64643 if t.Ident == "string" {644 z = 1645 }646 if args[0].HasString {647 return uint64(len(args[0].String)) + z // string constant648 }649 size := varlenString650 for _, s := range comp.strFlags[args[0].Ident].Values {651 s1 := uint64(len(s.Value)) + z652 if size != varlenString && size != s1 {653 return varlenString // strings of different lengths654 }655 size = s1656 }657 return size // all strings have the same length658 case 2:659 return args[1].Value // have explicit length660 default:661 panic("too many string args")662 }663}664var typeArgStringFlags = &typeArg{665 Check: func(comp *compiler, t *ast.Type) {666 if !t.HasString && t.Ident == "" {667 comp.error(t.Pos, "unexpected int %v, string arg must be a string literal or string flags", t.Value)668 return669 }670 if t.Ident != "" && comp.strFlags[t.Ident] == nil {671 comp.error(t.Pos, "unknown string flags %v", t.Ident)672 return673 }674 },675}676var typeFmt = &typeDesc{677 Names: []string{"fmt"},678 CanBeTypedef: true,679 CantBeOpt: true,680 Args: []namedArg{681 {Name: "format", Type: typeFmtFormat},682 {Name: "value", Type: typeArgType, IsArg: true},683 },684 Check: func(comp *compiler, t *ast.Type, args []*ast.Type, base prog.IntTypeCommon) {685 desc, _, _ := comp.getArgsBase(args[1], true)686 switch desc {687 case typeResource, typeInt, typeLen, typeFlags, typeProc:688 default:689 comp.error(t.Pos, "bad fmt value %v, expect an integer", args[1].Ident)690 return691 }692 },693 Gen: func(comp *compiler, t *ast.Type, args []*ast.Type, base prog.IntTypeCommon) prog.Type {694 var format prog.BinaryFormat695 var size uint64696 switch args[0].Ident {697 case "dec":698 format = prog.FormatStrDec699 size = 20700 case "hex":701 format = prog.FormatStrHex702 size = 18703 case "oct":704 format = prog.FormatStrOct705 size = 23706 }707 typ := comp.genType(args[1], comp.ptrSize)708 switch t := typ.(type) {709 case *prog.ResourceType:710 t.ArgFormat = format711 t.TypeSize = size712 t.TypeAlign = 1713 case *prog.IntType:714 t.ArgFormat = format715 t.TypeSize = size716 t.TypeAlign = 1717 case *prog.LenType:718 t.ArgFormat = format719 t.TypeSize = size720 t.TypeAlign = 1721 case *prog.FlagsType:722 t.ArgFormat = format723 t.TypeSize = size724 t.TypeAlign = 1725 case *prog.ProcType:726 t.ArgFormat = format727 t.TypeSize = size728 t.TypeAlign = 1729 case *prog.ConstType:730 // We don't allow fmt[const] directly, but flags with only 1 value731 // are transformed to ConstType.732 t.ArgFormat = format733 t.TypeSize = size734 t.TypeAlign = 1735 default:736 panic(fmt.Sprintf("unexpected type: %#v", typ))737 }738 return typ739 },740}741var typeFmtFormat = &typeArg{742 Names: []string{"dec", "hex", "oct"},743 Kind: kindIdent,744}745// typeArgType is used as placeholder for any type (e.g. ptr target type).746var typeArgType = &typeArg{}747var typeResource = &typeDesc{748 // No Names, but getTypeDesc knows how to match it.749 CanBeArgRet: canBeArgRet,750 CanBeResourceBase: func(comp *compiler, t *ast.Type) bool {751 return true752 },753 // Gen is assigned below to avoid initialization loop.754}755func init() {756 typeResource.Gen = func(comp *compiler, t *ast.Type, args []*ast.Type, base prog.IntTypeCommon) prog.Type {757 // Find and generate base type to get its size.758 var baseType *ast.Type759 for r := comp.resources[t.Ident]; r != nil; {760 baseType = r.Base761 r = comp.resources[r.Base.Ident]762 }763 baseProgType := comp.genType(baseType, 0)764 base.TypeSize = baseProgType.Size()765 base.TypeAlign = getIntAlignment(comp, base)766 return &prog.ResourceType{767 TypeCommon: base.TypeCommon,768 ArgFormat: baseProgType.Format(),769 }770 }771}772var typeStruct = &typeDesc{773 // No Names, but getTypeDesc knows how to match it.774 CantBeOpt: true,775 CanBeTypedef: true,776 // Varlen/Gen are assigned below due to initialization cycle.777}778func init() {779 typeStruct.CanBeArgRet = func(comp *compiler, t *ast.Type) (bool, bool) {780 // Allow unions to be arg if all options can be arg.781 s := comp.structs[t.Ident]782 if !s.IsUnion {783 return false, false784 }785 canBeArg := true786 for _, fld := range s.Fields {787 desc := comp.getTypeDesc(fld.Type)788 if desc == nil || desc == typeStruct || desc.CanBeArgRet == nil {789 return false, false790 }791 canBeArg1, _ := desc.CanBeArgRet(comp, fld.Type)792 if !canBeArg1 {793 canBeArg = false794 }795 }796 return canBeArg, false797 }798 typeStruct.Varlen = func(comp *compiler, t *ast.Type, args []*ast.Type) bool {799 return comp.structIsVarlen(t.Ident)800 }801 typeStruct.ZeroSize = func(comp *compiler, t *ast.Type, args []*ast.Type) bool {802 for _, fld := range comp.structs[t.Ident].Fields {803 if !comp.isZeroSize(fld.Type) {804 return false805 }806 }807 return true808 }809 typeStruct.Gen = func(comp *compiler, t *ast.Type, args []*ast.Type, base prog.IntTypeCommon) prog.Type {810 if typ := comp.structTypes[t.Ident]; typ != nil {811 return typ812 }813 s := comp.structs[t.Ident]814 common := genCommon(t.Ident, sizeUnassigned, false)815 common.IsVarlen = typeStruct.Varlen(comp, t, args)816 var typ prog.Type817 if s.IsUnion {818 typ = &prog.UnionType{819 TypeCommon: common,820 }821 } else {822 typ = &prog.StructType{823 TypeCommon: common,824 }825 }826 // Need to cache type in structTypes before generating fields to break recursion.827 comp.structTypes[t.Ident] = typ828 fields := comp.genFieldArray(s.Fields, make([]uint64, len(s.Fields)))829 if s.IsUnion {830 typ.(*prog.UnionType).Fields = fields831 for _, f := range fields {832 if a := f.Type.Alignment(); typ.(*prog.UnionType).TypeAlign < a {833 typ.(*prog.UnionType).TypeAlign = a834 }835 }836 } else {837 typ.(*prog.StructType).Fields = fields838 attrs := comp.parseAttrs(structAttrs, s, s.Attrs)839 if align := attrs[attrAlign]; align != 0 {840 typ.(*prog.StructType).TypeAlign = align841 } else if attrs[attrPacked] != 0 {842 typ.(*prog.StructType).TypeAlign = 1843 } else {844 for _, f := range fields {845 a := f.Type.Alignment()846 if typ.(*prog.StructType).TypeAlign < a {847 typ.(*prog.StructType).TypeAlign = a848 }849 }850 }851 }852 return typ853 }854}855var typeTypedef = &typeDesc{856 // No Names, but getTypeDesc knows how to match it.857 Check: func(comp *compiler, t *ast.Type, args []*ast.Type, base prog.IntTypeCommon) {858 panic("must not be called")859 },...

Full Screen

Full Screen

sam.go

Source:sam.go Github

copy

Full Screen

...189 } // FIXME: catch and collect non-std tags?190 }191 return &prog192}193type Alignment struct {194 Qname string // required | [!-?A-~]{1-255} | query template name195 Flag uint16 // required | [0-2^16 - 1] | bitwise flag196 RefName string // required | \*|[!-()+-<>-~][!-~]*197 Pos uint32 // required | [0-2^29-1]198 Mapq uint8 // required | [0-2^8-1]199 Cigar string // required | \*|([0-9]+[MIDNSHPX=])+200 NextRef string // required | \*|=|[!-()+-<>-~][!-~]*201 NextPos uint32 // required | [0-2^29-1]202 TemplateLen int32 // required | [-2^29+1 - 2^29-1]203 Seq string // required | \*|[A-Za-z=.]+204 Qual string // required ASCII Phred score+33205}206// FIXME: These regexp patterns should be compiled, since they'll be207// used over and over208func validateAlignment(a *Alignment) (bool, error){209 if m, _ := regexp.Match("*|[!-?A-~]+", []byte(a.Qname)); !m {210 return false, SAMerror{"Invalid qname in alignment"}211 }212 if (a.Flag < 0 || a.Flag > 0xFFFF) {213 return false, SAMerror{"Invalid flag in alignment"}214 }215 if m, _ := regexp.Match("*|[!-()+-<>-~][!-~]*", []byte(a.RefName)); !m {216 return false, SAMerror{"Invalid reference sequence name in alignment"}217 }218 if a.Pos < 0 || a.Pos > 0x1FFFFFFF {219 return false, SAMerror{"Alignment mapping position out of valid range"}220 }221 if a.Mapq < 0 || a.Mapq > 0xFF {222 return false, SAMerror{"Alignment mapping quality out of valid range"}223 }224 if m, _ := regexp.Match("*|([0-9]+[MIDNSHPX=])+", []byte(a.Cigar)); !m { 225 return false, SAMerror{"Invalid CIGAR string in alignment"}226 }227 if m, _ := regexp.Match("*|=|[!-()+-<>-~][!-~]*", []byte(a.NextRef)); !m {228 return false, SAMerror{"Invalid next reference name in alignment"}229 }230 if a.NextPos < 0 || a.NextPos > 0x1FFFFFFF {231 return false, SAMerror{"Alignment mapping position out of valid range"}232 }233 if a.TemplateLen < -0x1FFFFFFF || a.TemplateLen > 0x1FFFFFFF {234 return false, SAMerror{"Invalid template length"}235 }236 if m, _ := regexp.Match("*|[A-Za-z=.]+",[]byte(a.Seq)); !m {237 return false, SAMerror{"Invalid sequence in alignment"}238 }239 if m, _ := regexp.Match("*|[!-~]+",[]byte(a.Qual)); !m {240 return false, SAMerror{"Invalie Phred quality in alignment"}241 } 242 return true, nil243}244func parseAlignment(line string) *Alignment {245 fields := strings.Split(line, "\t")246 alignment := Alignment{}247 alignment.Qname = fields[0]248 flagVal, _ := strconv.Atoi(fields[1])249 alignment.Flag = uint16(flagVal)250 alignment.RefName = fields[2]251 posVal, _ := strconv.Atoi(fields[3])252 alignment.Pos = uint32(posVal)253 mapqVal, _ := strconv.Atoi(fields[4])254 alignment.Mapq = uint8(mapqVal)255 alignment.Cigar = fields[5]256 alignment.NextRef = fields[6]257 nextPosVal, _ := strconv.Atoi(fields[7])258 alignment.NextPos = uint32(nextPosVal)259 templateLenVal, _ := strconv.Atoi(fields[8])260 alignment.TemplateLen = int32(templateLenVal) 261 alignment.Seq = fields[9]262 alignment.Qual = fields[10]263 return &alignment264}265func bitIsSet(bit uint8, bitmap uint16) {266 if (bitmap & bit) == bit {267 return true268 }269 return false270}271func hasMultipleSegments(a *Alignment) bool {272 return bitIsSet(0x01, a.Flag)273}274func segmentIsUnmapped(a *Alignment) bool {275 return bitIsSet(0x04, a.Flag)276}277type SAMerror struct {278 str string279}280func (e SAMerror) Error() string {281 return fmt.Sprintf("sam: %s", e.str)282}283func ReadSAMFile(fileName string) (*HeaderLine, *list.List, *list.List, *list.List, *list.List, error) {284 file, err := os.Open(fileName);285 if err != nil {286 fmt.Println(err)287 return nil, nil, nil, nil, nil, err288 }289 reader := bufio.NewReader(file)290 // These will be returned so they must be declared in this scope291 var header *HeaderLine292 var rsdl, rgl, progl, al = list.New(), list.New(), list.New(), list.New()293 // Maps to keep track of values that must be unique. Used for checking for duplicate values.294 var rsdNames, rgIDs, progIDs = map[string]bool{}, map[string]bool{}, map[string]bool{}295 // separating the cases into separate handler functions doesn't296 // seem to win much, so I'm leaving this as it is for now, though297 // it is longer than I'd like.298 for line, _, err := reader.ReadLine(); err == nil; line, _, err = reader.ReadLine() {299 s := string(line)300 switch lineTag := s[1:3]; lineTag {301 case "HD": 302 header = parseHeader(s)303 if valid, err := validateHeader(header); !valid {304 return header, nil, nil, nil, nil, err305 }306 case "SQ":307 rsd := parseRefSeqDict(s)308 if valid, err := validateRefSeqDict(rsd); !valid {309 return header, nil, nil, nil, nil, err310 } else { 311 if rsdNames[rsd.Name] { // Make sure name is unique312 return header, rsdl, nil, nil, nil, SAMerror{"Reference sequence name is not unique"}313 } else { // Everything is OK314 rsdNames[rsd.Name] = true315 rsdl.PushBack(rsd)316 }317 }318 case "RG":319 rg := parseReadGroup(s)320 if valid, err := validateReadGroup(rg); !valid {321 return header, rsdl, rgl, nil, nil, err322 } else {323 if rgIDs[rg.ID] {324 return header, rsdl, rgl, nil, nil, SAMerror{"Read group name is not unique"}325 } else {326 rgIDs[rg.ID] = true327 rgl.PushBack(rg)328 }329 }330 case "PG":331 prog := parseProgram(s)332 if valid, err := validateProgram(prog); !valid {333 return header, rsdl, rgl, progl, nil, err334 } else {335 if progIDs[prog.ID] {336 return header, rsdl, rgl, progl, nil, SAMerror{"Program ID is not unique"}337 } else {338 progIDs[prog.ID] = true339 progl.PushBack(prog)340 }341 }342 case "CO":343 // FIXME: It should be possible for the QNAME field of an344 // alignment to have "HD", "SQ", "RG", "PG", or "CO" as345 // characters 1 and 2, so making alignment the default346 // lone type is not right.347 default: 348 a := parseAlignment(s)349 if valid, err := validateAlignment(a); !valid {350 return header, rsdl, rgl, progl, al , err351 } else {352 al.PushBack(a)353 }354 }355 }356 file.Close()357 return header, rsdl, rgl, progl, al, err358}359func ReadNextAlignment() {360}...

Full Screen

Full Screen

main.go

Source:main.go Github

copy

Full Screen

1package main2import (3 "fmt"4 "github.com/beckbria/advent-of-code/2019/lib"5 "github.com/beckbria/advent-of-code/2019/intcode"6)7const debug = false8func main() {9 p := intcode.ReadIntCode("2019/17/input.txt")10 sw := lib.NewStopwatch()11 // Part 112 fmt.Println(sw.Elapsed())13 fmt.Println(alignmentParams(p))14 // Part 215 sw.Reset()16 fmt.Println(collectDust(p))17 fmt.Println(sw.Elapsed())18}19const (20 scaffold = '#'21 open = '.'22 newLine = int64(10)23)24type grid [][]rune25func (g grid) print() {26 for _, row := range g {27 for _, c := range row {28 fmt.Print(string(c))29 }30 fmt.Print("\n")31 }32}33func (g grid) alignmentParams() []int64 {34 ap := []int64{}35 for y := 1; y < (len(g) - 1); y++ {36 for x := 1; x < (len(g[y]) - 1); x++ {37 if g[y][x] == scaffold && g[y-1][x] == scaffold && g[y+1][x] == scaffold && g[y][x-1] == scaffold && g[y][x+1] == scaffold {38 ap = append(ap, int64(x*y))39 }40 }41 }42 return ap43}44func readGrid(output []int64) grid {45 g := grid{}46 startLine := true47 for _, char := range output {48 if startLine {49 g = append(g, []rune{})50 startLine = false51 }52 if char == newLine {53 startLine = true54 } else {55 g[len(g)-1] = append(g[len(g)-1], rune(char))56 }57 }58 // Filter empty rows59 gf := grid{}60 for _, row := range g {61 if len(row) > 0 {62 gf = append(gf, row)63 }64 }65 return gf66}67func alignmentParams(p intcode.Program) int64 {68 c := intcode.NewComputer(p)69 io := intcode.NewStreamInputOutput([]int64{})70 c.Io = io71 c.Run()72 g := readGrid(io.Outputs)73 ap := g.alignmentParams()74 sum := int64(0)75 for _, i := range ap {76 sum += i77 }78 return sum79}80func collectDust(p intcode.Program) int64 {81 c := intcode.NewComputer(p)82 c.Memory[0] = int64(2)83 // Manually compressed84 input := robotProgram([]string{85 "A,B,A,C,B,A,B,C,C,B", // Main program86 "L,12,L,12,R,4", // A87 "R,10,R,6,R,4,R,4", // B88 "R,6,L,12,L,12"}) // C89 io := intcode.NewStreamInputOutput(input)90 c.Io = io91 c.Run()92 return io.LastOutput()93}94func robotProgram(routines []string) []int64 {95 prog := []int64{}96 for _, r := range routines {97 for _, c := range []rune(r) {98 prog = append(prog, int64(c))99 }100 prog = append(prog, newLine)101 }102 // Reject visual mode103 prog = append(prog, int64('r'))104 prog = append(prog, newLine)105 return prog106}...

Full Screen

Full Screen

Alignment

Using AI Code Generation

copy

Full Screen

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

Full Screen

Full Screen

Alignment

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 fmt.Println(prog.Alignment())4}5func Alignment() int {6}7func Alignment() int {8}9func Alignment() int {10}11func Alignment() int {12}13func Alignment() int {14}15func Alignment() int {16}17func Alignment() int {18}19func Alignment() int {20}21func Alignment() int {22}23func Alignment() int {24}25func Alignment() int {26}27func Alignment() int {28}29func Alignment() int {30}31func Alignment() int {32}33func Alignment() int {34}35func Alignment() int {36}

Full Screen

Full Screen

Alignment

Using AI Code Generation

copy

Full Screen

1import "fmt"2type prog struct {3}4func (p prog) Alignment() {5fmt.Println("Alignment method of prog class")6}7func main() {8p := prog{"Golang", "Go", 1.0}9p.Alignment()10}11import "fmt"12type prog struct {13}14func (p prog) Alignment() {15fmt.Println("Alignment method of prog class")16}17func main() {18p := prog{"Golang", "Go", 1.0}19p.Alignment()20}21import "fmt"22type prog struct {23}24func (p prog) Alignment() {25fmt.Println("Alignment method of prog class")26}27func main() {28p = prog{"Golang", "Go", 1.0}29p.Alignment()30}31import "fmt"32type prog struct {33}34func (p prog) Alignment() {35fmt.Println("Alignment method of prog class")36}37func main() {38p.Alignment()39}40import "fmt"41type prog struct {42}43func (p prog) Alignment() {44fmt.Println("Alignment method of prog class")45}46func main() {47p.Alignment()48fmt.Println(p)49}50{Golang Go 1}

Full Screen

Full Screen

Alignment

Using AI Code Generation

copy

Full Screen

1import "fmt"2import "prog"3func main() {4 fmt.Println(prog.Alignment())5}6TEXT main.Alignment(SB) /home/alex/go/src/1.go7 1.go:6 0x4c4e40 48c70424 08000000 MOVQ $8, 0(SP)8TEXT main.main(SB) /home/alex/go/src/2.go9 2.go:8 0x4c4e60 48c74424 08000000 MOVQ $8, 0x8(SP)

Full Screen

Full Screen

Alignment

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 obj := prog{4 }5 obj.Alignment()6}7import "fmt"8type prog struct {9}10func (p prog) Alignment() {11 fmt.Println(p.name, p.version)12}13import "fmt"14func main() {15 obj := prog{16 }17 obj.Alignment()18}19import "fmt"20type prog struct {21}22func (p prog) Alignment() {23 fmt.Println(p.name, p.version)24}25import "fmt"26func main() {27 obj := prog{28 }29 obj.Alignment()30}31import "fmt"32type prog struct {33}34func (p prog) Alignment() {35 fmt.Println(p.name, p.version)36}37import "fmt"38func main() {39 obj := prog{40 }41 obj.Alignment()42}43import "fmt"44type prog struct {45}46func (p prog) Alignment() {47 fmt.Println(p.name, p.version)

Full Screen

Full Screen

Alignment

Using AI Code Generation

copy

Full Screen

1import "fmt"2import "os"3import "strconv"4import "strings"5import "bufio"6import "github.com/ahmetb/go-linq"7import "github.com/tealeg/xlsx"8import "time"9import "log"10import "runtime"11import "math/rand"12import "sort"13import "math"14import "github.com/olekukonko/tablewriter"15import "bytes"16import "net/http"17import "io/ioutil"18import "encoding/json"19import "net/url"20import "io"21import "archive/zip"22import "path/filepath"23import "path"24func main() {

Full Screen

Full Screen

Alignment

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 var a = prog{progName: "golang", progDesc: "programming language"}4 fmt.Println(a)5 fmt.Println(a.Alignment())6}7import "fmt"8func main() {9 var a = prog{progName: "golang", progDesc: "programming language"}10 fmt.Println(a)11 fmt.Println(a.Alignment())12}13import "fmt"14func main() {15 var a = prog{progName: "golang", progDesc: "programming language"}16 fmt.Println(a)17 fmt.Println(a.Alignment())18}19import "fmt"20func main() {21 var a = prog{progName: "golang", progDesc: "programming language"}22 fmt.Println(a)23 fmt.Println(a.Alignment())24}25import "fmt"26func main() {27 var a = prog{progName: "golang", progDesc: "programming language"}28 fmt.Println(a)29 fmt.Println(a.Alignment())30}31import "fmt"32func main() {33 var a = prog{progName: "golang", progDesc: "programming language"}34 fmt.Println(a)35 fmt.Println(a.Alignment())36}37import "fmt"38func main() {39 var a = prog{progName: "golang", progDesc: "programming language"}40 fmt.Println(a)41 fmt.Println(a.Alignment())42}43import "fmt"44func main() {45 var a = prog{progName: "golang", progDesc: "programming language"}46 fmt.Println(a)47 fmt.Println(a.Alignment())48}49import "fmt"50func main() {51 var a = prog{progName: "golang", progDesc: "programming language"}52 fmt.Println(a)53 fmt.Println(a.Alignment())54}55import "fmt"56func main() {57 var a = prog{progName: "golang", progDesc: "programming language"}58 fmt.Println(a)59 fmt.Println(a.Alignment())60}61import "fmt"62func main() {63 var a = prog{progName: "golang", progDesc: "programming language"}64 fmt.Println(a)65 fmt.Println(a.Alignment())66}67import "fmt"

Full Screen

Full Screen

Alignment

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f, err := os.Open("test.creamy")4 if err != nil {5 panic(err)6 }7 defer f.Close()8 p, err := creamy.Decode(f)9 if err != nil {10 panic(err)11 }12 fmt.Println(p.Alignment())13}14import (15func main() {16 f, err := os.Open("test.creamy")17 if err != nil {18 panic(err)19 }20 defer f.Close()21 p, err := creamy.Decode(f)22 if err != nil {23 panic(err)24 }25 fmt.Println(p.IsAligned())26}27import (28func main() {29 f, err := os.Open("test.creamy")30 if err != nil {31 panic(err)32 }33 defer f.Close()34 p, err := creamy.Decode(f)35 if err != nil {36 panic(err)37 }38 fmt.Println(p.IsAligned())39}40import (41func main() {42 f, err := os.Open("test.creamy")43 if err != nil {44 panic(err)45 }46 defer f.Close()47 p, err := creamy.Decode(f)48 if err != nil {49 panic(err)50 }51 fmt.Println(p.Get(0))52}53import (54func main() {55 f, err := os.Open("test.creamy")

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