From bbad15ae75da01150f3b1fed1a3837f5f5bedc89 Mon Sep 17 00:00:00 2001 From: Alexander Egorenkov Date: Tue, 23 Jun 2020 17:46:15 +0200 Subject: target: support of big-endian architectures * Introduce the new target flag 'LittleEndian' which specifies of which endianness the target is. * Introduce the new requires flag 'littleendian' for tests to selectively enable/disable tests on either little-endian architectures or big-endian ones. * Disable KD unit test on s390x architecture because the test works only on little-endian architecture. Signed-off-by: Alexander Egorenkov --- sys/syz-extract/akaros.go | 1 + sys/syz-extract/fetch.go | 7 ++++--- sys/syz-extract/freebsd.go | 1 + sys/syz-extract/fuchsia.go | 1 + sys/syz-extract/linux.go | 1 + sys/syz-extract/netbsd.go | 3 ++- sys/syz-extract/openbsd.go | 3 ++- sys/syz-extract/trusty.go | 1 + sys/syz-extract/windows.go | 1 + sys/syz-sysgen/sysgen.go | 4 ++-- sys/targets/targets.go | 44 +++++++++++++++++++++++++++++++++++++------- sys/test/test/align0 | 2 +- sys/test/test/align0_be | 6 ++++++ sys/test/test/bf | 2 +- sys/test/test/bf2 | 2 +- sys/test/test/nla | 2 ++ 16 files changed, 64 insertions(+), 17 deletions(-) create mode 100644 sys/test/test/align0_be (limited to 'sys') diff --git a/sys/syz-extract/akaros.go b/sys/syz-extract/akaros.go index e587cabef..368f77f87 100644 --- a/sys/syz-extract/akaros.go +++ b/sys/syz-extract/akaros.go @@ -43,6 +43,7 @@ func (*akaros) processFile(arch *Arch, info *compiler.ConstInfo) (map[string]uin } params := &extractParams{ DeclarePrintf: true, + TargetEndian: arch.target.HostEndian, } return extract(info, "gcc", args, params) } diff --git a/sys/syz-extract/fetch.go b/sys/syz-extract/fetch.go index 129385b7e..dd0c6aeb3 100644 --- a/sys/syz-extract/fetch.go +++ b/sys/syz-extract/fetch.go @@ -24,6 +24,7 @@ type extractParams struct { DeclarePrintf bool DefineGlibcUse bool // workaround for incorrect flags to clang for fuchsia. ExtractFromELF bool + TargetEndian binary.ByteOrder } func extract(info *compiler.ConstInfo, cc string, args []string, params *extractParams) ( @@ -76,7 +77,7 @@ func extract(info *compiler.ConstInfo, cc string, args []string, params *extract var flagVals []uint64 if data.ExtractFromELF { - flagVals, err = extractFromELF(bin) + flagVals, err = extractFromELF(bin, params.TargetEndian) } else { flagVals, err = extractFromExecutable(bin) } @@ -146,7 +147,7 @@ func extractFromExecutable(binFile string) ([]uint64, error) { return vals, nil } -func extractFromELF(binFile string) ([]uint64, error) { +func extractFromELF(binFile string, targetEndian binary.ByteOrder) ([]uint64, error) { f, err := os.Open(binFile) if err != nil { return nil, err @@ -164,7 +165,7 @@ func extractFromELF(binFile string) ([]uint64, error) { return nil, err } vals := make([]uint64, len(data)/8) - if err := binary.Read(bytes.NewReader(data), binary.LittleEndian, &vals); err != nil { + if err := binary.Read(bytes.NewReader(data), targetEndian, &vals); err != nil { return nil, err } return vals, nil diff --git a/sys/syz-extract/freebsd.go b/sys/syz-extract/freebsd.go index 846e019a7..0c3e8fd8d 100644 --- a/sys/syz-extract/freebsd.go +++ b/sys/syz-extract/freebsd.go @@ -59,6 +59,7 @@ func (*freebsd) processFile(arch *Arch, info *compiler.ConstInfo) (map[string]ui params := &extractParams{ AddSource: "#include ", DeclarePrintf: true, + TargetEndian: arch.target.HostEndian, } return extract(info, "gcc", args, params) } diff --git a/sys/syz-extract/fuchsia.go b/sys/syz-extract/fuchsia.go index b768b81b5..8e802b5e5 100644 --- a/sys/syz-extract/fuchsia.go +++ b/sys/syz-extract/fuchsia.go @@ -44,6 +44,7 @@ func (*fuchsia) processFile(arch *Arch, info *compiler.ConstInfo) (map[string]ui params := &extractParams{ DeclarePrintf: true, DefineGlibcUse: true, + TargetEndian: arch.target.HostEndian, } return extract(info, cc, args, params) } diff --git a/sys/syz-extract/linux.go b/sys/syz-extract/linux.go index 7760bcb0f..ef3a9c6d3 100644 --- a/sys/syz-extract/linux.go +++ b/sys/syz-extract/linux.go @@ -179,6 +179,7 @@ func (*linux) processFile(arch *Arch, info *compiler.ConstInfo) (map[string]uint params := &extractParams{ AddSource: "#include ", ExtractFromELF: true, + TargetEndian: arch.target.HostEndian, } cc := arch.target.CCompiler res, undeclared, err := extract(info, cc, args, params) diff --git a/sys/syz-extract/netbsd.go b/sys/syz-extract/netbsd.go index 336111444..27ebe3ec3 100644 --- a/sys/syz-extract/netbsd.go +++ b/sys/syz-extract/netbsd.go @@ -92,7 +92,8 @@ func (*netbsd) processFile(arch *Arch, info *compiler.ConstInfo) (map[string]uin } } params := &extractParams{ - AddSource: "#include ", + AddSource: "#include ", + TargetEndian: arch.target.HostEndian, } res, undeclared, err := extract(info, "gcc", args, params) for orig, compats := range compatNames { diff --git a/sys/syz-extract/openbsd.go b/sys/syz-extract/openbsd.go index bcac28299..0e322fb27 100644 --- a/sys/syz-extract/openbsd.go +++ b/sys/syz-extract/openbsd.go @@ -82,7 +82,8 @@ func (*openbsd) processFile(arch *Arch, info *compiler.ConstInfo) (map[string]ui } } params := &extractParams{ - AddSource: "#include ", + AddSource: "#include ", + TargetEndian: arch.target.HostEndian, } res, undeclared, err := extract(info, "cc", args, params) for orig, compats := range compatNames { diff --git a/sys/syz-extract/trusty.go b/sys/syz-extract/trusty.go index 55943088a..0022d86ff 100644 --- a/sys/syz-extract/trusty.go +++ b/sys/syz-extract/trusty.go @@ -41,6 +41,7 @@ func (*trusty) processFile(arch *Arch, info *compiler.ConstInfo) (map[string]uin } params := &extractParams{ DeclarePrintf: true, + TargetEndian: arch.target.HostEndian, } return extract(info, "gcc", args, params) } diff --git a/sys/syz-extract/windows.go b/sys/syz-extract/windows.go index 1b9b13937..d0cda310e 100644 --- a/sys/syz-extract/windows.go +++ b/sys/syz-extract/windows.go @@ -20,6 +20,7 @@ func (*windows) prepareArch(arch *Arch) error { func (*windows) processFile(arch *Arch, info *compiler.ConstInfo) (map[string]uint64, map[string]bool, error) { params := &extractParams{ DeclarePrintf: true, + TargetEndian: arch.target.HostEndian, } return extract(info, "cl", nil, params) } diff --git a/sys/syz-sysgen/sysgen.go b/sys/syz-sysgen/sysgen.go index c7e171d0f..4759b6a16 100644 --- a/sys/syz-sysgen/sysgen.go +++ b/sys/syz-sysgen/sysgen.go @@ -199,12 +199,12 @@ func generate(target *targets.Target, prg *compiler.Prog, consts map[string]uint fmt.Fprintf(out, "func init() {\n") fmt.Fprintf(out, "\tRegisterTarget(&Target{"+ "OS: %q, Arch: %q, Revision: revision_%v, PtrSize: %v, "+ - "PageSize: %v, NumPages: %v, DataOffset: %v, Syscalls: syscalls_%v, "+ + "PageSize: %v, NumPages: %v, DataOffset: %v, LittleEndian: %v, Syscalls: syscalls_%v, "+ "Resources: resources_%v, Consts: consts_%v}, "+ "types_%v, InitTarget)\n}\n\n", target.OS, target.Arch, target.Arch, target.PtrSize, target.PageSize, target.NumPages, target.DataOffset, - target.Arch, target.Arch, target.Arch, target.Arch) + target.LittleEndian, target.Arch, target.Arch, target.Arch, target.Arch) fmt.Fprintf(out, "var resources_%v = ", target.Arch) serializer.Write(out, prg.Resources) diff --git a/sys/targets/targets.go b/sys/targets/targets.go index 44515d480..47cd87752 100644 --- a/sys/targets/targets.go +++ b/sys/targets/targets.go @@ -4,6 +4,7 @@ package targets import ( + "encoding/binary" "fmt" "os" "os/exec" @@ -23,6 +24,7 @@ type Target struct { NumPages uint64 DataOffset uint64 Int64Alignment uint64 + LittleEndian bool CFlags []string Triple string CCompiler string @@ -34,6 +36,7 @@ type Target struct { BrokenCompiler string // NeedSyscallDefine is used by csource package to decide when to emit __NR_* defines. NeedSyscallDefine func(nr uint64) bool + HostEndian binary.ByteOrder } type osCommon struct { @@ -140,6 +143,7 @@ var List = map[string]map[string]*Target{ "amd64": { PtrSize: 8, PageSize: 4 << 10, + LittleEndian: true, CFlags: []string{"-m64"}, Triple: "x86_64-linux-gnu", KernelArch: "x86_64", @@ -155,6 +159,7 @@ var List = map[string]map[string]*Target{ PtrSize: 4, PageSize: 4 << 10, Int64Alignment: 4, + LittleEndian: true, CFlags: []string{"-m32"}, Triple: "x86_64-linux-gnu", KernelArch: "i386", @@ -163,6 +168,7 @@ var List = map[string]map[string]*Target{ "arm64": { PtrSize: 8, PageSize: 4 << 10, + LittleEndian: true, Triple: "aarch64-linux-gnu", KernelArch: "arm64", KernelHeaderArch: "arm64", @@ -171,6 +177,7 @@ var List = map[string]map[string]*Target{ VMArch: "arm64", PtrSize: 4, PageSize: 4 << 10, + LittleEndian: true, CFlags: []string{"-D__LINUX_ARM_ARCH__=6", "-march=armv6"}, Triple: "arm-linux-gnueabi", KernelArch: "arm", @@ -180,6 +187,7 @@ var List = map[string]map[string]*Target{ VMArch: "mips64le", PtrSize: 8, PageSize: 4 << 10, + LittleEndian: true, CFlags: []string{"-march=mips64r2", "-mabi=64", "-EL"}, Triple: "mips64el-linux-gnuabi64", KernelArch: "mips", @@ -188,6 +196,7 @@ var List = map[string]map[string]*Target{ "ppc64le": { PtrSize: 8, PageSize: 64 << 10, + LittleEndian: true, CFlags: []string{"-D__powerpc64__"}, Triple: "powerpc64le-linux-gnu", KernelArch: "powerpc", @@ -198,6 +207,7 @@ var List = map[string]map[string]*Target{ "amd64": { PtrSize: 8, PageSize: 4 << 10, + LittleEndian: true, CCompiler: "clang", CFlags: []string{"-m64"}, NeedSyscallDefine: dontNeedSyscallDefine, @@ -210,6 +220,7 @@ var List = map[string]map[string]*Target{ // FreeBSD and using ld.lld due to collisions. DataOffset: 256 << 20, Int64Alignment: 4, + LittleEndian: true, CCompiler: "clang", CFlags: []string{"-m32"}, NeedSyscallDefine: dontNeedSyscallDefine, @@ -217,8 +228,9 @@ var List = map[string]map[string]*Target{ }, "netbsd": { "amd64": { - PtrSize: 8, - PageSize: 4 << 10, + PtrSize: 8, + PageSize: 4 << 10, + LittleEndian: true, CFlags: []string{ "-m64", "-static", @@ -229,10 +241,11 @@ var List = map[string]map[string]*Target{ }, "openbsd": { "amd64": { - PtrSize: 8, - PageSize: 4 << 10, - CCompiler: "c++", - CFlags: []string{"-m64", "-static", "-lutil"}, + PtrSize: 8, + PageSize: 4 << 10, + LittleEndian: true, + CCompiler: "c++", + CFlags: []string{"-m64", "-static", "-lutil"}, NeedSyscallDefine: func(nr uint64) bool { switch nr { case 8: // SYS___tfork @@ -264,6 +277,7 @@ var List = map[string]map[string]*Target{ "amd64": { PtrSize: 8, PageSize: 4 << 10, + LittleEndian: true, KernelHeaderArch: "x64", CCompiler: sourceDirVar + "/prebuilt/third_party/clang/linux-x64/bin/clang", Objdump: sourceDirVar + "/prebuilt/third_party/clang/linux-x64/bin/llvm-objdump", @@ -272,6 +286,7 @@ var List = map[string]map[string]*Target{ "arm64": { PtrSize: 8, PageSize: 4 << 10, + LittleEndian: true, KernelHeaderArch: "arm64", CCompiler: sourceDirVar + "/prebuilt/third_party/clang/linux-x64/bin/clang", Objdump: sourceDirVar + "/prebuilt/third_party/clang/linux-x64/bin/llvm-objdump", @@ -282,13 +297,15 @@ var List = map[string]map[string]*Target{ "amd64": { PtrSize: 8, // TODO(dvyukov): what should we do about 4k vs 64k? - PageSize: 4 << 10, + PageSize: 4 << 10, + LittleEndian: true, }, }, "akaros": { "amd64": { PtrSize: 8, PageSize: 4 << 10, + LittleEndian: true, KernelHeaderArch: "x86", NeedSyscallDefine: dontNeedSyscallDefine, CCompiler: sourceDirVar + "/toolchain/x86_64-ucb-akaros-gcc/bin/x86_64-ucb-akaros-g++", @@ -301,6 +318,7 @@ var List = map[string]map[string]*Target{ "arm": { PtrSize: 4, PageSize: 4 << 10, + LittleEndian: true, NeedSyscallDefine: dontNeedSyscallDefine, }, }, @@ -508,6 +526,18 @@ func initTarget(target *Target, OS, arch string) { for _, flags := range [][]string{commonCFlags, target.osCommon.cflags} { target.CFlags = append(target.CFlags, flags...) } + if OS == "test" { + if runtime.GOARCH != "s390x" { + target.LittleEndian = true + } else { + target.LittleEndian = false + } + } + if target.LittleEndian { + target.HostEndian = binary.LittleEndian + } else { + target.HostEndian = binary.BigEndian + } } func (target *Target) replaceSourceDir(param *string, sourceDir string) { diff --git a/sys/test/test/align0 b/sys/test/test/align0 index a73ca98a5..82272f5a9 100644 --- a/sys/test/test/align0 +++ b/sys/test/test/align0 @@ -1,5 +1,5 @@ # 32_shmem has 4-byte alignment for int64 and everything goes havoc. -# requires: -arch=32_shmem +# requires: -arch=32_shmem littleendian syz_compare(&AUTO="010000000200000003000400000000000500000000000000", 0x18, &AUTO=@align0={0x1, 0x2, 0x3, 0x4, 0x5}, AUTO) syz_compare(&AUTO="", 0x18, &AUTO=@align0={0x0, 0x0, 0x0, 0x0, 0x0}, 0x17) # EBADF diff --git a/sys/test/test/align0_be b/sys/test/test/align0_be new file mode 100644 index 000000000..00f251cc9 --- /dev/null +++ b/sys/test/test/align0_be @@ -0,0 +1,6 @@ +# 32_shmem has 4-byte alignment for int64 and everything goes havoc. +# requires: -arch=32_shmem -littleendian + +syz_compare(&AUTO="000100000000000203000004000000000000000000000005", 0x18, &AUTO=@align0={0x1, 0x2, 0x3, 0x4, 0x5}, AUTO) +syz_compare(&AUTO="", 0x18, &AUTO=@align0={0x0, 0x0, 0x0, 0x0, 0x0}, 0x17) # EBADF +syz_compare(&AUTO="", 0x18, &AUTO=@align0={0x1, 0x0, 0x0, 0x0, 0x0}, AUTO) # EINVAL diff --git a/sys/test/test/bf b/sys/test/test/bf index b1a251e66..f35ff1336 100644 --- a/sys/test/test/bf +++ b/sys/test/test/bf @@ -1,5 +1,5 @@ # 32_shmem has 4-byte alignment for int64 and everything goes havoc. -# requires: -arch=32_shmem +# requires: -arch=32_shmem littleendian syz_compare(&AUTO="ab03000000000000cdcdcdcdcdcdcdcdebffff03ab0303abaa00000000000000", 0x20, &AUTO=@bf0={0xabab, 0xcdcdcdcdcdcdcdcd, 0xabab, 0xffff, 0xffffff, 0xabab, 0xabab, 0xaaa}, AUTO) syz_compare(&AUTO="dcfcde563422f10e", 0x8, &AUTO=@bf2={0x0abc, 0x0bcd, 0xcdef, 0x123456, 0x78ef12}, AUTO) diff --git a/sys/test/test/bf2 b/sys/test/test/bf2 index 1f5f71877..e0345388b 100644 --- a/sys/test/test/bf2 +++ b/sys/test/test/bf2 @@ -1,5 +1,5 @@ # 32_shmem has 4-byte alignment for int64 and everything goes havoc. -# requires: -arch=32_shmem +# requires: -arch=32_shmem littleendian syz_compare(&AUTO="1200000034067800", AUTO, &AUTO=@bf4={0x12, {0x34, 0x56, 0x78}}, AUTO) syz_compare(&AUTO="1200000034060000", AUTO, &AUTO=@bf5={0x12, {0x34, 0x56}}, AUTO) diff --git a/sys/test/test/nla b/sys/test/test/nla index 72bd24ff7..b2c5bf1a9 100644 --- a/sys/test/test/nla +++ b/sys/test/test/nla @@ -1,3 +1,5 @@ +# requires: littleendian + syz_compare(&AUTO="0500aa0055000000", AUTO, &AUTO=@nla=[@a0={AUTO, AUTO, 0x55, ''}], AUTO) syz_compare(&AUTO="0600bb0055550000", AUTO, &AUTO=@nla=[@a1={AUTO, AUTO, 0x5555, ''}], AUTO) syz_compare(&AUTO="0800cc0055555555", AUTO, &AUTO=@nla=[@a2={AUTO, AUTO, 0x55555555, ''}], AUTO) -- cgit mrf-deployment