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

Fix wrong behavior on s390x #91

Merged
merged 2 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions asm/instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package asm
import (
"encoding/binary"
"fmt"
"github.com/cilium/ebpf/internal"
"io"
"math"
"strings"
Expand Down Expand Up @@ -433,15 +434,27 @@ type bpfInstruction struct {
type bpfRegisters uint8

func newBPFRegisters(dst, src Register) bpfRegisters {
return bpfRegisters((src << 4) | (dst & 0xF))
if internal.NativeEndian == binary.LittleEndian {
return bpfRegisters((src << 4) | (dst & 0xF))
} else {
return bpfRegisters((dst << 4) | (src & 0xF))
}
}

func (r bpfRegisters) Dst() Register {
return Register(r & 0xF)
if internal.NativeEndian == binary.LittleEndian {
return Register(r & 0xF)
}else {
return Register(r >> 4)
}
}

func (r bpfRegisters) Src() Register {
return Register(r >> 4)
if internal.NativeEndian == binary.LittleEndian {
return Register(r >> 4)
} else {
return Register(r & 0xf)
}
}

type unreferencedSymbolError struct {
Expand Down
31 changes: 31 additions & 0 deletions asm/instruction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"io/ioutil"
"math"
"testing"

"github.com/cilium/ebpf/internal"
)

var test64bitImmProg = []byte{
Expand Down Expand Up @@ -179,3 +181,32 @@ func ExampleInstructions_Format() {
// 1: LdImmDW dst: r0 imm: 42
// 3: Exit
}

var testSrcDstProg = []byte{
// on little-endian: r0 = r1
// on big-endian: be: r1 = r0
0xbf, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
}

func TestReadSrcDst(t *testing.T) {
var ins Instruction
n, err := ins.Unmarshal(bytes.NewReader(testSrcDstProg), binary.BigEndian)
if err != nil {
t.Fatal(err)
}
if want := uint64(InstructionSize); n != want {
t.Errorf("Expected %d bytes to be read, got %d", want, n)
}
var expDst, expSrc Register
if internal.NativeEndian == binary.LittleEndian {
expDst, expSrc = 0, 1
} else {
expDst, expSrc = 1, 0
}
if ins.Dst != expDst {
t.Errorf("Expected destination to be %v, got %v", expDst, ins.Dst)
}
if ins.Src != expSrc {
t.Errorf("Expected source to be %v, got %v", expSrc, ins.Src)
}
}
7 changes: 5 additions & 2 deletions elf_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ebpf

import (
"flag"
"fmt"
"path/filepath"
"reflect"
"strings"
Expand All @@ -11,7 +12,8 @@ import (
)

func TestLoadCollectionSpec(t *testing.T) {
files, err := filepath.Glob("testdata/loader-*.elf")
pattern := fmt.Sprintf("testdata/loader-*-%s.elf", testutils.GetHostEndianness())
files, err := filepath.Glob(pattern)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -209,7 +211,8 @@ func TestCollectionSpecDetach(t *testing.T) {
}

func TestLoadInvalidMap(t *testing.T) {
_, err := LoadCollectionSpec("testdata/invalid_map.elf")
path := fmt.Sprintf("testdata/invalid_map-%s.elf", testutils.GetHostEndianness())
_, err := LoadCollectionSpec(path)
t.Log(err)
if err == nil {
t.Fatal("should be fail")
Expand Down
2 changes: 1 addition & 1 deletion internal/btf/btf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestParseCurrentKernelBTF(t *testing.T) {
}

func TestLoadSpecFromElf(t *testing.T) {
fh, err := os.Open("../../testdata/loader-clang-9.elf")
fh, err := os.Open(fmt.Sprintf("../../testdata/loader-clang-9-%s.elf", testutils.GetHostEndianness()));
if err != nil {
t.Fatal(err)
}
Expand Down
15 changes: 15 additions & 0 deletions internal/testutils/endianness.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package testutils

import (
"encoding/binary"

"github.com/cilium/ebpf/internal"
)

func GetHostEndianness() string {
iii-i marked this conversation as resolved.
Show resolved Hide resolved
if internal.NativeEndian == binary.LittleEndian {
return "el"
} else {
return "eb"
}
}
21 changes: 12 additions & 9 deletions testdata/Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
LLVM_PREFIX ?= /usr/bin
CLANG ?= $(LLVM_PREFIX)/clang
CFLAGS := -target bpf -O2 -g -Wall -Werror $(CFLAGS)

.PHONY: all clean
all: loader-clang-6.0.elf loader-clang-7.elf loader-clang-8.elf loader-clang-9.elf rewrite.elf invalid_map.elf
all: loader-clang-6.0-el.elf loader-clang-7-el.elf loader-clang-8-el.elf loader-clang-9-el.elf rewrite-el.elf invalid_map-el.elf \
loader-clang-6.0-eb.elf loader-clang-7-eb.elf loader-clang-8-eb.elf loader-clang-9-eb.elf rewrite-eb.elf invalid_map-eb.elf

clean:
-$(RM) *.elf

loader-%.elf: loader.c
$* -target bpf -O2 -g \
-Wall -Werror \
-c $< -o $@
loader-%-el.elf: loader.c
$* $(CFLAGS) -mlittle-endian -c $< -o $@
iii-i marked this conversation as resolved.
Show resolved Hide resolved

%.elf : %.c
$(CLANG) -target bpf -O2 -g \
-Wall -Werror \
-c $< -o $@
loader-%-eb.elf: loader.c
$* $(CFLAGS) -mbig-endian -c $< -o $@

%-el.elf: %.c
$(CLANG) $(CFLAGS) -mlittle-endian -c $< -o $@

%-eb.elf : %.c
$(CLANG) $(CFLAGS) -mbig-endian -c $< -o $@
Binary file added testdata/invalid_map-eb.elf
Binary file not shown.
File renamed without changes.
Binary file added testdata/loader-clang-6.0-eb.elf
Binary file not shown.
File renamed without changes.
Binary file added testdata/loader-clang-7-eb.elf
Binary file not shown.
File renamed without changes.
Binary file added testdata/loader-clang-8-eb.elf
Binary file not shown.
File renamed without changes.
Binary file added testdata/loader-clang-9-eb.elf
Binary file not shown.
File renamed without changes.
Binary file added testdata/rewrite-eb.elf
Binary file not shown.
File renamed without changes.