Skip to content

Commit 7444c76

Browse files
committed
Fix crash on android because on image load we were taking a codepath that did not respect the mmap file function wrappers which would cause a crash in the Unity runtime. (#1419)
Adding an assert to ensure that we are not truncating data off of a guint64. Code Review changes: keeping uniformity to unsigned types and optimizing double call to mono_file_map_size
1 parent 24eb2f2 commit 7444c76

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

mono/metadata/w32process-unix.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3354,7 +3354,7 @@ mono_w32process_get_fileversion_info (const gunichar2 *filename, gpointer *data)
33543354
gpointer file_map;
33553355
gpointer versioninfo;
33563356
void *map_handle;
3357-
gint32 map_size;
3357+
guint32 map_size;
33583358
gsize datasize;
33593359

33603360
g_assert (data);

mono/utils/mono-proclib.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ gboolean
982982
mono_pe_file_time_date_stamp (const gunichar2 *filename, guint32 *out)
983983
{
984984
void *map_handle;
985-
gint32 map_size;
985+
guint32 map_size;
986986
gpointer file_map = mono_pe_file_map (filename, &map_size, &map_handle);
987987
if (!file_map)
988988
return FALSE;
@@ -1011,13 +1011,13 @@ mono_pe_file_time_date_stamp (const gunichar2 *filename, guint32 *out)
10111011
}
10121012

10131013
gpointer
1014-
mono_pe_file_map (const gunichar2 *filename, gint32 *map_size, void **handle)
1014+
mono_pe_file_map (const gunichar2 *filename, guint32 *map_size, void **handle)
10151015
{
10161016
gchar *filename_ext = NULL;
10171017
gchar *located_filename = NULL;
1018-
int fd = -1;
1019-
struct stat statbuf;
1018+
guint64 fsize = 0;
10201019
gpointer file_map = NULL;
1020+
MonoFileMap *filed = NULL;
10211021
ERROR_DECL (error);
10221022

10231023
/* According to the MSDN docs, a search path is applied to
@@ -1041,8 +1041,7 @@ mono_pe_file_map (const gunichar2 *filename, gint32 *map_size, void **handle)
10411041
goto exit;
10421042
}
10431043

1044-
fd = open (filename_ext, O_RDONLY, 0);
1045-
if (fd == -1 && (errno == ENOENT || errno == ENOTDIR) && IS_PORTABILITY_SET) {
1044+
if ((filed = mono_file_map_open (filename_ext)) == NULL && IS_PORTABILITY_SET) {
10461045
gint saved_errno = errno;
10471046

10481047
located_filename = mono_portability_find_file (filename_ext, TRUE);
@@ -1053,38 +1052,39 @@ mono_pe_file_map (const gunichar2 *filename, gint32 *map_size, void **handle)
10531052
goto exit;
10541053
}
10551054

1056-
fd = open (located_filename, O_RDONLY, 0);
1057-
if (fd == -1) {
1058-
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Error opening file %s (3): %s", __func__, filename_ext, strerror (errno));
1055+
if ((filed = mono_file_map_open (located_filename)) == NULL) {
1056+
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Error opening file %s (3): %s", __func__, located_filename, strerror (errno));
10591057
goto exit;
10601058
}
10611059
}
1062-
else if (fd == -1) {
1060+
else if (filed == NULL) {
10631061
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Error opening file %s (3): %s", __func__, filename_ext, strerror (errno));
10641062
goto exit;
10651063
}
10661064

1067-
if (fstat (fd, &statbuf) == -1) {
1065+
fsize = mono_file_map_size (filed);
1066+
if (fsize == 0) {
10681067
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Error stat()ing file %s: %s", __func__, filename_ext, strerror (errno));
10691068
goto exit;
10701069
}
1071-
*map_size = statbuf.st_size;
1070+
g_assert (fsize <= G_MAXUINT32);
1071+
*map_size = fsize;
10721072

10731073
/* Check basic file size */
1074-
if (statbuf.st_size < sizeof(IMAGE_DOS_HEADER)) {
1075-
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: File %s is too small: %" PRId64, __func__, filename_ext, (gint64) statbuf.st_size);
1074+
if (fsize < sizeof(IMAGE_DOS_HEADER)) {
1075+
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: File %s is too small: %" PRId64, __func__, filename_ext, fsize);
10761076

10771077
goto exit;
10781078
}
10791079

1080-
file_map = mono_file_map (statbuf.st_size, MONO_MMAP_READ | MONO_MMAP_PRIVATE, fd, 0, handle);
1080+
file_map = mono_file_map (fsize, MONO_MMAP_READ | MONO_MMAP_PRIVATE, mono_file_map_fd (filed), 0, handle);
10811081
if (file_map == NULL) {
10821082
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Error mmap()int file %s: %s", __func__, filename_ext, strerror (errno));
10831083
goto exit;
10841084
}
10851085
exit:
1086-
if (fd != -1)
1087-
close (fd);
1086+
if (filed)
1087+
mono_file_map_close (filed);
10881088
g_free (located_filename);
10891089
g_free (filename_ext);
10901090
return file_map;

mono/utils/mono-proclib.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ gboolean
328328
mono_pe_file_time_date_stamp (const gunichar2 *filename, guint32 *out);
329329

330330
gpointer
331-
mono_pe_file_map (const gunichar2 *filename, gint32 *map_size, void **handle);
331+
mono_pe_file_map (const gunichar2 *filename, guint32 *map_size, void **handle);
332332

333333
void
334334
mono_pe_file_unmap (gpointer file_map, void *handle);

0 commit comments

Comments
 (0)