Skip to content

Commit

Permalink
Add reproducer for Code-Hex#131
Browse files Browse the repository at this point in the history
Using `make test/run TARGET=TestRunIssue131`, I can reproduce Code-Hex#131 reliably
on my M1:

Run socat as vsock ssh proxyserver (port=2222)

Login with root and no password.

localhost login: [    3.053740] random: crng init done
finalizing handler 0x140000a2260 value: 0x100a3b020
panic: runtime/cgo: misuse of an invalid Handle

goroutine 17 [running, locked to thread]:
runtime/cgo.Handle.Value(...)
        /usr/local/go/src/runtime/cgo/handle.go:124
github.com/Code-Hex/vz/v3.shouldAcceptNewConnectionHandler(0x14000102000?, 0x0?, 0x14000000001?)
        /Users/teuf/dev/vz/socket.go:235 +0x11c
2023/07/07 13:49:05 exit status 2
exit status 1
FAIL    github.com/Code-Hex/vz/v3       5.220s
testing: warning: no tests to run
PASS
ok      github.com/Code-Hex/vz/v3/internal/progress     0.351s [no tests to run]
FAIL
make: *** [test/run] Error 1
  • Loading branch information
cfergeau committed Jul 7, 2023
1 parent fd31630 commit 2939bdc
Showing 1 changed file with 75 additions and 5 deletions.
80 changes: 75 additions & 5 deletions virtualization_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package vz_test

import (
"bytes"
"errors"
"fmt"
"io"
"os"
"runtime"
"syscall"
Expand Down Expand Up @@ -111,6 +113,14 @@ func (c *Container) NewSession(t *testing.T) *ssh.Session {
return sshSession
}

func getSocketDevice(t *testing.T, vm *vz.VirtualMachine) *vz.VirtioSocketDevice {
socketDevices := vm.SocketDevices()
if len(socketDevices) != 1 {
t.Fatalf("want the number of socket devices is 1 but got %d", len(socketDevices))
}
return socketDevices[0]
}

func newVirtualizationMachine(
t *testing.T,
configs ...func(*vz.VirtualMachineConfiguration) error,
Expand Down Expand Up @@ -145,11 +155,7 @@ func newVirtualizationMachine(
if err != nil {
t.Fatal(err)
}
socketDevices := vm.SocketDevices()
if len(socketDevices) != 1 {
t.Fatalf("want the number of socket devices is 1 but got %d", len(socketDevices))
}
socketDevice := socketDevices[0]
socketDevice := getSocketDevice(t, vm)

if canStart := vm.CanStart(); !canStart {
t.Fatal("want CanStart is true")
Expand Down Expand Up @@ -411,3 +417,67 @@ func TestRunIssue124(t *testing.T) {
t.Fatalf("want state %v but got %v", vz.VirtualMachineStateStopped, got)
}
}

func TestRunIssue131(t *testing.T) {
/*
if os.Getenv("TEST_ISSUE_131") != "1" {
t.Skip()
}
*/
container := newVirtualizationMachine(t,
func(vmc *vz.VirtualMachineConfiguration) error {
return setupConsoleConfig(vmc)
},
)
defer container.Close()

sshSession := container.NewSession(t)
defer sshSession.Close()

vm := container.VirtualMachine

if got := vm.State(); vz.VirtualMachineStateRunning != got {
t.Fatalf("want state %v but got %v", vz.VirtualMachineStateRunning, got)
}
socketDevice := getSocketDevice(t, vm)
listener, err := socketDevice.Listen(1234)
if err != nil {
t.Fatal(err)
}
runtime.GC()
iterCount := 10_000
go func() {
for i := 0; i < iterCount; i++ {
sshSession := container.NewSession(t)
defer sshSession.Close()
if err := sshSession.Run(`echo "Code-Hex/vz" | socat - VSOCK-CONNECT:2:1234`); err != nil {
t.Fatal(err)
}
runtime.GC()
}
}()
for i := 0; i < iterCount; i++ {
conn, err := listener.Accept()
if err != nil {
t.Fatal(err)
}
data, err := io.ReadAll(conn)
if err != nil {
t.Fatal(err)
}
data = bytes.TrimSpace(data)
if !bytes.Equal(data, []byte("Code-Hex/vz")) {
t.Fatalf("want [Code-Hex/vz] but got [%s]", string(data))
}
conn.Close()
}
t.Logf("Successfully iterated 10.000 times")

sshSession.Run("poweroff")
timeout := 5 * time.Second
waitState(t, timeout, vm, vz.VirtualMachineStateStopped)

if got := vm.State(); vz.VirtualMachineStateStopped != got {
t.Fatalf("want state %v but got %v", vz.VirtualMachineStateStopped, got)
}
}

0 comments on commit 2939bdc

Please sign in to comment.