|
From: Nicholas N. <nj...@ca...> - 2004-10-13 17:29:07
|
CVS commit by nethercote:
Cleaned up ume.h by moving some functions around.
M +90 -0 stage1.c 1.21 [POSSIBLY UNSAFE: printf]
M +1 -127 ume.c 1.30 [POSSIBLY UNSAFE: printf]
M +9 -19 ume.h 1.10
M +66 -1 vg_main.c 1.214
--- valgrind/coregrind/stage1.c #1.20:1.21
@@ -31,4 +31,5 @@
#define _FILE_OFFSET_BITS 64
+#include <errno.h>
#include <stdio.h>
#include <string.h>
@@ -37,5 +38,7 @@
#include <signal.h>
#include <fcntl.h>
+#include <sys/mman.h>
#include <sys/resource.h>
+#include <unistd.h>
#include "core.h"
@@ -52,4 +55,8 @@ static const char *valgrind_lib = VG_LIB
static const char stage2[] = "stage2";
+/*------------------------------------------------------------*/
+/*--- Auxv modification ---*/
+/*------------------------------------------------------------*/
+
/* Modify the auxv the kernel gave us to make it look like we were
execed as the shared object.
@@ -154,4 +161,87 @@ static void *fix_auxv(void *v_init_esp,
}
+
+/*------------------------------------------------------------*/
+/*--- Address space padding ---*/
+/*------------------------------------------------------------*/
+
+static void check_mmap(void* res, void* base, int len)
+{
+ if ((void*)-1 == res) {
+ fprintf(stderr, "valgrind: padding mmap(%p, %d) failed during startup.\n"
+ "valgrind: is there a hard virtual memory limit set?\n",
+ base, len);
+ exit(1);
+ }
+}
+
+typedef struct {
+ char* fillgap_start;
+ char* fillgap_end;
+ int fillgap_padfile;
+} fillgap_extra;
+
+static int fillgap(char *segstart, char *segend, const char *perm, off_t off,
+ int maj, int min, int ino, void* e)
+{
+ fillgap_extra* extra = e;
+
+ if (segstart >= extra->fillgap_end)
+ return 0;
+
+ if (segstart > extra->fillgap_start) {
+ void* res = mmap(extra->fillgap_start, segstart - extra->fillgap_start,
+ PROT_NONE, MAP_FIXED|MAP_PRIVATE,
+ extra->fillgap_padfile, 0);
+ check_mmap(res, extra->fillgap_start, segstart - extra->fillgap_start);
+ }
+ extra->fillgap_start = segend;
+
+ return 1;
+}
+
+// Choose a name for the padfile, open it.
+int as_openpadfile(void)
+{
+ char buf[256];
+ int padfile;
+ int seq = 1;
+ do {
+ snprintf(buf, 256, "/tmp/.pad.%d.%d", getpid(), seq++);
+ padfile = open(buf, O_RDWR|O_CREAT|O_EXCL, 0);
+ unlink(buf);
+ if (padfile == -1 && errno != EEXIST) {
+ fprintf(stderr, "valgrind: couldn't open padfile\n");
+ exit(44);
+ }
+ } while(padfile == -1);
+
+ return padfile;
+}
+
+// Pad all the empty spaces in a range of address space to stop interlopers.
+void as_pad(void *start, void *end, int padfile)
+{
+ fillgap_extra extra;
+ extra.fillgap_start = start;
+ extra.fillgap_end = end;
+ extra.fillgap_padfile = padfile;
+
+ foreach_map(fillgap, &extra);
+
+ if (extra.fillgap_start < extra.fillgap_end) {
+ void* res = mmap(extra.fillgap_start,
+ extra.fillgap_end - extra.fillgap_start,
+ PROT_NONE, MAP_FIXED|MAP_PRIVATE, padfile, 0);
+ check_mmap(res, extra.fillgap_start,
+ extra.fillgap_end - extra.fillgap_start);
+ }
+}
+
+
+/*------------------------------------------------------------*/
+/*--- main() and related pieces ---*/
+/*------------------------------------------------------------*/
+
static int prmap(char *start, char *end, const char *perm, off_t off, int maj,
int min, int ino, void* dummy) {
--- valgrind/coregrind/ume.c #1.29:1.30
@@ -56,7 +56,5 @@ static void check_mmap(void* res, void*
{
if ((void*)-1 == res) {
- fprintf(stderr, "valgrind: mmap(%p, %d) failed during startup.\n"
- "valgrind: is there a hard virtual memory limit set?\n",
- base, len);
+ fprintf(stderr, "valgrind: mmap(%p, %d) failed in UME.\n", base, len);
exit(1);
}
@@ -113,128 +111,4 @@ void foreach_map(int (*fn)(char *start,
}
-typedef struct {
- char* fillgap_start;
- char* fillgap_end;
- int fillgap_padfile;
-} fillgap_extra;
-
-static int fillgap(char *segstart, char *segend, const char *perm, off_t off,
- int maj, int min, int ino, void* e)
-{
- fillgap_extra* extra = e;
-
- if (segstart >= extra->fillgap_end)
- return 0;
-
- if (segstart > extra->fillgap_start) {
- void* res = mmap(extra->fillgap_start, segstart - extra->fillgap_start,
- PROT_NONE, MAP_FIXED|MAP_PRIVATE|MAP_NORESERVE,
- extra->fillgap_padfile, 0);
- check_mmap(res, extra->fillgap_start, segstart - extra->fillgap_start);
- }
- extra->fillgap_start = segend;
-
- return 1;
-}
-
-// Choose a name for the padfile, open it.
-int as_openpadfile(void)
-{
- char buf[256];
- int padfile;
- int seq = 1;
- do {
- snprintf(buf, 256, "/tmp/.pad.%d.%d", getpid(), seq++);
- padfile = open(buf, O_RDWR|O_CREAT|O_EXCL, 0);
- unlink(buf);
- if (padfile == -1 && errno != EEXIST) {
- fprintf(stderr, "valgrind: couldn't open padfile\n");
- exit(44);
- }
- } while(padfile == -1);
-
- return padfile;
-}
-
-// Pad all the empty spaces in a range of address space to stop interlopers.
-void as_pad(void *start, void *end, int padfile)
-{
- fillgap_extra extra;
- extra.fillgap_start = start;
- extra.fillgap_end = end;
- extra.fillgap_padfile = padfile;
-
- foreach_map(fillgap, &extra);
-
- if (extra.fillgap_start < extra.fillgap_end) {
- void* res = mmap(extra.fillgap_start,
- extra.fillgap_end - extra.fillgap_start,
- PROT_NONE, MAP_FIXED|MAP_PRIVATE|MAP_NORESERVE, padfile, 0);
- check_mmap(res, extra.fillgap_start,
- extra.fillgap_end - extra.fillgap_start);
- }
-}
-
-typedef struct {
- char* killpad_start;
- char* killpad_end;
- struct stat* killpad_padstat;
-} killpad_extra;
-
-static int killpad(char *segstart, char *segend, const char *perm, off_t off,
- int maj, int min, int ino, void* ex)
-{
- killpad_extra* extra = ex;
- void *b, *e;
- int res;
-
- assert(NULL != extra->killpad_padstat);
-
- if (extra->killpad_padstat->st_dev != makedev(maj, min) ||
- extra->killpad_padstat->st_ino != ino)
- return 1;
-
- if (segend <= extra->killpad_start || segstart >= extra->killpad_end)
- return 1;
-
- if (segstart <= extra->killpad_start)
- b = extra->killpad_start;
- else
- b = segstart;
-
- if (segend >= extra->killpad_end)
- e = extra->killpad_end;
- else
- e = segend;
-
- res = munmap(b, (char *)e-(char *)b);
- assert(0 == res);
-
- return 1;
-}
-
-// Remove padding of 'padfile' from a range of address space.
-void as_unpad(void *start, void *end, int padfile)
-{
- static struct stat padstat;
- killpad_extra extra;
- int res;
-
- assert(padfile > 0);
-
- res = fstat(padfile, &padstat);
- assert(0 == res);
- extra.killpad_padstat = &padstat;
- extra.killpad_start = start;
- extra.killpad_end = end;
- foreach_map(killpad, &extra);
-}
-
-void as_closepadfile(int padfile)
-{
- int res = close(padfile);
- assert(0 == res);
-}
-
/*------------------------------------------------------------*/
/*--- Finding auxv on the stack ---*/
--- valgrind/coregrind/ume.h #1.9:1.10
@@ -40,4 +40,13 @@
/*------------------------------------------------------------*/
+void foreach_map(int (*fn)(char *start, char *end,
+ const char *perm, off_t offset,
+ int maj, int min, int ino, void* extra),
+ void* extra);
+
+/*------------------------------------------------------------*/
+/*--- Loading ELF files ---*/
+/*------------------------------------------------------------*/
+
#if ELFSZ == 64
#define ESZ(x) Elf64_##x
@@ -51,13 +60,4 @@
typedef ESZ(Addr) addr_t;
-void foreach_map(int (*fn)(char *start, char *end,
- const char *perm, off_t offset,
- int maj, int min, int ino, void* extra),
- void* extra);
-
-/*------------------------------------------------------------*/
-/*--- Loading ELF files ---*/
-/*------------------------------------------------------------*/
-
// Info needed to load and run a program. IN/INOUT/OUT refers to the
// inputs/outputs of do_exec().
@@ -89,14 +89,4 @@ int do_exec(const char *exe, struct exei
/*------------------------------------------------------------*/
-/*--- Address space padding ---*/
-/*------------------------------------------------------------*/
-
-// Padding functions used at startup to force things where we want them.
-int as_openpadfile (void);
-void as_pad (void *start, void *end, int padfile);
-void as_unpad (void *start, void *end, int padfile);
-void as_closepadfile(int padfile);
-
-/*------------------------------------------------------------*/
/*--- Finding and dealing with auxv ---*/
/*------------------------------------------------------------*/
--- valgrind/coregrind/vg_main.c #1.213:1.214
@@ -1401,4 +1401,69 @@ static void load_client(char* cl_argv[],
}
+/*====================================================================*/
+/*=== Address space unpadding ===*/
+/*====================================================================*/
+
+typedef struct {
+ char* killpad_start;
+ char* killpad_end;
+ struct stat* killpad_padstat;
+} killpad_extra;
+
+static int killpad(char *segstart, char *segend, const char *perm, off_t off,
+ int maj, int min, int ino, void* ex)
+{
+ killpad_extra* extra = ex;
+ void *b, *e;
+ int res;
+
+ vg_assert(NULL != extra->killpad_padstat);
+
+ if (extra->killpad_padstat->st_dev != makedev(maj, min) ||
+ extra->killpad_padstat->st_ino != ino)
+ return 1;
+
+ if (segend <= extra->killpad_start || segstart >= extra->killpad_end)
+ return 1;
+
+ if (segstart <= extra->killpad_start)
+ b = extra->killpad_start;
+ else
+ b = segstart;
+
+ if (segend >= extra->killpad_end)
+ e = extra->killpad_end;
+ else
+ e = segend;
+
+ res = munmap(b, (char *)e-(char *)b);
+ vg_assert(0 == res);
+
+ return 1;
+}
+
+// Remove padding of 'padfile' from a range of address space.
+void as_unpad(void *start, void *end, int padfile)
+{
+ static struct stat padstat;
+ killpad_extra extra;
+ int res;
+
+ vg_assert(padfile > 0);
+
+ res = fstat(padfile, &padstat);
+ vg_assert(0 == res);
+ extra.killpad_padstat = &padstat;
+ extra.killpad_start = start;
+ extra.killpad_end = end;
+ foreach_map(killpad, &extra);
+}
+
+void as_closepadfile(int padfile)
+{
+ int res = close(padfile);
+ vg_assert(0 == res);
+}
+
/*====================================================================*/
@@ -2571,5 +2636,5 @@ int main(int argc, char **argv)
//--------------------------------------------------------------
- // Everything in place, unpad us
+ // Everything in place, remove padding done by stage1
// p: layout_remaining_space() [everything must be mapped in before now]
// p: load_client() [ditto]
|