func fdcount(bufs ...co.Obuf) int { count := 0 for _, b := range bufs { if b.Addr != 0 { var f native.Fdset32 b.Unpack(&f) count += len(f.Fds()) } } return count }
func CgcSyscall(u models.Usercorn) { // TODO: handle errors or something args, _ := u.ReadRegs(LinuxRegs) eax, _ := u.RegRead(uc.X86_REG_EAX) var ret uint64 switch eax { case 1: // _terminate syscall.Exit(int(args[0])) case 2: // transmit mem, _ := u.MemRead(args[1], args[2]) n, _ := syscall.Write(int(args[0]), mem) writeAddr(u, args[3], uint64(n)) case 3: // receive tmp := make([]byte, args[2]) n, _ := syscall.Read(int(args[0]), tmp) u.MemWrite(args[1], tmp[:n]) writeAddr(u, args[3], uint64(n)) case 5: // allocate addr, _ := u.Mmap(0, args[0]) // args[1] == is executable writeAddr(u, args[2], addr) case 6: // fdwait nfds := int(args[0]) var readSet, writeSet *native.Fdset32 var timeout native.Timespec u.StrucAt(args[1]).Unpack(&readSet) u.StrucAt(args[2]).Unpack(&writeSet) u.StrucAt(args[3]).Unpack(&timeout) readyFds := args[4] readNative := readSet.Native() writeNative := writeSet.Native() n, err := native.Select(nfds, readNative, writeNative, &timeout) if err != nil { ret = UINT32_MAX // FIXME? } else { numReady := int32(n) if readyFds != 0 { u.StrucAt(readyFds).Pack(numReady) } } case 7: // random tmp := make([]byte, args[1]) rand.Read(tmp) u.MemWrite(args[0], tmp) writeAddr(u, args[2], args[1]) } u.RegWrite(uc.X86_REG_EAX, ret) }
func (k *CgcKernel) Fdwait(nfds int, reads, writes, timeoutBuf co.Buf, readyFds co.Obuf) int { var readSet, writeSet *native.Fdset32 var timeout native.Timespec reads.Unpack(&readSet) writes.Unpack(&writeSet) timeoutBuf.Unpack(&timeout) readNative := readSet.Native() writeNative := writeSet.Native() n, err := native.Select(nfds, readNative, writeNative, &timeout) if err != nil { return -1 // FIXME? } else { readyFds.Pack(int32(n)) } return 0 }
func getfdset(f *native.Fdset32) *syscall.FdSet { if f == nil { return nil } return f.Native() }