changeset 42d846318962 in /z/repo/gem5
details: http://repo.gem5.org/gem5?cmd=changeset;node=42d846318962
description:
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.)
diffstat:
src/sim/syscall_emul.cc | 104 +++++++++++++++++++++++++++++------------------
src/sim/syscall_emul.hh | 54 ++++++++++++------------
2 files changed, 91 insertions(+), 67 deletions(-)
diffs (truncated from 396 to 300 lines):
diff -r ddc3d96d6313 -r 42d846318962 src/sim/syscall_emul.cc
--- a/src/sim/syscall_emul.cc Fri Jul 24 12:25:22 2015 -0700
+++ b/src/sim/syscall_emul.cc Fri Jul 24 12:25:23 2015 -0700
@@ -210,13 +210,17 @@
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 @@
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 @@
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 @@
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 @@
_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 @@
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 @@
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 @@
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 @@
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 @@
{
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 @@
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 @@
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 @@
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 @@
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 -r ddc3d96d6313 -r 42d846318962 src/sim/syscall_emul.hh
--- a/src/sim/syscall_emul.hh Fri Jul 24 12:25:22 2015 -0700
+++ b/src/sim/syscall_emul.hh Fri Jul 24 12:25:23 2015 -0700
@@ -809,20 +809,20 @@
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 @@
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
_______________________________________________
gem5-dev mailing list
[email protected]
http://m5sim.org/mailman/listinfo/gem5-dev