Skip to content

Commit 885ad10

Browse files
committed
New architecture: wasm
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]>
1 parent 625b0a9 commit 885ad10

File tree

5 files changed

+13
-9
lines changed

5 files changed

+13
-9
lines changed

btf/btf.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -632,9 +632,11 @@ func (s *Spec) TypeByName(name string, typ interface{}) error {
632632
wanted = typPtr.Elem().Type()
633633
}
634634

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

639641
types, err := s.AnyTypesByName(name)
640642
if err != nil {

internal/endian_le.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build 386 || amd64 || amd64p32 || arm || arm64 || loong64 || mipsle || mips64le || mips64p32le || ppc64le || riscv64
1+
//go:build 386 || amd64 || amd64p32 || arm || arm64 || loong64 || mipsle || mips64le || mips64p32le || ppc64le || riscv64 || wasm
22

33
package internal
44

internal/io.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ func NewBufferedSectionReader(ra io.ReaderAt, off, n int64) *bufio.Reader {
2727
// of a file into memory. bufio.NewReader uses a hardcoded default buffer
2828
// of 4096. Allow arches with larger pages to allocate more, but don't
2929
// allocate a fixed 4k buffer if we only need to read a small segment.
30+
pageSize := int64(4096)
3031
buf := n
31-
if ps := int64(os.Getpagesize()); n > ps {
32+
if ps := pageSize; n > ps {
3233
buf = ps
3334
}
3435

map.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"fmt"
77
"io"
88
"math/rand"
9-
"os"
9+
//"os"
1010
"path/filepath"
1111
"reflect"
1212
"slices"
@@ -507,7 +507,7 @@ func handleMapCreateError(attr sys.MapCreateAttr, spec *MapSpec, err error) erro
507507
// BPF_MAP_TYPE_RINGBUF's max_entries must be a power-of-2 multiple of kernel's page size.
508508
if errors.Is(err, unix.EINVAL) &&
509509
(attr.MapType == sys.BPF_MAP_TYPE_RINGBUF || attr.MapType == sys.BPF_MAP_TYPE_USER_RINGBUF) {
510-
pageSize := uint32(os.Getpagesize())
510+
pageSize := uint32(4096)
511511
maxEntries := attr.MaxEntries
512512
if maxEntries%pageSize != 0 || !internal.IsPow(maxEntries) {
513513
return fmt.Errorf("map create: %w (ring map size %d not a multiple of page size %d)", err, maxEntries, pageSize)
@@ -950,7 +950,7 @@ func (m *Map) nextKey(key interface{}, nextKeyOut sys.Pointer) error {
950950
}
951951

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

956956
// guessNonExistentKey attempts to perform a map lookup that returns ENOENT.

syscalls.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ var (
2727
// invalidBPFObjNameChar returns true if char may not appear in
2828
// a BPF object name.
2929
func invalidBPFObjNameChar(char rune) bool {
30-
dotAllowed := objNameAllowsDot() == nil
30+
//dotAllowed := objNameAllowsDot() == nil
31+
dotAllowed := true
3132

3233
switch {
3334
case char >= 'A' && char <= 'Z':

0 commit comments

Comments
 (0)