@matheusbsbs / matheus-bagliano / commits / c4712e4553
Fork 0

Replace mmap with xmmap, better handling MAP_FAILED.

In some cases we did not even bother to check the return value of mmap() and just assume it worked. This is bad, because if we are out of virtual address space the kernel returned MAP_FAILED and we would attempt to dereference that address, segfaulting without any real error output to the user. We are replacing all calls to mmap() with xmmap() and moving all MAP_FAILED checking into that single location. If a mmap call fails we try to release enough least-recently-used pack windows to possibly succeed, then retry the mmap() attempt. If we cannot mmap even after releasing pack memory then we die() as none of our callers have any reasonable recovery strategy for a failed mmap. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Shawn O. Pearce committed Dec 24, 2006 at 00:47 UTC c4712e4553f13d87d655325a57538f299402d457
6 files changed +22 -19
config.c
+1 -1
@@ -704,7 +704,7 @@ int git_config_set_multivar(const char* key, const char* value,
704 }
705
706 fstat(in_fd, &st);
707 - contents = mmap(NULL, st.st_size, PROT_READ,
707 + contents = xmmap(NULL, st.st_size, PROT_READ,
708 MAP_PRIVATE, in_fd, 0);
709 close(in_fd);
710
diff.c
+1 -3
@@ -1341,10 +1341,8 @@ int diff_populate_filespec(struct diff_filespec *s, int size_only)
1341 fd = open(s->path, O_RDONLY);
1342 if (fd < 0)
1343 goto err_empty;
1344 - s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
1344 + s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
1345 close(fd);
1346 - if (s->data == MAP_FAILED)
1347 - goto err_empty;
1346 s->should_munmap = 1;
1347 }
1348 else {
git-compat-util.h
+13
@@ -188,6 +188,19 @@ static inline void *xcalloc(size_t nmemb, size_t size)
188 return ret;
189 }
190
191 +static inline void *xmmap(void *start, size_t length,
192 + int prot, int flags, int fd, off_t offset)
193 +{
194 + void *ret = mmap(start, length, prot, flags, fd, offset);
195 + if (ret == MAP_FAILED) {
196 + release_pack_memory(length);
197 + ret = mmap(start, length, prot, flags, fd, offset);
198 + if (ret == MAP_FAILED)
199 + die("Out of memory? mmap failed: %s", strerror(errno));
200 + }
201 + return ret;
202 +}
203 +
204 static inline ssize_t xread(int fd, void *buf, size_t len)
205 {
206 ssize_t nr;
read-cache.c
+1 -1
@@ -798,7 +798,7 @@ int read_cache_from(const char *path)
798 cache_mmap_size = st.st_size;
799 errno = EINVAL;
800 if (cache_mmap_size >= sizeof(struct cache_header) + 20)
801 - cache_mmap = mmap(NULL, cache_mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
801 + cache_mmap = xmmap(NULL, cache_mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
802 }
803 close(fd);
804 if (cache_mmap == MAP_FAILED)
refs.c
+1 -1
@@ -1026,7 +1026,7 @@ int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *
1026 fstat(logfd, &st);
1027 if (!st.st_size)
1028 die("Log %s is empty.", logfile);
1029 - logdata = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, logfd, 0);
1029 + logdata = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, logfd, 0);
1030 close(logfd);
1031
1032 lastrec = NULL;
sha1_file.c
+5 -13
@@ -355,10 +355,8 @@ static void read_info_alternates(const char * relative_base, int depth)
355 close(fd);
356 return;
357 }
358 - map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
358 + map = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
359 close(fd);
360 - if (map == MAP_FAILED)
361 - return;
360
361 link_alt_odb_entries(map, map + st.st_size, '\n', relative_base, depth);
362
@@ -442,10 +440,8 @@ static int check_packed_git_idx(const char *path, unsigned long *idx_size_,
440 return -1;
441 }
442 idx_size = st.st_size;
445 - idx_map = mmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);
443 + idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);
444 close(fd);
447 - if (idx_map == MAP_FAILED)
448 - return -1;
445
446 index = idx_map;
447 *idx_map_ = idx_map;
@@ -630,7 +626,7 @@ unsigned char* use_pack(struct packed_git *p,
626 while (packed_git_limit < pack_mapped
627 && unuse_one_window(p))
628 ; /* nothing */
633 - win->base = mmap(NULL, win->len,
629 + win->base = xmmap(NULL, win->len,
630 PROT_READ, MAP_PRIVATE,
631 p->pack_fd, win->offset);
632 if (win->base == MAP_FAILED)
@@ -828,10 +824,8 @@ void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
824 */
825 sha1_file_open_flag = 0;
826 }
831 - map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
827 + map = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
828 close(fd);
833 - if (map == MAP_FAILED)
834 - return NULL;
829 *size = st.st_size;
830 return map;
831 }
@@ -1987,10 +1981,8 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, con
1981
1982 buf = "";
1983 if (size)
1990 - buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
1984 + buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
1985 close(fd);
1992 - if (buf == MAP_FAILED)
1993 - return -1;
1986
1987 if (!type)
1988 type = blob_type;