nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: cilium/ebpf
Pull request #799 (2022-09-21 06:00:04)

reopen may uint128 to [16]byte
The latest code seems to have changed quite a bit so it was changed to:

[format.go#L185](https://github.com/Benjamin-Yim/ebpf/blob/fix_uint128/btf/format.go#L185)

```golang
...
	case Unsigned, Signed:
		stem := "uint"
		if i.Encoding == Signed {
			stem = "int"
		}
		if i.Size > 8 {
			fmt.Fprintf(&gf.w, "[%d]byte /* %s%d */", i.Size, stem, i.Size*8)
		} else {
			fmt.Fprintf(&gf.w, "%s%d", stem, bits)
		}
```



Patch:
diff --git a/btf/format.go b/btf/format.go
index b1eaefe..4376ecc 100644
--- a/btf/format.go
+++ b/btf/format.go
@@ -174,8 +174,6 @@ func (gf *GoFormatter) writeIntLit(i *Int) error {
 			return fmt.Errorf("bool with size %d", i.Size)
 		}
 		gf.w.WriteString("bool")
-	case Signed:
-		fmt.Fprintf(&gf.w, "int%d", bits)
 	case Char:
 		if i.Size != 1 {
 			return fmt.Errorf("char with size %d", i.Size)
@@ -184,8 +182,16 @@ func (gf *GoFormatter) writeIntLit(i *Int) error {
 		// we are dealing with unsigned, since this works nicely with []byte
 		// in Go code.
 		fallthrough
-	case Unsigned:
-		fmt.Fprintf(&gf.w, "uint%d", bits)
+	case Unsigned, Signed:
+		stem := "uint"
+		if i.Encoding == Signed {
+			stem = "int"
+		}
+		if i.Size > 8 {
+			fmt.Fprintf(&gf.w, "[%d]byte /* %s%d */", i.Size, stem, i.Size*8)
+		} else {
+			fmt.Fprintf(&gf.w, "%s%d", stem, bits)
+		}
 	default:
 		return fmt.Errorf("can't encode %s", i.Encoding)
 	}


Test patch:
diff --git a/btf/format_test.go b/btf/format_test.go
index 40a4104..7ada84d 100644
--- a/btf/format_test.go
+++ b/btf/format_test.go
@@ -20,7 +20,7 @@ func TestGoTypeDeclaration(t *testing.T) {
 		{&Int{Size: 4, Encoding: Signed}, "type t int32"},
 		{&Int{Size: 8}, "type t uint64"},
 		{&Typedef{Name: "frob", Type: &Int{Size: 8}}, "type t uint64"},
-		{&Int{Size: 16}, "type t uint128"},
+		{&Int{Size: 16}, "type t [16]byte /* uint128 */"},
 		{&Enum{Values: []EnumValue{{"FOO", 32}}, Size: 4}, "type t uint32; const ( tFOO t = 32; )"},
 		{&Enum{Values: []EnumValue{{"BAR", 1}}, Size: 1, Signed: true}, "type t int8; const ( tBAR t = 1; )"},
 		{
Repository: cilium/ebpf
Pull request #817 (2022-10-11 11:34:48)

Add IsPinned() to RawLink
Map and Prog have IsPinned() func but RawLink doesn't have it. I added IsPinned() to RawLink. Also seems Unpin method of Rawlink doesn't have a test, I added it too.

boratanrikulu: @lmb, thank you for the review. I made updates

Patch:
diff --git a/link/link.go b/link/link.go
index 1127850..d4eeb92 100644
--- a/link/link.go
+++ b/link/link.go
@@ -230,6 +230,11 @@ func (l *RawLink) Unpin() error {
 	return nil
 }
 
+// IsPinned returns true if the Link has a non-empty pinned path.
+func (l *RawLink) IsPinned() bool {
+	return l.pinnedPath != ""
+}
+
 // Update implements the Link interface.
 func (l *RawLink) Update(new *ebpf.Program) error {
 	return l.UpdateArgs(RawLinkUpdateOptions{


Test patch:
diff --git a/link/link_test.go b/link/link_test.go
index bafbb47..a00156d 100644
--- a/link/link_test.go
+++ b/link/link_test.go
@@ -13,6 +13,8 @@ import (
 	"github.com/cilium/ebpf/internal/sys"
 	"github.com/cilium/ebpf/internal/testutils"
 	"github.com/cilium/ebpf/internal/unix"
+
+	qt "github.com/frankban/quicktest"
 )
 
 func TestRawLink(t *testing.T) {
@@ -50,8 +52,38 @@ func TestRawLink(t *testing.T) {
 	testLink(t, &linkCgroup{*link}, prog)
 }
 
+func TestUnpinRawLink(t *testing.T) {
+	cgroup, prog := mustCgroupFixtures(t)
+	link, _ := newPinnedRawLink(t, cgroup, prog)
+
+	qt.Assert(t, link.IsPinned(), qt.IsTrue)
+
+	err := link.Unpin()
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	qt.Assert(t, link.IsPinned(), qt.IsFalse)
+}
+
 func TestRawLinkLoadPinnedWithOptions(t *testing.T) {
 	cgroup, prog := mustCgroupFixtures(t)
+	link, path := newPinnedRawLink(t, cgroup, prog)
+
+	qt.Assert(t, link.IsPinned(), qt.IsTrue)
+
+	// It seems like the kernel ignores BPF_F_RDONLY when updating a link,
+	// so we can't test this.
+	_, err := loadPinnedRawLink(path, &ebpf.LoadPinOptions{
+		Flags: math.MaxUint32,
+	})
+	if !errors.Is(err, unix.EINVAL) {
+		t.Fatal("Invalid flags don't trigger an error:", err)
+	}
+}
+
+func newPinnedRawLink(t *testing.T, cgroup *os.File, prog *ebpf.Program) (*RawLink, string) {
+	t.Helper()
 
 	link, err := AttachRawLink(RawLinkOptions{
 		Target:  int(cgroup.Fd()),
@@ -70,14 +102,7 @@ func TestRawLinkLoadPinnedWithOptions(t *testing.T) {
 		t.Fatal(err)
 	}
 
-	// It seems like the kernel ignores BPF_F_RDONLY when updating a link,
-	// so we can't test this.
-	_, err = loadPinnedRawLink(path, &ebpf.LoadPinOptions{
-		Flags: math.MaxUint32,
-	})
-	if !errors.Is(err, unix.EINVAL) {
-		t.Fatal("Invalid flags don't trigger an error:", err)
-	}
+	return link, path
 }
 
 func mustCgroupFixtures(t *testing.T) (*os.File, *ebpf.Program) {
Repository: cilium/ebpf
Pull request #832 (2022-10-31 10:54:23)

asm: add .WithMetadata() for conveniently replacing individual Instructions
When replacing an Instruction in an Instructions parsed from an ELF, preserving Metadata is important, so this patch adds a convenient method for in-line assignment of Metadata from other instructions.

With func_info and line_info marshaling being driven by Metadata, accidentally stripping a func_info from the first Instruction will result in the following verifier error:

`invalid argument: missing bpf_line_info for func#0`

The verifier rejects line_infos for subprogs that don't start with a func_info.

cc @rgo3 


Patch:
diff --git a/asm/instruction.go b/asm/instruction.go
index f17d88b..19c5b64 100644
--- a/asm/instruction.go
+++ b/asm/instruction.go
@@ -354,6 +354,13 @@ func (ins Instruction) Size() uint64 {
 	return uint64(InstructionSize * ins.OpCode.rawInstructions())
 }
 
+// WithMetadata sets the given Metadata on the Instruction. e.g. to copy
+// Metadata from another Instruction when replacing it.
+func (ins Instruction) WithMetadata(meta Metadata) Instruction {
+	ins.Metadata = meta
+	return ins
+}
+
 type symbolMeta struct{}
 
 // WithSymbol marks the Instruction as a Symbol, which other Instructions


Test patch:
diff --git a/asm/instruction_test.go b/asm/instruction_test.go
index ef3a37d..4eded25 100644
--- a/asm/instruction_test.go
+++ b/asm/instruction_test.go
@@ -177,6 +177,19 @@ func TestInstructionsRewriteMapPtr(t *testing.T) {
 	}
 }
 
+func TestInstructionWithMetadata(t *testing.T) {
+	ins := LoadImm(R0, 123, DWord).WithSymbol("abc")
+	ins2 := LoadImm(R0, 567, DWord).WithMetadata(ins.Metadata)
+
+	if want, got := "abc", ins2.Symbol(); want != got {
+		t.Fatalf("unexpected Symbol value on ins2: want: %s, got: %s", want, got)
+	}
+
+	if want, got := ins.Metadata, ins2.Metadata; want != got {
+		t.Fatal("expected ins and isn2 Metadata to match")
+	}
+}
+
 // You can use format flags to change the way an eBPF
 // program is stringified.
 func ExampleInstructions_Format() {
Repository: cilium/ebpf
Pull request #1004 (2023-04-07 16:54:36)

bpf2go: Fall back to default module name when debug.ReadBuildInfo is not available
Since https://github.com/cilium/ebpf/commit/223b7d740c167a41a7ba82ad59f81c7ef375b843, bpf2go reads the module name from `debug.ReadBuildInfo()`. Unfortunately this fails when `bpf2go` is built with bazel which doesn't add the buildinfo (there is an open issue for it), causing a failure with `determine current module: missing build info`.

This change sets a default if BuildInfo is not available.

florianl: Is https://github.com/bazelbuild/rules_go/issues/3090 the issue on the bazel side, that you are referring to?


folbricht: Yes, that's the one. There doesn't seem to be a solution for it yet.
lmb: I don't mind merging this to get bazel going again, but I want to be very clear that we don't support build systems other than the Go toolchain. Aka we won't check for breakage as you've discovered :)
folbricht: Not supporting bazel is definitely fair. I'm just trying to set a reasonable default so things still work for anyone stuck with a build system that doesn't provide BuildInfo. 

Patch:
diff --git a/cmd/bpf2go/output.go b/cmd/bpf2go/output.go
index 241fad8..51f1970 100644
--- a/cmd/bpf2go/output.go
+++ b/cmd/bpf2go/output.go
@@ -112,10 +112,7 @@ func output(args outputArgs) error {
 		return err
 	}
 
-	module, err := currentModule()
-	if err != nil {
-		return err
-	}
+	module := currentModule()
 
 	gf := &btf.GoFormatter{
 		Names:      typeNames,
diff --git a/cmd/bpf2go/tools.go b/cmd/bpf2go/tools.go
index 1a3936f..d2e020b 100644
--- a/cmd/bpf2go/tools.go
+++ b/cmd/bpf2go/tools.go
@@ -83,11 +83,12 @@ func toUpperFirst(str string) string {
 	return string(unicode.ToUpper(first)) + str[n:]
 }
 
-func currentModule() (string, error) {
+func currentModule() string {
 	bi, ok := debug.ReadBuildInfo()
 	if !ok {
-		return "", fmt.Errorf("determine current module: missing build info")
+		// Fall back to constant since bazel doesn't support BuildInfo.
+		return "github.com/cilium/ebpf"
 	}
 
-	return bi.Main.Path, nil
+	return bi.Main.Path
 }


Test patch:
diff --git a/cmd/bpf2go/main_test.go b/cmd/bpf2go/main_test.go
index a9e2ab6..f8fa637 100644
--- a/cmd/bpf2go/main_test.go
+++ b/cmd/bpf2go/main_test.go
@@ -49,10 +49,7 @@ func TestRun(t *testing.T) {
 		}
 	}
 
-	module, err := currentModule()
-	if err != nil {
-		t.Fatal(err)
-	}
+	module := currentModule()
 
 	execInModule("go", "mod", "init", "bpf2go-test")
Repository: cilium/ebpf
Pull request #1616 (2024-11-21 09:45:53)

variable: copy VariableSpec.Type during initial assignment and during copy
Before this patch, VariableSpec shared its Type with the underlying datasec, including any prior copies. This commit copies when the type is assigned from the Datasec to the VariableSpec, as well as when the VariableSpec is copied during CollectionSpec.Copy().


Patch:
diff --git a/elf_reader.go b/elf_reader.go
index 0c13617..d4fd592 100644
--- a/elf_reader.go
+++ b/elf_reader.go
@@ -1233,7 +1233,9 @@ func (ec *elfCode) loadDataSections() error {
 						return fmt.Errorf("data section %s: variable %s size in datasec (%d) doesn't match ELF symbol size (%d)", sec.Name, name, v.Size, ev.size)
 					}
 
-					ev.t = vt
+					// Decouple the Var in the VariableSpec from the underlying DataSec in
+					// the MapSpec to avoid modifications from affecting map loads later on.
+					ev.t = btf.Copy(vt).(*btf.Var)
 				}
 			}
 		}
diff --git a/variable.go b/variable.go
index 837cd5b..288b173 100644
--- a/variable.go
+++ b/variable.go
@@ -112,7 +112,9 @@ func (s *VariableSpec) copy(cpy *CollectionSpec) *VariableSpec {
 		name:   s.name,
 		offset: s.offset,
 		size:   s.size,
-		t:      s.t,
+	}
+	if s.t != nil {
+		out.t = btf.Copy(s.t).(*btf.Var)
 	}
 
 	// Attempt to find a MapSpec with the same name in the copied CollectionSpec.


Test patch:
diff --git a/variable_test.go b/variable_test.go
index 3cc0d2c..e854e9a 100644
--- a/variable_test.go
+++ b/variable_test.go
@@ -62,6 +62,14 @@ func TestVariableSpecCopy(t *testing.T) {
 	// Verify that the original underlying MapSpec was not modified.
 	zero := make([]byte, 4)
 	qt.Assert(t, qt.DeepEquals(spec.Maps[".rodata"].Contents[0].Value.([]byte), zero))
+
+	// Check that modifications to the VariableSpec's Type don't affect the
+	// underlying MapSpec's type information on either the original or the copy.
+	cpy.Variables["var_rodata"].Type().Name = "modified"
+	spec.Variables["var_rodata"].Type().Name = "modified"
+
+	qt.Assert(t, qt.Equals(cpy.Maps[".rodata"].Value.(*btf.Datasec).Vars[0].Type.(*btf.Var).Name, "var_rodata"))
+	qt.Assert(t, qt.Equals(spec.Maps[".rodata"].Value.(*btf.Datasec).Vars[0].Type.(*btf.Var).Name, "var_rodata"))
 }
 
 func mustReturn(tb testing.TB, prog *Program, value uint32) {