From e2cbd043ceed572b438303dba22daf6594f5b42f Mon Sep 17 00:00:00 2001 From: Julius Athenstaedt Date: Mon, 19 Feb 2024 09:48:32 +0100 Subject: [PATCH 1/5] add submodule GSL v4.0.0.0 --- .gitmodules | 3 +++ external/GSL | 1 + 2 files changed, 4 insertions(+) create mode 160000 external/GSL diff --git a/.gitmodules b/.gitmodules index 276395241..1fe5c04e2 100644 --- a/.gitmodules +++ b/.gitmodules @@ -25,3 +25,6 @@ [submodule "external/MessagePack"] path = external/MessagePack url = https://github.com/GekkoFS/MessagePackCPP.git +[submodule "external/GSL"] + path = external/GSL + url = https://github.com/microsoft/GSL diff --git a/external/GSL b/external/GSL new file mode 160000 index 000000000..a35345671 --- /dev/null +++ b/external/GSL @@ -0,0 +1 @@ +Subproject commit a3534567187d2edc428efd3f13466ff75fe5805c -- GitLab From 7e1c7cce3d62df066891a84352b7a8f4a9b181cf Mon Sep 17 00:00:00 2001 From: Julius Athenstaedt Date: Mon, 19 Feb 2024 10:59:19 +0100 Subject: [PATCH 2/5] use GSL to narrow down syscalls in hooks.cpp --- CMakeLists.txt | 8 ++++++++ src/client/CMakeLists.txt | 2 ++ src/client/hooks.cpp | 7 ++++--- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a7f6b347d..ae0ec5d48 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -261,6 +261,14 @@ if (GKFS_ENABLE_CLIENT_METRICS) ) endif () +### GSL: Guidelines Support Library +include_from_source(gsl + MESSAGE "[${PROJECT_NAME}] Searching for GSL" + SOURCE_DIR ${GKFS_DEPENDENCIES_PATH}/GSL + GIT_REPOSITORY https://github.com/microsoft/GSL + GIT_TAG v4.0.0 +) + ################################################################################ ## Check configured variables/options and act accordingly ################################################################################ diff --git a/src/client/CMakeLists.txt b/src/client/CMakeLists.txt index 333c66269..bd838f7d7 100644 --- a/src/client/CMakeLists.txt +++ b/src/client/CMakeLists.txt @@ -97,6 +97,7 @@ target_link_libraries( fmt::fmt Threads::Threads Syscall_intercept::Syscall_intercept + Microsoft::GSL ) # Enable MSGPack metrics for intercept only if (GKFS_ENABLE_CLIENT_METRICS) @@ -116,6 +117,7 @@ target_link_libraries( hermes fmt::fmt Threads::Threads + Microsoft::GSL ) install( diff --git a/src/client/hooks.cpp b/src/client/hooks.cpp index c38688c59..0b8259849 100644 --- a/src/client/hooks.cpp +++ b/src/client/hooks.cpp @@ -36,6 +36,7 @@ #include #include +#include #include @@ -239,7 +240,7 @@ hook_pread(unsigned int fd, char* buf, size_t count, loff_t pos) { } /* Since kernel 2.6: pread() became pread64(), and pwrite() became * pwrite64(). */ - return syscall_no_intercept_wrapper(SYS_pread64, fd, buf, count, pos); + return gsl::narrow(syscall_no_intercept_wrapper(SYS_pread64, fd, buf, count, pos)); } int @@ -293,7 +294,7 @@ hook_pwrite(unsigned int fd, const char* buf, size_t count, loff_t pos) { } /* Since kernel 2.6: pread() became pread64(), and pwrite() became * pwrite64(). */ - return syscall_no_intercept_wrapper(SYS_pwrite64, fd, buf, count, pos); + return gsl::narrow(syscall_no_intercept_wrapper(SYS_pwrite64, fd, buf, count, pos)); } int @@ -595,7 +596,7 @@ hook_getdents64(unsigned int fd, struct linux_dirent64* dirp, if(CTX->file_map()->exist(fd)) { return with_errno(gkfs::syscall::gkfs_getdents64(fd, dirp, count)); } - return syscall_no_intercept_wrapper(SYS_getdents64, fd, dirp, count); + return gsl::narrow(syscall_no_intercept_wrapper(SYS_getdents64, fd, dirp, count)); } -- GitLab From 55f37b2e3e738cfb8ceefa945ddd583e4e55d98a Mon Sep 17 00:00:00 2001 From: Julius Athenstaedt Date: Mon, 19 Feb 2024 14:42:42 +0100 Subject: [PATCH 3/5] correcting syscall return types --- include/client/hooks.hpp | 22 +++--- src/client/hooks.cpp | 148 +++++++++++++++++++++------------------ 2 files changed, 92 insertions(+), 78 deletions(-) diff --git a/include/client/hooks.hpp b/include/client/hooks.hpp index 7044caffd..4b242bc9e 100644 --- a/include/client/hooks.hpp +++ b/include/client/hooks.hpp @@ -94,29 +94,29 @@ hook_fstat(unsigned int fd, struct stat* buf); int hook_fstatat(int dirfd, const char* cpath, struct stat* buf, int flags); -int +ssize_t hook_read(unsigned int fd, void* buf, size_t count); -int +ssize_t hook_pread(unsigned int fd, char* buf, size_t count, loff_t pos); int hook_readv(unsigned long fd, const struct iovec* iov, unsigned long iovcnt); -int +ssize_t hook_preadv(unsigned long fd, const struct iovec* iov, unsigned long iovcnt, unsigned long pos_l, unsigned long pos_h); -int +ssize_t hook_write(unsigned int fd, const char* buf, size_t count); -int +ssize_t hook_pwrite(unsigned int fd, const char* buf, size_t count, loff_t pos); -int +ssize_t hook_writev(unsigned long fd, const struct iovec* iov, unsigned long iovcnt); -int +ssize_t hook_pwritev(unsigned long fd, const struct iovec* iov, unsigned long iovcnt, unsigned long pos_l, unsigned long pos_h); @@ -158,10 +158,10 @@ hook_dup2(unsigned int oldfd, unsigned int newfd); int hook_dup3(unsigned int oldfd, unsigned int newfd, int flags); -int +long hook_getdents(unsigned int fd, struct linux_dirent* dirp, unsigned int count); -int +ssize_t hook_getdents64(unsigned int fd, struct linux_dirent64* dirp, unsigned int count); @@ -183,7 +183,7 @@ hook_fchdir(unsigned int fd); int hook_getcwd(char* buf, unsigned long size); -int +ssize_t hook_readlinkat(int dirfd, const char* cpath, char* buf, int bufsiz); int @@ -202,7 +202,7 @@ hook_fstatfs(unsigned int fd, struct statfs* buf); int hook_fsync(unsigned int fd); -int +ssize_t hook_getxattr(const char* path, const char* name, void* value, size_t size); int diff --git a/src/client/hooks.cpp b/src/client/hooks.cpp index 0b8259849..91c34c300 100644 --- a/src/client/hooks.cpp +++ b/src/client/hooks.cpp @@ -38,6 +38,7 @@ #include #include +#include #include extern "C" { @@ -68,12 +69,12 @@ hook_openat(int dirfd, const char* cpath, int flags, mode_t mode) { auto rstatus = CTX->relativize_fd_path(dirfd, cpath, resolved); switch(rstatus) { case gkfs::preload::RelativizeStatus::fd_unknown: - return syscall_no_intercept_wrapper(SYS_openat, dirfd, cpath, flags, - mode); + return gsl::narrow_cast(syscall_no_intercept_wrapper( + SYS_openat, dirfd, cpath, flags, mode)); case gkfs::preload::RelativizeStatus::external: - return syscall_no_intercept_wrapper(SYS_openat, dirfd, - resolved.c_str(), flags, mode); + return gsl::narrow_cast(syscall_no_intercept_wrapper( + SYS_openat, dirfd, resolved.c_str(), flags, mode)); case gkfs::preload::RelativizeStatus::fd_not_a_dir: return -ENOTDIR; @@ -97,7 +98,7 @@ hook_close(int fd) { if(ret == 0) return 0; - return syscall_no_intercept_wrapper(SYS_close, fd); + return gsl::narrow_cast(syscall_no_intercept_wrapper(SYS_close, fd)); } #ifdef SYS_stat int @@ -111,7 +112,8 @@ hook_stat(const char* path, struct stat* buf) { return with_errno(gkfs::syscall::gkfs_stat(rel_path, buf)); } - return syscall_no_intercept_wrapper(SYS_stat, rel_path.c_str(), buf); + return gsl::narrow_cast( + syscall_no_intercept_wrapper(SYS_stat, rel_path.c_str(), buf)); } #endif @@ -129,12 +131,12 @@ hook_statx(int dirfd, const char* path, int flags, unsigned int mask, auto rstatus = CTX->relativize_fd_path(dirfd, path, resolved); switch(rstatus) { case gkfs::preload::RelativizeStatus::fd_unknown: - return syscall_no_intercept_wrapper(SYS_statx, dirfd, path, flags, - mask, buf); + return gsl::narrow_cast(syscall_no_intercept_wrapper( + SYS_statx, dirfd, path, flags, mask, buf)); case gkfs::preload::RelativizeStatus::external: - return syscall_no_intercept_wrapper( - SYS_statx, dirfd, resolved.c_str(), flags, mask, buf); + return gsl::narrow_cast(syscall_no_intercept_wrapper( + SYS_statx, dirfd, resolved.c_str(), flags, mask, buf)); case gkfs::preload::RelativizeStatus::fd_not_a_dir: return -ENOTDIR; @@ -164,7 +166,8 @@ hook_lstat(const char* path, struct stat* buf) { if(CTX->relativize_path(path, rel_path)) { return with_errno(gkfs::syscall::gkfs_stat(rel_path, buf)); } - return syscall_no_intercept_wrapper(SYS_lstat, rel_path.c_str(), buf); + return gsl::narrow_cast( + syscall_no_intercept_wrapper(SYS_lstat, rel_path.c_str(), buf)); } #endif @@ -185,7 +188,8 @@ hook_fstat(unsigned int fd, struct stat* buf) { #endif return with_errno(gkfs::syscall::gkfs_stat(path, buf)); } - return syscall_no_intercept_wrapper(SYS_fstat, fd, buf); + return gsl::narrow_cast( + syscall_no_intercept_wrapper(SYS_fstat, fd, buf)); } int @@ -198,12 +202,12 @@ hook_fstatat(int dirfd, const char* cpath, struct stat* buf, int flags) { auto rstatus = CTX->relativize_fd_path(dirfd, cpath, resolved, flags); switch(rstatus) { case gkfs::preload::RelativizeStatus::fd_unknown: - return syscall_no_intercept_wrapper(SYS_newfstatat, dirfd, cpath, - buf, flags); + return gsl::narrow_cast(syscall_no_intercept_wrapper( + SYS_newfstatat, dirfd, cpath, buf, flags)); case gkfs::preload::RelativizeStatus::external: - return syscall_no_intercept_wrapper(SYS_newfstatat, dirfd, - resolved.c_str(), buf, flags); + return gsl::narrow_cast(syscall_no_intercept_wrapper( + SYS_newfstatat, dirfd, resolved.c_str(), buf, flags)); case gkfs::preload::RelativizeStatus::fd_not_a_dir: return -ENOTDIR; @@ -217,7 +221,7 @@ hook_fstatat(int dirfd, const char* cpath, struct stat* buf, int flags) { } } -int +ssize_t hook_read(unsigned int fd, void* buf, size_t count) { LOG(DEBUG, "{}() called with fd: {}, buf: {} count: {}", __func__, fd, @@ -229,7 +233,7 @@ hook_read(unsigned int fd, void* buf, size_t count) { return syscall_no_intercept_wrapper(SYS_read, fd, buf, count); } -int +ssize_t hook_pread(unsigned int fd, char* buf, size_t count, loff_t pos) { LOG(DEBUG, "{}() called with fd: {}, buf: {}, count: {}, pos: {}", __func__, @@ -240,7 +244,7 @@ hook_pread(unsigned int fd, char* buf, size_t count, loff_t pos) { } /* Since kernel 2.6: pread() became pread64(), and pwrite() became * pwrite64(). */ - return gsl::narrow(syscall_no_intercept_wrapper(SYS_pread64, fd, buf, count, pos)); + return syscall_no_intercept_wrapper(SYS_pread64, fd, buf, count, pos); } int @@ -255,7 +259,7 @@ hook_readv(unsigned long fd, const struct iovec* iov, unsigned long iovcnt) { return syscall_no_intercept_wrapper(SYS_readv, fd, iov, iovcnt); } -int +ssize_t hook_preadv(unsigned long fd, const struct iovec* iov, unsigned long iovcnt, unsigned long pos_l, unsigned long pos_h) { @@ -271,7 +275,7 @@ hook_preadv(unsigned long fd, const struct iovec* iov, unsigned long iovcnt, return syscall_no_intercept_wrapper(SYS_preadv, fd, iov, iovcnt, pos_l); } -int +ssize_t hook_write(unsigned int fd, const char* buf, size_t count) { LOG(DEBUG, "{}() called with fd: {}, buf: {}, count {}", __func__, fd, @@ -283,7 +287,7 @@ hook_write(unsigned int fd, const char* buf, size_t count) { return syscall_no_intercept_wrapper(SYS_write, fd, buf, count); } -int +ssize_t hook_pwrite(unsigned int fd, const char* buf, size_t count, loff_t pos) { LOG(DEBUG, "{}() called with fd: {}, buf: {}, count: {}, pos: {}", __func__, @@ -294,10 +298,10 @@ hook_pwrite(unsigned int fd, const char* buf, size_t count, loff_t pos) { } /* Since kernel 2.6: pread() became pread64(), and pwrite() became * pwrite64(). */ - return gsl::narrow(syscall_no_intercept_wrapper(SYS_pwrite64, fd, buf, count, pos)); + return syscall_no_intercept_wrapper(SYS_pwrite64, fd, buf, count, pos); } -int +ssize_t hook_writev(unsigned long fd, const struct iovec* iov, unsigned long iovcnt) { LOG(DEBUG, "{}() called with fd: {}, iov: {}, iovcnt: {}", __func__, fd, @@ -309,7 +313,7 @@ hook_writev(unsigned long fd, const struct iovec* iov, unsigned long iovcnt) { return syscall_no_intercept_wrapper(SYS_writev, fd, iov, iovcnt); } -int +ssize_t hook_pwritev(unsigned long fd, const struct iovec* iov, unsigned long iovcnt, unsigned long pos_l, unsigned long pos_h) { @@ -340,12 +344,12 @@ hook_unlinkat(int dirfd, const char* cpath, int flags) { auto rstatus = CTX->relativize_fd_path(dirfd, cpath, resolved, false); switch(rstatus) { case gkfs::preload::RelativizeStatus::fd_unknown: - return syscall_no_intercept_wrapper(SYS_unlinkat, dirfd, cpath, - flags); + return gsl::narrow_cast(syscall_no_intercept_wrapper( + SYS_unlinkat, dirfd, cpath, flags)); case gkfs::preload::RelativizeStatus::external: - return syscall_no_intercept_wrapper(SYS_unlinkat, dirfd, - resolved.c_str(), flags); + return gsl::narrow_cast(syscall_no_intercept_wrapper( + SYS_unlinkat, dirfd, resolved.c_str(), flags)); case gkfs::preload::RelativizeStatus::fd_not_a_dir: return -ENOTDIR; @@ -380,12 +384,12 @@ hook_symlinkat(const char* oldname, int newdfd, const char* newname) { CTX->relativize_fd_path(newdfd, newname, newname_resolved, false); switch(rstatus) { case gkfs::preload::RelativizeStatus::fd_unknown: - return syscall_no_intercept_wrapper(SYS_symlinkat, oldname, newdfd, - newname); + return gsl::narrow_cast(syscall_no_intercept_wrapper( + SYS_symlinkat, oldname, newdfd, newname)); case gkfs::preload::RelativizeStatus::external: - return syscall_no_intercept_wrapper(SYS_symlinkat, oldname, newdfd, - newname_resolved.c_str()); + return gsl::narrow_cast(syscall_no_intercept_wrapper( + SYS_symlinkat, oldname, newdfd, newname_resolved.c_str())); case gkfs::preload::RelativizeStatus::fd_not_a_dir: return -ENOTDIR; @@ -425,7 +429,8 @@ hook_access(const char* path, int mask) { } return ret; } - return syscall_no_intercept_wrapper(SYS_access, rel_path.c_str(), mask); + return gsl::narrow_cast( + syscall_no_intercept_wrapper(SYS_access, rel_path.c_str(), mask)); } #endif @@ -439,12 +444,12 @@ hook_faccessat(int dirfd, const char* cpath, int mode) { auto rstatus = CTX->relativize_fd_path(dirfd, cpath, resolved); switch(rstatus) { case gkfs::preload::RelativizeStatus::fd_unknown: - return syscall_no_intercept_wrapper(SYS_faccessat, dirfd, cpath, - mode); + return gsl::narrow_cast(syscall_no_intercept_wrapper( + SYS_faccessat, dirfd, cpath, mode)); case gkfs::preload::RelativizeStatus::external: - return syscall_no_intercept_wrapper(SYS_faccessat, dirfd, - resolved.c_str(), mode); + return gsl::narrow_cast(syscall_no_intercept_wrapper( + SYS_faccessat, dirfd, resolved.c_str(), mode)); case gkfs::preload::RelativizeStatus::fd_not_a_dir: return -ENOTDIR; @@ -470,12 +475,12 @@ hook_faccessat2(int dirfd, const char* cpath, int mode, int flags) { auto rstatus = CTX->relativize_fd_path(dirfd, cpath, resolved); switch(rstatus) { case gkfs::preload::RelativizeStatus::fd_unknown: - return syscall_no_intercept_wrapper(SYS_faccessat2, dirfd, cpath, - mode, flags); + return gsl::narrow_cast(syscall_no_intercept_wrapper( + SYS_faccessat2, dirfd, cpath, mode, flags)); case gkfs::preload::RelativizeStatus::external: - return syscall_no_intercept_wrapper(SYS_faccessat2, dirfd, - resolved.c_str(), mode, flags); + return gsl::narrow_cast(syscall_no_intercept_wrapper( + SYS_faccessat2, dirfd, resolved.c_str(), mode, flags)); case gkfs::preload::RelativizeStatus::fd_not_a_dir: return -ENOTDIR; @@ -521,7 +526,8 @@ hook_truncate(const char* path, long length) { if(CTX->relativize_path(path, rel_path)) { return with_errno(gkfs::syscall::gkfs_truncate(rel_path, length)); } - return syscall_no_intercept_wrapper(SYS_truncate, rel_path.c_str(), length); + return gsl::narrow_cast(syscall_no_intercept_wrapper( + SYS_truncate, rel_path.c_str(), length)); } int @@ -533,7 +539,8 @@ hook_ftruncate(unsigned int fd, unsigned long length) { auto path = CTX->file_map()->get(fd)->path(); return with_errno(gkfs::syscall::gkfs_truncate(path, length)); } - return syscall_no_intercept_wrapper(SYS_ftruncate, fd, length); + return gsl::narrow_cast( + syscall_no_intercept_wrapper(SYS_ftruncate, fd, length)); } int @@ -544,7 +551,7 @@ hook_dup(unsigned int fd) { if(CTX->file_map()->exist(fd)) { return with_errno(gkfs::syscall::gkfs_dup(fd)); } - return syscall_no_intercept_wrapper(SYS_dup, fd); + return gsl::narrow_cast(syscall_no_intercept_wrapper(SYS_dup, fd)); } #ifdef SYS_dup2 int @@ -555,7 +562,8 @@ hook_dup2(unsigned int oldfd, unsigned int newfd) { if(CTX->file_map()->exist(oldfd)) { return with_errno(gkfs::syscall::gkfs_dup2(oldfd, newfd)); } - return syscall_no_intercept_wrapper(SYS_dup2, oldfd, newfd); + return gsl::narrow_cast( + syscall_no_intercept_wrapper(SYS_dup2, oldfd, newfd)); } #endif int @@ -570,10 +578,11 @@ hook_dup3(unsigned int oldfd, unsigned int newfd, int flags) { LOG(WARNING, "{}() Not supported", __func__); return -ENOTSUP; } - return syscall_no_intercept_wrapper(SYS_dup3, oldfd, newfd, flags); + return gsl::narrow_cast( + syscall_no_intercept_wrapper(SYS_dup3, oldfd, newfd, flags)); } #ifdef SYS_getdents -int +long hook_getdents(unsigned int fd, struct linux_dirent* dirp, unsigned int count) { LOG(DEBUG, "{}() called with fd: {}, dirp: {}, count: {}", __func__, fd, @@ -586,7 +595,7 @@ hook_getdents(unsigned int fd, struct linux_dirent* dirp, unsigned int count) { } #endif -int +ssize_t hook_getdents64(unsigned int fd, struct linux_dirent64* dirp, unsigned int count) { @@ -596,7 +605,7 @@ hook_getdents64(unsigned int fd, struct linux_dirent64* dirp, if(CTX->file_map()->exist(fd)) { return with_errno(gkfs::syscall::gkfs_getdents64(fd, dirp, count)); } - return gsl::narrow(syscall_no_intercept_wrapper(SYS_getdents64, fd, dirp, count)); + return syscall_no_intercept_wrapper(SYS_getdents64, fd, dirp, count); } @@ -610,12 +619,12 @@ hook_mkdirat(int dirfd, const char* cpath, mode_t mode) { auto rstatus = CTX->relativize_fd_path(dirfd, cpath, resolved); switch(rstatus) { case gkfs::preload::RelativizeStatus::external: - return syscall_no_intercept_wrapper(SYS_mkdirat, dirfd, - resolved.c_str(), mode); + return gsl::narrow_cast(syscall_no_intercept_wrapper( + SYS_mkdirat, dirfd, resolved.c_str(), mode)); case gkfs::preload::RelativizeStatus::fd_unknown: - return syscall_no_intercept_wrapper(SYS_mkdirat, dirfd, cpath, - mode); + return gsl::narrow_cast(syscall_no_intercept_wrapper( + SYS_mkdirat, dirfd, cpath, mode)); case gkfs::preload::RelativizeStatus::fd_not_a_dir: return -ENOTDIR; @@ -640,12 +649,12 @@ hook_fchmodat(int dirfd, const char* cpath, mode_t mode) { auto rstatus = CTX->relativize_fd_path(dirfd, cpath, resolved); switch(rstatus) { case gkfs::preload::RelativizeStatus::fd_unknown: - return syscall_no_intercept_wrapper(SYS_fchmodat, dirfd, cpath, - mode); + return gsl::narrow_cast(syscall_no_intercept_wrapper( + SYS_fchmodat, dirfd, cpath, mode)); case gkfs::preload::RelativizeStatus::external: - return syscall_no_intercept_wrapper(SYS_fchmodat, dirfd, - resolved.c_str(), mode); + return gsl::narrow_cast(syscall_no_intercept_wrapper( + SYS_fchmodat, dirfd, resolved.c_str(), mode)); case gkfs::preload::RelativizeStatus::fd_not_a_dir: return -ENOTDIR; @@ -669,7 +678,8 @@ hook_fchmod(unsigned int fd, mode_t mode) { LOG(WARNING, "{}() operation not supported", __func__); return -ENOTSUP; } - return syscall_no_intercept_wrapper(SYS_fchmod, fd, mode); + return gsl::narrow_cast( + syscall_no_intercept_wrapper(SYS_fchmod, fd, mode)); } int @@ -760,7 +770,7 @@ hook_getcwd(char* buf, unsigned long size) { return (CTX->cwd().size() + 1); } -int +ssize_t hook_readlinkat(int dirfd, const char* cpath, char* buf, int bufsiz) { LOG(DEBUG, "{}() called with dirfd: {}, path \"{}\", buf: {}, bufsize: {}", @@ -797,7 +807,8 @@ hook_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg) { arg); if(!CTX->file_map()->exist(fd)) { - return syscall_no_intercept_wrapper(SYS_fcntl, fd, cmd, arg); + return gsl::narrow_cast( + syscall_no_intercept_wrapper(SYS_fcntl, fd, cmd, arg)); } int ret; switch(cmd) { @@ -928,8 +939,8 @@ hook_renameat(int olddfd, const char* oldname, int newdfd, const char* newname, return -EINVAL; } - return syscall_no_intercept_wrapper(SYS_renameat2, olddfd, oldpath_pass, - newdfd, newpath_pass, flags); + return gsl::narrow_cast(syscall_no_intercept_wrapper( + SYS_renameat2, olddfd, oldpath_pass, newdfd, newpath_pass, flags)); } int @@ -942,7 +953,8 @@ hook_statfs(const char* path, struct statfs* buf) { if(CTX->relativize_path(path, rel_path)) { return with_errno(gkfs::syscall::gkfs_statfs(buf)); } - return syscall_no_intercept_wrapper(SYS_statfs, rel_path.c_str(), buf); + return gsl::narrow_cast( + syscall_no_intercept_wrapper(SYS_statfs, rel_path.c_str(), buf)); } int @@ -953,7 +965,8 @@ hook_fstatfs(unsigned int fd, struct statfs* buf) { if(CTX->file_map()->exist(fd)) { return with_errno(gkfs::syscall::gkfs_statfs(buf)); } - return syscall_no_intercept_wrapper(SYS_fstatfs, fd, buf); + return gsl::narrow_cast( + syscall_no_intercept_wrapper(SYS_fstatfs, fd, buf)); } /* The function should broadcast a flush message (pmem_persist i.e.) if the @@ -965,10 +978,11 @@ hook_fsync(unsigned int fd) { if(CTX->file_map()->exist(fd)) { return with_errno(gkfs::syscall::gkfs_fsync(fd)); } - return syscall_no_intercept_wrapper(SYS_fsync, fd); + + return gsl::narrow_cast(syscall_no_intercept_wrapper(SYS_fsync, fd)); } -int +ssize_t hook_getxattr(const char* path, const char* name, void* value, size_t size) { LOG(DEBUG, "{}() called with path '{}' name '{}' value '{}' size '{}'", -- GitLab From 95147a1ff0e1b65c77ad6b854afa3a0111be7f17 Mon Sep 17 00:00:00 2001 From: Marc Vef Date: Fri, 8 Mar 2024 19:03:03 +0100 Subject: [PATCH 4/5] Review --- include/client/hooks.hpp | 2 +- src/client/hooks.cpp | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/client/hooks.hpp b/include/client/hooks.hpp index 4b242bc9e..97765d27b 100644 --- a/include/client/hooks.hpp +++ b/include/client/hooks.hpp @@ -100,7 +100,7 @@ hook_read(unsigned int fd, void* buf, size_t count); ssize_t hook_pread(unsigned int fd, char* buf, size_t count, loff_t pos); -int +ssize_t hook_readv(unsigned long fd, const struct iovec* iov, unsigned long iovcnt); ssize_t diff --git a/src/client/hooks.cpp b/src/client/hooks.cpp index 91c34c300..6f1b1c771 100644 --- a/src/client/hooks.cpp +++ b/src/client/hooks.cpp @@ -36,22 +36,24 @@ #include #include -#include + +#include #include -#include extern "C" { #include #include #include +#include } namespace { // TODO replace all internal gkfs errno variable usage with LEAF -inline int -with_errno(int ret) { +template +inline T +with_errno(T ret) { return (ret < 0) ? -errno : ret; } @@ -149,8 +151,6 @@ hook_statx(int dirfd, const char* path, int flags, unsigned int mask, LOG(ERROR, "{}() relativize status unknown: {}", __func__); return -EINVAL; } - - return syscall_no_intercept(SYS_statx, dirfd, path, flags, mask, buf); } #endif @@ -247,7 +247,7 @@ hook_pread(unsigned int fd, char* buf, size_t count, loff_t pos) { return syscall_no_intercept_wrapper(SYS_pread64, fd, buf, count, pos); } -int +ssize_t hook_readv(unsigned long fd, const struct iovec* iov, unsigned long iovcnt) { LOG(DEBUG, "{}() called with fd: {}, iov: {}, iovcnt: {}", __func__, fd, -- GitLab From 3c95f4e79ec508e6133fb1505049233a25a9e12d Mon Sep 17 00:00:00 2001 From: Julius Athenstaedt Date: Tue, 16 Apr 2024 14:10:14 +0200 Subject: [PATCH 5/5] update Changelog --- CHANGELOG.md | 2 ++ src/client/CMakeLists.txt | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 66be59338..5b5f4cf3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -87,6 +87,8 @@ replicas ([!166](https://storage.bsc.es/gitlab/hpc/gekkofs/-/merge_requests/141) ### New +- Use of GSL to narrow cast syscall return types, corrected syscall return types + ([!182](https://storage.bsc.es/gitlab/hpc/gekkofs/-/merge_requests/182)). - Support for client-side per process logging, activated with `LIBGKFS_LOG_PER_PROCESS` ([!179](https://storage.bsc.es/gitlab/hpc/gekkofs/-/merge_requests/179)). - Support mtime with option gkfs::config::metadata:: diff --git a/src/client/CMakeLists.txt b/src/client/CMakeLists.txt index bd838f7d7..c31abeedc 100644 --- a/src/client/CMakeLists.txt +++ b/src/client/CMakeLists.txt @@ -97,7 +97,7 @@ target_link_libraries( fmt::fmt Threads::Threads Syscall_intercept::Syscall_intercept - Microsoft::GSL + Microsoft.GSL::GSL ) # Enable MSGPack metrics for intercept only if (GKFS_ENABLE_CLIENT_METRICS) @@ -117,7 +117,7 @@ target_link_libraries( hermes fmt::fmt Threads::Threads - Microsoft::GSL + Microsoft.GSL::GSL ) install( -- GitLab