Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Goでバイナリを読む+α
Search
Atsushi Takayama
June 14, 2018
Technology
1
890
Goでバイナリを読む+α
Fukuoka.go #10 で話した内容です。
#golang #go #webassembly #wasm
Atsushi Takayama
June 14, 2018
Tweet
Share
More Decks by Atsushi Takayama
See All by Atsushi Takayama
最高の開発者体験の追求が開発生産性を改善し続ける文化を生み出した話
edvakf
3
1k
NeurIPS 2021 論文読み会: How Modular should Neural Module Networks Be for Systematic Generalization?
edvakf
0
130
8年物のJavaのシステムをKotlinに変えていく選択に至るまで
edvakf
2
1k
ピクシブ社内のImageFlux利用事例紹介
edvakf
1
2.7k
学びの文化を育む社内読書会のススメ
edvakf
0
220
フルCDNアーキテクチャでサービス設計した話
edvakf
5
3.7k
お前はこれまでに作ったAPIの数を覚えているのか?
edvakf
0
2.4k
「ふつうのRailsアプリケーション」についての考え方
edvakf
2
760
ggplot.galleryというお遊びウェブアプリケーションを作った話
edvakf
0
370
Other Decks in Technology
See All in Technology
AI時代のデータセンターネットワーク
lycorptech_jp
PRO
1
290
PHPからGoへのマイグレーション for DMMアフィリエイト
yabakokobayashi
1
170
多領域インシデントマネジメントへの挑戦:ハードウェアとソフトウェアの融合が生む課題/Challenge to multidisciplinary incident management: Issues created by the fusion of hardware and software
bitkey
PRO
2
110
Turing × atmaCup #18 - 1st Place Solution
hakubishin3
0
490
20241220_S3 tablesの使い方を検証してみた
handy
4
640
バクラクのドキュメント解析技術と実データにおける課題 / layerx-ccc-winter-2024
shimacos
2
1.1k
新機能VPCリソースエンドポイント機能検証から得られた考察
duelist2020jp
0
230
DUSt3R, MASt3R, MASt3R-SfM にみる3D基盤モデル
spatial_ai_network
2
190
開発生産性向上! 育成を「改善」と捉えるエンジニア育成戦略
shoota
2
400
サービスでLLMを採用したばっかりに振り回され続けたこの一年のあれやこれや
segavvy
2
500
マルチプロダクト開発の現場でAWS Security Hubを1年以上運用して得た教訓
muziyoshiz
3
2.4k
Oracle Cloud Infrastructure:2024年12月度サービス・アップデート
oracle4engineer
PRO
1
210
Featured
See All Featured
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
5
450
BBQ
matthewcrist
85
9.4k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
95
17k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
45
2.2k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
28
900
Keith and Marios Guide to Fast Websites
keithpitt
410
22k
Transcript
GoͰόΠφϦΛಡΉ +α Fukuoka.go #10 pixiv.inc Atsushi Takayama
• ߴࢁԹ / edvakf ϐΫγϒגࣜձࣾ ࣥߦһCTO ݉ ԬΦϑΟεϚωʔδϟʔ • Go,
Ruby, JavaScript, Scala, PHP ࣗݾհ
func init() {
GoͰόΠφϦΛಡΉ ͱ͍͏ྫΛݩʹɺGoͷ͜ͱΛΖ͏
όΠφϦΛಡΊΔͱ
ΧοίΠΠ
% 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 ........`.......
;Ή;Ή
}
func main() {
͜͏ͬͯಡΉͷਏ͍ͷͰ
ߏମʹϚοϐϯά͠·͢
type Module struct { Header ModuleHeader Sections []Section
} type ModuleHeader struct { Magic [4]byte Version uint32 } type Section struct { ... }
ͪΖΜ͏ͷ
type Reader interface { Read(p []byte) (n int,
err error) } ετϦʔϜΛҙࣝ͢Δͱ͖ io.Reader Λ͏ io.Reader
// 4όΠτΛio.Reader͔ΒಡΈࠐΉ var buf [4]byte num, err :=
r.Read(buf[:]) [4]byte ʮྻʯ buf[0:4] ͱ͔ buf[:] ͰྻͷҰ෦ͷϙΠϯλʔΛؚΉʮεϥΠεʯ ReadʹͤΔͷʮεϥΠεʯ NόΠτಡΉ
import "encoding/binary" var num int64 err := binary.Read(r,
binary.LittleEndian, &num) binary.Read ͷୈࡾҾ interface{} ৭ΜͳܕͷϙΠϯλʔ͕ͤΔ int/floatΛಡΉ
int/float͚ͩͰͳ͘
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) ߏମΛؙ͝ͱಡΉ
binary.Readศར
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
LLฒͷ͍উख
Α͘ݴΘΕΔGoͷັྗ
ඪ४ϥΠϒϥϦ͕γϯϓϧ͔ͭڧྗ ͓Θ͔Γ͍͚ͨͩͨͩΖ͏͔ʁ
Α͘ݴΘΕΔGoͷܽ
Τϥʔॲཧ͕DRY͡Όͳ͍
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 { // Τϥʔॲཧ
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
• Rob Pikeઌੜᐌ͘ɺ • Errors are values (ΤϥʔͨͩͷฦΓ)
• Τϥʔྫ֎ͱҧ͏ɻνΣοΫ͢ΔͷͰͳ͘ɺ దʹॲཧͤΑ • https://youtu.be/PAAkCSZUG1c?t=16m14s
}
ͦͦԿ࡞ͬͯΔͷʁ
WebAssemblyʹৄ͘͠ͳΓ͔ͨͬͨ
• CGoΛWasmʹίϯύΠϧ͢ΔͷطʹͰ͖Δ • ࠓޙͨ͘͞Μͷݴޠ͕WasmʹίϯύΠϧͰ͖ΔΑ͏ ʹͳΔͣ • Goͷ্ͰWasmΛ࣮ߦ͢ΔະདྷདྷΔͣ
Wasm→Ruby ISeqίϯύΠϥʔ mRuby→WasmίϯύΠϥʔ
Ӷҙ࡞த
• https://github.com/edvakf/wasm • https://github.com/edvakf/wagyu • ͠ͳͯ͘ɺ࣮༻త͡Όͳͯ͘ɺษڧ͢Δ͜ͱ ࣗମָ͕͚͠Ε͍͍ɻͦΜͳϓϩάϥϜΛॻ͜͏ͱ ࢥͬͯͬͯ·͢ɻ
os.Exit(0)