Skip to content

Commit 3c51fc0

Browse files
committed
Be more precise with some errno return values
1 parent acf923b commit 3c51fc0

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

lib/tinykvm/linux/system_calls.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ void Machine::setup_linux_system_calls()
171171
regs.rax = fstat(fd, &vstat);
172172
if (regs.rax == 0) {
173173
cpu.machine().copy_to_guest(regs.rsi, &vstat, sizeof(vstat));
174+
} else {
175+
regs.rax = -errno;
174176
}
175177
} catch (...) {
176178
regs.rax = -EBADF;
@@ -1118,7 +1120,7 @@ void Machine::setup_linux_system_calls()
11181120
if (fd > 0) {
11191121
regs.rax = cpu.machine().fds().manage(fd, false);
11201122
} else {
1121-
regs.rax = -1;
1123+
regs.rax = -errno;
11221124
}
11231125
} catch (...) {
11241126
regs.rax = -1;
@@ -1154,9 +1156,10 @@ void Machine::setup_linux_system_calls()
11541156

11551157
struct stat64 vstat;
11561158
// Path is in allow-list
1157-
regs.rax = fstatat64(fd, path.c_str(), &vstat, flags);
1158-
if (regs.rax == 0) {
1159+
const int result = fstatat64(fd, path.c_str(), &vstat, flags);
1160+
if (result == 0) {
11591161
cpu.machine().copy_to_guest(buffer, &vstat, sizeof(vstat));
1162+
regs.rax = 0;
11601163
} else {
11611164
regs.rax = -errno;
11621165
}
@@ -1379,26 +1382,31 @@ void Machine::setup_linux_system_calls()
13791382
Machine::install_syscall_handler(
13801383
SYS_statx, [] (vCPU& cpu) { // STATX
13811384
auto& regs = cpu.registers();
1382-
long fd = AT_FDCWD; // rdi
1385+
const int vfd = regs.rdi;
13831386
const auto vpath = regs.rsi;
13841387
const auto flags = regs.rdx;
13851388
const auto mask = regs.r10;
13861389
const auto buffer = regs.r8;
13871390
std::string path;
1391+
int fd = AT_FDCWD;
13881392

13891393
try {
13901394
path = cpu.machine().memcstring(vpath, PATH_MAX);
13911395
if (!cpu.machine().fds().is_readable_path(path)) {
13921396
regs.rax = -EPERM;
13931397
} else {
13941398
// Translate from vfd when fd != AT_FDCWD
1395-
if ((long)regs.rdi != AT_FDCWD)
1396-
fd = cpu.machine().fds().translate(regs.rdi);
1399+
if (vfd != AT_FDCWD)
1400+
fd = cpu.machine().fds().translate(vfd);
13971401

13981402
struct statx vstat;
1399-
regs.rax = statx(fd, path.c_str(), flags, mask, &vstat);
1400-
if (regs.rax == 0) {
1403+
const int result =
1404+
statx(fd, path.c_str(), flags, mask, &vstat);
1405+
if (result == 0) {
14011406
cpu.machine().copy_to_guest(buffer, &vstat, sizeof(vstat));
1407+
regs.rax = 0;
1408+
} else {
1409+
regs.rax = -errno;
14021410
}
14031411
}
14041412
} catch (...) {

0 commit comments

Comments
 (0)