From bcb6df3c537cc5662470a4708378fe400f9e8dc7 Mon Sep 17 00:00:00 2001 From: criyle Date: Fri, 12 Jul 2019 20:14:22 -0700 Subject: [PATCH] try fix memory usage calc --- cmd/run_program/main.go | 6 +++--- forkexec/fork.go | 2 +- tracer/tracer.go | 10 ++++----- tracer/tracer_track.go | 46 ++++++++++++++++++++++------------------- 4 files changed, 34 insertions(+), 30 deletions(-) diff --git a/cmd/run_program/main.go b/cmd/run_program/main.go index 92cd046..ff52cf4 100644 --- a/cmd/run_program/main.go +++ b/cmd/run_program/main.go @@ -100,9 +100,9 @@ func main() { Stack: stackLimit, }, TraceLimit: runprogram.TraceLimit{ - TimeLimit: timeLimit * 1e3, - RealTimeLimit: realTimeLimit * 1e3, - MemoryLimit: memoryLimit << 10, + TimeLimit: uint64(timeLimit * 1e3), + RealTimeLimit: uint64(realTimeLimit * 1e3), + MemoryLimit: uint64(memoryLimit << 10), }, Files: fds, SyscallAllowed: h.SyscallAllow, diff --git a/forkexec/fork.go b/forkexec/fork.go index 69ac3ff..3d01833 100644 --- a/forkexec/fork.go +++ b/forkexec/fork.go @@ -150,7 +150,7 @@ func (r *Runner) Start() (int, error) { // mkdirs(target) for _, p := range dirsToMake[i] { _, _, err1 = syscall.RawSyscall(syscall.SYS_MKDIR, uintptr(unsafe.Pointer(p)), 0755, 0) - if err1 != 0 { + if err1 != 0 && err1 != syscall.EEXIST { goto childerror } } diff --git a/tracer/tracer.go b/tracer/tracer.go index 5381dae..68315b0 100644 --- a/tracer/tracer.go +++ b/tracer/tracer.go @@ -50,8 +50,8 @@ func (t TraceCode) Error() string { // TraceResult is the result returned by strat trace type TraceResult struct { - UserTime uint // used user CPU time (in ms) - UserMem uint // used user memory (in kb) + UserTime uint64 // used user CPU time (in ms) + UserMem uint64 // used user memory (in kb) ExitCode int // exit code TraceStatus TraceCode // the final status for the process } @@ -64,9 +64,9 @@ type Runner interface { // ResLimit represents the resource limit for traced process type ResLimit struct { - TimeLimit uint // user CPU time limit (in ms) - RealTimeLimit uint // sig_kill will force the process to exit after this limit (in ms) - MemoryLimit uint // user memory limit (in kB) + TimeLimit uint64 // user CPU time limit (in ms) + RealTimeLimit uint64 // sig_kill will force the process to exit after this limit (in ms) + MemoryLimit uint64 // user memory limit (in kB) } // Handler defines customized handler for traced syscall diff --git a/tracer/tracer_track.go b/tracer/tracer_track.go index 835072b..5bbacc6 100644 --- a/tracer/tracer_track.go +++ b/tracer/tracer_track.go @@ -25,6 +25,7 @@ func Trace(handler Handler, runner Runner, limits ResLimit) (result TraceResult, traced = make(map[int]bool) // store all process that have set ptrace options execved = false // store whether the runner process have successfully execvd pid int // store pid of wait4 result + initMem uint64 // initial memory usage (likely due to original process) ) // ptrace is thread based (kernel proc) @@ -64,9 +65,6 @@ func Trace(handler Handler, runner Runner, limits ResLimit) (result TraceResult, // trace unixs for { - if err = clearRefs(pgid); err != nil { - return result, err - } if execved { // Wait for all child in the process group pid, err = unix.Wait4(-pgid, &wstatus, unix.WALL, &rusage) @@ -80,25 +78,31 @@ func Trace(handler Handler, runner Runner, limits ResLimit) (result TraceResult, } handler.Debug("------ ", pid, " ------") - // update resource usage and check against limits - userTime := uint(rusage.Utime.Sec*1e3 + rusage.Utime.Usec/1e3) // ms - userMem := uint(rusage.Maxrss) // kb - status := TraceCodeNormal // check limit + status := TraceCodeNormal + if pid == pgid { + if initMem == 0 { + initMem = uint64(rusage.Maxrss) + handler.Debug("initial memory:", initMem) + } + // update resource usage and check against limits + userTime := uint64(rusage.Utime.Sec*1e3 + rusage.Utime.Usec/1e3) // ms + userMem := uint64(rusage.Maxrss) - initMem // kb - // check tle / mle - if userTime > limits.TimeLimit { - status = TraceCodeTLE - } - if userMem > limits.MemoryLimit { - status = TraceCodeMLE - } - result = TraceResult{ - UserTime: userTime, - UserMem: userMem, - TraceStatus: status, - } - if status != TraceCodeNormal { - return result, status + // check tle / mle + if userTime > limits.TimeLimit { + status = TraceCodeTLE + } + if userMem > limits.MemoryLimit { + status = TraceCodeMLE + } + result = TraceResult{ + UserTime: userTime, + UserMem: userMem, + TraceStatus: status, + } + if status != TraceCodeNormal { + return result, status + } } // check process status