mirror of
https://github.com/hanzoai/visor.git
synced 2026-09-24 17:04:14 +00:00
To kill the subprocess, we kill the syscall thread, which in turn kills all stub threads by virtue of being their parent. This means we have two critical sections that need to be protected: 1. Creating new sysmsg threads -- the function expects that it's new baby will stay alive at least while it's being created; 2. Using the syscall thread itself. We also kick out any currently sleeping task goroutines, and put guard-checks on wait loops and possible entrances to subprocess functionality so that tasks don't come back to a dead subprocess. Additionally, killed subprocesses were not properly decref'ed before, this CL also fixes that. PiperOrigin-RevId: 977651945
206 lines
5.5 KiB
Go
206 lines
5.5 KiB
Go
// Copyright 2026 The gVisor Authors.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package systrap
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"os/exec"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"golang.org/x/sys/unix"
|
|
"gvisor.dev/gvisor/pkg/sentry/platform/systrap/sysmsg"
|
|
)
|
|
|
|
func newTestSharedContext(t *testing.T) *sharedContext {
|
|
t.Helper()
|
|
|
|
cmd := exec.Command(os.Args[0], "-test.run=TestStuckSubprocessHelper")
|
|
cmd.Env = append(os.Environ(), "GVISOR_STUCK_SUBPROCESS_HELPER=1")
|
|
if err := cmd.Start(); err != nil {
|
|
t.Fatalf("failed to start helper subprocess: %v", err)
|
|
}
|
|
t.Cleanup(func() {
|
|
cmd.Process.Kill()
|
|
cmd.Wait()
|
|
})
|
|
|
|
threadID := uint32(1)
|
|
child := &thread{tgid: int32(cmd.Process.Pid), tid: int32(cmd.Process.Pid)}
|
|
queue := &contextQueue{}
|
|
queue.init()
|
|
atomic.AddUint32(&queue.end, 1)
|
|
s := &subprocess{
|
|
contextQueue: queue,
|
|
syscallThread: &syscallThread{
|
|
thread: child,
|
|
},
|
|
sysmsgThreads: map[uint32]*sysmsgThread{
|
|
threadID: {thread: child},
|
|
},
|
|
}
|
|
shared := &sysmsg.ThreadContext{}
|
|
shared.Init(threadID)
|
|
atomic.StoreUint64(&shared.AckedTime, 1)
|
|
return &sharedContext{
|
|
subprocess: s,
|
|
shared: shared,
|
|
}
|
|
}
|
|
|
|
func TestSleepOnStateStuckContext(t *testing.T) {
|
|
sc := newTestSharedContext(t)
|
|
|
|
err := sc.sleepOnStateWithTimeout(sysmsg.ContextStateNone, 15*time.Millisecond, 10*time.Millisecond)
|
|
if !errors.Is(err, errStuckContext) {
|
|
t.Fatalf("sleepOnStateWithTimeout got error %v, want %v", err, errStuckContext)
|
|
}
|
|
}
|
|
|
|
func TestSleepOnStateStuckContextWhenStubIsGone(t *testing.T) {
|
|
sc := newTestSharedContext(t)
|
|
|
|
missing := &thread{tgid: 1 << 30, tid: 1 << 30}
|
|
sc.subprocess.sysmsgThreads[1].thread = missing
|
|
|
|
err := sc.sleepOnStateWithTimeout(sysmsg.ContextStateNone, time.Second, 10*time.Millisecond)
|
|
if !errors.Is(err, errStubThreadGone) {
|
|
t.Fatalf("sleepOnStateWithTimeout got error %v, want %v", err, errStubThreadGone)
|
|
}
|
|
}
|
|
|
|
func TestSleepOnStateRepeatedInterruptsStillStuck(t *testing.T) {
|
|
sc := newTestSharedContext(t)
|
|
|
|
// Interrupts are resent on every checkup wakeup. The resends must not
|
|
// push the stuck deadline out.
|
|
start := time.Now()
|
|
err := sc.sleepOnStateWithTimeout(sysmsg.ContextStateNone, 30*time.Millisecond, 5*time.Millisecond)
|
|
if !errors.Is(err, errStuckContext) {
|
|
t.Fatalf("sleepOnStateWithTimeout got error %v, want %v", err, errStuckContext)
|
|
}
|
|
if elapsed := time.Since(start); elapsed > time.Second {
|
|
t.Errorf("stuck context detection took %v, want under 1s", elapsed)
|
|
}
|
|
}
|
|
|
|
func TestSleepOnStateRecoveredContext(t *testing.T) {
|
|
sc := newTestSharedContext(t)
|
|
|
|
stateChanged := make(chan struct{})
|
|
go func() {
|
|
for atomic.LoadUint32(&sc.shared.Interrupt) == 0 {
|
|
time.Sleep(time.Millisecond)
|
|
}
|
|
sc.setState(sysmsg.ContextStateSyscall)
|
|
close(stateChanged)
|
|
}()
|
|
|
|
err := sc.sleepOnStateWithTimeout(sysmsg.ContextStateNone, 5*time.Millisecond, 10*time.Millisecond)
|
|
<-stateChanged
|
|
if err != nil {
|
|
t.Fatalf("sleepOnStateWithTimeout got error %v, want nil", err)
|
|
}
|
|
}
|
|
|
|
func TestSleepOnStateDeadSubprocess(t *testing.T) {
|
|
sc := newTestSharedContext(t)
|
|
sc.subprocess.dead.Store(true)
|
|
|
|
err := sc.sleepOnState(sysmsg.ContextStateNone)
|
|
if !errors.Is(err, errDeadSubprocess) {
|
|
t.Fatalf("sleepOnState got error %v, want %v", err, errDeadSubprocess)
|
|
}
|
|
}
|
|
|
|
func TestWaitOnStateDeadSubprocess(t *testing.T) {
|
|
sc := newTestSharedContext(t)
|
|
sc.subprocess.dead.Store(true)
|
|
|
|
err := sc.subprocess.waitOnState(sc)
|
|
if !errors.Is(err, errDeadSubprocess) {
|
|
t.Fatalf("waitOnState got error %v, want %v", err, errDeadSubprocess)
|
|
}
|
|
}
|
|
|
|
func TestKickSysmsgThreadDeadSubprocess(t *testing.T) {
|
|
sc := newTestSharedContext(t)
|
|
sc.subprocess.dead.Store(true)
|
|
|
|
if sc.subprocess.kickSysmsgThread() {
|
|
t.Fatalf("kickSysmsgThread got true, want false when subprocess is dead")
|
|
}
|
|
}
|
|
|
|
func TestWithAliveRLockDeadSubprocess(t *testing.T) {
|
|
s := &subprocess{}
|
|
s.dead.Store(true)
|
|
|
|
called := false
|
|
err := s.withAliveRLock(func() error {
|
|
called = true
|
|
return nil
|
|
})
|
|
if !errors.Is(err, errDeadSubprocess) {
|
|
t.Fatalf("withAliveRLock got error %v, want %v", err, errDeadSubprocess)
|
|
}
|
|
if called {
|
|
t.Fatalf("withAliveRLock executed callback when dead")
|
|
}
|
|
}
|
|
|
|
func TestSyscallDeadSubprocess(t *testing.T) {
|
|
s := &subprocess{}
|
|
s.dead.Store(true)
|
|
|
|
if _, err := s.syscall(unix.SYS_MMAP); !errors.Is(err, errDeadSubprocess) {
|
|
t.Fatalf("syscall got error %v, want %v", err, errDeadSubprocess)
|
|
}
|
|
}
|
|
|
|
func TestCreateSysmsgThreadDeadSubprocess(t *testing.T) {
|
|
s := &subprocess{}
|
|
s.dead.Store(true)
|
|
|
|
if err := s.createSysmsgThread(); !errors.Is(err, errDeadSubprocess) {
|
|
t.Fatalf("createSysmsgThread got error %v, want %v", err, errDeadSubprocess)
|
|
}
|
|
}
|
|
|
|
func TestReleaseDeadSubprocessDecRefs(t *testing.T) {
|
|
sc := newTestSharedContext(t)
|
|
s := sc.subprocess
|
|
s.subprocessRefs.InitRefs()
|
|
s.dead.Store(true)
|
|
|
|
released := false
|
|
// Set ref count to 1 and verify DecRef fires.
|
|
s.DecRef(func() {
|
|
released = true
|
|
})
|
|
if !released {
|
|
t.Fatalf("expected subprocess to be released")
|
|
}
|
|
}
|
|
|
|
func TestStuckSubprocessHelper(t *testing.T) {
|
|
if os.Getenv("GVISOR_STUCK_SUBPROCESS_HELPER") == "" {
|
|
return
|
|
}
|
|
select {}
|
|
}
|