arch, sim-se: Fix off_t size mismatch bug

Some system calls were using incorrect sizing for
offset parametres, which was causing the ABI to pass
wrong values due to size mismatches. One such syscall
is lseek, which in the Arm syscall table was
incorrectly marked as llseek, which does not exist
in aarch64 Linux. In addition, the off_t alias for
general Linux was changed from an unsigned to a
signed type, to accurately reflect the behaviour
in the real-life Linux operating system.

Change-Id: Iada4b66a8933466c162ba9ec901dbdae73c73a18
Reviewed-by: Giacomo Travaglini <giacomo.travaglini@arm.com>
This commit is contained in:
Tiberiu Bucur
2024-08-20 14:56:22 +01:00
parent 9b9b9ffbff
commit f74260c552
13 changed files with 149 additions and 43 deletions

View File

@@ -163,7 +163,7 @@ class SyscallTable32 : public SyscallDescTable<EmuLinux::SyscallABI32>
{ base + 14, "mknod", mknodFunc },
{ base + 15, "chmod", chmodFunc<ArmLinux32> },
{ base + 16, "lchown", chownFunc },
{ base + 19, "lseek", lseekFunc },
{ base + 19, "lseek", lseekFunc<ArmLinux32> },
{ base + 20, "getpid", getpidFunc },
{ base + 21, "mount" },
{ base + 22, "umount" },
@@ -567,7 +567,7 @@ class SyscallTable64 : public SyscallDescTable<EmuLinux::SyscallABI64>
#else
{ base + 61, "getdents64" },
#endif
{ base + 62, "llseek", lseekFunc },
{ base + 62, "lseek", lseekFunc<ArmLinux64> },
{ base + 63, "read", readFunc<ArmLinux64> },
{ base + 64, "write", writeFunc<ArmLinux64> },
{ base + 65, "readv" },
@@ -817,7 +817,7 @@ class SyscallTable64 : public SyscallDescTable<EmuLinux::SyscallABI64>
{ base + 1054, "newfstatat" },
{ base + 1055, "fstatfs" },
{ base + 1056, "statfs" },
{ base + 1057, "lseek", lseekFunc },
{ base + 1057, "lseek", lseekFunc<ArmLinux64> },
{ base + 1058, "mmap", mmapFunc<ArmLinux64> },
{ base + 1059, "alarm" },
{ base + 1060, "getpgrp" },

View File

@@ -1,4 +1,15 @@
/*
* Copyright (c) 2024 Arm Limited
*
* The license below extends only to copyright in the software and shall
* not be construed as granting a license to any other intellectual
* property including but not limited to intellectual property relating
* to a hardware implementation of the functionality of the software
* licensed hereunder. You may use the software subject to the license
* terms below provided that you ensure that this notice is replicated
* unmodified and in its entirety in all distributions of the software,
* modified or unmodified, in source code or in binary form.
*
* Copyright (c) 2006 The Regents of The University of Michigan
* All rights reserved.
*
@@ -192,6 +203,7 @@ class MipsLinux : public Linux, public OpenFlagTable<MipsLinux>
uint32_t mem_unit; /* Memory unit size in bytes */
};
typedef int32_t off_t;
};
} // namespace gem5

View File

@@ -1,4 +1,15 @@
/*
* Copyright (c) 2024 Arm Limited
*
* The license below extends only to copyright in the software and shall
* not be construed as granting a license to any other intellectual
* property including but not limited to intellectual property relating
* to a hardware implementation of the functionality of the software
* licensed hereunder. You may use the software subject to the license
* terms below provided that you ensure that this notice is replicated
* unmodified and in its entirety in all distributions of the software,
* modified or unmodified, in source code or in binary form.
*
* Copyright 2005 The Regents of The University of Michigan
* Copyright 2007 MIPS Technologies, Inc.
* Copyright 2020 Google Inc.
@@ -177,7 +188,7 @@ SyscallDescTable<MipsISA::SEWorkload::SyscallABI> EmuLinux::syscallDescs = {
{ 4016, "lchown", chownFunc },
{ 4017, "break", brkFunc },
{ 4018, "unused#18" },
{ 4019, "lseek", lseekFunc },
{ 4019, "lseek", lseekFunc<MipsLinux> },
{ 4020, "getpid", getpidFunc },
{ 4021, "mount" },
{ 4022, "umount" },

View File

@@ -1,4 +1,15 @@
/*
* Copyright (c) 2024 Arm Limited
*
* The license below extends only to copyright in the software and shall
* not be construed as granting a license to any other intellectual
* property including but not limited to intellectual property relating
* to a hardware implementation of the functionality of the software
* licensed hereunder. You may use the software subject to the license
* terms below provided that you ensure that this notice is replicated
* unmodified and in its entirety in all distributions of the software,
* modified or unmodified, in source code or in binary form.
*
* Copyright 2003-2005 The Regents of The University of Michigan
* Copyright 2007-2008 The Florida State University
* Copyright 2009 The University of Edinburgh
@@ -126,7 +137,7 @@ SyscallDescTable<PowerISA::SEWorkload::SyscallABI> EmuLinux::syscallDescs = {
{ 16, "lchown", chownFunc },
{ 17, "break", brkFunc }, //???
{ 18, "unused#18" }, //???
{ 19, "lseek", lseekFunc },
{ 19, "lseek", lseekFunc<PowerLinux> },
{ 20, "getpid", getpidFunc },
{ 21, "mount" },
{ 22, "umount" },

View File

@@ -1,4 +1,15 @@
/*
* Copyright (c) 2024 Arm Limited
*
* The license below extends only to copyright in the software and shall
* not be construed as granting a license to any other intellectual
* property including but not limited to intellectual property relating
* to a hardware implementation of the functionality of the software
* licensed hereunder. You may use the software subject to the license
* terms below provided that you ensure that this notice is replicated
* unmodified and in its entirety in all distributions of the software,
* modified or unmodified, in source code or in binary form.
*
* Copyright 2005 The Regents of The University of Michigan
* Copyright 2007 MIPS Technologies, Inc.
* Copyright 2016 The University of Virginia
@@ -190,7 +201,7 @@ SyscallDescTable<SEWorkload::SyscallABI64> EmuLinux::syscallDescs64 = {
#else
{ 61, "getdents64" },
#endif
{ 62, "lseek", lseekFunc },
{ 62, "lseek", lseekFunc<RiscvLinux64> },
{ 63, "read", readFunc<RiscvLinux64> },
{ 64, "write", writeFunc<RiscvLinux64> },
{ 66, "writev", writevFunc<RiscvLinux64> },
@@ -432,7 +443,7 @@ SyscallDescTable<SEWorkload::SyscallABI64> EmuLinux::syscallDescs64 = {
{ 1054, "newfstatat", newfstatatFunc<RiscvLinux64> },
{ 1055, "fstatfs", fstatfsFunc<RiscvLinux64> },
{ 1056, "statfs", statfsFunc<RiscvLinux64> },
{ 1057, "lseek", lseekFunc },
{ 1057, "lseek", lseekFunc<RiscvLinux64> },
{ 1058, "mmap", mmapFunc<RiscvLinux64> },
{ 1059, "alarm" },
{ 1060, "getpgrp", getpgrpFunc },
@@ -529,7 +540,7 @@ SyscallDescTable<SEWorkload::SyscallABI32> EmuLinux::syscallDescs32 = {
#else
{ 61, "getdents64" },
#endif
{ 62, "lseek", lseekFunc },
{ 62, "lseek", lseekFunc<RiscvLinux32> },
{ 63, "read", readFunc<RiscvLinux32> },
{ 64, "write", writeFunc<RiscvLinux32> },
{ 66, "writev", writevFunc<RiscvLinux32> },
@@ -771,7 +782,7 @@ SyscallDescTable<SEWorkload::SyscallABI32> EmuLinux::syscallDescs32 = {
{ 1054, "newfstatat", newfstatatFunc<RiscvLinux32> },
{ 1055, "fstatfs", fstatfsFunc<RiscvLinux32> },
{ 1056, "statfs", statfsFunc<RiscvLinux32> },
{ 1057, "lseek", lseekFunc },
{ 1057, "lseek", lseekFunc<RiscvLinux32> },
{ 1058, "mmap", mmapFunc<RiscvLinux32> },
{ 1059, "alarm" },
{ 1060, "getpgrp", getpgrpFunc },

View File

@@ -1,4 +1,15 @@
/*
* Copyright (c) 2024 Arm Limited
*
* The license below extends only to copyright in the software and shall
* not be construed as granting a license to any other intellectual
* property including but not limited to intellectual property relating
* to a hardware implementation of the functionality of the software
* licensed hereunder. You may use the software subject to the license
* terms below provided that you ensure that this notice is replicated
* unmodified and in its entirety in all distributions of the software,
* modified or unmodified, in source code or in binary form.
*
* Copyright (c) 2003-2005 The Regents of The University of Michigan
* All rights reserved.
*
@@ -242,6 +253,8 @@ class SparcLinux : public Linux, public OpenFlagTable<SparcLinux>
ptc->setReg(SparcISA::SyscallPseudoReturnReg, (RegVal)0);
ctc->setReg(SparcISA::SyscallPseudoReturnReg, 1);
}
typedef int64_t off_t;
};
class Sparc32Linux : public SparcLinux
@@ -288,6 +301,8 @@ class Sparc32Linux : public SparcLinux
uint32_t mem_unit; /* Memory unit size in bytes */
};
typedef int32_t off_t;
/// Resource constants for getrlimit() (overide some generics).
static const unsigned TGT_RLIMIT_NPROC = 7;
static const unsigned TGT_RLIMIT_NOFILE = 6;

View File

@@ -1,4 +1,15 @@
/*
* Copyright (c) 2024 Arm Limited
*
* The license below extends only to copyright in the software and shall
* not be construed as granting a license to any other intellectual
* property including but not limited to intellectual property relating
* to a hardware implementation of the functionality of the software
* licensed hereunder. You may use the software subject to the license
* terms below provided that you ensure that this notice is replicated
* unmodified and in its entirety in all distributions of the software,
* modified or unmodified, in source code or in binary form.
*
* Copyright (c) 2003-2005 The Regents of The University of Michigan
* All rights reserved.
*
@@ -104,7 +115,7 @@ SyscallDescTable<SEWorkload::SyscallABI32> EmuLinux::syscall32Descs = {
{ 16, "lchown" }, // 32 bit
{ 17, "brk", brkFunc },
{ 18, "perfctr" }, // 32 bit
{ 19, "lseek", lseekFunc }, // 32 bit
{ 19, "lseek", lseekFunc<Sparc32Linux> }, // 32 bit
{ 20, "getpid", getpidFunc },
{ 21, "capget" },
{ 22, "capset" },
@@ -407,7 +418,7 @@ SyscallDescTable<SEWorkload::SyscallABI64> EmuLinux::syscallDescs = {
{ 16, "lchown" },
{ 17, "brk", brkFunc },
{ 18, "perfctr" },
{ 19, "lseek", lseekFunc },
{ 19, "lseek", lseekFunc<SparcLinux> },
{ 20, "getpid", getpidFunc },
{ 21, "capget" },
{ 22, "capset" },

View File

@@ -1,4 +1,5 @@
/*
* Copyright (c) 2024 Arm Limited
* Copyright (c) 2007 The Hewlett-Packard Development Company
* All rights reserved.
*
@@ -240,7 +241,7 @@ class X86Linux64 : public X86Linux, public OpenFlagTable<X86Linux64>
//@{
/// Basic X86_64 Linux types
typedef uint64_t size_t;
typedef uint64_t off_t;
typedef int64_t off_t;
typedef int64_t time_t;
typedef int64_t clock_t;
//@}
@@ -387,7 +388,7 @@ class X86Linux32 : public X86Linux, public OpenFlagTable<X86Linux32>
//@{
/// Basic X86 Linux types
typedef uint32_t size_t;
typedef uint32_t off_t;
typedef int32_t off_t;
typedef int32_t time_t;
typedef int32_t clock_t;
//@}

View File

@@ -1,4 +1,15 @@
/*
* Copyright (c) 2024 Arm Limited
*
* The license below extends only to copyright in the software and shall
* not be construed as granting a license to any other intellectual
* property including but not limited to intellectual property relating
* to a hardware implementation of the functionality of the software
* licensed hereunder. You may use the software subject to the license
* terms below provided that you ensure that this notice is replicated
* unmodified and in its entirety in all distributions of the software,
* modified or unmodified, in source code or in binary form.
*
* Copyright 2020 Google Inc.
*
* Redistribution and use in source and binary forms, with or without
@@ -58,7 +69,7 @@ SyscallDescTable<EmuLinux::SyscallABI32> EmuLinux::syscallDescs32 = {
{ 16, "lchown" },
{ 17, "break" },
{ 18, "oldstat" },
{ 19, "lseek" },
{ 19, "lseek", lseekFunc<X86Linux32> },
{ 20, "getpid", getpidFunc },
{ 21, "mount" },
{ 22, "umount" },

View File

@@ -1,4 +1,15 @@
/*
* Copyright (c) 2024 Arm Limited
*
* The license below extends only to copyright in the software and shall
* not be construed as granting a license to any other intellectual
* property including but not limited to intellectual property relating
* to a hardware implementation of the functionality of the software
* licensed hereunder. You may use the software subject to the license
* terms below provided that you ensure that this notice is replicated
* unmodified and in its entirety in all distributions of the software,
* modified or unmodified, in source code or in binary form.
*
* Copyright 2020 Google Inc.
*
* Redistribution and use in source and binary forms, with or without
@@ -47,7 +58,7 @@ SyscallDescTable<EmuLinux::SyscallABI64> EmuLinux::syscallDescs64 = {
{ 5, "fstat", fstat64Func<X86Linux64> },
{ 6, "lstat", lstat64Func<X86Linux64> },
{ 7, "poll", pollFunc<X86Linux64> },
{ 8, "lseek", lseekFunc },
{ 8, "lseek", lseekFunc<X86Linux64> },
{ 9, "mmap", mmapFunc<X86Linux64> },
{ 10, "mprotect", ignoreFunc },
{ 11, "munmap", munmapFunc<X86Linux64> },

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Arm Limited
* Copyright (c) 2021, 2024 Arm Limited
* All rights reserved
*
* The license below extends only to copyright in the software and shall
@@ -66,7 +66,7 @@ class Linux : public OperatingSystem
//@{
/// Basic Linux types.
typedef uint64_t size_t;
typedef uint64_t off_t;
typedef int64_t off_t;
typedef int64_t time_t;
typedef int64_t clock_t;
typedef uint32_t uid_t;

View File

@@ -1,4 +1,16 @@
/*
* Copyright (c) 2024 Arm Limited
*
* The license below extends only to copyright in the software and
* shall not be construed as granting a license to any other
* intellectual property including but not limited to intellectual
* property relating to a hardware implementation of the
* functionality of the software licensed hereunder. You may use the
* software subject to the license terms below provided that you
* ensure that this notice is replicated unmodified and in its
* entirety in all distributions of the software, modified or
* unmodified, in source code or in binary form.
*
* Copyright (c) 2003-2005 The Regents of The University of Michigan
* All rights reserved.
*
@@ -292,26 +304,10 @@ closeFunc(SyscallDesc *desc, ThreadContext *tc, int tgt_fd)
return p->fds->closeFDEntry(tgt_fd);
}
SyscallReturn
lseekFunc(SyscallDesc *desc, ThreadContext *tc,
int tgt_fd, uint64_t offs, int whence)
{
auto p = tc->getProcessPtr();
auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
if (!ffdp)
return -EBADF;
int sim_fd = ffdp->getSimFD();
off_t result = lseek(sim_fd, offs, whence);
return (result == (off_t)-1) ? -errno : result;
}
SyscallReturn
_llseekFunc(SyscallDesc *desc, ThreadContext *tc,
int tgt_fd, uint64_t offset_high, uint32_t offset_low,
int tgt_fd, uint32_t offset_high, uint32_t offset_low,
VPtr<> result_ptr, int whence)
{
auto p = tc->getProcessPtr();
@@ -321,7 +317,7 @@ _llseekFunc(SyscallDesc *desc, ThreadContext *tc,
return -EBADF;
int sim_fd = ffdp->getSimFD();
uint64_t offset = (offset_high << 32) | offset_low;
uint64_t offset = ((uint64_t) offset_high << 32) | offset_low;
uint64_t result = lseek(sim_fd, offset, whence);
result = htog(result, tc->getSystemPtr()->getGuestByteOrder());

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2013, 2015, 2019-2021, 2023 Arm Limited
* Copyright (c) 2012-2013, 2015, 2019-2021, 2023-2024 Arm Limited
* Copyright (c) 2015 Advanced Micro Devices, Inc.
* All rights reserved
*
@@ -160,13 +160,9 @@ SyscallReturn brkFunc(SyscallDesc *desc, ThreadContext *tc, VPtr<> new_brk);
/// Target close() handler.
SyscallReturn closeFunc(SyscallDesc *desc, ThreadContext *tc, int tgt_fd);
/// Target lseek() handler.
SyscallReturn lseekFunc(SyscallDesc *desc, ThreadContext *tc,
int tgt_fd, uint64_t offs, int whence);
/// Target _llseek() handler.
SyscallReturn _llseekFunc(SyscallDesc *desc, ThreadContext *tc,
int tgt_fd, uint64_t offset_high,
int tgt_fd, uint32_t offset_high,
uint32_t offset_low, VPtr<> result_ptr, int whence);
/// Target shutdown() handler.
@@ -976,6 +972,24 @@ openFunc(SyscallDesc *desc, ThreadContext *tc,
desc, tc, OS::TGT_AT_FDCWD, pathname, tgt_flags, mode);
}
/// Target lseek() handler
template <class OS>
SyscallReturn
lseekFunc(SyscallDesc *desc, ThreadContext *tc,
int tgt_fd, typename OS::off_t offs, int whence)
{
auto p = tc->getProcessPtr();
auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
if (!ffdp)
return -EBADF;
int sim_fd = ffdp->getSimFD();
off_t result = lseek(sim_fd, offs, whence);
return (result == (off_t)-1) ? -errno : result;
}
/// Target unlinkat() handler.
template <class OS>
SyscallReturn
@@ -2092,7 +2106,8 @@ mmapFunc(SyscallDesc *desc, ThreadContext *tc,
template <class OS>
SyscallReturn
pread64Func(SyscallDesc *desc, ThreadContext *tc,
int tgt_fd, VPtr<> bufPtr, int nbytes, int offset)
int tgt_fd, VPtr<> bufPtr, int nbytes,
typename OS::off_t offset)
{
auto p = tc->getProcessPtr();
@@ -2113,7 +2128,8 @@ pread64Func(SyscallDesc *desc, ThreadContext *tc,
template <class OS>
SyscallReturn
pwrite64Func(SyscallDesc *desc, ThreadContext *tc,
int tgt_fd, VPtr<> bufPtr, int nbytes, int offset)
int tgt_fd, VPtr<> bufPtr, int nbytes,
typename OS::off_t offset)
{
auto p = tc->getProcessPtr();