nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: tetratelabs/wazero
Pull request #1837 (2023-11-15 00:03:46)

wazevo(arm64): fixes lowerSelectVec
#1496 


Patch:
diff --git a/internal/engine/wazevo/backend/isa/arm64/lower_instr.go b/internal/engine/wazevo/backend/isa/arm64/lower_instr.go
index 987d358f..b29165f0 100644
--- a/internal/engine/wazevo/backend/isa/arm64/lower_instr.go
+++ b/internal/engine/wazevo/backend/isa/arm64/lower_instr.go
@@ -1906,18 +1906,22 @@ func (m *machine) lowerSelect(c, x, y, result ssa.Value) {
 }
 
 func (m *machine) lowerSelectVec(rc, rn, rm, rd operand) {
-	// First we clear the unnecessary bits of rc by ANDing it with 1.
-	one := m.compiler.AllocateVReg(ssa.TypeI32)
-	m.lowerConstantI32(one, 1)
+	// First, we copy the condition to a temporary register in case rc is used somewhere else.
+	tmp := m.compiler.AllocateVReg(ssa.TypeI32)
+	mov := m.allocateInstr()
+	mov.asFpuMov128(tmp, rc.nr())
+	m.insert(mov)
+
+	// Next is to clear the unnecessary bits of rc by ANDing it with 1, and store it to a temporary register.
+	oneOrZero := m.compiler.AllocateVReg(ssa.TypeI32)
 	and := m.allocateInstr()
-	oneOrZero := operandNR(m.compiler.AllocateVReg(ssa.TypeI32))
-	and.asALU(aluOpAnd, oneOrZero, rc, operandNR(one), false)
+	and.asALUBitmaskImm(aluOpAnd, oneOrZero, tmp, 1, false)
 	m.insert(and)
 
 	// Sets all bits to 1 if rc is not zero.
 	allOneOrZero := operandNR(m.compiler.AllocateVReg(ssa.TypeI64))
 	alu := m.allocateInstr()
-	alu.asALU(aluOpSub, allOneOrZero, operandNR(xzrVReg), oneOrZero, true)
+	alu.asALU(aluOpSub, allOneOrZero, operandNR(xzrVReg), operandNR(oneOrZero), true)
 	m.insert(alu)
 
 	// Then move the bits to the result vector register.


Test patch:
diff --git a/internal/engine/wazevo/backend/isa/arm64/lower_instr_test.go b/internal/engine/wazevo/backend/isa/arm64/lower_instr_test.go
index 63c49e78..c2f3d26b 100644
--- a/internal/engine/wazevo/backend/isa/arm64/lower_instr_test.go
+++ b/internal/engine/wazevo/backend/isa/arm64/lower_instr_test.go
@@ -853,8 +853,8 @@ func TestMachine_lowerSelectVec(t *testing.T) {
 
 	m.lowerSelectVec(c, rn, rm, rd)
 	require.Equal(t, `
-orr w5?, wzr, #0x1
-and w6?, w1?, w5?
+mov v5?.16b, v1?.16b
+and w6?, w5?, #0x1
 sub x7?, xzr, x6?
 dup v4?.2d, x7?
 bsl v4?.16b, v2?.16b, v3?.16b
Repository: tetratelabs/wazero
Pull request #1839 (2023-11-15 06:26:02)

wazevo(arm64): fixes Vector select
#1496 


Patch:
diff --git a/internal/engine/wazevo/backend/isa/arm64/lower_instr.go b/internal/engine/wazevo/backend/isa/arm64/lower_instr.go
index b29165f0..9d335cb7 100644
--- a/internal/engine/wazevo/backend/isa/arm64/lower_instr.go
+++ b/internal/engine/wazevo/backend/isa/arm64/lower_instr.go
@@ -1909,7 +1909,7 @@ func (m *machine) lowerSelectVec(rc, rn, rm, rd operand) {
 	// First, we copy the condition to a temporary register in case rc is used somewhere else.
 	tmp := m.compiler.AllocateVReg(ssa.TypeI32)
 	mov := m.allocateInstr()
-	mov.asFpuMov128(tmp, rc.nr())
+	mov.asMove32(tmp, rc.nr())
 	m.insert(mov)
 
 	// Next is to clear the unnecessary bits of rc by ANDing it with 1, and store it to a temporary register.


Test patch:
diff --git a/internal/engine/wazevo/backend/isa/arm64/lower_instr_test.go b/internal/engine/wazevo/backend/isa/arm64/lower_instr_test.go
index c2f3d26b..aa43c75c 100644
--- a/internal/engine/wazevo/backend/isa/arm64/lower_instr_test.go
+++ b/internal/engine/wazevo/backend/isa/arm64/lower_instr_test.go
@@ -853,7 +853,7 @@ func TestMachine_lowerSelectVec(t *testing.T) {
 
 	m.lowerSelectVec(c, rn, rm, rd)
 	require.Equal(t, `
-mov v5?.16b, v1?.16b
+mov w5?, w1?
 and w6?, w5?, #0x1
 sub x7?, xzr, x6?
 dup v4?.2d, x7?
Repository: tetratelabs/wazero
Pull request #1863 (2023-12-12 04:47:45)

Fix `fd_fdstat_set_flags` truncating with O_TRUNC
This commit fixes a buf when `fd_fdstat_set_flags` would erroneously truncate an open file if it is opened with the O_TRUNC flag.  This happens because the O_TRUNC flag causes the file to be truncated when it is opened.

This fixes https://github.com/tetratelabs/wazero/issues/1862


Patch:
diff --git a/internal/sysfs/osfile.go b/internal/sysfs/osfile.go
index 13bd418f..cecab8cb 100644
--- a/internal/sysfs/osfile.go
+++ b/internal/sysfs/osfile.go
@@ -83,8 +83,8 @@ func (f *osFile) SetAppend(enable bool) (errno experimentalsys.Errno) {
 		f.flag &= ^experimentalsys.O_APPEND
 	}
 
-	// Clear any create flag, as we are re-opening, not re-creating.
-	f.flag &= ^experimentalsys.O_CREAT
+	// Clear any create or trunc flag, as we are re-opening, not re-creating.
+	f.flag &= ^(experimentalsys.O_CREAT | experimentalsys.O_TRUNC)
 
 	// appendMode (bool) cannot be changed later, so we have to re-open the
 	// file. https://github.com/golang/go/blob/go1.20/src/os/file_unix.go#L60


Test patch:
diff --git a/imports/wasi_snapshot_preview1/fs_test.go b/imports/wasi_snapshot_preview1/fs_test.go
index fc23e897..c17c6d9d 100644
--- a/imports/wasi_snapshot_preview1/fs_test.go
+++ b/imports/wasi_snapshot_preview1/fs_test.go
@@ -480,6 +480,42 @@ func Test_fdFdstatGet_StdioNonblock(t *testing.T) {
 	}
 }
 
+func Test_fdFdstatSetFlagsWithTrunc(t *testing.T) {
+	tmpDir := t.TempDir()
+	fileName := "test"
+
+	mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().
+		WithFSConfig(wazero.NewFSConfig().WithDirMount(tmpDir, "/")))
+	defer r.Close(testCtx)
+
+	fsc := mod.(*wasm.ModuleInstance).Sys.FS()
+	preopen := fsc.RootFS()
+
+	fd, errno := fsc.OpenFile(preopen, fileName, experimentalsys.O_RDWR|experimentalsys.O_CREAT|experimentalsys.O_EXCL|experimentalsys.O_TRUNC, 0o600)
+	require.EqualErrno(t, 0, errno)
+
+	// Write the initial text to the file.
+	f, ok := fsc.LookupFile(fd)
+	require.True(t, ok)
+	n, _ := f.File.Write([]byte("abc"))
+	require.Equal(t, n, 3)
+
+	buf, err := os.ReadFile(joinPath(tmpDir, fileName))
+	require.NoError(t, err)
+	require.Equal(t, "abc", string(buf))
+
+	requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdFdstatSetFlagsName, uint64(fd), uint64(0))
+	require.Equal(t, `
+==> wasi_snapshot_preview1.fd_fdstat_set_flags(fd=4,flags=)
+<== errno=ESUCCESS
+`, "\n"+log.String())
+	log.Reset()
+
+	buf, err = os.ReadFile(joinPath(tmpDir, fileName))
+	require.NoError(t, err)
+	require.Equal(t, "abc", string(buf))
+}
+
 func Test_fdFdstatSetFlags(t *testing.T) {
 	tmpDir := t.TempDir() // open before loop to ensure no locking problems.
Repository: tetratelabs/wazero
Pull request #1400 (2023-04-24 23:38:11)

logging: correctly omits result.newoffset in Before


Patch:
diff --git a/internal/wasip1/logging/logging.go b/internal/wasip1/logging/logging.go
index 01c62970..86a7f0a0 100644
--- a/internal/wasip1/logging/logging.go
+++ b/internal/wasip1/logging/logging.go
@@ -157,6 +157,7 @@ func Config(fnd api.FunctionDefinition) (pSampler logging.ParamSampler, pLoggers
 			name = resultParamName(name)
 			logger = logMemI64(idx).Log
 			rLoggers = append(rLoggers, resultParamLogger(name, logger))
+			continue
 		case "result.filestat":
 			name = resultParamName(name)
 			logger = logFilestat(idx).Log


Test patch:
diff --git a/imports/wasi_snapshot_preview1/fs_test.go b/imports/wasi_snapshot_preview1/fs_test.go
index 1862cb39..46990609 100644
--- a/imports/wasi_snapshot_preview1/fs_test.go
+++ b/imports/wasi_snapshot_preview1/fs_test.go
@@ -2519,7 +2519,7 @@ func Test_fdSeek(t *testing.T) {
 				'?',
 			},
 			expectedLog: `
-==> wasi_snapshot_preview1.fd_seek(fd=4,offset=4,whence=0,4557430888798830399)
+==> wasi_snapshot_preview1.fd_seek(fd=4,offset=4,whence=0)
 <== (newoffset=4,errno=ESUCCESS)
 `,
 		},
@@ -2534,7 +2534,7 @@ func Test_fdSeek(t *testing.T) {
 				'?',
 			},
 			expectedLog: `
-==> wasi_snapshot_preview1.fd_seek(fd=4,offset=1,whence=1,4557430888798830399)
+==> wasi_snapshot_preview1.fd_seek(fd=4,offset=1,whence=1)
 <== (newoffset=2,errno=ESUCCESS)
 `,
 		},
@@ -2549,7 +2549,7 @@ func Test_fdSeek(t *testing.T) {
 				'?',
 			},
 			expectedLog: `
-==> wasi_snapshot_preview1.fd_seek(fd=4,offset=-1,whence=2,4557430888798830399)
+==> wasi_snapshot_preview1.fd_seek(fd=4,offset=-1,whence=2)
 <== (newoffset=5,errno=ESUCCESS)
 `,
 		},
@@ -2610,7 +2610,7 @@ func Test_fdSeek_Errors(t *testing.T) {
 			fd:            42, // arbitrary invalid fd
 			expectedErrno: wasip1.ErrnoBadf,
 			expectedLog: `
-==> wasi_snapshot_preview1.fd_seek(fd=42,offset=0,whence=0,0)
+==> wasi_snapshot_preview1.fd_seek(fd=42,offset=0,whence=0)
 <== (newoffset=,errno=EBADF)
 `,
 		},
@@ -2620,7 +2620,7 @@ func Test_fdSeek_Errors(t *testing.T) {
 			whence:        3, // invalid whence, the largest whence io.SeekEnd(2) + 1
 			expectedErrno: wasip1.ErrnoInval,
 			expectedLog: `
-==> wasi_snapshot_preview1.fd_seek(fd=4,offset=0,whence=3,0)
+==> wasi_snapshot_preview1.fd_seek(fd=4,offset=0,whence=3)
 <== (newoffset=,errno=EINVAL)
 `,
 		},
@@ -2629,7 +2629,7 @@ func Test_fdSeek_Errors(t *testing.T) {
 			fd:            dirFD,
 			expectedErrno: wasip1.ErrnoBadf,
 			expectedLog: `
-==> wasi_snapshot_preview1.fd_seek(fd=5,offset=0,whence=0,0)
+==> wasi_snapshot_preview1.fd_seek(fd=5,offset=0,whence=0)
 <== (newoffset=,errno=EBADF)
 `,
 		},
@@ -2639,7 +2639,7 @@ func Test_fdSeek_Errors(t *testing.T) {
 			resultNewoffset: memorySize,
 			expectedErrno:   wasip1.ErrnoFault,
 			expectedLog: `
-==> wasi_snapshot_preview1.fd_seek(fd=4,offset=0,whence=0,)
+==> wasi_snapshot_preview1.fd_seek(fd=4,offset=0,whence=0)
 <== (newoffset=,errno=EFAULT)
 `,
 		},
Repository: tetratelabs/wazero
Pull request #1402 (2023-04-25 06:24:16)

wasi: fixes bug where zero length iovec breaks fd_read
We should tolerate noop input vs break. This fixes where a zero length iovec would short circuit latter ones.

Thanks again to our bug piper @jerbob92!


Patch:
diff --git a/imports/wasi_snapshot_preview1/fs.go b/imports/wasi_snapshot_preview1/fs.go
index 70b60c9f..7bfc30a8 100644
--- a/imports/wasi_snapshot_preview1/fs.go
+++ b/imports/wasi_snapshot_preview1/fs.go
@@ -708,6 +708,10 @@ func fdReadOrPread(mod api.Module, params []uint64, isPread bool) syscall.Errno
 		offset := le.Uint32(iovsBuf[iovsPos:])
 		l := le.Uint32(iovsBuf[iovsPos+4:])
 
+		if l == 0 { // A zero length iovec could be ahead of another.
+			continue
+		}
+
 		b, ok := mem.Read(offset, l)
 		if !ok {
 			return syscall.EFAULT


Test patch:
diff --git a/imports/wasi_snapshot_preview1/fs_test.go b/imports/wasi_snapshot_preview1/fs_test.go
index 46990609..d294d558 100644
--- a/imports/wasi_snapshot_preview1/fs_test.go
+++ b/imports/wasi_snapshot_preview1/fs_test.go
@@ -1662,19 +1662,21 @@ func Test_fdRead(t *testing.T) {
 	iovs := uint32(1) // arbitrary offset
 	initialMemory := []byte{
 		'?',         // `iovs` is after this
-		18, 0, 0, 0, // = iovs[0].offset
+		26, 0, 0, 0, // = iovs[0].offset
 		4, 0, 0, 0, // = iovs[0].length
-		23, 0, 0, 0, // = iovs[1].offset
-		2, 0, 0, 0, // = iovs[1].length
+		31, 0, 0, 0, // = iovs[1].offset
+		0, 0, 0, 0, // = iovs[1].length == 0 !!
+		31, 0, 0, 0, // = iovs[2].offset
+		2, 0, 0, 0, // = iovs[2].length
 		'?',
 	}
-	iovsCount := uint32(2)    // The count of iovs
-	resultNread := uint32(26) // arbitrary offset
+	iovsCount := uint32(3)    // The count of iovs
+	resultNread := uint32(34) // arbitrary offset
 	expectedMemory := append(
 		initialMemory,
 		'w', 'a', 'z', 'e', // iovs[0].length bytes
-		'?',      // iovs[1].offset is after this
-		'r', 'o', // iovs[1].length bytes
+		'?',      // iovs[2].offset is after this
+		'r', 'o', // iovs[2].length bytes
 		'?',        // resultNread is after this
 		6, 0, 0, 0, // sum(iovs[...].length) == length of "wazero"
 		'?',
@@ -1687,7 +1689,7 @@ func Test_fdRead(t *testing.T) {
 
 	requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdReadName, uint64(fd), uint64(iovs), uint64(iovsCount), uint64(resultNread))
 	require.Equal(t, `
-==> wasi_snapshot_preview1.fd_read(fd=4,iovs=1,iovs_len=2)
+==> wasi_snapshot_preview1.fd_read(fd=4,iovs=1,iovs_len=3)
 <== (nread=6,errno=ESUCCESS)
 `, "\n"+log.String())