How to use Decode method of powerpc Package

Best Syzkaller code snippet using powerpc.Decode

macho.go

Source:macho.go Github

copy

Full Screen

...15 interp.RegisterFormat(decode.Format{16 Name: format.MACHO,17 Description: "Mach-O macOS executable",18 Groups: []string{format.PROBE},19 DecodeFn: machoDecode,20 Files: machoFS,21 Functions: []string{"_help"},22 })23}24//nolint:revive25const (26 MH_MAGIC = 0xfeed_face27 MH_CIGAM = 0xcefa_edfe28 MH_MAGIC_64 = 0xfeed_facf29 MH_CIGAM_64 = 0xcffa_edfe30 FAT_MAGIC = 0xcafe_babe31 FAT_CIGAM = 0xbeba_feca32)33var magicSymMapper = scalar.UToDescription{34 MH_MAGIC: "32-bit little endian",35 MH_CIGAM: "32-bit big endian",36 MH_MAGIC_64: "64-bit little endian",37 MH_CIGAM_64: "64-bit big endian",38}39var endianNames = scalar.UToSymStr{40 MH_MAGIC: "little_endian",41 MH_CIGAM: "big_endian",42 MH_MAGIC_64: "little_endian",43 MH_CIGAM_64: "big_endian",44}45var cpuTypes = scalar.UToSymStr{46 0xff_ff_ff_ff: "any",47 1: "vax",48 2: "romp",49 4: "ns32032",50 5: "ns32332",51 6: "mc680x0",52 7: "x86",53 8: "mips",54 9: "ns32532",55 10: "mc98000",56 11: "hppa",57 12: "arm",58 13: "mc88000",59 14: "sparc",60 15: "i860",61 16: "i860_little",62 17: "rs6000",63 18: "powerpc",64 0x1000007: "x86_64",65 0x100000c: "arm64",66 0x1000013: "powerpc64",67 255: "veo",68}69func intelSubTypeHelper(f, m uint64) uint64 {70 return f + (m << 4)71}72var cpuSubTypes = map[uint64]scalar.UToSymStr{73 0xff_ff_ff_ff: {74 0xff_ff_ff_ff: "multiple",75 },76 1: {77 0xff_ff_ff_ff: "multiple",78 0: "vax_all",79 1: "vax780",80 2: "vax785",81 3: "vax750",82 4: "vax730",83 5: "uvaxi",84 6: "uvaxii",85 7: "vax8200",86 8: "vax8500",87 9: "vax8600",88 10: "vax8650",89 11: "vax8800",90 12: "uvaxiii",91 },92 6: {93 0xff_ff_ff_ff: "multiple",94 1: "mc680x0_all", // 1: mc6803095 2: "mc68040",96 3: "mc68030_only",97 },98 7: {99 0xff_ff_ff_ff: "multiple",100 intelSubTypeHelper(3, 0): "i386_all", // i386101 intelSubTypeHelper(4, 0): "i486",102 intelSubTypeHelper(4, 8): "486sx",103 intelSubTypeHelper(5, 0): "pent",104 intelSubTypeHelper(6, 1): "pentpro",105 intelSubTypeHelper(6, 3): "pentii_m3",106 intelSubTypeHelper(6, 5): "pentii_m5",107 intelSubTypeHelper(7, 6): "celeron",108 intelSubTypeHelper(7, 7): "celeron_mobile",109 intelSubTypeHelper(8, 0): "pentium_3",110 intelSubTypeHelper(8, 1): "pentium_3_m",111 intelSubTypeHelper(8, 2): "pentium_3_xeon",112 intelSubTypeHelper(9, 0): "pentium_m",113 intelSubTypeHelper(10, 0): "pentium_4",114 intelSubTypeHelper(10, 1): "pentium_4_m",115 intelSubTypeHelper(11, 0): "itanium",116 intelSubTypeHelper(11, 1): "itanium_2",117 intelSubTypeHelper(12, 0): "xeon",118 intelSubTypeHelper(12, 1): "xeon_2",119 },120 8: {121 0xff_ff_ff_ff: "multiple",122 0: "mips_all",123 1: "mips_r2300",124 2: "mips_r2600",125 3: "mips_r2800",126 4: "mips_r2000a",127 5: "mips_r2000",128 6: "mips_r3000a",129 7: "mips_r3000",130 },131 10: {132 0xff_ff_ff_ff: "multiple",133 0: "mc98000_all",134 1: "mc98001",135 },136 11: {137 0xff_ff_ff_ff: "multiple",138 0: "hppa_all",139 1: "hppa_7100",140 2: "hppa_7100_lc",141 },142 12: {143 0xff_ff_ff_ff: "multiple",144 0: "arm_all",145 5: "arm_v4t",146 6: "arm_v6",147 7: "arm_v5tej",148 8: "arm_xscale",149 9: "arm_v7",150 10: "arm_v7f",151 11: "arm_v7s",152 12: "arm_v7k",153 13: "arm_v8",154 14: "arm_v6m",155 15: "arm_v7m",156 16: "arm_v7em",157 },158 13: {159 0xff_ff_ff_ff: "multiple",160 0: "mc88000_all",161 1: "mc88100",162 2: "mc88110",163 },164 14: {165 0xff_ff_ff_ff: "multiple",166 0: "sparc_all",167 },168 15: {169 0xff_ff_ff_ff: "multiple",170 0: "i860_all",171 1: "i860_a860",172 },173 18: {174 0xff_ff_ff_ff: "multiple",175 0: "powerpc_all",176 1: "powerpc_601",177 2: "powerpc_602",178 3: "powerpc_603",179 4: "powerpc_603e",180 5: "powerpc_603ev",181 6: "powerpc_604",182 7: "powerpc_604e",183 8: "powerpc_620",184 9: "powerpc_750",185 10: "powerpc_7400",186 11: "powerpc_7450",187 100: "powerpc_970",188 },189 0x1000012: {190 0xff_ff_ff_ff: "multiple",191 0: "arm64_all",192 1: "arm64_v8",193 2: "arm64_e",194 },195}196var fileTypes = scalar.UToSymStr{197 0x1: "object",198 0x2: "execute",199 0x3: "fvmlib",200 0x4: "core",201 0x5: "preload",202 0x6: "dylib",203 0x7: "dylinker",204 0x8: "bundle",205 0x9: "dylib_stub",206 0xa: "dsym",207 0xb: "kext_bundle",208}209//nolint:revive210const (211 LC_REQ_DYLD = 0x80000000212 LC_SEGMENT = 0x1213 LC_SYMTAB = 0x2214 LC_SYMSEG = 0x3215 LC_THREAD = 0x4216 LC_UNIXTHREAD = 0x5217 LC_LOADFVMLIB = 0x6218 LC_IDFVMLIB = 0x7219 LC_IDENT = 0x8 // not implemented220 LC_FVMFILE = 0x9 // not implemented221 LC_PREPAGE = 0xa // not implemented222 LC_DYSYMTAB = 0xb223 LC_LOAD_DYLIB = 0xc224 LC_ID_DYLIB = 0xd225 LC_LOAD_DYLINKER = 0xe226 LC_ID_DYLINKER = 0xf227 LC_PREBOUND_DYLIB = 0x10228 LC_ROUTINES = 0x11229 LC_SUB_FRAMEWORK = 0x12230 LC_SUB_UMBRELLA = 0x13231 LC_SUB_CLIENT = 0x14232 LC_SUB_LIBRARY = 0x15233 LC_TWOLEVEL_HINTS = 0x16234 LC_PREBIND_CKSUM = 0x17 // not implemented235 LC_LOAD_WEAK_DYLIB = 0x80000018236 LC_SEGMENT_64 = 0x19237 LC_ROUTINES_64 = 0x1a238 LC_UUID = 0x1b239 LC_RPATH = 0x8000001c240 LC_CODE_SIGNATURE = 0x1d241 LC_SEGMENT_SPLIT_INFO = 0x1e242 LC_REEXPORT_DYLIB = 0x8000001f243 LC_LAZY_LOAD_DYLIB = 0x20244 LC_ENCRYPTION_INFO = 0x21245 LC_DYLD_INFO = 0x22246 LC_DYLD_INFO_ONLY = 0x80000022247 LC_LOAD_UPWARD_DYLIB = 0x80000023248 LC_VERSION_MIN_MACOSX = 0x24249 LC_VERSION_MIN_IPHONEOS = 0x25250 LC_FUNCTION_STARTS = 0x26251 LC_DYLD_ENVIRONMENT = 0x27252 LC_MAIN = 0x80000028253 LC_DATA_IN_CODE = 0x29254 LC_SOURCE_VERSION = 0x2a255 LC_DYLIB_CODE_SIGN_DRS = 0x2b256 LC_ENCRYPTION_INFO_64 = 0x2c257 LC_LINKER_OPTION = 0x2d258 LC_LINKER_OPTIMIZATION_HINT = 0x2e259 LC_VERSION_MIN_TVOS = 0x2f260 LC_VERSION_MIN_WATCHOS = 0x30261 LC_NOTE = 0x31 // not implemented262 LC_BUILD_VERSION = 0x32263)264var loadCommands = scalar.UToSymStr{265 LC_REQ_DYLD: "req_dyld",266 LC_SEGMENT: "segment",267 LC_SYMTAB: "symtab",268 LC_SYMSEG: "symseg",269 LC_THREAD: "thread",270 LC_UNIXTHREAD: "unixthread",271 LC_LOADFVMLIB: "loadfvmlib",272 LC_IDFVMLIB: "idfvmlib",273 LC_IDENT: "ident",274 LC_FVMFILE: "fvmfile",275 LC_PREPAGE: "prepage",276 LC_DYSYMTAB: "dysymtab",277 LC_LOAD_DYLIB: "load_dylib",278 LC_ID_DYLIB: "id_dylib",279 LC_LOAD_DYLINKER: "load_dylinker",280 LC_ID_DYLINKER: "id_dylinker",281 LC_PREBOUND_DYLIB: "prebound_dylib",282 LC_ROUTINES: "routines",283 LC_SUB_FRAMEWORK: "sub_framework",284 LC_SUB_UMBRELLA: "sub_umbrella",285 LC_SUB_CLIENT: "sub_client",286 LC_SUB_LIBRARY: "sub_library",287 LC_TWOLEVEL_HINTS: "twolevel_hints",288 LC_PREBIND_CKSUM: "prebind_cksum",289 LC_LOAD_WEAK_DYLIB: "load_weak_dylib",290 LC_SEGMENT_64: "segment_64",291 LC_ROUTINES_64: "routines_64",292 LC_UUID: "uuid",293 LC_RPATH: "rpath",294 LC_CODE_SIGNATURE: "code_signature",295 LC_SEGMENT_SPLIT_INFO: "segment_split_info",296 LC_REEXPORT_DYLIB: "reexport_dylib",297 LC_LAZY_LOAD_DYLIB: "lazy_load_dylib",298 LC_ENCRYPTION_INFO: "encryption_info",299 LC_DYLD_INFO: "dyld_info",300 LC_DYLD_INFO_ONLY: "dyld_info_only",301 LC_LOAD_UPWARD_DYLIB: "load_upward_dylib",302 LC_VERSION_MIN_MACOSX: "version_min_macosx",303 LC_VERSION_MIN_IPHONEOS: "version_min_iphoneos",304 LC_FUNCTION_STARTS: "function_starts",305 LC_DYLD_ENVIRONMENT: "dyld_environment",306 LC_MAIN: "main",307 LC_DATA_IN_CODE: "data_in_code",308 LC_SOURCE_VERSION: "source_version",309 LC_DYLIB_CODE_SIGN_DRS: "dylib_code_sign_drs",310 LC_ENCRYPTION_INFO_64: "encryption_info_64",311 LC_LINKER_OPTION: "linker_option",312 LC_LINKER_OPTIMIZATION_HINT: "linker_optimization_hint",313 LC_VERSION_MIN_TVOS: "version_min_tvos",314 LC_VERSION_MIN_WATCHOS: "version_min_watchos",315 LC_NOTE: "note",316 LC_BUILD_VERSION: "build_version",317}318var sectionTypes = scalar.UToSymStr{319 0x0: "regular",320 0x1: "zerofill",321 0x2: "cstring_literals",322 0x3: "4byte_literals",323 0x4: "8byte_literals",324 0x5: "literal_pointers",325 0x6: "non_lazy_symbol_pointers",326 0x7: "lazy_symbol_pointers",327 0x8: "symbol_stubs",328 0x9: "mod_init_func_pointers",329 0xa: "mod_term_func_pointers",330 0xb: "coalesced",331 0xc: "gb_zerofill",332 0xd: "interposing",333 0xe: "16byte_literals",334 0xf: "dtrace_dof",335 0x10: "lazy_dylib_symbol_pointers",336 0x11: "thread_local_regular",337 0x12: "thread_local_zerofill",338 0x13: "thread_local_variables",339 0x14: "thread_local_variable_pointers",340 0x15: "thread_local_init_function_pointers",341}342func machoDecode(d *decode.D, in any) any {343 ofileDecode(d)344 return nil345}346func ofileDecode(d *decode.D) {347 var archBits int348 var cpuType uint64349 var ncmds uint64350 magicBuffer := d.U32LE()351 if magicBuffer == MH_MAGIC || magicBuffer == MH_MAGIC_64 {352 d.Endian = decode.LittleEndian353 if magicBuffer == MH_MAGIC {354 archBits = 32355 } else {356 archBits = 64357 }358 } else if magicBuffer == MH_CIGAM || magicBuffer == MH_CIGAM_64 {359 d.Endian = decode.BigEndian360 if magicBuffer == MH_CIGAM {361 archBits = 32362 } else {363 archBits = 64364 }365 } else if magicBuffer == FAT_MAGIC {366 d.Endian = decode.LittleEndian367 fatParse(d)368 return369 } else if magicBuffer == FAT_CIGAM {370 d.Endian = decode.BigEndian371 fatParse(d)372 return373 } else {374 // AR files are also valid OFiles but they should be parsed by `-d ar`375 d.Fatalf("Invalid magic field")376 }377 d.SeekRel(-4 * 8)378 d.FieldStruct("header", func(d *decode.D) {379 d.FieldValueS("arch_bits", int64(archBits))380 magic := d.FieldU32("magic", magicSymMapper, scalar.ActualHex)381 d.FieldValueU("bits", uint64(archBits))382 d.FieldValueStr("endian", endianNames[magic])383 cpuType = d.FieldU32("cputype", cpuTypes, scalar.ActualHex)384 d.FieldU32("cpusubtype", cpuSubTypes[cpuType], scalar.ActualHex)385 d.FieldU32("filetype", fileTypes)386 ncmds = d.FieldU32("ncdms")387 d.FieldU32("sizeofncdms")388 d.FieldStruct("flags", parseMachHeaderFlags)389 if archBits == 64 {390 d.FieldRawLen("reserved", 4*8, d.BitBufIsZero())391 }392 })393 d.FieldArray("load_commands", func(d *decode.D) {394 d.RangeSorted = false395 for i := uint64(0); i < ncmds; i++ {396 d.FieldStruct("load_command", func(d *decode.D) {397 cmd := d.FieldU32("cmd", loadCommands, scalar.ActualHex)398 cmdsize := d.FieldU32("cmdsize")399 switch cmd {400 case LC_UUID:401 d.FieldStruct("uuid_command", func(d *decode.D) {402 d.FieldRawLen("uuid", 16*8)403 })404 case LC_SEGMENT, LC_SEGMENT_64:405 // nsect := (cmdsize - uint64(archBits)) / uint64(archBits)406 var nsects uint64407 d.FieldStruct("segment_command", func(d *decode.D) {408 d.FieldValueS("arch_bits", int64(archBits))409 d.FieldUTF8NullFixedLen("segname", 16) // OPCODE_DECODER segname==__TEXT410 if archBits == 32 {411 d.FieldU32("vmaddr", scalar.ActualHex)412 d.FieldU32("vmsize")413 d.FieldU32("fileoff")414 d.FieldU32("tfilesize")415 } else {416 d.FieldU64("vmaddr", scalar.ActualHex)417 d.FieldU64("vmsize")418 d.FieldU64("fileoff")419 d.FieldU64("tfilesize")420 }421 d.FieldS32("initprot")422 d.FieldS32("maxprot")423 nsects = d.FieldU32("nsects")424 d.FieldStruct("flags", parseSegmentFlags)425 })426 d.FieldArray("sections", func(d *decode.D) {427 d.RangeSorted = false428 for i := uint64(0); i < nsects; i++ {429 d.FieldStruct("section", func(d *decode.D) {430 // OPCODE_DECODER sectname==__text431 d.FieldUTF8NullFixedLen("sectname", 16)432 d.FieldUTF8NullFixedLen("segname", 16)433 var size uint64434 if archBits == 32 {435 d.FieldU32("address", scalar.ActualHex)436 size = d.FieldU32("size")437 } else {438 d.FieldU64("address", scalar.ActualHex)439 size = d.FieldU64("size")440 }441 offset := d.FieldU32("offset")442 d.FieldU32("align")443 d.FieldU32("reloff")444 d.FieldU32("nreloc")445 // get section type446 d.FieldStruct("flags", parseSectionFlags)447 d.FieldU8("type", sectionTypes)448 d.FieldU32("reserved1")449 d.FieldU32("reserved2")450 if archBits == 64 {451 d.FieldU32("reserved3")452 }453 d.RangeFn(int64(offset)*8, int64(size)*8, func(d *decode.D) {454 d.FieldRawLen("data", d.BitsLeft())455 })456 })457 }458 })459 case LC_TWOLEVEL_HINTS:460 d.FieldU32("offset")461 d.FieldU32("nhints")462 case LC_LOAD_DYLIB, LC_ID_DYLIB, LC_LOAD_UPWARD_DYLIB, LC_LOAD_WEAK_DYLIB, LC_LAZY_LOAD_DYLIB, LC_REEXPORT_DYLIB:463 d.FieldStruct("dylib_command", func(d *decode.D) {464 offset := d.FieldU32("offset")465 d.FieldU32("timestamp", timestampMapper)466 d.FieldU32("current_version")467 d.FieldU32("compatibility_version")468 d.FieldUTF8NullFixedLen("name", int(cmdsize)-int(offset))469 })470 case LC_LOAD_DYLINKER, LC_ID_DYLINKER, LC_DYLD_ENVIRONMENT:471 offset := d.FieldU32("offset")472 d.FieldUTF8NullFixedLen("name", int(cmdsize)-int(offset))473 case LC_RPATH:474 offset := d.FieldU32("offset")475 d.FieldUTF8NullFixedLen("name", int(cmdsize)-int(offset))476 case LC_PREBOUND_DYLIB:477 // https://github.com/aidansteele/osx-abi-macho-file-format-reference#prebound_dylib_command478 d.U32() // name_offset479 nmodules := d.FieldU32("nmodules")480 d.U32() // linked_modules_offset481 d.FieldUTF8Null("name")482 d.FieldBitBufFn("linked_modules", func(d *decode.D) bitio.ReaderAtSeeker {483 return d.RawLen(int64((nmodules / 8) + (nmodules % 8)))484 })485 case LC_THREAD, LC_UNIXTHREAD:486 d.FieldU32("flavor")487 count := d.FieldU32("count")488 d.FieldStruct("state", func(d *decode.D) {489 switch cpuType {490 case 0x7:491 threadStateI386Decode(d)492 case 0xC:493 threadStateARM32Decode(d)494 case 0x13:495 threadStatePPC32Decode(d)496 case 0x1000007:497 threadStateX8664Decode(d)498 case 0x100000C:499 threadStateARM64Decode(d)500 case 0x1000013:501 threadStatePPC64Decode(d)502 default:503 d.FieldRawLen("state", int64(count*32))504 }505 })506 case LC_ROUTINES, LC_ROUTINES_64:507 if archBits == 32 {508 d.FieldU32("init_address", scalar.ActualHex)509 d.FieldU32("init_module")510 d.FieldU32("reserved1")511 d.FieldU32("reserved2")512 d.FieldU32("reserved3")513 d.FieldU32("reserved4")514 d.FieldU32("reserved5")515 d.FieldU32("reserved6")516 } else {517 d.FieldU64("init_address", scalar.ActualHex)518 d.FieldU64("init_module")519 d.FieldU64("reserved1")520 d.FieldU64("reserved2")521 d.FieldU64("reserved3")522 d.FieldU64("reserved4")523 d.FieldU64("reserved5")524 d.FieldU64("reserved6")525 }526 case LC_SUB_UMBRELLA, LC_SUB_LIBRARY, LC_SUB_CLIENT, LC_SUB_FRAMEWORK:527 offset := d.FieldU32("offset")528 d.FieldUTF8NullFixedLen("name", int(cmdsize)-int(offset))529 case LC_SYMTAB:530 d.FieldU32("symoff")531 d.FieldU32("nsyms")532 d.FieldU32("stroff")533 d.FieldU32("strsize")534 case LC_DYSYMTAB:535 d.FieldU32("ilocalsym")536 d.FieldU32("nlocalsym")537 d.FieldU32("iextdefsym")538 d.FieldU32("nextdefsym")539 d.FieldU32("iundefsym")540 d.FieldU32("nundefsym")541 d.FieldU32("tocoff")542 d.FieldU32("ntoc")543 d.FieldU32("modtaboff")544 d.FieldU32("nmodtab")545 d.FieldU32("extrefsymoff")546 d.FieldU32("nextrefsyms")547 d.FieldU32("indirectsymoff")548 d.FieldU32("nindirectsyms")549 d.FieldU32("extreloff")550 d.FieldU32("nextrel")551 d.FieldU32("locreloff")552 d.FieldU32("nlocrel")553 case LC_BUILD_VERSION:554 d.FieldU32("platform")555 d.FieldU32("minos")556 d.FieldU32("sdk")557 ntools := d.FieldU32("ntools")558 var ntoolsIdx uint64559 d.FieldStructArrayLoop("tools", "tool", func() bool {560 return ntoolsIdx < ntools561 }, func(d *decode.D) {562 d.FieldU32("tool")563 d.FieldU32("version")564 ntoolsIdx++565 })566 case LC_CODE_SIGNATURE, LC_SEGMENT_SPLIT_INFO, LC_FUNCTION_STARTS, LC_DATA_IN_CODE, LC_DYLIB_CODE_SIGN_DRS, LC_LINKER_OPTIMIZATION_HINT:567 d.FieldStruct("linkedit_data", func(d *decode.D) {568 d.FieldU32("off")569 d.FieldU32("size")570 })571 case LC_VERSION_MIN_IPHONEOS, LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_TVOS, LC_VERSION_MIN_WATCHOS:572 d.FieldU32("version")573 d.FieldU32("sdk")574 case LC_DYLD_INFO, LC_DYLD_INFO_ONLY:575 d.FieldStruct("dyld_info", func(d *decode.D) {576 d.FieldU32("rebase_off")577 d.FieldU32("rebase_size")578 d.FieldU32("bind_off")579 d.FieldU32("bind_size")580 d.FieldU32("weak_bind_off")581 d.FieldU32("weak_bind_size")582 d.FieldU32("lazy_bind_off")583 d.FieldU32("lazy_bind_size")584 d.FieldU32("export_off")585 d.FieldU32("export_size")586 })587 case LC_MAIN:588 d.FieldStruct("entrypoint", func(d *decode.D) {589 d.FieldU64("entryoff")590 d.FieldU64("stacksize")591 })592 case LC_SOURCE_VERSION:593 d.FieldStruct("source_version_tag", func(d *decode.D) {594 d.FieldU64("tag")595 })596 case LC_LINKER_OPTION:597 d.FieldStruct("linker_option", func(d *decode.D) {598 count := d.FieldU32("count")599 d.FieldUTF8NullFixedLen("option", int(count))600 })601 case LC_ENCRYPTION_INFO, LC_ENCRYPTION_INFO_64:602 d.FieldStruct("encryption_info", func(d *decode.D) {603 offset := d.FieldU32("offset")604 size := d.FieldU32("size")605 d.FieldU32("id")606 d.RangeFn(int64(offset)*8, int64(size)*8, func(d *decode.D) {607 d.FieldRawLen("data", d.BitsLeft())608 })609 })610 case LC_IDFVMLIB, LC_LOADFVMLIB:611 d.FieldStruct("fvmlib", func(d *decode.D) {612 offset := d.FieldU32("offset")613 d.FieldU32("minor_version")614 d.FieldU32("header_addr", scalar.ActualHex)615 d.FieldUTF8NullFixedLen("name", int(cmdsize)-int(offset))616 })617 default:618 if _, ok := loadCommands[cmd]; !ok {619 d.SeekRel(int64((cmdsize - 8) * 8))620 // Seek Rel so the parts are marked unknown621 }622 }623 })624 }625 })626}627func fatParse(d *decode.D) {628 // Go to start of the file again629 d.SeekAbs(0)630 var narchs uint64631 var ofileOffsets []uint64632 d.FieldStruct("fat_header", func(d *decode.D) {633 d.FieldU32("magic", scalar.ActualHex)634 narchs = d.FieldU32("narchs")635 narchsIdx := 0636 d.FieldStructArrayLoop("archs", "fat_arch", func() bool {637 return narchsIdx < int(narchs)638 }, func(d *decode.D) {639 // parse FatArch640 // beware cputype and cpusubtype changes from ofile header to fat header641 cpuType := d.FieldU32("cputype", cpuTypes, scalar.ActualHex)642 d.FieldU32("cpusubtype", cpuSubTypes[cpuType], scalar.ActualHex)643 ofileOffsets = append(ofileOffsets, d.FieldU32("offset"))644 d.FieldU32("size")645 d.FieldU32("align")646 narchsIdx++647 })648 })649 nfilesIdx := 0650 d.FieldStructArrayLoop("files", "file", func() bool {651 return nfilesIdx < int(narchs)652 }, func(d *decode.D) {653 d.SeekAbs(int64(ofileOffsets[nfilesIdx]) * 8)654 ofileDecode(d)655 nfilesIdx++656 })657}658func parseMachHeaderFlags(d *decode.D) {659 d.FieldRawLen("reserved", 6)660 d.FieldBool("app_extension_safe")661 d.FieldBool("no_heap_execution")662 d.FieldBool("has_tlv_descriptors")663 d.FieldBool("dead_strippable_dylib")664 d.FieldBool("pie")665 d.FieldBool("no_reexported_dylibs")666 d.FieldBool("setuid_safe")667 d.FieldBool("root_safe")668 d.FieldBool("allow_stack_execution")669 d.FieldBool("binds_to_weak")670 d.FieldBool("weak_defines")671 d.FieldBool("canonical")672 d.FieldBool("subsections_via_symbols")673 d.FieldBool("allmodsbound")674 d.FieldBool("prebindable")675 d.FieldBool("nofixprebinding")676 d.FieldBool("nomultidefs")677 d.FieldBool("force_flat")678 d.FieldBool("twolevel")679 d.FieldBool("lazy_init")680 d.FieldBool("split_segs")681 d.FieldBool("prebound")682 d.FieldBool("bindatload")683 d.FieldBool("dyldlink")684 d.FieldBool("incrlink")685 d.FieldBool("noundefs")686}687func parseSegmentFlags(d *decode.D) {688 d.FieldRawLen("reserved", 28)689 d.FieldBool("protected_version_1")690 d.FieldBool("noreloc")691 d.FieldBool("fvmlib")692 d.FieldBool("highvm")693}694func parseSectionFlags(d *decode.D) {695 d.FieldBool("attr_pure_instructions")696 d.FieldBool("attr_no_toc")697 d.FieldBool("attr_strip_static_syms")698 d.FieldBool("attr_no_dead_strip")699 d.FieldBool("attr_live_support")700 d.FieldBool("attr_self_modifying_code")701 d.FieldBool("attr_debug")702 d.FieldRawLen("reserved", 14)703 d.FieldBool("attr_some_instructions")704 d.FieldBool("attr_ext_reloc")705 d.FieldBool("attr_loc_reloc")706}707var timestampMapper = scalar.Fn(func(s scalar.S) (scalar.S, error) {708 ts, ok := s.Actual.(uint64)709 if !ok {710 return s, nil711 }712 s.Sym = time.UnixMilli(int64(ts)).UTC().String()713 return s, nil714})715func threadStateI386Decode(d *decode.D) {716 d.FieldU32("eax")717 d.FieldU32("ebx")718 d.FieldU32("ecx")719 d.FieldU32("edx")720 d.FieldU32("edi")721 d.FieldU32("esi")722 d.FieldU32("ebp")723 d.FieldU32("esp")724 d.FieldU32("ss")725 d.FieldU32("eflags")726 d.FieldU32("eip")727 d.FieldU32("cs")728 d.FieldU32("ds")729 d.FieldU32("es")730 d.FieldU32("fs")731 d.FieldU32("gs")732}733func threadStateX8664Decode(d *decode.D) {734 d.FieldU64("rax")735 d.FieldU64("rbx")736 d.FieldU64("rcx")737 d.FieldU64("rdx")738 d.FieldU64("rdi")739 d.FieldU64("rsi")740 d.FieldU64("rbp")741 d.FieldU64("rsp")742 d.FieldU64("r8")743 d.FieldU64("r9")744 d.FieldU64("r10")745 d.FieldU64("r11")746 d.FieldU64("r12")747 d.FieldU64("r13")748 d.FieldU64("r14")749 d.FieldU64("r15")750 d.FieldU64("rip")751 d.FieldU64("rflags")752 d.FieldU64("cs")753 d.FieldU64("fs")754 d.FieldU64("gs")755}756func threadStateARM32Decode(d *decode.D) {757 rIdx := 0758 d.FieldStructArrayLoop("r", "r", func() bool {759 return rIdx < 13760 }, func(d *decode.D) {761 d.FieldU32("value")762 rIdx++763 })764 d.FieldU32("sp")765 d.FieldU32("lr")766 d.FieldU32("pc")767 d.FieldU32("cpsr")768}769func threadStateARM64Decode(d *decode.D) {770 rIdx := 0771 d.FieldStructArrayLoop("r", "r", func() bool {772 return rIdx < 29773 }, func(d *decode.D) {774 d.FieldU64("value")775 rIdx++776 })777 d.FieldU64("fp")778 d.FieldU64("lr")779 d.FieldU64("sp")780 d.FieldU64("pc")781 d.FieldU32("cpsr")782 d.FieldU32("pad")783}784func threadStatePPC32Decode(d *decode.D) {785 srrIdx := 0786 d.FieldStructArrayLoop("srr", "srr", func() bool {787 return srrIdx < 2788 }, func(d *decode.D) {789 d.FieldU32("value")790 srrIdx++791 })792 rIdx := 0793 d.FieldStructArrayLoop("r", "r", func() bool {794 return rIdx < 32795 }, func(d *decode.D) {796 d.FieldU32("value")797 rIdx++798 })799 d.FieldU32("ct")800 d.FieldU32("xer")801 d.FieldU32("lr")802 d.FieldU32("ctr")803 d.FieldU32("mq")804 d.FieldU32("vrsave")805}806func threadStatePPC64Decode(d *decode.D) {807 srrIdx := 0808 d.FieldStructArrayLoop("srr", "srr", func() bool {809 return srrIdx < 2810 }, func(d *decode.D) {811 d.FieldU64("value")812 srrIdx++813 })814 rIdx := 0815 d.FieldStructArrayLoop("r", "r", func() bool {816 return rIdx < 32817 }, func(d *decode.D) {818 d.FieldU64("value")819 rIdx++820 })...

Full Screen

Full Screen

run_test.go

Source:run_test.go Github

copy

Full Screen

...48 mc Machine49 )50 for _, arch := range archList {51 mc = SetArch(arch)52 correct, _ := hex.DecodeString(testCodes[arch][0])53 code,_ := mc.Assemble(testCodes[arch][1])54 if !reflect.DeepEqual(code, correct) {55 t.Errorf("[-]: %s(o: %x, x: %x)", arch, correct, code)56 }57 }58 mc.Finalize()59}60func TestXI8086Emulate(t *testing.T) {61 var (62 arch string = "i8086"63 readReg string = "ax"64 correctReg = uint64(0x1122)65 correctData = []byte("\x22\x11")66 )67 mc := SetArch(arch)68 code, _ := hex.DecodeString(testCodes[arch][0])69 if err := mc.Emulate(code); err != nil {70 t.Errorf("[-] %s,%s ]", arch, err)71 }72 reg, _ := mc.mu.RegRead(mc.regs[readReg])73 if reg != correctReg {74 t.Errorf("[-] %s register(o: %x, x: %x) ]", arch, correctReg, reg)75 }76 sp, _ := mc.mu.RegRead(mc.sp)77 data, _ := mc.mu.MemRead(sp, uint64(mc.bit/8))78 if !reflect.DeepEqual(data, correctData) {79 t.Errorf("[-] %s stack(o: %x, x: %x)", arch, correctData, data)80 }81 mc.Finalize()82}83func TestXX86Emulate(t *testing.T) {84 var (85 arch string = "x86"86 readReg string = "eax"87 correctReg = uint64(0x11223344)88 correctData = []byte("\x44\x33\x22\x11")89 )90 mc := SetArch(arch)91 code, _ := hex.DecodeString(testCodes[arch][0])92 if err := mc.Emulate(code); err != nil {93 t.Errorf("[-] %s,%s ]", arch, err)94 }95 reg, _ := mc.mu.RegRead(mc.regs[readReg])96 if reg != correctReg {97 t.Errorf("[-] %s register(o: %x, x: %x) ]", arch, correctReg, reg)98 }99 sp, _ := mc.mu.RegRead(mc.sp)100 data, _ := mc.mu.MemRead(sp, uint64(mc.bit/8))101 if !reflect.DeepEqual(data, correctData) {102 t.Errorf("[-] %s stack(o: %x, x: %x)", arch, correctData, data)103 }104 mc.Finalize()105}106func TestXX64Emulate(t *testing.T) {107 var (108 arch string = "x64"109 readReg string = "rax"110 correctReg = uint64(0x1122334455667788)111 correctData = []byte("\x88\x77\x66\x55\x44\x33\x22\x11")112 )113 mc := SetArch(arch)114 code, _ := hex.DecodeString(testCodes[arch][0])115 if err := mc.Emulate(code); err != nil {116 t.Errorf("[-] %s,%s ]", arch, err)117 }118 reg, _ := mc.mu.RegRead(mc.regs[readReg])119 if reg != correctReg {120 t.Errorf("[-] %s register(o: %x, x: %x) ]", arch, correctReg, reg)121 }122 sp, _ := mc.mu.RegRead(mc.sp)123 data, _ := mc.mu.MemRead(sp, uint64(mc.bit/8))124 if !reflect.DeepEqual(data, correctData) {125 t.Errorf("[-] %s stack(o: %x, x: %x)", arch, correctData, data)126 }127 mc.Finalize()128}129func TestXMipsEmulate(t *testing.T) {130 var (131 arch string = "mips"132 readReg string = "t0"133 correctReg = uint64(0x11223344)134 correctData = []byte("\x44\x33\x22\x11")135 )136 mc := SetArch(arch)137 code, _ := hex.DecodeString(testCodes[arch][0])138 if err := mc.Emulate(code); err != nil {139 t.Errorf("[-] %s,%s ]", arch, err)140 }141 reg, _ := mc.mu.RegRead(mc.regs[readReg])142 if reg != correctReg {143 t.Errorf("[-] %s register(o: %x, x: %x) ]", arch, correctReg, reg)144 }145 sp, _ := mc.mu.RegRead(mc.sp)146 data, _ := mc.mu.MemRead(sp, uint64(mc.bit/8))147 if !reflect.DeepEqual(data, correctData) {148 t.Errorf("[-] %s stack(o: %x, x: %x)", arch, correctData, data)149 }150 mc.Finalize()151}152func TestXMipsbeEmulate(t *testing.T) {153 var (154 arch string = "mipsbe"155 readReg string = "t0"156 correctReg = uint64(0x11223344)157 correctData = []byte("\x11\x22\x33\x44")158 )159 mc := SetArch(arch)160 code, _ := hex.DecodeString(testCodes[arch][0])161 if err := mc.Emulate(code); err != nil {162 t.Errorf("[-] %s,%s ]", arch, err)163 }164 reg, _ := mc.mu.RegRead(mc.regs[readReg])165 if reg != correctReg {166 t.Errorf("[-] %s register(o: %x, x: %x) ]", arch, correctReg, reg)167 }168 sp, _ := mc.mu.RegRead(mc.sp)169 data, _ := mc.mu.MemRead(sp, uint64(mc.bit/8))170 if !reflect.DeepEqual(data, correctData) {171 t.Errorf("[-] %s stack(o: %x, x: %x)", arch, correctData, data)172 }173 mc.Finalize()174}175func TestXArmEmulate(t *testing.T) {176 var (177 arch string = "arm"178 readReg string = "r0"179 correctReg = uint64(0x11223344)180 correctData = []byte("\x44\x33\x22\x11")181 )182 mc := SetArch(arch)183 code, _ := hex.DecodeString(testCodes[arch][0])184 if err := mc.Emulate(code); err != nil {185 t.Errorf("[-] %s,%s ]", arch, err)186 }187 reg, _ := mc.mu.RegRead(mc.regs[readReg])188 if reg != correctReg {189 t.Errorf("[-] %s register(o: %x, x: %x) ]", arch, correctReg, reg)190 }191 sp, _ := mc.mu.RegRead(mc.sp)192 data, _ := mc.mu.MemRead(sp, uint64(mc.bit/8))193 if !reflect.DeepEqual(data, correctData) {194 t.Errorf("[-] %s stack(o: %x, x: %x)", arch, correctData, data)195 }196 mc.Finalize()197}198func TestXArmbeEmulate(t *testing.T) {199 var (200 arch string = "armbe"201 readReg string = "r0"202 correctReg = uint64(0x11223344)203 correctData = []byte("\x11\x22\x33\x44")204 )205 mc := SetArch(arch)206 code, _ := hex.DecodeString(testCodes[arch][0])207 if err := mc.Emulate(code); err != nil {208 t.Errorf("[-] %s,%s ]", arch, err)209 }210 reg, _ := mc.mu.RegRead(mc.regs[readReg])211 if reg != correctReg {212 t.Errorf("[-] %s register(o: %x, x: %x) ]", arch, correctReg, reg)213 }214 sp, _ := mc.mu.RegRead(mc.sp)215 data, _ := mc.mu.MemRead(sp, uint64(mc.bit/8))216 if !reflect.DeepEqual(data, correctData) {217 t.Errorf("[-] %s stack(o: %x, x: %x)", arch, correctData, data)218 }219 mc.Finalize()220}221func TestXSparcEmulate(t *testing.T) {222 var (223 arch string = "sparc"224 readReg string = "g1"225 correctReg = uint64(0x11223344)226 correctData = []byte("\x11\x22\x33\x44")227 )228 mc := SetArch(arch)229 code, _ := hex.DecodeString(testCodes[arch][0])230 if err := mc.Emulate(code); err != nil {231 t.Errorf("[-] %s,%s ]", arch, err)232 }233 reg, _ := mc.mu.RegRead(mc.regs[readReg])234 if reg != correctReg {235 t.Errorf("[-] %s register(o: %x, x: %x) ]", arch, correctReg, reg)236 }237 sp, _ := mc.mu.RegRead(mc.sp)238 data, _ := mc.mu.MemRead(sp, uint64(mc.bit/8))239 if !reflect.DeepEqual(data, correctData) {240 t.Errorf("[-] %s stack(o: %x, x: %x)", arch, correctData, data)241 }242 mc.Finalize()243}244func TestXThumbEmulate(t *testing.T) {245 var (246 arch string = "thumb"247 readReg string = "r0"248 correctReg = uint64(0x1122)249 correctData = []byte("\x22\x11")250 )251 mc := SetArch(arch)252 code, _ := hex.DecodeString(testCodes[arch][0])253 if err := mc.Emulate(code); err != nil {254 t.Errorf("[-] %s,%s ]", arch, err)255 }256 reg, _ := mc.mu.RegRead(mc.regs[readReg])257 if reg != correctReg {258 t.Errorf("[-] %s register(o: %x, x: %x) ]", arch, correctReg, reg)259 }260 sp, _ := mc.mu.RegRead(mc.sp)261 data, _ := mc.mu.MemRead(sp, uint64(mc.bit/8))262 if !reflect.DeepEqual(data, correctData) {263 t.Errorf("[-] %s stack(o: %x, x: %x)", arch, correctData, data)264 }265 mc.Finalize()266}267// fixme: sp+2 is correct?268func TestXThumbbeEmulate(t *testing.T) {269 var (270 arch string = "thumbbe"271 readReg string = "r0"272 correctReg = uint64(0x1122)273 correctData = []byte("\x11\x22")274 )275 mc := SetArch(arch)276 code, _ := hex.DecodeString(testCodes[arch][0])277 if err := mc.Emulate(code); err != nil {278 t.Errorf("[-] %s,%s ]", arch, err)279 }280 reg, _ := mc.mu.RegRead(mc.regs[readReg])281 if reg != correctReg {282 t.Errorf("[-] %s register(o: %x, x: %x) ]", arch, correctReg, reg)283 }284 sp, _ := mc.mu.RegRead(mc.sp)285 data, _ := mc.mu.MemRead(sp+2, uint64(mc.bit/8))286 if !reflect.DeepEqual(data, correctData) {287 t.Errorf("[-] %s stack(o: %x, x: %x)", arch, correctData, data)288 }289 mc.Finalize()290}...

Full Screen

Full Screen

Decode

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 powerpc.Decode()4 fmt.Println("PowerPC Decode")5}6import (7func main() {8 amd64.Decode()9 fmt.Println("AMD64 Decode")10}11import (12func main() {13 arm.Decode()14 fmt.Println("ARM Decode")15}16import (17func main() {18 x86.Decode()19 fmt.Println("X86 Decode")20}21import (22func main() {23 mips.Decode()24 fmt.Println("MIPS Decode")25}26import (27func main() {28 s390x.Decode()29 fmt.Println("S390X Decode")30}31import (32func main() {33 s390.Decode()34 fmt.Println("S390 Decode")35}36import (37func main() {38 ppc64le.Decode()39 fmt.Println("PPC64LE Decode")40}41import (42func main() {43 ppc64.Decode()44 fmt.Println("PPC64 Decode")45}46import (47func main() {48 riscv64.Decode()49 fmt.Println("RISCV64 Decode")50}

Full Screen

Full Screen

Decode

Using AI Code Generation

copy

Full Screen

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

Full Screen

Full Screen

Decode

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Enter the instruction to be decoded:")4 fmt.Scanln(&inst)5 p := powerpc.NewPowerPc()6 p.Decode(inst)7}8import (9func main() {10 fmt.Println("Enter the instruction to be decoded:")11 fmt.Scanln(&inst)12 p := powerpc.NewPowerPc()13 p.GetInstruction(inst)14}15import (16func main() {17 fmt.Println("Enter the instruction to be decoded:")18 fmt.Scanln(&inst)19 p := powerpc.NewPowerPc()20 p.GetInstruction(inst)21}22import (23func main() {24 fmt.Println("Enter the instruction to be decoded:")25 fmt.Scanln(&inst)26 p := powerpc.NewPowerPc()27 p.GetInstruction(inst)28}29import (30func main() {31 fmt.Println("Enter the instruction to be decoded:")32 fmt.Scanln(&inst)33 p := powerpc.NewPowerPc()34 p.GetInstruction(inst)35}36import (37func main() {38 fmt.Println("Enter the instruction to be decoded:")39 fmt.Scanln(&inst)40 p := powerpc.NewPowerPc()41 p.GetInstruction(inst)42}43import (44func main() {45 fmt.Println("Enter the instruction to be decoded:")46 fmt.Scanln(&inst)47 p := powerpc.NewPowerPc()48 p.GetInstruction(inst)49}

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful