Skip to content

Commit

Permalink
sys/targets: mark big-endian targets
Browse files Browse the repository at this point in the history
Litte-endian is kind of default (except for s390).
So instead of saying that each arch is litte-endian,
mark only s390 as big-endian.
  • Loading branch information
dvyukov committed Jun 4, 2024
1 parent 06bf810 commit 11f2afa
Show file tree
Hide file tree
Showing 16 changed files with 55 additions and 82 deletions.
4 changes: 2 additions & 2 deletions pkg/csource/csource.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,11 +409,11 @@ func (ctx *context) copyin(w *bytes.Buffer, csumSeq *int, copyin prog.ExecCopyin
panic("bitfield+string format")
}
htobe := ""
if ctx.target.LittleEndian && arg.Format == prog.FormatBigEndian {
if !ctx.target.BigEndian && arg.Format == prog.FormatBigEndian {
htobe = fmt.Sprintf("htobe%v", arg.Size*8)
}
bitfieldOffset := arg.BitfieldOffset
if !ctx.target.LittleEndian {
if ctx.target.BigEndian {
bitfieldOffset = arg.Size*8 - arg.BitfieldOffset - arg.BitfieldLength
}
fmt.Fprintf(w, "\tNONFAILING(STORE_BY_BITMASK(uint%v, %v, 0x%x, %v, %v, %v));\n",
Expand Down
12 changes: 3 additions & 9 deletions pkg/report/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package report

import (
"bytes"
"encoding/binary"
"fmt"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -580,11 +579,6 @@ func (ctx *linux) decompileWithOffset(parsed parsedOpcodes) (*decompiledOpcodes,
}

func (ctx *linux) parseOpcodes(codeSlice string) (parsedOpcodes, error) {
binaryOps := binary.ByteOrder(binary.BigEndian)
if ctx.target.LittleEndian {
binaryOps = binary.LittleEndian
}

width := 0
bytes := []byte{}
trapOffset := -1
Expand Down Expand Up @@ -620,11 +614,11 @@ func (ctx *linux) parseOpcodes(codeSlice string) (parsedOpcodes, error) {
case 1:
extraBytes[0] = byte(number)
case 2:
binaryOps.PutUint16(extraBytes, uint16(number))
ctx.target.HostEndian.PutUint16(extraBytes, uint16(number))
case 4:
binaryOps.PutUint32(extraBytes, uint32(number))
ctx.target.HostEndian.PutUint32(extraBytes, uint32(number))
case 8:
binaryOps.PutUint64(extraBytes, number)
ctx.target.HostEndian.PutUint64(extraBytes, number)
default:
return parsedOpcodes{}, fmt.Errorf("invalid opcodes string: invalid width %v", width)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/runtest/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ nextSandbox:
properties := map[string]bool{
"manual": ctx.Tests != "", // "manual" tests run only if selected by the filter explicitly.
"sandbox=" + sandbox: true,
"littleendian": ctx.Target.LittleEndian,
"bigendian": sysTarget.BigEndian,
}
for _, threaded := range []bool{false, true} {
name := name
Expand Down
16 changes: 8 additions & 8 deletions prog/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,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
LittleEndian bool
OS string
Arch string
Revision string // unique hash representing revision of the descriptions
PtrSize uint64
PageSize uint64
NumPages uint64
DataOffset uint64
BigEndian bool

Syscalls []*Syscall
Resources []*ResourceDesc
Expand Down
4 changes: 2 additions & 2 deletions sys/syz-sysgen/sysgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,11 @@ 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, LittleEndian: %v, "+
"NumPages: %v, DataOffset: %v, BigEndian: %v, "+
"Syscalls: syscalls_%v, Resources: resources_%v, Consts: consts_%v,"+
"Flags: flags_%v}, types_%v, InitTarget)\n}\n\n",
target.OS, target.Arch, target.Arch, target.PtrSize, target.PageSize,
target.NumPages, target.DataOffset, target.LittleEndian,
target.NumPages, target.DataOffset, target.BigEndian,
target.Arch, target.Arch, target.Arch, target.Arch, target.Arch)

fmt.Fprintf(out, "var resources_%v = ", target.Arch)
Expand Down
77 changes: 28 additions & 49 deletions sys/targets/targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Target struct {
NumPages uint64
DataOffset uint64
Int64Alignment uint64
LittleEndian bool
BigEndian bool
CFlags []string
CxxFlags []string
Triple string
Expand Down Expand Up @@ -250,7 +250,6 @@ 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",
Expand All @@ -266,7 +265,6 @@ 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",
Expand All @@ -275,7 +273,6 @@ var List = map[string]map[string]*Target{
ARM64: {
PtrSize: 8,
PageSize: 4 << 10,
LittleEndian: true,
Triple: "aarch64-linux-gnu",
KernelArch: "arm64",
KernelHeaderArch: "arm64",
Expand All @@ -284,7 +281,6 @@ 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",
Expand All @@ -293,7 +289,6 @@ var List = map[string]map[string]*Target{
MIPS64LE: {
PtrSize: 8,
PageSize: 4 << 10,
LittleEndian: true,
CFlags: []string{"-march=mips64r2", "-mabi=64", "-EL"},
Triple: "mips64el-linux-gnuabi64",
KernelArch: "mips",
Expand All @@ -302,7 +297,6 @@ 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",
Expand All @@ -313,7 +307,7 @@ var List = map[string]map[string]*Target{
PageSize: 4 << 10,
DataOffset: 0xfffff000,
CFlags: []string{"-fPIE"},
LittleEndian: false,
BigEndian: true,
Triple: "s390x-linux-gnu",
KernelArch: "s390",
KernelHeaderArch: "s390",
Expand All @@ -328,30 +322,27 @@ var List = map[string]map[string]*Target{
RiscV64: {
PtrSize: 8,
PageSize: 4 << 10,
LittleEndian: true,
Triple: "riscv64-linux-gnu",
KernelArch: "riscv",
KernelHeaderArch: "riscv",
},
},
FreeBSD: {
AMD64: {
PtrSize: 8,
PageSize: 4 << 10,
LittleEndian: true,
CCompiler: "clang",
CFlags: []string{"-m64", "--target=x86_64-unknown-freebsd14.0"},
PtrSize: 8,
PageSize: 4 << 10,
CCompiler: "clang",
CFlags: []string{"-m64", "--target=x86_64-unknown-freebsd14.0"},
NeedSyscallDefine: func(nr uint64) bool {
// freebsd_12_shm_open, shm_open2, shm_rename, __realpathat, close_range, copy_file_range
return nr == 482 || nr >= 569
},
},
ARM64: {
PtrSize: 8,
PageSize: 4 << 10,
LittleEndian: true,
CCompiler: "clang",
CFlags: []string{"-m64", "--target=aarch64-unknown-freebsd14.0"},
PtrSize: 8,
PageSize: 4 << 10,
CCompiler: "clang",
CFlags: []string{"-m64", "--target=aarch64-unknown-freebsd14.0"},
NeedSyscallDefine: func(nr uint64) bool {
// freebsd_12_shm_open, shm_open2, shm_rename, __realpathat, close_range, copy_file_range
return nr == 482 || nr >= 569
Expand All @@ -365,7 +356,6 @@ 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", "--target=i386-unknown-freebsd14.0"},
NeedSyscallDefine: func(nr uint64) bool {
Expand All @@ -374,11 +364,10 @@ var List = map[string]map[string]*Target{
},
},
RiscV64: {
PtrSize: 8,
PageSize: 4 << 10,
LittleEndian: true,
CCompiler: "clang",
CFlags: []string{"-m64", "--target=riscv64-unknown-freebsd14.0"},
PtrSize: 8,
PageSize: 4 << 10,
CCompiler: "clang",
CFlags: []string{"-m64", "--target=riscv64-unknown-freebsd14.0"},
NeedSyscallDefine: func(nr uint64) bool {
// freebsd_12_shm_open, shm_open2, shm_rename, __realpathat, close_range, copy_file_range
return nr == 482 || nr >= 569
Expand All @@ -387,11 +376,10 @@ var List = map[string]map[string]*Target{
},
Darwin: {
AMD64: {
PtrSize: 8,
PageSize: 4 << 10,
DataOffset: 512 << 24,
LittleEndian: true,
CCompiler: "clang",
PtrSize: 8,
PageSize: 4 << 10,
DataOffset: 512 << 24,
CCompiler: "clang",
CFlags: []string{
"-m64",
"-I", sourceDirVar + "/san",
Expand All @@ -403,9 +391,8 @@ var List = map[string]map[string]*Target{
},
NetBSD: {
AMD64: {
PtrSize: 8,
PageSize: 4 << 10,
LittleEndian: true,
PtrSize: 8,
PageSize: 4 << 10,
CFlags: []string{
"-m64",
"-static-pie",
Expand All @@ -416,10 +403,9 @@ var List = map[string]map[string]*Target{
},
OpenBSD: {
AMD64: {
PtrSize: 8,
PageSize: 4 << 10,
LittleEndian: true,
CCompiler: "c++",
PtrSize: 8,
PageSize: 4 << 10,
CCompiler: "c++",
// PIE is enabled on OpenBSD by default, so no need for -static-pie.
CFlags: []string{"-m64", "-static", "-lutil"},
NeedSyscallDefine: func(nr uint64) bool {
Expand Down Expand Up @@ -453,7 +439,6 @@ 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",
Expand All @@ -462,7 +447,6 @@ 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",
Expand All @@ -473,15 +457,13 @@ var List = map[string]map[string]*Target{
AMD64: {
PtrSize: 8,
// TODO(dvyukov): what should we do about 4k vs 64k?
PageSize: 4 << 10,
LittleEndian: true,
PageSize: 4 << 10,
},
},
Trusty: {
ARM: {
PtrSize: 4,
PageSize: 4 << 10,
LittleEndian: true,
NeedSyscallDefine: dontNeedSyscallDefine,
},
},
Expand Down Expand Up @@ -738,15 +720,12 @@ func initTarget(target *Target, OS, arch string) {
target.CFlags = append(target.CFlags, flags...)
}
if OS == TestOS {
if runtime.GOARCH != S390x {
target.LittleEndian = true
} else {
target.LittleEndian = false
if runtime.GOARCH == S390x {
target.BigEndian = true
}
}
if target.LittleEndian {
target.HostEndian = binary.LittleEndian
} else {
target.HostEndian = binary.LittleEndian
if target.BigEndian {
target.HostEndian = binary.BigEndian
}
// Temporal hack.
Expand Down
2 changes: 1 addition & 1 deletion sys/test/test/align0
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# 32 has 4-byte alignment for int64 and everything goes havoc.
# requires: -arch=32 littleendian
# requires: -arch=32 -bigendian

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
Expand Down
2 changes: 1 addition & 1 deletion sys/test/test/align0_be
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# 32 has 4-byte alignment for int64 and everything goes havoc.
# requires: -arch=32 -littleendian
# requires: -arch=32 bigendian

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
Expand Down
2 changes: 1 addition & 1 deletion sys/test/test/bf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# 32 has 4-byte alignment for int64 and everything goes havoc.
# requires: -arch=32 littleendian
# requires: -arch=32 -bigendian

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)
Expand Down
2 changes: 1 addition & 1 deletion sys/test/test/bf2
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# 32 has 4-byte alignment for int64 and everything goes havoc.
# requires: -arch=32 littleendian
# requires: -arch=32 -bigendian

syz_compare(&AUTO="1200000034067800", AUTO, &AUTO=@bf4={0x12, {0x34, 0x56, 0x78}}, AUTO)
syz_compare(&AUTO="1200000034060000", AUTO, &AUTO=@bf5={0x12, {0x34, 0x56}}, AUTO)
Expand Down
2 changes: 1 addition & 1 deletion sys/test/test/bf2_be
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# 32 has 4-byte alignment for int64 and everything goes havoc.
# requires: -arch=32 -littleendian
# requires: -arch=32 bigendian

syz_compare(&AUTO="1200000034607800", AUTO, &AUTO=@bf4={0x12, {0x34, 0x56, 0x78}}, AUTO)
syz_compare(&AUTO="1200000034600000", AUTO, &AUTO=@bf5={0x12, {0x34, 0x56}}, AUTO)
Expand Down
2 changes: 1 addition & 1 deletion sys/test/test/bf_be
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# 32 has 4-byte alignment for int64 and everything goes havoc.
# requires: -arch=32 -littleendian
# requires: -arch=32 bigendian

syz_compare(&AUTO="eac0000000000000cdcdcdcdcdcdcdcd5fffffc075607560aa", 0x20, &AUTO=@bf0={0xabab, 0xcdcdcdcdcdcdcdcd, 0xabab, 0xffff, 0xffffff, 0xabab, 0xabab, 0xaaa}, AUTO)
syz_compare(&AUTO="ccddef23456ef120", 0x8, &AUTO=@bf2={0x0abc, 0x0bcd, 0xcdef, 0x123456, 0x78ef12}, AUTO)
Expand Down
4 changes: 2 additions & 2 deletions sys/test/test/expressions
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# requires: littleendian
# requires: -bigendian

syz_compare(&AUTO="00000000", 0x4, &AUTO=@conditional={0x0, @void, @void}, AUTO)
syz_compare(&AUTO="02000000ffffffff", 0x8, &AUTO=@conditional={0x2, @value={AUTO}, @void}, AUTO)
Expand All @@ -7,4 +7,4 @@ syz_compare(&AUTO="06000000ffffffffaaaa000000000000", 0x10, &AUTO=@conditional={
syz_compare(&AUTO="00ff0000", 0x4, &AUTO=@conditional2={0x0, @void, 0xff}, AUTO)
syz_compare(&AUTO="0134120000ff0000", 0x8, &AUTO=@conditional2={0x1, @value=0x1234, 0xff}, AUTO)
syz_compare(&AUTO="1100220000330000", 0x8, &AUTO=@conditional3={0x11, {0x0, @void, 0x22}, 0x33}, AUTO)
syz_compare(&AUTO="1101ddccbbaa220000330000", 0xc, &AUTO=@conditional3={0x11, {0x1, @value=0xaabbccdd, 0x22}, 0x33}, AUTO)
syz_compare(&AUTO="1101ddccbbaa220000330000", 0xc, &AUTO=@conditional3={0x11, {0x1, @value=0xaabbccdd, 0x22}, 0x33}, AUTO)
2 changes: 1 addition & 1 deletion sys/test/test/nla
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# requires: littleendian
# requires: -bigendian

syz_compare(&AUTO="0500aa0055000000", AUTO, &AUTO=@nla=[@a0={AUTO, AUTO, 0x55, ''}], AUTO)
syz_compare(&AUTO="0600bb0055550000", AUTO, &AUTO=@nla=[@a1={AUTO, AUTO, 0x5555, ''}], AUTO)
Expand Down
2 changes: 1 addition & 1 deletion sys/test/test/overlay
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# requires: littleendian
# requires: -bigendian

syz_compare(&AUTO="1111111122222222", AUTO, &AUTO=@overlay0={0x11111111, 0x22222222, <r0=>0x0, <r1=>0x0}, AUTO)
syz_compare(&AUTO="11111111", AUTO, &AUTO=@overlayres=@res32=r0, AUTO)
Expand Down
2 changes: 1 addition & 1 deletion sys/test/test/overlay_be
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# requires: -littleendian
# requires: bigendian

syz_compare(&AUTO="1111111122222222", AUTO, &AUTO=@overlay0={0x11111111, 0x22222222, <r0=>0x0, <r1=>0x0}, AUTO)
syz_compare(&AUTO="11111111", AUTO, &AUTO=@overlayres=@res32=r0, AUTO)
Expand Down

0 comments on commit 11f2afa

Please sign in to comment.