Skip to content

RuntimeModule: make the C++ code actually build on Windows #83132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions stdlib/public/RuntimeModule/Win32Extras.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,49 @@

#ifdef _WIN32

#include "modules/OS/Libc.h"

#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>

#include "modules/OS/Libc.h"
#include <io.h>
#include <stddef.h>
#include <type_traits>

extern "C" ssize_t pread(int fd, void *buf, size_t nbyte, off_t offset) {
HANDLE hFile = _get_osfhandle(fd);
namespace {
namespace runtime {
using ssize_t = std::make_signed<size_t>::type;
}
}

extern "C" runtime::ssize_t pread(int fd, void *buf, size_t nbyte, size_t offset) {
intptr_t hFile = _get_osfhandle(fd);
OVERLAPPED ovl = {0};
DWORD dwBytesRead = 0;

ovl.Offset = (DWORD)offset;
ovl.OffsetHigh = (DWORD)(offset >> 32);
ovl.Offset = static_cast<DWORD>(offset & 0xffff);
ovl.OffsetHigh = static_cast<DWORD>(offset >> 32);

if (!ReadFile(hFile, buf, (DWORD)count, &dwBytesRead, &ovl)) {
if (!ReadFile(reinterpret_cast<HANDLE>(hFile), buf, static_cast<DWORD>(nbyte),
&dwBytesRead, &ovl)) {
errno = EIO;
return -1;
}

return dwBytesRead;
}

extern "C" ssize_t pwrite(int fd, const void *buf, size_t nbyte, off_t offset) {
HANDLE hFile = _get_osfhandle(fd);
extern "C" runtime::ssize_t pwrite(int fd, const void *buf, size_t nbyte, size_t offset) {
intptr_t hFile = _get_osfhandle(fd);
OVERLAPPED ovl = {0};
DWORD dwBytesRead = 0;

ovl.Offset = (DWORD)offset;
ovl.OffsetHigh = (DWORD)(offset >> 32);
ovl.Offset = static_cast<DWORD>(offset & 0xffff);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guessing this should be offset & 0xffffffff.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, yes!

ovl.OffsetHigh = static_cast<DWORD>(offset >> 32);

if (!WriteFile(hFile, buf, (DWORD)count, &dwBytesRead, &ovl)) {
if (!WriteFile(reinterpret_cast<HANDLE>(hFile), buf, static_cast<DWORD>(nbyte),
&dwBytesRead, &ovl)) {
errno = EIO;
return -1;
}
Expand All @@ -54,4 +68,3 @@ extern "C" ssize_t pwrite(int fd, const void *buf, size_t nbyte, off_t offset) {
}

#endif // _WIN32

10 changes: 10 additions & 0 deletions stdlib/public/RuntimeModule/modules/OS/Libc.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#if defined(_WIN32)
#include <io.h>
#endif

// .. Swift affordances ........................................................

Expand All @@ -33,7 +36,14 @@ namespace backtrace {
/* open() is usually declared as a variadic function; these don't import into
Swift. */
static inline int _swift_open(const char *filename, int oflag, int mode) {
#if defined(_WIN32)
int fh;
if (_sopen_s(&fh, filename, oflag, _SH_DENYNO, mode))
return -1;
return fh;
#else
return open(filename, oflag, mode);
#endif
}

/* errno is typically not going to be easily accessible (it's often a macro),
Expand Down