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
880
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
120
8年物のJavaのシステムをKotlinに変えていく選択に至るまで
edvakf
2
1k
ピクシブ社内のImageFlux利用事例紹介
edvakf
1
2.7k
学びの文化を育む社内読書会のススメ
edvakf
0
210
フルCDNアーキテクチャでサービス設計した話
edvakf
5
3.7k
お前はこれまでに作ったAPIの数を覚えているのか?
edvakf
0
2.4k
「ふつうのRailsアプリケーション」についての考え方
edvakf
2
750
ggplot.galleryというお遊びウェブアプリケーションを作った話
edvakf
0
360
Other Decks in Technology
See All in Technology
OCI Vault 概要
oracle4engineer
PRO
0
9.7k
RubyのWebアプリケーションを50倍速くする方法 / How to Make a Ruby Web Application 50 Times Faster
hogelog
3
950
エンジニア人生の拡張性を高める 「探索型キャリア設計」の提案
tenshoku_draft
1
130
FlutterアプリにおけるSLI/SLOを用いたユーザー体験の可視化と計測基盤構築
ostk0069
0
110
rootlessコンテナのすゝめ - 研究室サーバーでもできる安全なコンテナ管理
kitsuya0828
3
390
これまでの計測・開発・デプロイ方法全部見せます! / Findy ISUCON 2024-11-14
tohutohu
3
370
Oracle Cloud Infrastructureデータベース・クラウド:各バージョンのサポート期間
oracle4engineer
PRO
29
13k
10XにおけるData Contractの導入について: Data Contract事例共有会
10xinc
6
670
OCI Network Firewall 概要
oracle4engineer
PRO
0
4.2k
SSMRunbook作成の勘所_20241120
koichiotomo
3
160
ノーコードデータ分析ツールで体験する時系列データ分析超入門
negi111111
0
420
日経電子版のStoreKit2フルリニューアル
shimastripe
1
140
Featured
See All Featured
Building an army of robots
kneath
302
43k
Intergalactic Javascript Robots from Outer Space
tanoku
269
27k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
126
18k
Designing for humans not robots
tammielis
250
25k
Documentation Writing (for coders)
carmenintech
65
4.4k
Music & Morning Musume
bryan
46
6.2k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
130
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
24k
GitHub's CSS Performance
jonrohan
1030
460k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
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)