Skip to content

Commit

Permalink
New architecture: wasm
Browse files Browse the repository at this point in the history
cilium/ebpf currently does not compile to wasm with tinygo. This patch
makes it compile.

I am working on a website where users can submit ebpf binaries (either
in ELF format or as an Inspektor Gadget export) and the website will
parse the ebpf binary client-side with wasm. The wasm code is written in
Go, using cilium/ebpf and is compiled with tinygo.

My wasm code uses ebpf.LoadCollectionSpecFromReader() to display
information about the ebpf binary. But it will not call
ebpf.NewCollection() because the wasm/javascript environment in the
browser cannot interact with the Linux kernel.

Signed-off-by: Alban Crequy <[email protected]>
  • Loading branch information
alban committed Aug 26, 2024
1 parent 625b0a9 commit 885ad10
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
8 changes: 5 additions & 3 deletions btf/btf.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,9 +632,11 @@ func (s *Spec) TypeByName(name string, typ interface{}) error {
wanted = typPtr.Elem().Type()
}

if !wanted.AssignableTo(typeInterface) {
return fmt.Errorf("%T does not satisfy Type interface", typ)
}
// https://github.com/tinygo-org/tinygo/issues/4277

//if !wanted.AssignableTo(typeInterface) {
// return fmt.Errorf("%T does not satisfy Type interface", typ)
//}

types, err := s.AnyTypesByName(name)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/endian_le.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build 386 || amd64 || amd64p32 || arm || arm64 || loong64 || mipsle || mips64le || mips64p32le || ppc64le || riscv64
//go:build 386 || amd64 || amd64p32 || arm || arm64 || loong64 || mipsle || mips64le || mips64p32le || ppc64le || riscv64 || wasm

package internal

Expand Down
3 changes: 2 additions & 1 deletion internal/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ func NewBufferedSectionReader(ra io.ReaderAt, off, n int64) *bufio.Reader {
// of a file into memory. bufio.NewReader uses a hardcoded default buffer
// of 4096. Allow arches with larger pages to allocate more, but don't
// allocate a fixed 4k buffer if we only need to read a small segment.
pageSize := int64(4096)
buf := n
if ps := int64(os.Getpagesize()); n > ps {
if ps := pageSize; n > ps {
buf = ps
}

Expand Down
6 changes: 3 additions & 3 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"io"
"math/rand"
"os"
//"os"
"path/filepath"
"reflect"
"slices"
Expand Down Expand Up @@ -507,7 +507,7 @@ func handleMapCreateError(attr sys.MapCreateAttr, spec *MapSpec, err error) erro
// BPF_MAP_TYPE_RINGBUF's max_entries must be a power-of-2 multiple of kernel's page size.
if errors.Is(err, unix.EINVAL) &&
(attr.MapType == sys.BPF_MAP_TYPE_RINGBUF || attr.MapType == sys.BPF_MAP_TYPE_USER_RINGBUF) {
pageSize := uint32(os.Getpagesize())
pageSize := uint32(4096)
maxEntries := attr.MaxEntries
if maxEntries%pageSize != 0 || !internal.IsPow(maxEntries) {
return fmt.Errorf("map create: %w (ring map size %d not a multiple of page size %d)", err, maxEntries, pageSize)
Expand Down Expand Up @@ -950,7 +950,7 @@ func (m *Map) nextKey(key interface{}, nextKeyOut sys.Pointer) error {
}

var mmapProtectedPage = sync.OnceValues(func() ([]byte, error) {
return unix.Mmap(-1, 0, os.Getpagesize(), unix.PROT_NONE, unix.MAP_ANON|unix.MAP_SHARED)
return unix.Mmap(-1, 0, 4096, unix.PROT_NONE, unix.MAP_ANON|unix.MAP_SHARED)
})

// guessNonExistentKey attempts to perform a map lookup that returns ENOENT.
Expand Down
3 changes: 2 additions & 1 deletion syscalls.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ var (
// invalidBPFObjNameChar returns true if char may not appear in
// a BPF object name.
func invalidBPFObjNameChar(char rune) bool {
dotAllowed := objNameAllowsDot() == nil
//dotAllowed := objNameAllowsDot() == nil
dotAllowed := true

switch {
case char >= 'A' && char <= 'Z':
Expand Down

0 comments on commit 885ad10

Please sign in to comment.