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 <Alexander.Egorenkov@ibm.com>
This commit is contained in:
Alexander Egorenkov 2020-06-23 17:46:15 +02:00 committed by Dmitry Vyukov
parent 6930bbef3b
commit bbad15ae75
19 changed files with 75 additions and 24 deletions

View File

@ -1,6 +1,8 @@
// Copyright 2017 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
// +build !s390x
package kd
import (

View File

@ -200,6 +200,7 @@ nextSandbox:
properties := map[string]bool{
"arch=" + ctx.Target.Arch: true,
"sandbox=" + sandbox: true,
"littleendian": ctx.Target.LittleEndian,
}
for _, threaded := range []bool{false, true} {
name := name

View File

@ -13,13 +13,14 @@ import (
// Target describes target OS/arch pair.
type Target struct {
OS string
Arch string
Revision string // unique hash representing revision of the descriptions
PtrSize uint64
PageSize uint64
NumPages uint64
DataOffset uint64
OS string
Arch string
Revision string // unique hash representing revision of the descriptions
PtrSize uint64
PageSize uint64
NumPages uint64
DataOffset uint64
LittleEndian bool
Syscalls []*Syscall
Resources []*ResourceDesc

View File

@ -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)
}

View File

@ -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

View File

@ -59,6 +59,7 @@ func (*freebsd) processFile(arch *Arch, info *compiler.ConstInfo) (map[string]ui
params := &extractParams{
AddSource: "#include <sys/syscall.h>",
DeclarePrintf: true,
TargetEndian: arch.target.HostEndian,
}
return extract(info, "gcc", args, params)
}

View File

@ -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)
}

View File

@ -179,6 +179,7 @@ func (*linux) processFile(arch *Arch, info *compiler.ConstInfo) (map[string]uint
params := &extractParams{
AddSource: "#include <asm/unistd.h>",
ExtractFromELF: true,
TargetEndian: arch.target.HostEndian,
}
cc := arch.target.CCompiler
res, undeclared, err := extract(info, cc, args, params)

View File

@ -92,7 +92,8 @@ func (*netbsd) processFile(arch *Arch, info *compiler.ConstInfo) (map[string]uin
}
}
params := &extractParams{
AddSource: "#include <sys/syscall.h>",
AddSource: "#include <sys/syscall.h>",
TargetEndian: arch.target.HostEndian,
}
res, undeclared, err := extract(info, "gcc", args, params)
for orig, compats := range compatNames {

View File

@ -82,7 +82,8 @@ func (*openbsd) processFile(arch *Arch, info *compiler.ConstInfo) (map[string]ui
}
}
params := &extractParams{
AddSource: "#include <sys/syscall.h>",
AddSource: "#include <sys/syscall.h>",
TargetEndian: arch.target.HostEndian,
}
res, undeclared, err := extract(info, "cc", args, params)
for orig, compats := range compatNames {

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)

View File

@ -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) {

View File

@ -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

6
sys/test/test/align0_be Normal file
View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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)