Skip to content
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

[mono] Move basic w32file* and w32process* functions to eventpipe #66731

Merged
merged 4 commits into from
Mar 18, 2022
Merged
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
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
/src/mono/mono/metadata/thread* @lateralusX @lambdageek
/src/mono/mono/metadata/w32* @lateralusX @lambdageek

/src/mono/mono/eventpipe @lateralusX @lambdageek

/src/mono/mono/mini @vargaz @lambdageek @SamMonoRT
/src/mono/mono/mini/*cfgdump* @vargaz
/src/mono/mono/mini/*exceptions* @vargaz @BrzVlad
Expand Down
11 changes: 4 additions & 7 deletions src/mono/cmake/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
/* Host Platform is Darwin */
#cmakedefine HOST_DARWIN 1

/* Host Platform is OSX or Mac Catalyst */
#cmakedefine HOST_OSX 1

/* Host Platform is iOS */
#cmakedefine HOST_IOS 1

Expand Down Expand Up @@ -154,9 +157,6 @@
/* Define to 1 if you have the <sys/sysctl.h> header file. */
#cmakedefine HAVE_SYS_SYSCTL_H 1

/* Define to 1 if you have the <libproc.h> header file. */
#cmakedefine HAVE_LIBPROC_H 1

/* Define to 1 if you have the <sys/prctl.h> header file. */
#cmakedefine HAVE_SYS_PRCTL_H 1

Expand Down Expand Up @@ -399,9 +399,6 @@
/* Define to 1 if you have the `getpwuid_r' function. */
#cmakedefine HAVE_GETPWUID_R 1

/* Define to 1 if you have the `readlink' function. */
#cmakedefine HAVE_READLINK 1

/* Define to 1 if you have the `chmod' function. */
#cmakedefine HAVE_CHMOD 1

Expand Down Expand Up @@ -819,7 +816,7 @@
/* The JIT/AOT targets Mac Catalyst */
#cmakedefine TARGET_MACCAT 1

/* The JIT/AOT targets OSX */
/* The JIT/AOT targets OSX or Mac Catalyst */
#cmakedefine TARGET_OSX 1

/* The JIT/AOT targets Apple platforms */
Expand Down
4 changes: 2 additions & 2 deletions src/mono/cmake/configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ ac_check_headers (
sys/types.h sys/stat.h sys/filio.h sys/sockio.h sys/utime.h sys/un.h sys/syscall.h sys/uio.h sys/param.h
sys/prctl.h sys/socket.h sys/utsname.h sys/select.h sys/poll.h sys/wait.h sts/auxv.h sys/resource.h
sys/ioctl.h sys/errno.h sys/sendfile.h sys/statvfs.h sys/statfs.h sys/mman.h sys/mount.h sys/time.h sys/random.h
strings.h stdint.h unistd.h signal.h setjmp.h syslog.h netdb.h utime.h semaphore.h libproc.h alloca.h ucontext.h pwd.h elf.h
strings.h stdint.h unistd.h signal.h setjmp.h syslog.h netdb.h utime.h semaphore.h alloca.h ucontext.h pwd.h elf.h
gnu/lib-names.h netinet/tcp.h netinet/in.h link.h arpa/inet.h unwind.h poll.h wchar.h linux/magic.h
android/legacy_signal_inlines.h execinfo.h pthread.h pthread_np.h net/if.h dirent.h
CommonCrypto/CommonDigest.h dlfcn.h getopt.h pwd.h alloca.h
Expand All @@ -77,7 +77,7 @@ ac_check_headers (
ac_check_funcs (
sigaction kill clock_nanosleep backtrace_symbols mkstemp mmap
getrusage dladdr sysconf getrlimit prctl nl_langinfo
sched_getaffinity sched_setaffinity getpwuid_r readlink chmod lstat getdtablesize ftruncate msync
sched_getaffinity sched_setaffinity getpwuid_r chmod lstat getdtablesize ftruncate msync
getpeername utime utimes openlog closelog atexit popen strerror_r inet_pton inet_aton
shm_open poll getfsstat mremap posix_fadvise vsnprintf sendfile statfs statvfs setpgid system
fork execv execve waitpid localtime_r mkdtemp getrandom execvp strlcpy stpcpy strtok_r rewinddir
Expand Down
195 changes: 191 additions & 4 deletions src/mono/mono/eventpipe/ep-rt-mono.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
#include <mono/mini/mini-runtime.h>
#include <mono/sgen/sgen-conf.h>
#include <mono/sgen/sgen-tagged-pointer.h>
#include "mono/utils/mono-logger-internals.h"
#include <mono/utils/mono-logger-internals.h>
#include <minipal/getexepath.h>
#include <runtime_version.h>
#include <clretwallmain.h>

Expand Down Expand Up @@ -1123,6 +1124,19 @@ ep_rt_mono_rand_try_get_bytes (
uint8_t *buffer,
size_t buffer_size);

ep_rt_file_handle_t
ep_rt_mono_file_open_write(const ep_char8_t *path);

bool
ep_rt_mono_file_close (ep_rt_file_handle_t handle);

bool
ep_rt_mono_file_write (
ep_rt_file_handle_t handle,
const uint8_t *buffer,
uint32_t numbytes,
uint32_t *byteswritten);

EventPipeThread *
ep_rt_mono_thread_get_or_create (void);

Expand Down Expand Up @@ -2069,6 +2083,179 @@ ep_rt_mono_rand_try_get_bytes (
return mono_rand_try_get_bytes (&_ep_rt_mono_rand_provider, (guchar *)buffer, (gssize)buffer_size, error);
}

char *
ep_rt_mono_get_managed_cmd_line ()
{
return mono_runtime_get_managed_cmd_line ();
}

char *
ep_rt_mono_get_os_cmd_line ()
{
MONO_REQ_GC_NEUTRAL_MODE;

// we only return the native host here since getting the full commandline is complicated and
// it's not super important to have the correct value since it'll only be used during startup
// until we have the managed commandline
char *host_path = minipal_getexepath ();

// minipal_getexepath doesn't use Mono APIs to allocate strings so
// we can't use g_free (which the callers of this method expect to do)
// so create another copy and return that one
char *res = g_strdup (host_path);
free (host_path);
return res;
}

#ifdef HOST_WIN32

ep_rt_file_handle_t
ep_rt_mono_file_open_write (const ep_char8_t *path)
{
if (!path)
return INVALID_HANDLE_VALUE;

ep_char16_t *path_utf16 = ep_rt_utf8_to_utf16_string (path, -1);

if (!path_utf16)
return INVALID_HANDLE_VALUE;

ep_rt_file_handle_t res;
MONO_ENTER_GC_SAFE;
res = (ep_rt_file_handle_t)CreateFileW (path_utf16, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
MONO_EXIT_GC_SAFE;
ep_rt_utf16_string_free (path_utf16);

return res;
}

bool
ep_rt_mono_file_close (ep_rt_file_handle_t handle)
{
bool res;
MONO_ENTER_GC_SAFE;
res = CloseHandle (handle);
MONO_EXIT_GC_SAFE;
return res;
}

static
void
win32_io_interrupt_handler (void *ignored)
{
}

bool
ep_rt_mono_file_write (
ep_rt_file_handle_t handle,
const uint8_t *buffer,
uint32_t numbytes,
uint32_t *byteswritten)
{
MONO_REQ_GC_UNSAFE_MODE;

bool res;
akoeplinger marked this conversation as resolved.
Show resolved Hide resolved
MonoThreadInfo *info = mono_thread_info_current ();
gboolean alerted = FALSE;

if (info) {
mono_thread_info_install_interrupt (win32_io_interrupt_handler, NULL, &alerted);
if (alerted) {
return false;
}
mono_win32_enter_blocking_io_call (info, handle);
}

MONO_ENTER_GC_SAFE;
if (info && mono_thread_info_is_interrupt_state (info)) {
res = false;
} else {
res = WriteFile (handle, buffer, numbytes, (PDWORD)byteswritten, NULL) ? true : false;
}
MONO_EXIT_GC_SAFE;

if (info) {
mono_win32_leave_blocking_io_call (info, handle);
mono_thread_info_uninstall_interrupt (&alerted);
}

return res;
}

#else

#include <fcntl.h>
#include <unistd.h>

ep_rt_file_handle_t
ep_rt_mono_file_open_write (const ep_char8_t *path)
{
int fd;
mode_t perms = 0666;

if (!path)
return INVALID_HANDLE_VALUE;

MONO_ENTER_GC_SAFE;
fd = creat (path, perms);
MONO_EXIT_GC_SAFE;

if (fd == -1)
return INVALID_HANDLE_VALUE;

return (ep_rt_file_handle_t)(ptrdiff_t)fd;
}

bool
ep_rt_mono_file_close (ep_rt_file_handle_t handle)
{
int fd = (int)(ptrdiff_t)handle;

MONO_ENTER_GC_SAFE;
close (fd);
MONO_EXIT_GC_SAFE;

return true;
}

bool
ep_rt_mono_file_write (
ep_rt_file_handle_t handle,
const uint8_t *buffer,
uint32_t numbytes,
uint32_t *byteswritten)
{
MONO_REQ_GC_UNSAFE_MODE;

int fd = (int)(ptrdiff_t)handle;
uint32_t ret;
MonoThreadInfo *info = mono_thread_info_current ();

if (byteswritten != NULL)
*byteswritten = 0;

do {
MONO_ENTER_GC_SAFE;
ret = write (fd, buffer, numbytes);
MONO_EXIT_GC_SAFE;
} while (ret == -1 && errno == EINTR &&
!mono_thread_info_is_interrupt_state (info));

if (ret == -1) {
if (errno == EINTR)
ret = 0;
else
return false;
}

if (byteswritten != NULL)
*byteswritten = ret;

return true;
}

#endif // HOST_WIN32

EventPipeThread *
ep_rt_mono_thread_get_or_create (void)
{
Expand Down Expand Up @@ -2224,7 +2411,7 @@ static const int64_t SECS_TO_NS = 1000000000;
static const int64_t MSECS_TO_MIS = 1000;

/* clock_gettime () is found by configure on Apple builds, but its only present from ios 10, macos 10.12, tvos 10 and watchos 3 */
#if defined (HAVE_CLOCK_MONOTONIC) && (defined(TARGET_IOS) || defined(TARGET_OSX) || defined(TARGET_WATCHOS) || defined(TARGET_TVOS))
#if defined (HAVE_CLOCK_MONOTONIC) && (defined(HOST_IOS) || defined(HOST_OSX) || defined(HOST_WATCHOS) || defined(HOST_TVOS))
Copy link
Member

Choose a reason for hiding this comment

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

isn't HOST_DARWIN supposed to encompass all of these?

Copy link
Member

Choose a reason for hiding this comment

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

Also: ios 10, macos 10.12, etc are pretty old. Do we still need this?

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

yes HOST_DARWIN would work too, good point. I can change that in a separate cleanup since we have similar code elsewhere.

Regarding removing this altogether, we have #58737 which tracks removing it from Android/iOS but last time we tried we hit various issues (#62978) so it needs a bit more care.

#undef HAVE_CLOCK_MONOTONIC
#endif

Expand Down Expand Up @@ -2375,15 +2562,15 @@ ep_rt_mono_system_timestamp_get (void)

#ifndef HOST_WIN32
#if defined(__APPLE__)
#if defined (TARGET_OSX)
#if defined (HOST_OSX)
G_BEGIN_DECLS
gchar ***_NSGetEnviron(void);
G_END_DECLS
#define environ (*_NSGetEnviron())
#else
static char *_ep_rt_mono_environ[1] = { NULL };
#define environ _ep_rt_mono_environ
#endif /* defined (TARGET_OSX) */
#endif /* defined (HOST_OSX) */
#else
G_BEGIN_DECLS
extern char **environ;
Expand Down
Loading