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
Shopifyアプリ開発における Shopifyの機能活用
sonatard
4
250
Python(PYNQ)がテーマのAMD主催のFPGAコンテストに参加してきた
iotengineer22
0
470
ドメイン名の終活について - JPAAWG 7th -
mikit
33
20k
隣接領域をBeyondするFinatextのエンジニア組織設計 / beyond-engineering-areas
stajima
1
270
第1回 国土交通省 データコンペ参加者向け勉強会③- Snowflake x estie編 -
estie
0
130
EventHub Startup CTO of the year 2024 ピッチ資料
eventhub
0
110
AGIについてChatGPTに聞いてみた
blueb
0
130
TanStack Routerに移行するのかい しないのかい、どっちなんだい! / Are you going to migrate to TanStack Router or not? Which one is it?
kaminashi
0
580
スクラム成熟度セルフチェックツールを作って得た学びとその活用法
coincheck_recruit
1
140
開発生産性を上げながらビジネスも30倍成長させてきたチームの姿
kamina_zzz
2
1.7k
ExaDB-D dbaascli で出来ること
oracle4engineer
PRO
0
3.8k
10XにおけるData Contractの導入について: Data Contract事例共有会
10xinc
5
610
Featured
See All Featured
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Imperfection Machines: The Place of Print at Facebook
scottboms
265
13k
Documentation Writing (for coders)
carmenintech
65
4.4k
Done Done
chrislema
181
16k
Optimizing for Happiness
mojombo
376
70k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
Building Applications with DynamoDB
mza
90
6.1k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.2k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Visualization
eitanlees
145
15k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
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)