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

Goでバイナリを読む+α

 Goでバイナリを読む+α

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

Atsushi Takayama

June 14, 2018
Tweet

More Decks by Atsushi Takayama

Other Decks in Technology

Transcript

  1. GoͰόΠφϦΛಡΉ



    Fukuoka.go #10

    pixiv.inc
    Atsushi Takayama

    View full-size slide

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


    ࣗݾ঺հ

    View full-size slide



  3. func init() {

    View full-size slide



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

    View full-size slide



  5. όΠφϦΛಡΊΔͱ

    View full-size slide



  6. % 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 ........`.......

    View full-size slide



  7. func main() {

    View full-size slide



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

    View full-size slide



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

    View full-size slide



  10. type Module struct {
    Header ModuleHeader
    Sections []Section
    }
    type ModuleHeader struct {
    Magic [4]byte
    Version uint32
    }
    type Section struct {
    ...
    }

    View full-size slide



  11. ΋ͪΖΜ࢖͏ͷ͸

    View full-size slide



  12. type Reader interface {
    Read(p []byte) (n int, err error)
    }
    ετϦʔϜΛҙࣝ͢Δͱ͖͸ io.Reader Λ࢖͏
    io.Reader

    View full-size slide



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

    View full-size slide



  14. import "encoding/binary"
    var num int64
    err := binary.Read(r, binary.LittleEndian, &num)
    binary.Read ͷୈࡾҾ਺͸ interface{}
    ৭ΜͳܕͷϙΠϯλʔ͕౉ͤΔ
    int/floatΛಡΉ

    View full-size slide



  15. int/float͚ͩͰͳ͘

    View full-size slide



  16. 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)
    ߏ଄ମΛؙ͝ͱಡΉ

    View full-size slide



  17. binary.Readศར

    View full-size slide



  18. 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

    View full-size slide



  19. LLฒͷ࢖͍উख

    View full-size slide



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

    View full-size slide



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

    View full-size slide



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

    View full-size slide



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

    View full-size slide



  24. 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 {
    // Τϥʔॲཧ

    View full-size slide



  25. 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

    View full-size slide



  26. • Rob Pikeઌੜᐌ͘ɺ
    • Errors are values (Τϥʔ͸ͨͩͷฦΓ஋)
    • Τϥʔ͸ྫ֎ͱ͸ҧ͏ɻνΣοΫ͢ΔͷͰ͸ͳ͘ɺ
    ద੾ʹॲཧͤΑ
    • https://youtu.be/PAAkCSZUG1c?t=16m14s

    View full-size slide



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

    View full-size slide



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

    View full-size slide



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

    View full-size slide



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

    View full-size slide



  31. Ӷҙ࡞੒த

    View full-size slide



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

    View full-size slide