Loading src/client/hooks.cpp +19 −27 Changes for src/client/hooks.cpp: 19 added lines, 27 removed lines. Original line number Diff line number Diff line Loading @@ -808,8 +808,8 @@ hook_ftruncate(unsigned int fd, unsigned long length) { LOG(DEBUG, "{}() called with fd: {}, offset: {}", __func__, fd, length); if(CTX->file_map()->exist(fd)) { auto path = CTX->file_map()->get(fd)->path(); if(auto file = CTX->file_map()->get(fd)) { auto path = file->path(); return with_errno(gkfs::syscall::gkfs_truncate(path, length)); } return gsl::narrow_cast<int>( Loading Loading @@ -1199,8 +1199,8 @@ hook_futimens(unsigned int fd, const struct timespec times[2]) { LOG(DEBUG, "{}() called with fd: {}, times: {}", __func__, fd, fmt::ptr(times)); if(CTX->file_map()->exist(fd)) { auto path = CTX->file_map()->get(fd)->path(); if(auto file = CTX->file_map()->get(fd)) { auto path = file->path(); return with_errno(gkfs::syscall::gkfs_utimensat(path, times)); } return gsl::narrow_cast<int>( Loading Loading @@ -1250,7 +1250,9 @@ hook_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg) { // cmd, // arg); if(!CTX->file_map()->exist(fd)) { // Keep the open file alive if another thread closes fd during this call. auto file = CTX->file_map()->get(fd); if(!file) { return gsl::narrow_cast<int>( syscall_no_intercept_wrapper(SYS_fcntl, fd, cmd, arg)); } Loading @@ -1267,14 +1269,12 @@ hook_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg) { if(ret == -1) { return -errno; } CTX->file_map()->get(fd)->set_flag( gkfs::filemap::OpenFile_flags::cloexec, true); file->set_flag(gkfs::filemap::OpenFile_flags::cloexec, true); return ret; case F_GETFD: LOG(DEBUG, "{}() F_GETFD on fd {}", __func__, fd); if(CTX->file_map()->get(fd)->get_flag( gkfs::filemap::OpenFile_flags::cloexec)) { if(file->get_flag(gkfs::filemap::OpenFile_flags::cloexec)) { return FD_CLOEXEC; } return 0; Loading @@ -1282,16 +1282,13 @@ hook_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg) { case F_GETFL: LOG(DEBUG, "{}() F_GETFL on fd {}", __func__, fd); ret = 0; if(CTX->file_map()->get(fd)->get_flag( gkfs::filemap::OpenFile_flags::rdonly)) { if(file->get_flag(gkfs::filemap::OpenFile_flags::rdonly)) { ret |= O_RDONLY; } if(CTX->file_map()->get(fd)->get_flag( gkfs::filemap::OpenFile_flags::wronly)) { if(file->get_flag(gkfs::filemap::OpenFile_flags::wronly)) { ret |= O_WRONLY; } if(CTX->file_map()->get(fd)->get_flag( gkfs::filemap::OpenFile_flags::rdwr)) { if(file->get_flag(gkfs::filemap::OpenFile_flags::rdwr)) { ret |= O_RDWR; } return ret; Loading @@ -1299,20 +1296,16 @@ hook_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg) { LOG(DEBUG, "{}() F_SETFL on fd {}", __func__, fd); // get flags from arg and setup if(arg & O_RDONLY) { CTX->file_map()->get(fd)->set_flag( gkfs::filemap::OpenFile_flags::rdonly, true); file->set_flag(gkfs::filemap::OpenFile_flags::rdonly, true); } if(arg & O_WRONLY) { CTX->file_map()->get(fd)->set_flag( gkfs::filemap::OpenFile_flags::wronly, true); file->set_flag(gkfs::filemap::OpenFile_flags::wronly, true); } if(arg & O_RDWR) { CTX->file_map()->get(fd)->set_flag( gkfs::filemap::OpenFile_flags::rdwr, true); file->set_flag(gkfs::filemap::OpenFile_flags::rdwr, true); } if(arg & O_APPEND) { CTX->file_map()->get(fd)->set_flag( gkfs::filemap::OpenFile_flags::append, true); file->set_flag(gkfs::filemap::OpenFile_flags::append, true); } if(arg & O_NONBLOCK) { LOG(DEBUG, "[GKFS] F_SETFL {} NONBLOCK", fd); Loading @@ -1322,16 +1315,15 @@ hook_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg) { } if(arg & O_CLOEXEC) { LOG(DEBUG, "[GKFS] F_SETFL {} CLOEXEC", fd); CTX->file_map()->get(fd)->set_flag( gkfs::filemap::OpenFile_flags::cloexec, true); file->set_flag(gkfs::filemap::OpenFile_flags::cloexec, true); } return 0; case F_SETFD: LOG(DEBUG, "{}() [fd: {}, cmd: F_SETFD, FD_CLOEXEC: {}]", __func__, fd, (arg & FD_CLOEXEC)); CTX->file_map()->get(fd)->set_flag( gkfs::filemap::OpenFile_flags::cloexec, (arg & FD_CLOEXEC)); file->set_flag(gkfs::filemap::OpenFile_flags::cloexec, (arg & FD_CLOEXEC)); return 0; default: LOG(ERROR, "{}() unrecognized command {} on fd {}", __func__, cmd, Loading src/client/open_file_map.cpp +12 −5 Changes for src/client/open_file_map.cpp: 12 added lines, 5 removed lines. Original line number Diff line number Diff line Loading @@ -292,14 +292,21 @@ OpenFileMap::dup(const int oldfd) { return -1; } auto newfd = safe_generate_fd_idx_(); if(newfd < 0) return newfd; auto& shard = get_shard(newfd); lock_guard<recursive_mutex> lock(shard.mutex); shard.files.insert(make_pair(newfd, open_file)); total_files_++; return newfd; } int OpenFileMap::dup2(const int oldfd, const int newfd) { if(newfd < 0) { errno = EBADF; return -1; } auto open_file = get(oldfd); if(open_file == nullptr) { errno = EBADF; Loading @@ -307,10 +314,6 @@ OpenFileMap::dup2(const int oldfd, const int newfd) { } if(oldfd == newfd) return newfd; // remove newfd if exists in filemap silently if(exist(newfd)) { remove(newfd); } // to prevent duplicate fd idx in the future. First three fd are reservered // by os streams that we do not overwrite if(get_fd_idx() < newfd && newfd != 0 && newfd != 1 && newfd != 2) Loading @@ -318,7 +321,11 @@ OpenFileMap::dup2(const int oldfd, const int newfd) { auto& shard = get_shard(newfd); lock_guard<recursive_mutex> lock(shard.mutex); shard.files.insert(make_pair(newfd, open_file)); // Replace the target under one lock. Removing it first would expose a // transient missing descriptor to concurrent lookups and duplications. auto result = shard.files.insert_or_assign(newfd, open_file); if(result.second) total_files_++; return newfd; } Loading tests/integration/syscalls/test_fd_close_races.py 0 → 100644 +84 −0 Changes for tests/integration/syscalls/test_fd_close_races.py: 84 added lines, 0 removed lines. Original line number Diff line number Diff line import sys import pytest def test_dup_keeps_last_descriptor_visible(gkfs_shell, gkfs_daemon): """Duplicated descriptors must keep the map nonempty after source close.""" shell = gkfs_shell command = shell.run("env", "LIBGKFS_LOG=none", sys.executable, "-c", """ import fcntl import os import sys path = sys.argv[1] for duplicate in (lambda fd: os.dup(fd), lambda fd: os.dup2(fd, 200000)): fd = os.open(path, os.O_CREAT | os.O_RDWR, 0o600) copy = duplicate(fd) os.close(fd) assert fcntl.fcntl(copy, fcntl.F_GETFL) & os.O_RDWR os.ftruncate(copy, 0) assert os.fstat(copy).st_size == 0 os.close(copy) # Replacing an existing target must not lose or double-count its map entry. fd = os.open(path, os.O_RDWR) target = os.open(path, os.O_RDWR) assert os.dup2(fd, target) == target os.close(fd) assert os.fstat(target).st_size == 0 os.close(target) os.unlink(path) print('PASS duplicate descriptor lifetime') """, str(gkfs_daemon.mountdir / "dup-lifetime"), timeout=60) assert command.exit_code == 0, command.stderr.decode(errors="replace") assert b"PASS duplicate descriptor lifetime" in command.stdout @pytest.mark.parametrize("shell_fixture", ["gkfs_shell", "gkfs_shellLibc"]) def test_fd_hooks_racing_close(shell_fixture, request, gkfs_daemon): """Same-process close must not invalidate a hook's retained open file.""" shell = request.getfixturevalue(shell_fixture) command = shell.run("env", "LIBGKFS_LOG=none", sys.executable, "-c", """ import concurrent.futures import errno import fcntl import os import sys import threading path = sys.argv[1] source = os.open(path, os.O_CREAT | os.O_RDWR, 0o600) target = 200000 ready = threading.Barrier(5) def worker(operation): ready.wait(timeout=20) for _ in range(1000): try: operation() except OSError as error: # Either the hook acquired the file before close, or it sees EBADF. assert error.errno == errno.EBADF, error operations = ( lambda: fcntl.fcntl(target, fcntl.F_GETFL), lambda: fcntl.fcntl(target, fcntl.F_SETFD, fcntl.FD_CLOEXEC), lambda: os.ftruncate(target, 0), lambda: os.utime(target, ns=(1000000000, 1000000000)), ) with concurrent.futures.ThreadPoolExecutor(max_workers=4) as pool: futures = [pool.submit(worker, operation) for operation in operations] ready.wait(timeout=20) for _ in range(1000): os.dup2(source, target) os.close(target) for future in futures: future.result(timeout=30) os.close(source) os.unlink(path) print('PASS concurrent fd hooks and close') """, str(gkfs_daemon.mountdir / "close-race"), timeout=120) assert command.exit_code == 0, command.stderr.decode(errors="replace") assert b"PASS concurrent fd hooks and close" in command.stdout No newline at end of file Loading
src/client/hooks.cpp +19 −27 Changes for src/client/hooks.cpp: 19 added lines, 27 removed lines. Original line number Diff line number Diff line Loading @@ -808,8 +808,8 @@ hook_ftruncate(unsigned int fd, unsigned long length) { LOG(DEBUG, "{}() called with fd: {}, offset: {}", __func__, fd, length); if(CTX->file_map()->exist(fd)) { auto path = CTX->file_map()->get(fd)->path(); if(auto file = CTX->file_map()->get(fd)) { auto path = file->path(); return with_errno(gkfs::syscall::gkfs_truncate(path, length)); } return gsl::narrow_cast<int>( Loading Loading @@ -1199,8 +1199,8 @@ hook_futimens(unsigned int fd, const struct timespec times[2]) { LOG(DEBUG, "{}() called with fd: {}, times: {}", __func__, fd, fmt::ptr(times)); if(CTX->file_map()->exist(fd)) { auto path = CTX->file_map()->get(fd)->path(); if(auto file = CTX->file_map()->get(fd)) { auto path = file->path(); return with_errno(gkfs::syscall::gkfs_utimensat(path, times)); } return gsl::narrow_cast<int>( Loading Loading @@ -1250,7 +1250,9 @@ hook_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg) { // cmd, // arg); if(!CTX->file_map()->exist(fd)) { // Keep the open file alive if another thread closes fd during this call. auto file = CTX->file_map()->get(fd); if(!file) { return gsl::narrow_cast<int>( syscall_no_intercept_wrapper(SYS_fcntl, fd, cmd, arg)); } Loading @@ -1267,14 +1269,12 @@ hook_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg) { if(ret == -1) { return -errno; } CTX->file_map()->get(fd)->set_flag( gkfs::filemap::OpenFile_flags::cloexec, true); file->set_flag(gkfs::filemap::OpenFile_flags::cloexec, true); return ret; case F_GETFD: LOG(DEBUG, "{}() F_GETFD on fd {}", __func__, fd); if(CTX->file_map()->get(fd)->get_flag( gkfs::filemap::OpenFile_flags::cloexec)) { if(file->get_flag(gkfs::filemap::OpenFile_flags::cloexec)) { return FD_CLOEXEC; } return 0; Loading @@ -1282,16 +1282,13 @@ hook_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg) { case F_GETFL: LOG(DEBUG, "{}() F_GETFL on fd {}", __func__, fd); ret = 0; if(CTX->file_map()->get(fd)->get_flag( gkfs::filemap::OpenFile_flags::rdonly)) { if(file->get_flag(gkfs::filemap::OpenFile_flags::rdonly)) { ret |= O_RDONLY; } if(CTX->file_map()->get(fd)->get_flag( gkfs::filemap::OpenFile_flags::wronly)) { if(file->get_flag(gkfs::filemap::OpenFile_flags::wronly)) { ret |= O_WRONLY; } if(CTX->file_map()->get(fd)->get_flag( gkfs::filemap::OpenFile_flags::rdwr)) { if(file->get_flag(gkfs::filemap::OpenFile_flags::rdwr)) { ret |= O_RDWR; } return ret; Loading @@ -1299,20 +1296,16 @@ hook_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg) { LOG(DEBUG, "{}() F_SETFL on fd {}", __func__, fd); // get flags from arg and setup if(arg & O_RDONLY) { CTX->file_map()->get(fd)->set_flag( gkfs::filemap::OpenFile_flags::rdonly, true); file->set_flag(gkfs::filemap::OpenFile_flags::rdonly, true); } if(arg & O_WRONLY) { CTX->file_map()->get(fd)->set_flag( gkfs::filemap::OpenFile_flags::wronly, true); file->set_flag(gkfs::filemap::OpenFile_flags::wronly, true); } if(arg & O_RDWR) { CTX->file_map()->get(fd)->set_flag( gkfs::filemap::OpenFile_flags::rdwr, true); file->set_flag(gkfs::filemap::OpenFile_flags::rdwr, true); } if(arg & O_APPEND) { CTX->file_map()->get(fd)->set_flag( gkfs::filemap::OpenFile_flags::append, true); file->set_flag(gkfs::filemap::OpenFile_flags::append, true); } if(arg & O_NONBLOCK) { LOG(DEBUG, "[GKFS] F_SETFL {} NONBLOCK", fd); Loading @@ -1322,16 +1315,15 @@ hook_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg) { } if(arg & O_CLOEXEC) { LOG(DEBUG, "[GKFS] F_SETFL {} CLOEXEC", fd); CTX->file_map()->get(fd)->set_flag( gkfs::filemap::OpenFile_flags::cloexec, true); file->set_flag(gkfs::filemap::OpenFile_flags::cloexec, true); } return 0; case F_SETFD: LOG(DEBUG, "{}() [fd: {}, cmd: F_SETFD, FD_CLOEXEC: {}]", __func__, fd, (arg & FD_CLOEXEC)); CTX->file_map()->get(fd)->set_flag( gkfs::filemap::OpenFile_flags::cloexec, (arg & FD_CLOEXEC)); file->set_flag(gkfs::filemap::OpenFile_flags::cloexec, (arg & FD_CLOEXEC)); return 0; default: LOG(ERROR, "{}() unrecognized command {} on fd {}", __func__, cmd, Loading
src/client/open_file_map.cpp +12 −5 Changes for src/client/open_file_map.cpp: 12 added lines, 5 removed lines. Original line number Diff line number Diff line Loading @@ -292,14 +292,21 @@ OpenFileMap::dup(const int oldfd) { return -1; } auto newfd = safe_generate_fd_idx_(); if(newfd < 0) return newfd; auto& shard = get_shard(newfd); lock_guard<recursive_mutex> lock(shard.mutex); shard.files.insert(make_pair(newfd, open_file)); total_files_++; return newfd; } int OpenFileMap::dup2(const int oldfd, const int newfd) { if(newfd < 0) { errno = EBADF; return -1; } auto open_file = get(oldfd); if(open_file == nullptr) { errno = EBADF; Loading @@ -307,10 +314,6 @@ OpenFileMap::dup2(const int oldfd, const int newfd) { } if(oldfd == newfd) return newfd; // remove newfd if exists in filemap silently if(exist(newfd)) { remove(newfd); } // to prevent duplicate fd idx in the future. First three fd are reservered // by os streams that we do not overwrite if(get_fd_idx() < newfd && newfd != 0 && newfd != 1 && newfd != 2) Loading @@ -318,7 +321,11 @@ OpenFileMap::dup2(const int oldfd, const int newfd) { auto& shard = get_shard(newfd); lock_guard<recursive_mutex> lock(shard.mutex); shard.files.insert(make_pair(newfd, open_file)); // Replace the target under one lock. Removing it first would expose a // transient missing descriptor to concurrent lookups and duplications. auto result = shard.files.insert_or_assign(newfd, open_file); if(result.second) total_files_++; return newfd; } Loading
tests/integration/syscalls/test_fd_close_races.py 0 → 100644 +84 −0 Changes for tests/integration/syscalls/test_fd_close_races.py: 84 added lines, 0 removed lines. Original line number Diff line number Diff line import sys import pytest def test_dup_keeps_last_descriptor_visible(gkfs_shell, gkfs_daemon): """Duplicated descriptors must keep the map nonempty after source close.""" shell = gkfs_shell command = shell.run("env", "LIBGKFS_LOG=none", sys.executable, "-c", """ import fcntl import os import sys path = sys.argv[1] for duplicate in (lambda fd: os.dup(fd), lambda fd: os.dup2(fd, 200000)): fd = os.open(path, os.O_CREAT | os.O_RDWR, 0o600) copy = duplicate(fd) os.close(fd) assert fcntl.fcntl(copy, fcntl.F_GETFL) & os.O_RDWR os.ftruncate(copy, 0) assert os.fstat(copy).st_size == 0 os.close(copy) # Replacing an existing target must not lose or double-count its map entry. fd = os.open(path, os.O_RDWR) target = os.open(path, os.O_RDWR) assert os.dup2(fd, target) == target os.close(fd) assert os.fstat(target).st_size == 0 os.close(target) os.unlink(path) print('PASS duplicate descriptor lifetime') """, str(gkfs_daemon.mountdir / "dup-lifetime"), timeout=60) assert command.exit_code == 0, command.stderr.decode(errors="replace") assert b"PASS duplicate descriptor lifetime" in command.stdout @pytest.mark.parametrize("shell_fixture", ["gkfs_shell", "gkfs_shellLibc"]) def test_fd_hooks_racing_close(shell_fixture, request, gkfs_daemon): """Same-process close must not invalidate a hook's retained open file.""" shell = request.getfixturevalue(shell_fixture) command = shell.run("env", "LIBGKFS_LOG=none", sys.executable, "-c", """ import concurrent.futures import errno import fcntl import os import sys import threading path = sys.argv[1] source = os.open(path, os.O_CREAT | os.O_RDWR, 0o600) target = 200000 ready = threading.Barrier(5) def worker(operation): ready.wait(timeout=20) for _ in range(1000): try: operation() except OSError as error: # Either the hook acquired the file before close, or it sees EBADF. assert error.errno == errno.EBADF, error operations = ( lambda: fcntl.fcntl(target, fcntl.F_GETFL), lambda: fcntl.fcntl(target, fcntl.F_SETFD, fcntl.FD_CLOEXEC), lambda: os.ftruncate(target, 0), lambda: os.utime(target, ns=(1000000000, 1000000000)), ) with concurrent.futures.ThreadPoolExecutor(max_workers=4) as pool: futures = [pool.submit(worker, operation) for operation in operations] ready.wait(timeout=20) for _ in range(1000): os.dup2(source, target) os.close(target) for future in futures: future.result(timeout=30) os.close(source) os.unlink(path) print('PASS concurrent fd hooks and close') """, str(gkfs_daemon.mountdir / "close-race"), timeout=120) assert command.exit_code == 0, command.stderr.decode(errors="replace") assert b"PASS concurrent fd hooks and close" in command.stdout No newline at end of file