Skip to content

Commit

Permalink
Fix platform-specific build issues (mono#7342)
Browse files Browse the repository at this point in the history
* Fix platform-specific build issues

These changes allow some of the Mono code used by the debugger to build
on a few additional platforms. Specifically, this includes platforms:

* That don't have access to the full Win32 API
* That don't have access to the full Posix API
* That don't support IPv6

* Wrap `access` and `_access` in `g_access`

* Convert indentation to tabs

* Wrap `mktemp` and `_mktemp` in `g_mktemp`

* Wrap `open` and `_open` in `g_open`

* Wrap `write` and `_write` in `g_write`

* Fix indentation

* Use proper asserts

* Add a configure check for `access`

* Remove the use of HAVE_XBOXONE_WINAPI_SUPPORT

* Replace the previous definition of `g_open`

I missed that it was here in my earlier commit, and ended up redefining
it.

* Use `g_error` instead of `g_assert(0)`
  • Loading branch information
joshpeterson authored and luhenry committed Mar 15, 2018
1 parent 2d0eee9 commit 2883e56
Show file tree
Hide file tree
Showing 25 changed files with 190 additions and 37 deletions.
15 changes: 15 additions & 0 deletions configure.ac
Expand Up @@ -2638,6 +2638,21 @@ if test x$host_win32 = xno; then
], [
AC_MSG_RESULT(no)
])
dnl **********************************
dnl *** Check for access ***
dnl **********************************
AC_MSG_CHECKING(for access)
AC_TRY_LINK([
#include <unistd.h>
], [
access(NULL,0);
], [
# Yes, we have it...
AC_MSG_RESULT(yes)
AC_DEFINE(HAVE_ACCESS, 1, [Have access])
], [
AC_MSG_RESULT(no)
])

dnl **********************************
dnl *** Checks for proclib ***
Expand Down
23 changes: 23 additions & 0 deletions mono/eglib/glib.h
Expand Up @@ -914,10 +914,33 @@ GFileError g_file_error_from_errno (gint err_no);
gint g_file_open_tmp (const gchar *tmpl, gchar **name_used, GError **gerror);
gboolean g_file_test (const gchar *filename, GFileTest test);

#ifdef G_OS_WIN32
#define g_open _open
#else
#define g_open open
#endif
#define g_rename rename
#define g_stat stat
#ifdef G_OS_WIN32
#define g_access _access
#else
#define g_access access
#endif
#ifdef G_OS_WIN32
#define g_mktemp _mktemp
#else
#define g_mktemp mktemp
#endif
#ifdef G_OS_WIN32
#define g_unlink _unlink
#else
#define g_unlink unlink
#endif
#ifdef G_OS_WIN32
#define g_write _write
#else
#define g_write write
#endif
#define g_fopen fopen
#define g_lstat lstat
#define g_rmdir rmdir
Expand Down
8 changes: 6 additions & 2 deletions mono/eglib/gpath.c
Expand Up @@ -251,11 +251,13 @@ g_find_program_in_path (const gchar *program)

x = NULL;
probe_path = g_build_path (G_DIR_SEPARATOR_S, l, program, NULL);
if (access (probe_path, X_OK) == 0){ /* FIXME: on windows this is just a read permissions test */
#ifdef HAVE_ACCESS
if (g_access (probe_path, X_OK) == 0){ /* FIXME: on windows this is just a read permissions test */
g_free (curdir);
g_free (p);
return probe_path;
}
#endif
g_free (probe_path);

#ifdef G_OS_WIN32
Expand All @@ -265,12 +267,14 @@ g_find_program_in_path (const gchar *program)
while (suffix_list[listx]) {
program_exe = g_strjoin(NULL,program,suffix_list[listx],NULL);
probe_path = g_build_path (G_DIR_SEPARATOR_S, l, program_exe, NULL);
if (access (probe_path, X_OK) == 0){ /* FIXME: on windows this is just a read permissions test */
#ifdef HAVE_ACCESS
if (g_access (probe_path, X_OK) == 0){ /* FIXME: on windows this is just a read permissions test */
g_free (curdir);
g_free (p);
g_free (program_exe);
return probe_path;
}
#endif
listx++;
g_free (probe_path);
g_free (program_exe);
Expand Down
4 changes: 2 additions & 2 deletions mono/eglib/gunicode-win32-uwp.c
Expand Up @@ -19,9 +19,9 @@ g_get_charset (G_CONST_RETURN char **charset)
{
if (eg_my_charset == NULL) {
static char buf [14];
CPINFOEXA cp_info;
CPINFOEXW cp_info;

GetCPInfoExA (CP_ACP, 0, &cp_info);
GetCPInfoExW (CP_ACP, 0, &cp_info);
sprintf (buf, "CP%u", cp_info.CodePage);
eg_my_charset = buf;
is_utf8 = FALSE;
Expand Down
4 changes: 4 additions & 0 deletions mono/metadata/threads.c
Expand Up @@ -2047,6 +2047,8 @@ ves_icall_System_Threading_WaitHandle_Wait_internal (gpointer *handles, gint32 n
return map_native_wait_result_to_managed (ret, numhandles);
}

#if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)

gint32
ves_icall_System_Threading_WaitHandle_SignalAndWait_Internal (gpointer toSignal, gpointer toWait, gint32 ms, MonoError *error)
{
Expand Down Expand Up @@ -2074,6 +2076,8 @@ ves_icall_System_Threading_WaitHandle_SignalAndWait_Internal (gpointer toSignal,
return map_native_wait_result_to_managed (ret, 1);
}

#endif

gint32 ves_icall_System_Threading_Interlocked_Increment_Int (gint32 *location)
{
return mono_atomic_inc_i32 (location);
Expand Down
4 changes: 4 additions & 0 deletions mono/mini/debugger-agent.c
Expand Up @@ -1379,7 +1379,11 @@ socket_transport_connect (const char *address)
break; /* Success */

MONO_ENTER_GC_SAFE;
#ifdef HOST_WIN32
closesocket (sfd);
#else
close (sfd);
#endif
MONO_EXIT_GC_SAFE;
}

Expand Down
2 changes: 1 addition & 1 deletion mono/utils/atomic.h
Expand Up @@ -167,7 +167,7 @@ static inline void
mono_atomic_store_i8 (volatile gint8 *dst, gint8 val)
{
#if (_MSC_VER >= 1600)
InterlockedExchange8 ((CHAR volatile *)dst, (CHAR)val);
_InterlockedExchange8 ((CHAR volatile *)dst, (CHAR)val);
#else
*dst = val;
mono_memory_barrier ();
Expand Down
4 changes: 3 additions & 1 deletion mono/utils/dlmalloc.c
Expand Up @@ -461,7 +461,9 @@ DEFAULT_MMAP_THRESHOLD default: 256K
#endif /* _WIN32 */
#endif /* WIN32 */
#ifdef WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#define HAVE_MMAP 1
#define HAVE_MORECORE 0
Expand Down Expand Up @@ -1214,7 +1216,7 @@ extern void* sbrk(ptrdiff_t);
# define _SC_PAGE_SIZE _SC_PAGESIZE
# endif
# endif
# ifdef _SC_PAGE_SIZE
# if defined (HAVE_SYSCONF) && defined (_SC_PAGESIZE)
# define malloc_getpagesize sysconf(_SC_PAGE_SIZE)
# else
# if defined(BSD) || defined(DGUX) || defined(HAVE_GETPAGESIZE)
Expand Down
22 changes: 22 additions & 0 deletions mono/utils/mono-context.c
Expand Up @@ -395,11 +395,24 @@ mono_monoctx_to_sigctx (MonoContext *mctx, void *ctx)
#include <mono/arch/arm/arm-codegen.h>
#include <mono/arch/arm/arm-vfp-codegen.h>

#ifdef HOST_WIN32
#include <windows.h>
#endif

void
mono_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
{
#ifdef MONO_CROSS_COMPILE
g_assert_not_reached ();
#elif defined(HOST_WIN32)
CONTEXT *context = (CONTEXT*)sigctx;

mctx->pc = context->Pc;
mctx->cpsr = context->Cpsr;
memcpy (&mctx->regs, &context->R0, sizeof (DWORD) * 16);

/* Why are we only copying 16 registers?! There are 32! */
memcpy (&mctx->fregs, &context->D, sizeof (double) * 16);
#else
arm_ucontext *my_uc = sigctx;

Expand All @@ -418,6 +431,15 @@ mono_monoctx_to_sigctx (MonoContext *mctx, void *ctx)
{
#ifdef MONO_CROSS_COMPILE
g_assert_not_reached ();
#elif defined(HOST_WIN32)
CONTEXT *context = (CONTEXT*)ctx;

context->Pc = mctx->pc;
context->Cpsr = mctx->cpsr;
memcpy (&context->R0, &mctx->regs, sizeof (DWORD) * 16);

/* Why are we only copying 16 registers?! There are 32! */
memcpy (&context->D, &mctx->fregs, sizeof (double) * 16);
#else
arm_ucontext *my_uc = ctx;

Expand Down
8 changes: 8 additions & 0 deletions mono/utils/mono-dl-windows.c
Expand Up @@ -55,7 +55,11 @@ mono_dl_open_file (const char *file, int flags)
#endif
guint32 last_error = 0;

#if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
hModule = LoadLibrary (file_utf16);
#else
hModule = LoadPackagedLibrary (file_utf16, NULL);
#endif
if (!hModule)
last_error = GetLastError ();

Expand All @@ -68,7 +72,11 @@ mono_dl_open_file (const char *file, int flags)
if (!hModule)
SetLastError (last_error);
} else {
#if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
hModule = GetModuleHandle (NULL);
#else
g_error("Not supported");
#endif
}
return hModule;
}
Expand Down
2 changes: 1 addition & 1 deletion mono/utils/mono-filemap.c
Expand Up @@ -57,7 +57,7 @@ int
mono_file_map_fd (MonoFileMap *fmap)
{
#ifdef WIN32
return fileno ((FILE*)fmap);
return _fileno ((FILE*)fmap);
#else
return (int)(size_t)fmap;
#endif
Expand Down
25 changes: 25 additions & 0 deletions mono/utils/mono-hwcap-arm.c
Expand Up @@ -94,6 +94,31 @@ mono_hwcap_arch_init (void)
}

/* TODO: Find a way to detect features like Thumb and VFP. */
#elif defined (_WIN32)
/* From MSDN:
* Windows on ARM presumes that it is running on an ARMv7 architecture at all times.
* Floating-point support in the form of VFPv3-D32 or later must be present in hardware.
* The VFP must support both single-precision and double-precision floating-point in hardware.
*
* The Windows runtime does not support emulation of floating-point to enable running on non-VFP hardware.
* Advanced SIMD Extensions (NEON) support—this includes both integer and floating-point operations—must also be present in hardware.
* No run-time support for emulation is provided.
*
* Integer divide support (UDIV/SDIV) is strongly recommended but not required.
* Platforms that lack integer divide support may incur a performance penalty because
* these operations have to be trapped and possibly patched.
*
* The instruction set for Windows on ARM is strictly limited to Thumb-2.
* All code executed on this platform is expected to start and remain in Thumb mode at all times.
*/
mono_hwcap_arm_is_v5 = TRUE;
mono_hwcap_arm_is_v6 = TRUE;
mono_hwcap_arm_is_v7 = TRUE;
mono_hwcap_arm_has_vfp = TRUE;
mono_hwcap_arm_has_vfp3 = TRUE;
mono_hwcap_arm_has_vfp3_d16 = TRUE;
mono_hwcap_arm_has_thumb = TRUE;
mono_hwcap_arm_has_thumb2 = TRUE;
#else
/* We can't use the auxiliary vector on Android due to
* permissions, so fall back to /proc/cpuinfo. We also
Expand Down
2 changes: 1 addition & 1 deletion mono/utils/mono-hwcap-x86.c
Expand Up @@ -147,7 +147,7 @@ mono_hwcap_arch_init (void)
}
}

#if defined(HAVE_UNISTD_H)
#if defined(HAVE_UNISTD_H) && defined(HAVE_ACCESS)
mono_hwcap_x86_is_xen = !access ("/proc/xen", F_OK);
#endif
}
4 changes: 4 additions & 0 deletions mono/utils/mono-os-wait-win32.c
Expand Up @@ -152,6 +152,8 @@ mono_win32_wait_for_multiple_objects_ex (DWORD count, CONST HANDLE *handles, BOO
return result;
}

#if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)

DWORD
mono_win32_signal_object_and_wait (HANDLE toSignal, HANDLE toWait, DWORD timeout, BOOL alertable)
{
Expand All @@ -173,6 +175,8 @@ mono_win32_signal_object_and_wait (HANDLE toSignal, HANDLE toWait, DWORD timeout
return result;
}

#endif

#if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
DWORD
mono_win32_msg_wait_for_multiple_objects_ex (DWORD count, CONST HANDLE *handles, DWORD timeout, DWORD wakeMask, DWORD flags)
Expand Down
11 changes: 7 additions & 4 deletions mono/utils/mono-proclib.c
Expand Up @@ -32,8 +32,10 @@
#ifdef HAVE_SYS_SYSCTL_H
#include <sys/sysctl.h>
#endif
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
#endif
#if defined(__HAIKU__)
#include <os/kernel/OS.h>
#endif
Expand Down Expand Up @@ -519,7 +521,7 @@ get_user_hz (void)
{
static int user_hz = 0;
if (user_hz == 0) {
#ifdef _SC_CLK_TCK
#if defined (_SC_CLK_TCK) && defined (HAVE_SYSCONF)
user_hz = sysconf (_SC_CLK_TCK);
#endif
if (user_hz == 0)
Expand Down Expand Up @@ -778,7 +780,7 @@ mono_cpu_count (void)
* [5] https://github.com/dotnet/coreclr/blob/7058273693db2555f127ce16e6b0c5b40fb04867/src/pal/src/misc/sysinfo.cpp#L148
*/

#ifdef _SC_NPROCESSORS_CONF
#if defined (_SC_NPROCESSORS_CONF) && defined (HAVE_SYSCONF)
{
int count = sysconf (_SC_NPROCESSORS_CONF);
if (count > 0)
Expand All @@ -795,7 +797,7 @@ mono_cpu_count (void)
return CPU_COUNT (&set);
}
#endif
#ifdef _SC_NPROCESSORS_ONLN
#if defined (_SC_NPROCESSORS_ONLN) && defined (HAVE_SYSCONF)
{
int count = sysconf (_SC_NPROCESSORS_ONLN);
if (count > 0)
Expand Down Expand Up @@ -930,6 +932,7 @@ gint32
mono_cpu_usage (MonoCpuUsageState *prev)
{
gint32 cpu_usage = 0;
#ifdef HAVE_GETRUSAGE
gint64 cpu_total_time;
gint64 cpu_busy_time;
struct rusage resource_usage;
Expand Down Expand Up @@ -957,7 +960,7 @@ mono_cpu_usage (MonoCpuUsageState *prev)

if (cpu_total_time > 0 && cpu_busy_time > 0)
cpu_usage = (gint32)(cpu_busy_time * 100 / cpu_total_time);

#endif
return cpu_usage;
}
#endif /* !HOST_WIN32 */
5 changes: 3 additions & 2 deletions mono/utils/mono-stdlib.c
Expand Up @@ -35,7 +35,8 @@ mono_mkstemp (char *templ)

len = strlen (templ);
do {
t = mktemp (templ);
t = g_mktemp (templ);

if (t == NULL) {
errno = EINVAL;
return -1;
Expand All @@ -45,7 +46,7 @@ mono_mkstemp (char *templ)
return -1;
}

ret = open (templ, O_RDWR | O_BINARY | O_CREAT | O_EXCL, 0600);
ret = g_open (templ, O_RDWR | O_BINARY | O_CREAT | O_EXCL, 0600);
if (ret == -1) {
if (errno != EEXIST)
return -1;
Expand Down
2 changes: 1 addition & 1 deletion mono/utils/mono-threads-debug.h
Expand Up @@ -9,7 +9,7 @@
#define MOSTLY_ASYNC_SAFE_PRINTF(...) do { \
char __buff[1024]; __buff [0] = '\0'; \
g_snprintf (__buff, sizeof (__buff), __VA_ARGS__); \
write (1, __buff, (guint32)strlen (__buff)); \
g_write (1, __buff, (guint32)strlen (__buff)); \
} while (0)

#if 1
Expand Down

0 comments on commit 2883e56

Please sign in to comment.