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
870
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
990
ピクシブ社内のImageFlux利用事例紹介
edvakf
1
2.6k
学びの文化を育む社内読書会のススメ
edvakf
0
200
フルCDNアーキテクチャでサービス設計した話
edvakf
5
3.7k
お前はこれまでに作ったAPIの数を覚えているのか?
edvakf
0
2.4k
「ふつうのRailsアプリケーション」についての考え方
edvakf
2
740
ggplot.galleryというお遊びウェブアプリケーションを作った話
edvakf
0
350
Other Decks in Technology
See All in Technology
君は隠しイベントを見つけれるか?
mujyun
0
280
グローバル展開を見据えたサービスにおける機械翻訳プラクティス / dp-ai-translating
cyberagentdevelopers
PRO
1
150
10分でわかるfreee エンジニア向け会社説明資料
freee
18
520k
一休.comレストランにおけるRustの活用
kymmt90
3
580
サイバーエージェントにおける生成AIのリスキリング施策の取り組み / cyber-ai-reskilling
cyberagentdevelopers
PRO
2
200
顧客が本当に必要だったもの - パフォーマンス改善編 / Make what is needed
soudai
24
6.7k
CyberAgent 生成AI Deep Dive with Amazon Web Services / genai-aws
cyberagentdevelopers
PRO
1
480
2024-10-30-reInventStandby_StudyGroup_Intro
shinichirokawano
1
620
とあるユーザー企業におけるリスクベースで考えるセキュリティ業務のお話し
4su_para
3
320
プロダクトエンジニアが活躍する環境を作りたくて 事業責任者になった話 ~プロダクトエンジニアの行き着く先~
gimupop
1
460
Amazon FSx for NetApp ONTAPを利用するにあたっての要件整理と設計のポイント
non97
1
160
Fargateを使った研修の話
takesection
0
110
Featured
See All Featured
Ruby is Unlike a Banana
tanoku
96
11k
Code Review Best Practice
trishagee
64
17k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Build The Right Thing And Hit Your Dates
maggiecrowley
32
2.4k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
46
2.1k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.2k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
9
680
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.8k
Statistics for Hackers
jakevdp
796
220k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
504
140k
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)