Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Little Endian と Big Endian のエミュレート #5

Closed
kawasin73 opened this issue Nov 8, 2018 · 8 comments
Closed

Little Endian と Big Endian のエミュレート #5

kawasin73 opened this issue Nov 8, 2018 · 8 comments
Assignees

Comments

@kawasin73
Copy link
Owner

zcbit はバイトオーダーの異なるマシン間で作成されたビットベクトルのファイルを利用可能にするために作成されている。

PC で広く利用されている Intel CPU は主に Little Endian であるが、Big Endian のマシンでも正常に動作することをテストする必要がある。

Little Endian と Big Endian のどちらでもテストを実行する手法を調査する。

  • バイトオーダーをエミュレートする
  • CI などで異なるアーキテクチャを選択する
@kawasin73 kawasin73 self-assigned this Nov 8, 2018
@kawasin73 kawasin73 mentioned this issue Nov 8, 2018
15 tasks
@kawasin73
Copy link
Owner Author

kawasin73 commented Nov 11, 2018

Travis で、QEMU を利用して ARM のアーキテクチャの上でテストをしている模様

Linux カーネルには、binfmt_misc という仕組みがあり、実行バイナリの先頭のバイト列に一致するパターンがあった場合、指定のインタープリタを実行することができる。
Linuxのbinfmt_miscを使ってGoスクリプトを直接実行できるようにする

Docker の multiarch/qemu-user-static:register イメージを実行することで、ホストマシンの binfmt_misc に複数のアーキテクチャのバイナリを QEMU で実行するように修正する。

docker run --rm --privileged multiarch/qemu-user-static:register --reset

これを Travis 上で実行し、ARM の Docker イメージを実行することで ARM アーキテクチャで実行することが可能になる。

Go 言語の ARM 版 Docker イメージは https://hub.docker.com/r/arm64v8/golang/ である。

参考:amd64 以外のDocker イメージ

@kawasin73
Copy link
Owner Author

kawasin73 commented Nov 11, 2018

以下のエラーが発生して Arm 版の Dockerイメージ arm64v8/golang が実行できない。

docker: no matching manifest for linux/amd64 in the manifest list entries.

docker-library/official-images#3835

この問題に該当する

manifest が適合せずにイメージのプルに失敗している。

@kawasin73
Copy link
Owner Author

Go 言語は qemu の arm に対応していない。

https://www.reddit.com/r/golang/comments/6j08ux/what_the_best_arm_emulator_for_testing_go_binary/
golang/go#13024 (comment)

Yeah, I think we lost the ability to run Go binaries with
qemu-arm long time ago. tcg fatal error also seems to
suggest that it's a qemu bug. (the arm port used to be
developed with qemu-arm, but then as the port is able
to run on real hardwares, we no longer test on qemu-arm.)

@kawasin73
Copy link
Owner Author

以下の手順で arm64 上で Go のテストを実行したが、Little Endian であった。

sudo docker run --rm -it multiarch/debian-debootstrap:arm64-jessie bash
wget --no-check-certificate https://dl.google.com/go/go1.11.2.linux-arm64.tar.gz
tar -xvf go1.11.2.linux-arm64.tar.gz
mv go /usr/local
export GOROOT=/usr/local/go
export GOPATH=/root/go
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
cd /root
apt-get install -y git
git clone https://github.com/kawasin73/bitset.git

@kawasin73
Copy link
Owner Author

kawasin73 commented Nov 12, 2018

Travis で s390x を動かすことができている。 travis-ci/travis-ci#8869 (comment)
この上で Golang を動かすことを考える。

公式の Golang の s390x 向けのアーカイブ をダウンロード・解凍して go version を実行するも以下のエラーが発生

$ sudo docker run --rm -it multiarch/debian-debootstrap:s390x-jessie bash
root@3e8ef6ffcd82:~# go test
Illegal instruction (core dumped)

macOS 上でテストコードをコンパイルして、テストバイナリを Docker 上で実行するも同じくエラーが発生した。

# on macOS
GOOS=linux GOARCH=s390x go test -c
sudo docker run --rm -v $(pwd):/root -it multiarch/debian-debootstrap:s390x-jessie bash
# on Docker container
$ ./bitset.test -test.v
Illegal instruction (core dumped)

@kawasin73 kawasin73 mentioned this issue Nov 12, 2018
2 tasks
@kawasin73
Copy link
Owner Author

Go 言語が対応するアーキテクチャのうち Big Endian は以下の通り。

https://github.com/golang/go/wiki/MinimumRequirements#architectures

@kawasin73
Copy link
Owner Author

kawasin73 commented Nov 12, 2018

できた

GOOS=linux GOARCH=ppc64 go test -c -o bitset.test
docker run --rm -v $(pwd):/root multiarch/fedora:25-ppc64 /root/bitset.test -test.v

macOS + vagrant + Ubuntu 環境の Docker では動いた。

しかし、travis では動かなかった。以下のエラーが発生する。
https://travis-ci.com/kawasin73/bitset/jobs/157899588

0.42s$ docker run --rm -v $(pwd):/root  multiarch/fedora:25-ppc64 /root/bitset.test -test.v
standard_init_linux.go:185: exec user process caused "no such file or directory"
The command "docker run --rm -v $(pwd):/root  multiarch/fedora:25-ppc64 /root/bitset.test -test.v" exited with 1.

@kawasin73
Copy link
Owner Author

kawasin73 commented Nov 12, 2018

multiarch/qemu-user-static リポジトリで公開されている qemu-ppc64-static をダウンロードして ppc64 のアーキテクチャをエミュレートしてテストを行うことにした。

テストコードのバイナリ(go test -c で出力できる)を linux/ppc64 のバイナリにクロスコンパイルして実行している。

wget https://github.com/multiarch/qemu-user-static/releases/download/v3.0.0/qemu-ppc64-static
chmod 755 qemu-ppc64-static
GOOS=linux GOARCH=ppc64 go test -c -o bitset.test
./qemu-ppc64-static ./bitset.test -test.v

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant