Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Goでバイナリを読む+α

 Goでバイナリを読む+α

Fukuoka.go #10 で話した内容です。
#golang #go #webassembly #wasm

003f59d1b2e3b864c744f8eb21f68e82?s=128

Atsushi Takayama

June 14, 2018
Tweet

Transcript

  1. GoͰόΠφϦΛಡΉ +α   Fukuoka.go #10  pixiv.inc Atsushi Takayama

  2. • ߴࢁԹ / edvakf ϐΫγϒגࣜձࣾ ࣥߦ໾һCTO ݉ ෱ԬΦϑΟεϚωʔδϟʔ • Go,

    Ruby, JavaScript, Scala, PHP   ࣗݾ঺հ
  3.   func init() {

  4.   GoͰόΠφϦΛಡΉ ͱ͍͏ྫΛݩʹɺGoͷ͜ͱΛ஌Ζ͏

  5.   όΠφϦΛಡΊΔͱ

  6.   ΧοίΠΠ

  7.   % cat testdata/hello.wasm | xxd -g1 | head

    -n 30 00000000: 00 61 73 6d 0d 00 00 00 01 ae 83 80 80 00 30 60 .asm..........0` 00000010: 01 7f 01 7f 60 03 7f 7f 7f 01 7f 60 01 7c 01 7f ....`......`.|.. 00000020: 60 02 7f 7f 01 7f 60 03 7f 7f 7f 00 60 05 7f 7f `.....`.....`... 00000030: 7f 7f 7f 01 7f 60 08 7f 7f 7f 7f 7f 7f 7f 7f 01 .....`.......... 00000040: 7f 60 01 7f 00 60 06 7f 7f 7f 7f 7f 7f 01 7f 60 .`...`.........` 00000050: 02 7f 7f 00 60 04 7f 7f 7f 7f 01 7f 60 00 00 60 ....`.......`..` 00000060: 04 7f 7f 7f 7f 00 60 06 7f 7f 7f 7f 7f 7f 00 60 ......`........` 00000070: 05 7f 7f 7f 7f 7f 00 60 07 7f 7f 7f 7f 7f 7f 7f .......`........ 00000080: 01 7f 60 06 7f 7f 7f 7f 7f 7c 01 7f 60 05 7f 7f ..`......|..`... 00000090: 7e 7f 7f 00 60 0b 7f 7f 7f 7f 7f 7f 7f 7f 7f 7f ~...`........... 000000a0: 7f 01 7f 60 0f 7f 7f 7f 7f 7f 7f 7f 7f 7f 7f 7f ...`............ 000000b0: 7f 7f 7f 7f 00 60 03 7f 7f 7f 01 7d 60 07 7f 7f .....`.....}`... 000000c0: 7f 7f 7f 7f 7f 00 60 0a 7f 7f 7f 7f 7f 7f 7f 7f ......`......... 000000d0: 7f 7f 00 60 03 7f 7f 7f 01 7c 60 00 01 7f 60 04 ...`.....|`...`. 000000e0: 7f 7f 7f 7f 01 7e 60 05 7f 7f 7f 7f 7e 01 7f 60 .....~`.....~..` 000000f0: 05 7f 7f 7f 7f 7c 01 7f 60 0c 7f 7f 7f 7f 7f 7f .....|..`....... 00000100: 7f 7f 7f 7f 7f 7f 01 7f 60 10 7f 7f 7f 7f 7f 7f ........`.......
  8.   ;Ή;Ή

  9.   }

  10.   func main() {

  11.   ͜͏΍ͬͯಡΉͷ͸ਏ͍ͷͰ

  12.   ߏ଄ମʹϚοϐϯά͠·͢

  13.   type Module struct { Header ModuleHeader Sections []Section

    } type ModuleHeader struct { Magic [4]byte Version uint32 } type Section struct { ... }
  14.   ΋ͪΖΜ࢖͏ͷ͸

  15.   type Reader interface { Read(p []byte) (n int,

    err error) } ετϦʔϜΛҙࣝ͢Δͱ͖͸ io.Reader Λ࢖͏ io.Reader
  16.   // 4όΠτΛio.Reader͔ΒಡΈࠐΉ var buf [4]byte num, err :=

    r.Read(buf[:]) [4]byte ͸ʮ഑ྻʯ buf[0:4] ͱ͔ buf[:] Ͱ഑ྻͷҰ෦΁ͷϙΠϯλʔΛؚΉʮεϥΠεʯ Readʹ౉ͤΔͷ͸ʮεϥΠεʯ NόΠτಡΉ
  17.   import "encoding/binary" var num int64 err := binary.Read(r,

    binary.LittleEndian, &num) binary.Read ͷୈࡾҾ਺͸ interface{} ৭ΜͳܕͷϙΠϯλʔ͕౉ͤΔ int/floatΛಡΉ
  18.   int/float͚ͩͰͳ͘

  19.   b := []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21,

    0x09, 0x40, 0xff, 0x01, 0x02, 0x03, 0xbe, 0xef} r := bytes.NewReader(b) var data struct { PI float64 Uate uint8 Mine [3]byte Too uint16 } err := binary.Read(r, binary.LittleEndian, &data) ߏ଄ମΛؙ͝ͱಡΉ
  20.   binary.Readศར

  21.   fmt.Printf("%v\n", data) // => {3.141592653589793 255 [1 2

    3] 61374} fmt.Printf("%+v\n", data) // => {PI:3.141592653589793 Uate:255 Mine:[1 2 3] Too:61374} fmt.Printf("%#v\n", data) // => struct { PI float64; Uate uint8; Mine [3]uint8; Too uint16 }{PI: 3.141592653589793, Uate:0xff, Mine:[3]uint8{0x1, 0x2, 0x3}, Too:0xefbe} fmtͷ%v
  22.   LLฒͷ࢖͍উख

  23.   Α͘ݴΘΕΔGoͷັྗ

  24.   ඪ४ϥΠϒϥϦ͕γϯϓϧ͔ͭڧྗ ͓Θ͔Γ͍͚ͨͩͨͩΖ͏͔ʁ

  25.   Α͘ݴΘΕΔGoͷܽ఺

  26.   Τϥʔॲཧ͕DRY͡Όͳ͍

  27.   var buf [4]byte num, err := r.Read(buf[:]) if

    err != nil { // Τϥʔॲཧ } var num int64 err = binary.Read(r, binary.LittleEndian, &num) if err != nil { // Τϥʔॲཧ } err = binary.Read(r, binary.LittleEndian, &data) if err != nil { // Τϥʔॲཧ
  28.  

  29.   type decoder struct { r io.Reader err error

    } func (d *decoder) read(r io.Reader, buf []byte) { if d.err != nil || len(buf) == 0 { return } _, d.err = r.Read(buf) } // https://github.com/sbinet/wasm/blob/master/decoder.go
  30.   • Rob Pikeઌੜᐌ͘ɺ • Errors are values (Τϥʔ͸ͨͩͷฦΓ஋)

    • Τϥʔ͸ྫ֎ͱ͸ҧ͏ɻνΣοΫ͢ΔͷͰ͸ͳ͘ɺ ద੾ʹॲཧͤΑ • https://youtu.be/PAAkCSZUG1c?t=16m14s
  31.   }

  32.   ͦ΋ͦ΋Կ࡞ͬͯΔͷʁ

  33.   WebAssemblyʹৄ͘͠ͳΓ͔ͨͬͨ

  34.  

  35.   • C΍GoΛWasmʹίϯύΠϧ͢Δͷ͸طʹͰ͖Δ • ࠓޙͨ͘͞Μͷݴޠ͕WasmʹίϯύΠϧͰ͖ΔΑ͏ ʹͳΔ͸ͣ • Goͷ্ͰWasmΛ࣮ߦ͢Δະདྷ΋དྷΔ͸ͣ

  36.   Wasm→Ruby ISeqίϯύΠϥʔ mRuby→WasmίϯύΠϥʔ

  37.   Ӷҙ࡞੒த

  38.   • https://github.com/edvakf/wasm • https://github.com/edvakf/wagyu • ׬੒͠ͳͯ͘΋ɺ࣮༻త͡Όͳͯ͘΋ɺษڧ͢Δ͜ͱ ࣗମָ͕͚͠Ε͹͍͍ɻͦΜͳϓϩάϥϜΛॻ͜͏ͱ ࢥͬͯ΍ͬͯ·͢ɻ

  39.   os.Exit(0)