From d5a7f09eb15bec3ecf2318418589d223beab02f4 Mon Sep 17 00:00:00 2001 From: Brandon Potter Date: Fri, 24 Jul 2015 12:25:23 -0700 Subject: [PATCH] syscall_emul: standardized file descriptor name and add return checks. The patch clarifies whether file descriptors are host file descriptors or target file descriptors in the system call code. (Host file descriptors are file descriptors which have been allocated through real system calls where target file descriptors are allocated from an array in the Process class.) --- src/sim/syscall_emul.cc | 104 ++++++++++++++++++++++++---------------- src/sim/syscall_emul.hh | 54 +++++++++++---------- 2 files changed, 91 insertions(+), 67 deletions(-) diff --git a/src/sim/syscall_emul.cc b/src/sim/syscall_emul.cc index 4f1cd2a75..97b91331e 100644 --- a/src/sim/syscall_emul.cc +++ b/src/sim/syscall_emul.cc @@ -210,13 +210,17 @@ SyscallReturn closeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) { int index = 0; - int target_fd = p->getSyscallArg(tc, index); - int sim_fd = p->sim_fd(target_fd); + int tgt_fd = p->getSyscallArg(tc, index); + + int sim_fd = p->sim_fd(tgt_fd); + if (sim_fd < 0) + return -EBADF; + int status = 0; if (sim_fd > 2) status = close(sim_fd); if (status >= 0) - p->reset_fd_entry(target_fd); + p->reset_fd_entry(tgt_fd); return status; } @@ -225,13 +229,16 @@ SyscallReturn readFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) { int index = 0; - int fd = p->sim_fd(p->getSyscallArg(tc, index)); - assert(fd >= 0); + int tgt_fd = p->getSyscallArg(tc, index); Addr bufPtr = p->getSyscallArg(tc, index); int nbytes = p->getSyscallArg(tc, index); BufferArg bufArg(bufPtr, nbytes); - int bytes_read = read(fd, bufArg.bufferPtr(), nbytes); + int sim_fd = p->sim_fd(tgt_fd); + if (sim_fd < 0) + return -EBADF; + + int bytes_read = read(sim_fd, bufArg.bufferPtr(), nbytes); if (bytes_read != -1) bufArg.copyOut(tc->getMemProxy()); @@ -243,16 +250,20 @@ SyscallReturn writeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) { int index = 0; - int fd = p->sim_fd(p->getSyscallArg(tc, index)); + int tgt_fd = p->getSyscallArg(tc, index); Addr bufPtr = p->getSyscallArg(tc, index); int nbytes = p->getSyscallArg(tc, index); BufferArg bufArg(bufPtr, nbytes); + int sim_fd = p->sim_fd(tgt_fd); + if (sim_fd < 0) + return -EBADF; + bufArg.copyIn(tc->getMemProxy()); - int bytes_written = write(fd, bufArg.bufferPtr(), nbytes); + int bytes_written = write(sim_fd, bufArg.bufferPtr(), nbytes); - fsync(fd); + fsync(sim_fd); return bytes_written; } @@ -262,12 +273,15 @@ SyscallReturn lseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) { int index = 0; - int fd = p->sim_fd(p->getSyscallArg(tc, index)); - assert(fd >= 0); + int tgt_fd = p->getSyscallArg(tc, index); uint64_t offs = p->getSyscallArg(tc, index); int whence = p->getSyscallArg(tc, index); - off_t result = lseek(fd, offs, whence); + int sim_fd = p->sim_fd(tgt_fd); + if (sim_fd < 0) + return -EBADF; + + off_t result = lseek(sim_fd, offs, whence); return (result == (off_t)-1) ? -errno : result; } @@ -277,16 +291,19 @@ SyscallReturn _llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) { int index = 0; - int fd = p->sim_fd(p->getSyscallArg(tc, index)); - assert(fd >= 0); + int tgt_fd = p->getSyscallArg(tc, index); uint64_t offset_high = p->getSyscallArg(tc, index); uint32_t offset_low = p->getSyscallArg(tc, index); Addr result_ptr = p->getSyscallArg(tc, index); int whence = p->getSyscallArg(tc, index); + int sim_fd = p->sim_fd(tgt_fd); + if (sim_fd < 0) + return -EBADF; + uint64_t offset = (offset_high << 32) | offset_low; - uint64_t result = lseek(fd, offset, whence); + uint64_t result = lseek(sim_fd, offset, whence); result = TheISA::htog(result); if (result == (off_t)-1) { @@ -481,14 +498,14 @@ ftruncateFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc) { int index = 0; - int fd = process->sim_fd(process->getSyscallArg(tc, index)); + int tgt_fd = process->getSyscallArg(tc, index); + off_t length = process->getSyscallArg(tc, index); - if (fd < 0) + int sim_fd = process->sim_fd(tgt_fd); + if (sim_fd < 0) return -EBADF; - off_t length = process->getSyscallArg(tc, index); - - int result = ftruncate(fd, length); + int result = ftruncate(sim_fd, length); return (result == -1) ? -errno : result; } @@ -520,17 +537,17 @@ ftruncate64Func(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc) { int index = 0; - int fd = process->sim_fd(process->getSyscallArg(tc, index)); + int tgt_fd = process->getSyscallArg(tc, index); + int64_t length = process->getSyscallArg(tc, index, 64); - if (fd < 0) + int sim_fd = process->sim_fd(tgt_fd); + if (sim_fd < 0) return -EBADF; - int64_t length = process->getSyscallArg(tc, index, 64); - #if NO_STAT64 - int result = ftruncate(fd, length); + int result = ftruncate(sim_fd, length); #else - int result = ftruncate64(fd, length); + int result = ftruncate64(sim_fd, length); #endif return (result == -1) ? -errno : result; } @@ -572,9 +589,10 @@ SyscallReturn fchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc) { int index = 0; - int fd = process->sim_fd(process->getSyscallArg(tc, index)); + int tgt_fd = process->getSyscallArg(tc, index); - if (fd < 0) + int sim_fd = process->sim_fd(tgt_fd); + if (sim_fd < 0) return -EBADF; /* XXX endianess */ @@ -583,7 +601,7 @@ fchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc) uint32_t group = process->getSyscallArg(tc, index); gid_t hostGroup = group; - int result = fchown(fd, hostOwner, hostGroup); + int result = fchown(sim_fd, hostOwner, hostGroup); return (result == -1) ? -errno : result; } @@ -593,6 +611,7 @@ dupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc) { int index = 0; int tgt_fd = process->getSyscallArg(tc, index); + int sim_fd = process->sim_fd(tgt_fd); if (sim_fd < 0) return -EBADF; @@ -610,9 +629,10 @@ fcntlFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc) { int index = 0; - int fd = process->getSyscallArg(tc, index); + int tgt_fd = process->getSyscallArg(tc, index); - if (fd < 0 || process->sim_fd(fd) < 0) + int sim_fd = process->sim_fd(tgt_fd); + if (sim_fd < 0) return -EBADF; int cmd = process->getSyscallArg(tc, index); @@ -620,7 +640,7 @@ fcntlFunc(SyscallDesc *desc, int num, LiveProcess *process, case 0: // F_DUPFD // if we really wanted to support this, we'd need to do it // in the target fd space. - warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd); + warn("fcntl(%d, F_DUPFD) not supported, error returned\n", tgt_fd); return -EMFILE; case 1: // F_GETFD (get close-on-exec flag) @@ -631,15 +651,15 @@ fcntlFunc(SyscallDesc *desc, int num, LiveProcess *process, case 4: // F_SETFL (set file flags) // not sure if this is totally valid, but we'll pass it through // to the underlying OS - warn("fcntl(%d, %d) passed through to host\n", fd, cmd); - return fcntl(process->sim_fd(fd), cmd); + warn("fcntl(%d, %d) passed through to host\n", tgt_fd, cmd); + return fcntl(sim_fd, cmd); // return 0; case 7: // F_GETLK (get lock) case 8: // F_SETLK (set lock) case 9: // F_SETLKW (set lock and wait) // don't mess with file locking... just act like it's OK - warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd); + warn("File lock call (fcntl(%d, %d)) ignored.\n", tgt_fd, cmd); return 0; default: @@ -653,27 +673,29 @@ fcntl64Func(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc) { int index = 0; - int fd = process->getSyscallArg(tc, index); + int tgt_fd = process->getSyscallArg(tc, index); - if (fd < 0 || process->sim_fd(fd) < 0) + int sim_fd = process->sim_fd(tgt_fd); + if (sim_fd < 0) return -EBADF; int cmd = process->getSyscallArg(tc, index); switch (cmd) { case 33: //F_GETLK64 - warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd); + warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", tgt_fd); return -EMFILE; case 34: // F_SETLK64 case 35: // F_SETLKW64 - warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd); + warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", + tgt_fd); return -EMFILE; default: // not sure if this is totally valid, but we'll pass it through // to the underlying OS - warn("fcntl64(%d, %d) passed through to host\n", fd, cmd); - return fcntl(process->sim_fd(fd), cmd); + warn("fcntl64(%d, %d) passed through to host\n", tgt_fd, cmd); + return fcntl(sim_fd, cmd); // return 0; } } diff --git a/src/sim/syscall_emul.hh b/src/sim/syscall_emul.hh index b942cb601..5ed697314 100644 --- a/src/sim/syscall_emul.hh +++ b/src/sim/syscall_emul.hh @@ -809,20 +809,20 @@ fchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc) { int index = 0; - int fd = process->getSyscallArg(tc, index); - if (fd < 0 || process->sim_fd(fd) < 0) { - // doesn't map to any simulator fd: not a valid target fd + int tgt_fd = process->getSyscallArg(tc, index); + uint32_t mode = process->getSyscallArg(tc, index); + + int sim_fd = process->sim_fd(tgt_fd); + if (sim_fd < 0) return -EBADF; - } - uint32_t mode = process->getSyscallArg(tc, index); mode_t hostMode = 0; // XXX translate mode flags via OS::someting??? hostMode = mode; // do the fchmod - int result = fchmod(process->sim_fd(fd), hostMode); + int result = fchmod(sim_fd, hostMode); if (result < 0) return -errno; @@ -1003,25 +1003,25 @@ fstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc) { int index = 0; - int fd = process->getSyscallArg(tc, index); + int tgt_fd = process->getSyscallArg(tc, index); Addr bufPtr = process->getSyscallArg(tc, index); - if (fd < 0 || process->sim_fd(fd) < 0) { - // doesn't map to any simulator fd: not a valid target fd + + int sim_fd = process->sim_fd(tgt_fd); + if (sim_fd < 0) return -EBADF; - } #if NO_STAT64 struct stat hostBuf; - int result = fstat(process->sim_fd(fd), &hostBuf); + int result = fstat(sim_fd, &hostBuf); #else struct stat64 hostBuf; - int result = fstat64(process->sim_fd(fd), &hostBuf); + int result = fstat64(sim_fd, &hostBuf); #endif if (result < 0) return -errno; - copyOutStat64Buf(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1)); + copyOutStat64Buf(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); return 0; } @@ -1097,21 +1097,22 @@ fstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc) { int index = 0; - int fd = process->sim_fd(process->getSyscallArg(tc, index)); + int tgt_fd = process->getSyscallArg(tc, index); Addr bufPtr = process->getSyscallArg(tc, index); - DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd); + DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", tgt_fd); - if (fd < 0) + int sim_fd = process->sim_fd(tgt_fd); + if (sim_fd < 0) return -EBADF; struct stat hostBuf; - int result = fstat(fd, &hostBuf); + int result = fstat(sim_fd, &hostBuf); if (result < 0) return -errno; - copyOutStatBuf(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1)); + copyOutStatBuf(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); return 0; } @@ -1154,14 +1155,15 @@ fstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc) { int index = 0; - int fd = process->sim_fd(process->getSyscallArg(tc, index)); + int tgt_fd = process->getSyscallArg(tc, index); Addr bufPtr = process->getSyscallArg(tc, index); - if (fd < 0) + int sim_fd = process->sim_fd(tgt_fd); + if (sim_fd < 0) return -EBADF; struct statfs hostBuf; - int result = fstatfs(fd, &hostBuf); + int result = fstatfs(sim_fd, &hostBuf); if (result < 0) return -errno; @@ -1179,11 +1181,11 @@ writevFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc) { int index = 0; - int fd = process->getSyscallArg(tc, index); - if (fd < 0 || process->sim_fd(fd) < 0) { - // doesn't map to any simulator fd: not a valid target fd + int tgt_fd = process->getSyscallArg(tc, index); + + int sim_fd = process->sim_fd(tgt_fd); + if (sim_fd < 0) return -EBADF; - } SETranslatingPortProxy &p = tc->getMemProxy(); uint64_t tiov_base = process->getSyscallArg(tc, index); @@ -1200,7 +1202,7 @@ writevFunc(SyscallDesc *desc, int callnum, LiveProcess *process, hiov[i].iov_len); } - int result = writev(process->sim_fd(fd), hiov, count); + int result = writev(sim_fd, hiov, count); for (size_t i = 0; i < count; ++i) delete [] (char *)hiov[i].iov_base; -- 2.30.2