nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: tetratelabs/wazero
Pull request #739 (2022-08-08 17:01:42)

Fix fdSeek: result newoffset should be a uint64le

ncruces: Sorry, I had forgotten to `git add` the test.
ncruces: BTW, I need this for [dcraw](https://github.com/ncruces/dcraw/releases/tag/v9.28.3-wasm) (compiled with `zig cc --target=wasm32-wasi`), which is a nice filesystem stress test since it does a bunch of `ftell`/`fseek`s.

Was working fine with `wasmer` and `wasmtime`, with this it works fine on `wazero`.

Patch:
diff --git a/wasi_snapshot_preview1/fs.go b/wasi_snapshot_preview1/fs.go
index 4b4cfecf..5a1866df 100644
--- a/wasi_snapshot_preview1/fs.go
+++ b/wasi_snapshot_preview1/fs.go
@@ -500,7 +500,7 @@ var fdSeek = wasm.NewGoFunc(
 			return ErrnoIo
 		}
 
-		if !mod.Memory().WriteUint32Le(ctx, resultNewoffset, uint32(newOffset)) {
+		if !mod.Memory().WriteUint64Le(ctx, resultNewoffset, uint64(newOffset)) {
 			return ErrnoFault
 		}
 


Test patch:
diff --git a/wasi_snapshot_preview1/fs_test.go b/wasi_snapshot_preview1/fs_test.go
index 852c1447..fa15b9a4 100644
--- a/wasi_snapshot_preview1/fs_test.go
+++ b/wasi_snapshot_preview1/fs_test.go
@@ -657,8 +657,8 @@ func Test_fdSeek(t *testing.T) {
 			whence:         io.SeekStart,
 			expectedOffset: 4, // = offset
 			expectedMemory: []byte{
-				'?',        // resultNewoffset is after this
-				4, 0, 0, 0, // = expectedOffset
+				'?',                    // resultNewoffset is after this
+				4, 0, 0, 0, 0, 0, 0, 0, // = expectedOffset
 				'?',
 			},
 			expectedLog: `
@@ -674,8 +674,8 @@ func Test_fdSeek(t *testing.T) {
 			whence:         io.SeekCurrent,
 			expectedOffset: 2, // = 1 (the initial offset of the test file) + 1 (offset)
 			expectedMemory: []byte{
-				'?',        // resultNewoffset is after this
-				2, 0, 0, 0, // = expectedOffset
+				'?',                    // resultNewoffset is after this
+				2, 0, 0, 0, 0, 0, 0, 0, // = expectedOffset
 				'?',
 			},
 			expectedLog: `
@@ -691,8 +691,8 @@ func Test_fdSeek(t *testing.T) {
 			whence:         io.SeekEnd,
 			expectedOffset: 5, // = 6 (the size of the test file with content "wazero") + -1 (offset)
 			expectedMemory: []byte{
-				'?',        // resultNewoffset is after this
-				5, 0, 0, 0, // = expectedOffset
+				'?',                    // resultNewoffset is after this
+				5, 0, 0, 0, 0, 0, 0, 0, // = expectedOffset
 				'?',
 			},
 			expectedLog: `
Repository: tetratelabs/wazero
Pull request #752 (2022-08-19 03:01:40)

Accept nil ctx in NewRuntime
Similar to other methods taking `ctx`


Patch:
diff --git a/runtime.go b/runtime.go
index 80232a94..c8548711 100644
--- a/runtime.go
+++ b/runtime.go
@@ -127,6 +127,9 @@ func NewRuntime(ctx context.Context) Runtime {
 
 // NewRuntimeWithConfig returns a runtime with the given configuration.
 func NewRuntimeWithConfig(ctx context.Context, rConfig RuntimeConfig) Runtime {
+	if ctx == nil {
+		ctx = context.Background()
+	}
 	if v := ctx.Value(version.WazeroVersionKey{}); v == nil {
 		ctx = context.WithValue(ctx, version.WazeroVersionKey{}, wazeroVersion)
 	}


Test patch:
diff --git a/runtime_test.go b/runtime_test.go
index 22b41424..1d9b0ea9 100644
--- a/runtime_test.go
+++ b/runtime_test.go
@@ -22,16 +22,35 @@ var (
 )
 
 func TestNewRuntimeWithConfig_version(t *testing.T) {
-	cfg := NewRuntimeConfig().(*runtimeConfig)
-	oldNewEngine := cfg.newEngine
-	cfg.newEngine = func(ctx context.Context, features wasm.Features) wasm.Engine {
-		// Ensures that wazeroVersion is propagated to the engine.
-		v := ctx.Value(version.WazeroVersionKey{})
-		require.NotNil(t, v)
-		require.Equal(t, wazeroVersion, v.(string))
-		return oldNewEngine(ctx, features)
+	// Make sure nil ctx doesn't panic
+	tests := []struct {
+		name string
+		ctx  context.Context
+	}{
+		{
+			name: "not nil",
+			ctx:  testCtx,
+		},
+		{
+			name: "nil",
+			ctx:  nil,
+		},
+	}
+	for _, tc := range tests {
+		tt := tc
+		t.Run(tt.name, func(t *testing.T) {
+			cfg := NewRuntimeConfig().(*runtimeConfig)
+			oldNewEngine := cfg.newEngine
+			cfg.newEngine = func(ctx context.Context, features wasm.Features) wasm.Engine {
+				// Ensures that wazeroVersion is propagated to the engine.
+				v := ctx.Value(version.WazeroVersionKey{})
+				require.NotNil(t, v)
+				require.Equal(t, wazeroVersion, v.(string))
+				return oldNewEngine(ctx, features)
+			}
+			_ = NewRuntimeWithConfig(tt.ctx, cfg)
+		})
 	}
-	_ = NewRuntimeWithConfig(testCtx, cfg)
 }
 
 func TestRuntime_CompileModule(t *testing.T) {
Repository: tetratelabs/wazero
Pull request #772 (2022-08-29 01:40:43)

filecache: include GOOS/GOARCH in the key
This allows us to avoid crashes with `GOARCH=amd64 make test` after running `make test` on M1 Mac

Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>

codefromthecrypt: need go version? or are we ok
mathetake: no need go version as the assembly independent of it :D 

Patch:
diff --git a/internal/compilationcache/file_cache.go b/internal/compilationcache/file_cache.go
index fc7e0116..6811d886 100644
--- a/internal/compilationcache/file_cache.go
+++ b/internal/compilationcache/file_cache.go
@@ -7,6 +7,7 @@ import (
 	"io"
 	"os"
 	"path"
+	"runtime"
 	"sync"
 )
 
@@ -40,7 +41,7 @@ type fileReadCloser struct {
 }
 
 func (fc *fileCache) path(key Key) string {
-	return path.Join(fc.dirPath, hex.EncodeToString(key[:]))
+	return path.Join(fc.dirPath, runtime.GOARCH+"-"+runtime.GOOS+"-"+hex.EncodeToString(key[:]))
 }
 
 func (fc *fileCache) Get(key Key) (content io.ReadCloser, ok bool, err error) {


Test patch:
diff --git a/internal/compilationcache/file_cache_test.go b/internal/compilationcache/file_cache_test.go
index 84e508ea..c9cdd4bf 100644
--- a/internal/compilationcache/file_cache_test.go
+++ b/internal/compilationcache/file_cache_test.go
@@ -2,8 +2,11 @@ package compilationcache
 
 import (
 	"bytes"
+	"fmt"
 	"io"
 	"os"
+	"runtime"
+	"strings"
 	"testing"
 
 	"github.com/tetratelabs/wazero/internal/testing/require"
@@ -133,3 +136,11 @@ func TestFileCache_Get(t *testing.T) {
 		require.False(t, ok)
 	})
 }
+
+func TestFileCache_path(t *testing.T) {
+	fc := &fileCache{dirPath: "/tmp/.wazero"}
+	actual := fc.path(Key{1, 2, 3, 4, 5})
+	require.Contains(t, actual, fmt.Sprintf("%s-%s-", runtime.GOARCH, runtime.GOOS))
+	require.True(t, strings.HasPrefix(actual, fc.dirPath))
+	require.True(t, strings.HasSuffix(actual, "0102030405000000000000000000000000000000000000000000000000000000"))
+}
Repository: tetratelabs/wazero
Pull request #850 (2022-11-09 07:12:31)

Adds more context for ADR offset overrun
This is mainly so we can tell if changing this will work. e.g. if 20 bits are enough for #847 

codefromthecrypt: I'm guessing an offset >20bits will be near impossible, but yeah just curious.

Patch:
diff --git a/internal/asm/arm64/impl.go b/internal/asm/arm64/impl.go
index a67f4466..bf5c102b 100644
--- a/internal/asm/arm64/impl.go
+++ b/internal/asm/arm64/impl.go
@@ -1838,7 +1838,7 @@ func (a *AssemblerImpl) encodeADR(n *nodeImpl) (err error) {
 		if offset > math.MaxUint8 {
 			// We could support up to 20-bit integer, but byte should be enough for our impl.
 			// If the necessity comes up, we could fix the below to support larger offsets.
-			return fmt.Errorf("BUG: too large offset for ADR")
+			return fmt.Errorf("BUG: too large offset for ADR: %d", offset)
 		}
 
 		// Now ready to write an offset byte.


Test patch:
diff --git a/internal/asm/arm64/impl_4_test.go b/internal/asm/arm64/impl_4_test.go
index a4c16252..e4a60915 100644
--- a/internal/asm/arm64/impl_4_test.go
+++ b/internal/asm/arm64/impl_4_test.go
@@ -702,6 +702,6 @@ func TestAssemblerImpl_encodeReadInstructionAddress(t *testing.T) {
 		targetNode.offsetInBinaryField = uint64(math.MaxInt64)
 
 		err := cb(nil)
-		require.EqualError(t, err, "BUG: too large offset for ADR")
+		require.EqualError(t, err, "BUG: too large offset for ADR: 9223372036854775807")
 	})
 }
Repository: tetratelabs/wazero
Pull request #852 (2022-11-11 23:20:20)

Optimize setting the ModuleInstance.DataInstances
Signed-off-by: Clifton Kaznocha <ckaznocha@users.noreply.github.com>


Patch:
diff --git a/internal/wasm/store.go b/internal/wasm/store.go
index ed70b4ba..233e99d9 100644
--- a/internal/wasm/store.go
+++ b/internal/wasm/store.go
@@ -181,13 +181,6 @@ func (m *ModuleInstance) addSections(module *Module, importedFunctions, function
 	}
 
 	m.BuildExports(module.ExportSection)
-	m.buildDataInstances(module.DataSection)
-}
-
-func (m *ModuleInstance) buildDataInstances(segments []*DataSegment) {
-	for _, d := range segments {
-		m.DataInstances = append(m.DataInstances, d.Init)
-	}
 }
 
 func (m *ModuleInstance) buildElementInstances(elements []*ElementSegment) {
@@ -237,11 +230,13 @@ func (m *ModuleInstance) validateData(data []*DataSegment) (err error) {
 	return
 }
 
-// applyData uses the given data segments and mutate the memory according to the initial contents on it.
-// This is called after all the validation phase passes and out of bounds memory access error here is
-// not a validation error, but rather a runtime error.
+// applyData uses the given data segments and mutate the memory according to the initial contents on it
+// and populate the `DataInstances`. This is called after all the validation phase passes and out of
+// bounds memory access error here is not a validation error, but rather a runtime error.
 func (m *ModuleInstance) applyData(data []*DataSegment) error {
+	m.DataInstances = make([][]byte, len(data))
 	for i, d := range data {
+		m.DataInstances[i] = d.Init
 		if !d.IsPassive() {
 			offset := executeConstExpression(m.Globals, d.OffsetExpression).(int32)
 			if offset < 0 || int(offset)+len(d.Init) > len(m.Memory.Buffer) {


Test patch:
diff --git a/internal/wasm/store_test.go b/internal/wasm/store_test.go
index 48296f34..00d5420e 100644
--- a/internal/wasm/store_test.go
+++ b/internal/wasm/store_test.go
@@ -788,6 +788,7 @@ func TestModuleInstance_applyData(t *testing.T) {
 		})
 		require.NoError(t, err)
 		require.Equal(t, []byte{0xa, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x5}, m.Memory.Buffer)
+		require.Equal(t, [][]byte{{0xa, 0xf}, {0x1, 0x5}}, m.DataInstances)
 	})
 	t.Run("error", func(t *testing.T) {
 		m := &ModuleInstance{Memory: &MemoryInstance{Buffer: make([]byte, 5)}}
Repository: tetratelabs/wazero
Pull request #883 (2022-12-03 06:57:35)

validation: not panic with redundant Else instrs
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>


Patch:
diff --git a/internal/wasm/func_validation.go b/internal/wasm/func_validation.go
index 4f1c3307..49188673 100644
--- a/internal/wasm/func_validation.go
+++ b/internal/wasm/func_validation.go
@@ -1451,6 +1451,9 @@ func (m *Module) validateFunctionWithMaxStackValues(
 			valueTypeStack.pushStackLimit(len(bt.Params))
 			pc += num
 		} else if op == OpcodeElse {
+			if len(controlBlockStack) == 0 {
+				return fmt.Errorf("redundant Else instruction at %#x", pc)
+			}
 			bl := controlBlockStack[len(controlBlockStack)-1]
 			bl.elseAt = pc
 			// Check the type soundness of the instructions *before* entering this else Op.


Test patch:
diff --git a/internal/wasm/func_validation_test.go b/internal/wasm/func_validation_test.go
index 59564e7d..07f7e1f6 100644
--- a/internal/wasm/func_validation_test.go
+++ b/internal/wasm/func_validation_test.go
@@ -3553,3 +3553,14 @@ func TestFunctionValidation_redundantEnd(t *testing.T) {
 	err := m.validateFunction(api.CoreFeaturesV2, 0, nil, nil, nil, nil, nil)
 	require.EqualError(t, err, "redundant End instruction at 0x1")
 }
+
+// TestFunctionValidation_redundantEnd is found in th validation fuzzing.
+func TestFunctionValidation_redundantElse(t *testing.T) {
+	m := &Module{
+		TypeSection:     []*FunctionType{{}},
+		FunctionSection: []Index{0},
+		CodeSection:     []*Code{{Body: []byte{OpcodeEnd, OpcodeElse}}},
+	}
+	err := m.validateFunction(api.CoreFeaturesV2, 0, nil, nil, nil, nil, nil)
+	require.EqualError(t, err, "redundant Else instruction at 0x1")
+}
Repository: tetratelabs/wazero
Pull request #932 (2022-12-16 01:40:34)

Cache FunctionType key in the binary pkg
following suggestion from #930 


Patch:
diff --git a/internal/wasm/binary/function.go b/internal/wasm/binary/function.go
index fdf80201..56da539a 100644
--- a/internal/wasm/binary/function.go
+++ b/internal/wasm/binary/function.go
@@ -97,5 +97,9 @@ func decodeFunctionType(enabledFeatures api.CoreFeatures, r *bytes.Reader) (*was
 		Params:  paramTypes,
 		Results: resultTypes,
 	}
+
+	// cache the key for the function type
+	_ = ret.String()
+
 	return ret, nil
 }


Test patch:
diff --git a/internal/wasm/binary/decoder_test.go b/internal/wasm/binary/decoder_test.go
index c4051e87..20e346bd 100644
--- a/internal/wasm/binary/decoder_test.go
+++ b/internal/wasm/binary/decoder_test.go
@@ -84,6 +84,10 @@ func TestDecodeModule(t *testing.T) {
 		t.Run(tc.name, func(t *testing.T) {
 			m, e := DecodeModule(EncodeModule(tc.input), api.CoreFeaturesV1, wasm.MemoryLimitPages, false, false, false)
 			require.NoError(t, e)
+			// Set the FunctionType keys on the input.
+			for _, f := range tc.input.TypeSection {
+				_ = f.String()
+			}
 			require.Equal(t, tc.input, m)
 		})
 	}
diff --git a/internal/wasm/binary/function_test.go b/internal/wasm/binary/function_test.go
index 46aad1a5..d73fa1c0 100644
--- a/internal/wasm/binary/function_test.go
+++ b/internal/wasm/binary/function_test.go
@@ -85,6 +85,8 @@ func TestFunctionType(t *testing.T) {
 		t.Run(fmt.Sprintf("decode - %s", tc.name), func(t *testing.T) {
 			binary, err := decodeFunctionType(api.CoreFeaturesV2, bytes.NewReader(b))
 			require.NoError(t, err)
+			// Set the FunctionType key on the input.
+			_ = tc.input.String()
 			require.Equal(t, binary, tc.input)
 		})
 	}
Repository: tetratelabs/wazero
Pull request #933 (2022-12-16 01:51:06)

Fixes unnecessary allocation in decodeValueTypes
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>


Patch:
diff --git a/internal/wasm/binary/function.go b/internal/wasm/binary/function.go
index fdf80201..56da539a 100644
--- a/internal/wasm/binary/function.go
+++ b/internal/wasm/binary/function.go
@@ -97,5 +97,9 @@ func decodeFunctionType(enabledFeatures api.CoreFeatures, r *bytes.Reader) (*was
 		Params:  paramTypes,
 		Results: resultTypes,
 	}
+
+	// cache the key for the function type
+	_ = ret.String()
+
 	return ret, nil
 }
diff --git a/internal/wasm/binary/value.go b/internal/wasm/binary/value.go
index 123db09b..6a305472 100644
--- a/internal/wasm/binary/value.go
+++ b/internal/wasm/binary/value.go
@@ -49,18 +49,17 @@ func decodeValueTypes(r *bytes.Reader, num uint32) ([]wasm.ValueType, error) {
 	if num == 0 {
 		return nil, nil
 	}
+
 	ret := make([]wasm.ValueType, num)
-	buf := make([]wasm.ValueType, num)
-	_, err := io.ReadFull(r, buf)
+	_, err := io.ReadFull(r, ret)
 	if err != nil {
 		return nil, err
 	}
 
-	for i, v := range buf {
+	for _, v := range ret {
 		switch v {
 		case wasm.ValueTypeI32, wasm.ValueTypeF32, wasm.ValueTypeI64, wasm.ValueTypeF64,
 			wasm.ValueTypeExternref, wasm.ValueTypeFuncref, wasm.ValueTypeV128:
-			ret[i] = v
 		default:
 			return nil, fmt.Errorf("invalid value type: %d", v)
 		}


Test patch:
diff --git a/internal/wasm/binary/decoder_test.go b/internal/wasm/binary/decoder_test.go
index c4051e87..20e346bd 100644
--- a/internal/wasm/binary/decoder_test.go
+++ b/internal/wasm/binary/decoder_test.go
@@ -84,6 +84,10 @@ func TestDecodeModule(t *testing.T) {
 		t.Run(tc.name, func(t *testing.T) {
 			m, e := DecodeModule(EncodeModule(tc.input), api.CoreFeaturesV1, wasm.MemoryLimitPages, false, false, false)
 			require.NoError(t, e)
+			// Set the FunctionType keys on the input.
+			for _, f := range tc.input.TypeSection {
+				_ = f.String()
+			}
 			require.Equal(t, tc.input, m)
 		})
 	}
diff --git a/internal/wasm/binary/function_test.go b/internal/wasm/binary/function_test.go
index 46aad1a5..d73fa1c0 100644
--- a/internal/wasm/binary/function_test.go
+++ b/internal/wasm/binary/function_test.go
@@ -85,6 +85,8 @@ func TestFunctionType(t *testing.T) {
 		t.Run(fmt.Sprintf("decode - %s", tc.name), func(t *testing.T) {
 			binary, err := decodeFunctionType(api.CoreFeaturesV2, bytes.NewReader(b))
 			require.NoError(t, err)
+			// Set the FunctionType key on the input.
+			_ = tc.input.String()
 			require.Equal(t, binary, tc.input)
 		})
 	}
Repository: tetratelabs/wazero
Pull request #961 (2022-12-26 06:57:17)

asm(arm64): adds UDF instruction
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>


Patch:
diff --git a/internal/asm/arm64/consts.go b/internal/asm/arm64/consts.go
index 343b013d..9df4d5f6 100644
--- a/internal/asm/arm64/consts.go
+++ b/internal/asm/arm64/consts.go
@@ -929,6 +929,9 @@ const (
 	// FCVTN is the FCVTN instruction https://developer.arm.com/documentation/ddi0596/2021-12/SIMD-FP-Instructions/FCVTN--FCVTN2--Floating-point-Convert-to-lower-precision-Narrow--vector--?lang=en
 	FCVTN
 
+	// UDF is the UDF instruction https://developer.arm.com/documentation/ddi0596/2021-12/Base-Instructions/UDF--Permanently-Undefined-?lang=en
+	UDF
+
 	// instructionEnd is always placed at the bottom of this iota definition to be used in the test.
 	instructionEnd
 )
@@ -1462,6 +1465,8 @@ func InstructionName(i asm.Instruction) string {
 		return "FLDRD"
 	case FLDRS:
 		return "FLDRS"
+	case UDF:
+		return "UDF"
 	}
 	panic(fmt.Errorf("unknown instruction %d", i))
 }
diff --git a/internal/asm/arm64/impl.go b/internal/asm/arm64/impl.go
index bbf0063c..87c9f34d 100644
--- a/internal/asm/arm64/impl.go
+++ b/internal/asm/arm64/impl.go
@@ -757,7 +757,11 @@ func errorEncodingUnsupported(n *nodeImpl) error {
 }
 
 func (a *AssemblerImpl) encodeNoneToNone(n *nodeImpl) (err error) {
-	if n.instruction != NOP {
+	switch n.instruction {
+	case UDF:
+		a.buf.Write([]byte{0, 0, 0, 0})
+	case NOP:
+	default:
 		err = errorEncodingUnsupported(n)
 	}
 	return


Test patch:
diff --git a/internal/asm/arm64/impl_1_test.go b/internal/asm/arm64/impl_1_test.go
index 1cfe437e..3f364e4b 100644
--- a/internal/asm/arm64/impl_1_test.go
+++ b/internal/asm/arm64/impl_1_test.go
@@ -682,7 +682,7 @@ func TestAssemblerImpl_encodeNoneToNone(t *testing.T) {
 		err := a.encodeNoneToNone(&nodeImpl{instruction: ADD})
 		require.EqualError(t, err, "ADD is unsupported for from:none,to:none type")
 	})
-	t.Run("ok", func(t *testing.T) {
+	t.Run("NOP", func(t *testing.T) {
 		a := NewAssembler(asm.NilRegister)
 		err := a.encodeNoneToNone(&nodeImpl{instruction: NOP})
 		require.NoError(t, err)
@@ -691,6 +691,14 @@ func TestAssemblerImpl_encodeNoneToNone(t *testing.T) {
 		actual := a.buf.Bytes()
 		require.Zero(t, len(actual))
 	})
+	t.Run("UDF", func(t *testing.T) {
+		a := NewAssembler(asm.NilRegister)
+		err := a.encodeNoneToNone(&nodeImpl{instruction: UDF})
+		require.NoError(t, err)
+
+		actual := a.buf.Bytes()
+		require.Equal(t, []byte{0x0, 0x0, 0x0, 0x0}, actual, hex.EncodeToString(actual))
+	})
 }
 
 func Test_validateMemoryOffset(t *testing.T) {