nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: tetratelabs/wazero
Pull request #485 (2022-04-20 01:10:33)

Fix validate start section with imports
Previously, when searching the signature of imported function start section,
we haven't skipped non-function imports for indexes.

Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>

mathetake: ps this is the first bug I found via #481 
mathetake: ```
(module
  (func $g (import "env" "foo"))
  (global $x (import "env" "glob") i32)
  (func $f (import "env" "bar"))
  (start $f)
)

```

This is the valid module (wasm-tools validate succeeds), but our validation fails.

Patch:
diff --git a/internal/wasm/module.go b/internal/wasm/module.go
index f350d61c..3d8a6931 100644
--- a/internal/wasm/module.go
+++ b/internal/wasm/module.go
@@ -191,9 +191,9 @@ func (m *Module) TypeOfFunction(funcIdx Index) *FunctionType {
 		return nil
 	}
 	funcImportCount := Index(0)
-	for i, im := range m.ImportSection {
+	for _, im := range m.ImportSection {
 		if im.Type == ExternTypeFunc {
-			if funcIdx == Index(i) {
+			if funcIdx == funcImportCount {
 				if im.DescFunc >= typeSectionLength {
 					return nil
 				}


Test patch:
diff --git a/internal/wasm/module_test.go b/internal/wasm/module_test.go
index 196f823e..0bd3e33c 100644
--- a/internal/wasm/module_test.go
+++ b/internal/wasm/module_test.go
@@ -331,6 +331,21 @@ func TestModule_validateStartSection(t *testing.T) {
 			})
 		}
 	})
+	t.Run("imported valid func", func(t *testing.T) {
+		index := Index(1)
+		m := Module{
+			StartSection: &index,
+			TypeSection:  []*FunctionType{{}, {Results: []ValueType{ValueTypeI32}}},
+			ImportSection: []*Import{
+				{Type: ExternTypeFunc, DescFunc: 1},
+				// import with index 1 is global but this should be skipped when searching imported functions.
+				{Type: ExternTypeGlobal},
+				{Type: ExternTypeFunc, DescFunc: 0}, // This one must be selected.
+			},
+		}
+		err := m.validateStartSection()
+		require.NoError(t, err)
+	})
 }
 
 func TestModule_validateGlobals(t *testing.T) {