Upgrade to Pro — share decks privately, control downloads, hide ads and more …

go:embedでExplainable Binaryを作る/fukoukago17_go_code_embedding

go:embedでExplainable Binaryを作る/fukoukago17_go_code_embedding

Cd3d2cb2dadf5488935fe0ddaea7938a?s=128

monochromegane

June 10, 2021
Tweet

Transcript

  1. monochromegane/go-code-embedding ࡾ୐༔հ / Pepabo R&D Institute, GMO Pepabo, Inc. 2021.06.10

    Fukuoka.go#17 go:embedͰ Explainable BinaryΛ࡞Δ
  2. 1SJODJQBMFOHJOFFS :VTVLF.*:",&!NPOPDISPNFHBOF 1FQBCP3%*OTUJUVUF (.01FQBCP *OD IUUQTCMPHNPOPDISPNFHBOFDPN

  3. 1. എܠ: Binaryͷઆ໌ੑ 2. ՝୊: Explainable Binaryʹ޲͚ͨैདྷͷऔΓ૊Έ 3. ఏҊ: go:embedΛ༻͍ͨExplainable

    Binary 4. ·ͱΊ 3 ໨࣍
  4. 1. എܠ Binaryͷઆ໌ੑ

  5. • Goݴޠ࣮૷ͷϓϩμΫτ͸όΠφϦܗࣜʹΑΔ഑෍͕Մೳ • όΠφϦ୯ମ͔Β͸ʢ࢓༷ʹਫ਼௨্ͨ͠Ͱղੳ͠ͳ͚Ε͹ʣਖ਼֬ͳڍಈ͸೺Ѳ Ͱ͖ͳ͍ • go:embedͷಋೖʹΑͬͯΞηοτ΋όΠφϦʹؚ·ΕΔΑ͏ʹͳΔ 5 ͸͡Ίʹ •

    ར༻͢ΔϓϩμΫτͷόΠφϦͷڍಈΛ೺Ѳ͢Δ͜ͱ͕ॏཁͰ͋Δɻ • → ຊใࠂͰ͸͜ͷ఺ΛɺόΠφϦͷઆ໌ੑʢExplainabilityʣͱͯ͠࿦͡Δ
  6. 2. ՝୊ Explainable Binaryʹ޲͚ͨैདྷͷऔ૊

  7. • όΠφϦͱιʔείʔυΛඥ෇͚Δ͜ͱͰઆ໌ੑΛ֬อ͢Δ • Կ΋ࡦΛߨ͡ͳ͚Ε͹ɺόΠφϦ͸ιʔείʔυͱඥ෇͔ͳ͍ • ιʔείʔυ΋ߋ৽͞ΕΔ • ϓϩμΫτͰಠࣗʹ࠾൪ͨ͠όʔδϣϯ൪߸Λදࣔ͢Δ • mybinary

    -v • VCSͷϦϏδϣϯ൪߸ΛϏϧυ࣌ʹຒΊࠐΉ • go build —ldflag “-X …” 7 ैདྷͷऔ૊ $ myapp -v $ go build --ldflag “-X …”
  8. • Ϗϧυϓϩηεͱͷဃ཭ʢ௥Ճ͞ΕͨॴఆϏϧυϓϩηεͷεΩοϓʣ • όʔδϣϯΛߋ৽͢ΔɺϦϏδϣϯΛຒΊࠐΉϓϩηε͸εΩοϓՄೳͳͨ ΊɺಉҰͷ஋Ͱ͋ͬͯ΋ಈ࡞ͷҟͳΔόΠφϦΛ࡞Εͯ͠·͏ • ϦϙδτϦ΍ϦϏδϣϯͷফࣦ • ϦϙδτϦ΍ಛఆϦϏδϣϯ͕Ӭଓతʹଘࡏ͢Δ͜ͱ͸อূͰ͖ͳ͍ •

    ྫ: mimemagic (Your bundle is locked to mimemagic (0.3.5), but that version could not be found) 8 ैདྷͷऔ૊ͷݒ೦఺
  9. 3. ఏҊ go:embedΛ༻͍ͨExplainable Binary

  10. • ॴఆͷ௥ՃϏϧυϓϩηεΛεΩοϓͯ͠΋ιʔείʔυͱဃ཭͠ͳ͍ • → ɹ • ϦϙδτϦ΍ಛఆϦϏδϣϯ͕ফࣦͯ͠΋ιʔείʔυΛࢀরͰ͖Δ • → 10

    ݒ೦఺Λղফͨ͠όΠφϦ
  11. • ॴఆͷ௥ՃϏϧυϓϩηεΛεΩοϓͯ͠΋ιʔείʔυͱဃ཭͠ͳ͍ • → go build (without ldflag) ͚ͩͰඞ࣮ͣߦ͞ΕΔϓϩηε •

    ϦϙδτϦ΍ಛఆϦϏδϣϯ͕ফࣦͯ͠΋ιʔείʔυΛࢀরͰ͖Δ • → 11 ݒ೦఺Λղফͨ͠όΠφϦ
  12. • ॴఆͷ௥ՃϏϧυϓϩηεΛεΩοϓͯ͠΋ιʔείʔυͱဃ཭͠ͳ͍ • → go build (without ldflag) ͚ͩͰඞ࣮ͣߦ͞ΕΔϓϩηε •

    ϦϙδτϦ΍ಛఆϦϏδϣϯ͕ফࣦͯ͠΋ιʔείʔυΛࢀরͰ͖Δ • → ιʔείʔυࣗମΛόΠφϦʹ಺แ͢Δ 12 ݒ೦఺Λղফͨ͠όΠφϦ
  13. • ॴఆͷ௥ՃϏϧυϓϩηεΛεΩοϓͯ͠΋ιʔείʔυͱဃ཭͠ͳ͍ • → go build (without ldflag) ͚ͩͰඞ࣮ͣߦ͞ΕΔϓϩηε •

    ϦϙδτϦ΍ಛఆϦϏδϣϯ͕ফࣦͯ͠΋ιʔείʔυΛࢀরͰ͖Δ • → ιʔείʔυࣗମΛόΠφϦʹ಺แ͢Δ 13 ݒ೦఺Λղফͨ͠όΠφϦ • go:embedͰόΠφϦʹιʔείʔυΛࠝแ͢Δͱ͍͏ఏҊ
  14. monochromegane/ go-code-embedding

  15. • ιʔείʔυΛόΠφϦʹ૊ΈࠐΈɺӾཡ͢ΔͨΊͷδΣωϨʔλ • ίϚϯυҰൃͰࣗ࡞ͷϓϩμΫτʹҎԼͷΞΫηαΦϓγϣϯΛ௥Ճ • ιʔείʔυදࣔͷΦϓγϣϯ • ιʔείʔυҰཡͷΦϓγϣϯ • ιʔείʔυ෮ݩͷΦϓγϣϯ

    15 ػೳ # myapp --help Usage of myapp: -list-code list code -restore-code restore code -restore-path string restore code to specified path (default ".") -show-code string show specified code Usage of myapp: -output-dir string Output directory (default ".") -pkg string Package name to use in the generated code. (default "main")
  16. 16 ࢖͍ํ

  17. 17 ࢖͍ํ

  18. 18 ࢓૊Έ Source code Generated code IUUQTJDPOTDPNMJDFOTF

  19. 19 ࢓૊Έ Source code Generated code 1. go:embed directive

  20. 20 ࢓૊Έ Source code Generated code 2. Option flags

  21. 21 ࢓૊Έ Source code Generated code 3. Method implementations

  22. 22 ࢓૊Έ Source code Generated code $ go build -o

    myapp Explainable binary -list-code list code -restore-code restore code -restore-path string restore code to specified path (default ".") -show-code string show specified code Source code
  23. monochromegane/ go-embedding-accessor

  24. • ΍Γ͔ͨͬͨ͜ͱ͸ɺ೚ҙͷ૊ΈࠐΈର৅ʹର͢ΔΞΫηαͷੜ੒ • ιʔείʔυͷ૊ΈࠐΈ͸ͦͷಛघܥͱߟ͑ͨ • → go-embedding-accessorͱ࣮ͯ͠૷ • ೚ҙͷgo:embedσΟϨΫςΟϒจࣈྻΛ౉ͯ͠ΞΫηαΛੜ੒͢Δ 24

    ࣮૷ͷҰൠԽ
  25. • ը૾ͳͲͷΞηοτΛόΠφϦʹ૊ΈࠐΜͩࡍʹɺόΠφϦ͔Βը૾Λ෮ݩ͢ ΔΑ͏ͳϢʔεέʔεʹରԠ • go:embedͷීٴʹ൐͍ɺΞηοτ΁ͷΞΫηα͸χʔζ͕૿͢ʢ͔΋ʣ 25 ࣮૷ͷҰൠԽ

  26. ͓·͚

  27. • ιʔείʔυΛόΠφϦʹಉࠝ͢ΔΞΠσΟΞ͸ࣗ਎Ͱઌߦ࣮૷͍ͯͨ͠ • ʰ໺ྑόΠφϦʹͳͬͯ΋େৎ෉ɻGoͷόΠφϦʹιʔείʔυΛఴ෇͢ ΔπʔϧΛͭͬͨ͘ɻʱ(2015.8) (*1) • monochromegane/go-bincode (*2) •

    ઌߦ࣮૷͸go-bindataʹґଘ͍͕ͯͨ͠ɺඪ४ͷembedػೳʹஔ͖׵͑Δ͜ ͱ͕Ͱ͖ɺ࣮૷΋γϯϓϧʹͳͬͨɻ • go:embedͷීٴ͕ݟࠐ·ΕΔ͜ͱ͔ΒɺվΊͯఏҊʢωλ੾ΕͰ͸ͳ͍ʣ 27 ࣗ਎Ͱͷઌߦ࣮૷  IUUQTCMPHNPOPDISPNFHBOFDPNCMPHHPCJODPEF  IUUQTHJUIVCDPNNPOPDISPNFHBOFHPCJODPEF
  28. 4. ·ͱΊ

  29. • GoόΠφϦͷઆ໌ੑʹண໨͠ɺιʔείʔυͱͷඥ෇͚ͷ՝୊Λ੔ཧͨ͠ • ࠷௿ݶͷϏϧυϓϩηεͰ͋ͬͯ΋ιʔείʔυΛಉࠝՄೳͳπʔϧͱͯ͠ go-code-embeddingΛఏҊ • go:embed΁ͷΞΫηαͷχʔζΛଊ͑ͯɺgo-embedding-accessorΛ࣮૷ ͠ɺଟ༷ͳϢʔεέʔεʹॊೈʹରԠͨ͠ • ఏҊʹΑΓɺಉࠝͨ͠ιʔείʔυ΍ΞηοτΛදࣔɺ෮ݩ͢Δɺઆ໌Մೳੑ

    ͕͋ΔExplainable BinaryΛఏڙͰ͖Δ 29 ·ͱΊ ࡾGo
  30. None