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
C++26 エラー性動作
faithandbrave
2
720
統計データで2024年の クラウド・インフラ動向を眺める
ysknsid25
2
840
Oracle Cloudの生成AIサービスって実際どこまで使えるの? エンジニア目線で試してみた
minorun365
PRO
4
280
alecthomas/kong はいいぞ / kamakura.go#7
fujiwara3
1
300
GitHub Copilot のテクニック集/GitHub Copilot Techniques
rayuron
27
12k
Amazon SageMaker Unified Studio(Preview)、Lakehouse と Amazon S3 Tables
ishikawa_satoru
0
150
2024年にチャレンジしたことを振り返るぞ
mitchan
0
130
PHP ユーザのための OpenTelemetry 入門 / phpcon2024-opentelemetry
shin1x1
1
190
kargoの魅力について伝える
magisystem0408
0
200
AWS re:Invent 2024で発表された コードを書く開発者向け機能について
maruto
0
190
Turing × atmaCup #18 - 1st Place Solution
hakubishin3
0
480
Qiita埋め込み用スライド
naoki_0531
0
4.7k
Featured
See All Featured
Building a Scalable Design System with Sketch
lauravandoore
460
33k
How To Stay Up To Date on Web Technology
chriscoyier
789
250k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
YesSQL, Process and Tooling at Scale
rocio
169
14k
Typedesign – Prime Four
hannesfritz
40
2.4k
It's Worth the Effort
3n
183
28k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
Optimizing for Happiness
mojombo
376
70k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
Keith and Marios Guide to Fast Websites
keithpitt
410
22k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
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)