r9274@Kushana: nickm | 2006-10-19 16:16:58 -0400

Add unit tests for tor_mmap_file(); make tor_mmap_t.size always be the size of the file (not the size of the mapping); add an extra argument to read_file_to_str() so it can return the size of the result string.


svn:r8762
This commit is contained in:
Nick Mathewson 2006-10-19 23:05:02 +00:00
parent 126a3f699a
commit 7551c44a53
11 changed files with 100 additions and 28 deletions

View File

@ -42,6 +42,9 @@ Changes in version 0.1.2.3-alpha - 2006-10-??
service circuits. service circuits.
- Correctly set maximum connection limit on Cygwin. - Correctly set maximum connection limit on Cygwin.
- Try to detect windows correctly when cross-compiling. - Try to detect windows correctly when cross-compiling.
- Detect the size of the routers file correctly even if it is corrupted
(on systems without mmap) or not page-aligned (on systems with mmap).
This bug was harmless.
Changes in version 0.1.2.2-alpha - 2006-10-07 Changes in version 0.1.2.2-alpha - 2006-10-07

View File

@ -113,14 +113,19 @@ const char compat_c_id[] =
#endif #endif
#ifdef HAVE_SYS_MMAN_H #ifdef HAVE_SYS_MMAN_H
typedef struct tor_mmap_impl_t {
tor_mmap_t base;
size_t mapping_size; /**< Size of the actual mapping. (This is this file
* size, rounded up to the nearest page.) */
} tor_mmap_impl_t;
tor_mmap_t * tor_mmap_t *
tor_mmap_file(const char *filename) tor_mmap_file(const char *filename)
{ {
int fd; /* router file */ int fd; /* router file */
char *string; char *string;
int page_size; int page_size;
tor_mmap_t *res; tor_mmap_impl_t *res;
size_t size; size_t size, filesize;
tor_assert(filename); tor_assert(filename);
@ -130,7 +135,7 @@ tor_mmap_file(const char *filename)
return NULL; return NULL;
} }
size = lseek(fd, 0, SEEK_END); size = filesize = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET); lseek(fd, 0, SEEK_SET);
/* ensure page alignment */ /* ensure page alignment */
page_size = getpagesize(); page_size = getpagesize();
@ -146,28 +151,31 @@ tor_mmap_file(const char *filename)
close(fd); close(fd);
res = tor_malloc_zero(sizeof(tor_mmap_t)); res = tor_malloc_zero(sizeof(tor_mmap_impl_t));
res->data = string; res->base.data = string;
res->size = size; res->base.size = filesize;
res->mapping_size = size;
return res; return &(res->base);
} }
void void
tor_munmap_file(tor_mmap_t *handle) tor_munmap_file(tor_mmap_t *handle)
{ {
munmap((char*)handle->data, handle->size); tor_mmap_impl_t *h = (tor_mmap_impl_t*)
tor_free(handle); (((char*)handle) - STRUCT_OFFSET(tor_mmap_impl_t, base));
munmap((char*)h->base.data, h->mapping_size);
tor_free(h);
} }
#elif defined(MS_WINDOWS) #elif defined(MS_WINDOWS)
typedef struct win_mmap_t { typedef struct win_mmap_t {
tor_mmap_t base; tor_mmap_t base;
HANDLE file_handle; HANDLE file_handle;
HANDLE mmap_handle; HANDLE mmap_handle;
} tor_mmap_impl_t; } win_mmap_t;
tor_mmap_t * tor_mmap_t *
tor_mmap_file(const char *filename) tor_mmap_file(const char *filename)
{ {
struct win_mmap_t *res = tor_malloc_zero(sizeof(struct win_mmap_t)); win_mmap_t *res = tor_malloc_zero(sizeof(win_mmap_t));
res->mmap_handle = res->file_handle = INVALID_HANDLE_VALUE; res->mmap_handle = res->file_handle = INVALID_HANDLE_VALUE;
res->file_handle = CreateFile(filename, res->file_handle = CreateFile(filename,
@ -208,8 +216,8 @@ tor_mmap_file(const char *filename)
void void
tor_munmap_file(tor_mmap_t *handle) tor_munmap_file(tor_mmap_t *handle)
{ {
struct win_mmap_t *h = (struct win_mmap_t*) win_mmap_t *h = (win_mmap_t*)
(((char*)handle) - STRUCT_OFFSET(struct win_mmap_t, base)); (((char*)handle) - STRUCT_OFFSET(win_mmap_t, base));
if (handle->data) if (handle->data)
/*this is an ugly cast, but without it, "data" in struct tor_mmap_t would /*this is an ugly cast, but without it, "data" in struct tor_mmap_t would
@ -226,13 +234,14 @@ tor_munmap_file(tor_mmap_t *handle)
tor_mmap_t * tor_mmap_t *
tor_mmap_file(const char *filename) tor_mmap_file(const char *filename)
{ {
char *res = read_file_to_str(filename, 1); size_t size;
char *res = read_file_to_str(filename, 1, &size);
tor_mmap_t *handle; tor_mmap_t *handle;
if (! res) if (! res)
return NULL; return NULL;
handle = tor_malloc_zero(sizeof(tor_mmap_t)); handle = tor_malloc_zero(sizeof(tor_mmap_t));
handle->data = res; handle->data = res;
handle->size = strlen(res) + 1; handle->size = size;
return handle; return handle;
} }
void void

View File

@ -129,10 +129,11 @@ size_t strlcpy(char *dst, const char *src, size_t siz) ATTR_NONNULL((1,2));
#define U64_LITERAL(n) (n ## llu) #define U64_LITERAL(n) (n ## llu)
#endif #endif
/** Opaque bookkeeping type used for mmap accounting. */ /** Represents an mmaped file. Allocated via tor_mmap_file; freed with
* tor_munmap_file. */
typedef struct tor_mmap_t { typedef struct tor_mmap_t {
const char *data; const char *data; /**< Mapping of the file's contents. */
size_t size; size_t size; /**< Size of the file. */
} tor_mmap_t; } tor_mmap_t;
tor_mmap_t *tor_mmap_file(const char *filename) ATTR_NONNULL((1)); tor_mmap_t *tor_mmap_file(const char *filename) ATTR_NONNULL((1));

View File

@ -460,7 +460,7 @@ crypto_pk_read_private_key_from_filename(crypto_pk_env_t *env,
int r; int r;
/* Read the file into a string. */ /* Read the file into a string. */
contents = read_file_to_str(keyfile, 0); contents = read_file_to_str(keyfile, 0, NULL);
if (!contents) { if (!contents) {
log_warn(LD_CRYPTO, "Error reading private key from \"%s\"", keyfile); log_warn(LD_CRYPTO, "Error reading private key from \"%s\"", keyfile);
return -1; return -1;

View File

@ -1288,6 +1288,9 @@ append_bytes_to_file(const char *fname, const char *str, size_t len,
/** Read the contents of <b>filename</b> into a newly allocated /** Read the contents of <b>filename</b> into a newly allocated
* string; return the string on success or NULL on failure. * string; return the string on success or NULL on failure.
*
* If <b>size_out</b> is provided, store the length of the result in
* <b>size_out</b>
*/ */
/* /*
* This function <em>may</em> return an erroneous result if the file * This function <em>may</em> return an erroneous result if the file
@ -1297,7 +1300,7 @@ append_bytes_to_file(const char *fname, const char *str, size_t len,
* be truncated. * be truncated.
*/ */
char * char *
read_file_to_str(const char *filename, int bin) read_file_to_str(const char *filename, int bin, size_t *size_out)
{ {
int fd; /* router file */ int fd; /* router file */
struct stat statbuf; struct stat statbuf;
@ -1351,6 +1354,8 @@ read_file_to_str(const char *filename, int bin)
} }
#endif #endif
close(fd); close(fd);
if (size_out)
*size_out = (size_t) r;
return string; return string;
} }

View File

@ -180,7 +180,8 @@ int write_chunks_to_file(const char *fname, const struct smartlist_t *chunks,
int append_bytes_to_file(const char *fname, const char *str, size_t len, int append_bytes_to_file(const char *fname, const char *str, size_t len,
int bin); int bin);
char *read_file_to_str(const char *filename, int bin) ATTR_MALLOC; char *read_file_to_str(const char *filename, int bin, size_t *size_out)
ATTR_MALLOC;
char *parse_line_from_str(char *line, char **key_out, char **value_out); char *parse_line_from_str(char *line, char **key_out, char **value_out);
char *expand_filename(const char *filename); char *expand_filename(const char *filename);
struct smartlist_t *tor_listdir(const char *dirname); struct smartlist_t *tor_listdir(const char *dirname);

View File

@ -2801,7 +2801,7 @@ options_init_from_torrc(int argc, char **argv)
/* get config lines, assign them */ /* get config lines, assign them */
if (file_status(fname) != FN_FILE || if (file_status(fname) != FN_FILE ||
!(cf = read_file_to_str(fname,0))) { !(cf = read_file_to_str(fname,0,NULL))) {
if (using_default_torrc == 1) { if (using_default_torrc == 1) {
log(LOG_NOTICE, LD_CONFIG, "Configuration file \"%s\" not present, " log(LOG_NOTICE, LD_CONFIG, "Configuration file \"%s\" not present, "
"using reasonable defaults.", fname); "using reasonable defaults.", fname);
@ -3421,7 +3421,7 @@ write_configuration_file(const char *fname, or_options_t *options)
if (fname) { if (fname) {
switch (file_status(fname)) { switch (file_status(fname)) {
case FN_FILE: case FN_FILE:
old_val = read_file_to_str(fname, 0); old_val = read_file_to_str(fname, 0, NULL);
if (strcmpstart(old_val, GENERATED_FILE_PREFIX)) { if (strcmpstart(old_val, GENERATED_FILE_PREFIX)) {
rename_old = 1; rename_old = 1;
} }
@ -3835,7 +3835,7 @@ or_state_load(void)
fname = get_or_state_fname(); fname = get_or_state_fname();
switch (file_status(fname)) { switch (file_status(fname)) {
case FN_FILE: case FN_FILE:
if (!(contents = read_file_to_str(fname, 0))) { if (!(contents = read_file_to_str(fname, 0, NULL))) {
log_warn(LD_FS, "Unable to read state file \"%s\"", fname); log_warn(LD_FS, "Unable to read state file \"%s\"", fname);
goto done; goto done;
} }

View File

@ -148,7 +148,7 @@ dirserv_load_fingerprint_file(void)
log_info(LD_GENERAL, log_info(LD_GENERAL,
"Reloading approved fingerprints from \"%s\"...", fname); "Reloading approved fingerprints from \"%s\"...", fname);
cf = read_file_to_str(fname, 0); cf = read_file_to_str(fname, 0, NULL);
if (!cf) { if (!cf) {
if (options->NamingAuthoritativeDir) { if (options->NamingAuthoritativeDir) {
log_warn(LD_FS, "Cannot open fingerprint file '%s'. Failing.", fname); log_warn(LD_FS, "Cannot open fingerprint file '%s'. Failing.", fname);

View File

@ -587,7 +587,7 @@ read_bandwidth_usage(void)
tor_snprintf(fname, sizeof(fname), "%s/bw_accounting", tor_snprintf(fname, sizeof(fname), "%s/bw_accounting",
get_options()->DataDirectory); get_options()->DataDirectory);
if (!(s = read_file_to_str(fname, 0))) { if (!(s = read_file_to_str(fname, 0, NULL))) {
return 0; return 0;
} }
elts = smartlist_create(); elts = smartlist_create();

View File

@ -136,7 +136,7 @@ router_reload_networkstatus(void)
} }
tor_snprintf(filename,sizeof(filename),"%s/cached-status/%s", tor_snprintf(filename,sizeof(filename),"%s/cached-status/%s",
get_options()->DataDirectory, fn); get_options()->DataDirectory, fn);
s = read_file_to_str(filename, 0); s = read_file_to_str(filename, 0, NULL);
if (s) { if (s) {
stat(filename, &st); stat(filename, &st);
if (router_set_networkstatus(s, st.st_mtime, NS_FROM_CACHE, NULL)<0) { if (router_set_networkstatus(s, st.st_mtime, NS_FROM_CACHE, NULL)<0) {
@ -369,7 +369,7 @@ router_reload_router_list(void)
tor_snprintf(fname, fname_len, "%s/cached-routers.new", tor_snprintf(fname, fname_len, "%s/cached-routers.new",
options->DataDirectory); options->DataDirectory);
contents = read_file_to_str(fname, 1); contents = read_file_to_str(fname, 1, NULL);
if (contents) { if (contents) {
stat(fname, &st); stat(fname, &st);
router_load_routers_from_string(contents, router_load_routers_from_string(contents,

View File

@ -1163,6 +1163,58 @@ test_strmap(void)
strmap_free(map,NULL); strmap_free(map,NULL);
} }
static void
test_mmap(void)
{
char *fname1 = tor_strdup(get_fname("mapped_1"));
char *fname2 = tor_strdup(get_fname("mapped_2"));
char *fname3 = tor_strdup(get_fname("mapped_3"));
const size_t buflen = 17000;
char *buf = tor_malloc(17000);
tor_mmap_t *mapping;
crypto_rand(buf, buflen);
write_str_to_file(fname1, "Short file.", 1);
write_bytes_to_file(fname2, buf, buflen, 1);
write_bytes_to_file(fname3, buf, 16384, 1);
mapping = tor_mmap_file(fname1);
test_assert(mapping);
test_eq(mapping->size, strlen("Short file."));
test_streq(mapping->data, "Short file.");
/* make sure we can unlink. */
test_assert(unlink(fname1) == 0);
test_streq(mapping->data, "Short file.");
tor_munmap_file(mapping);
/* Make sure that we fail to map a no-longer-existant file. */
mapping = tor_mmap_file(fname1);
test_assert(mapping == NULL);
/* Now try a big file that stretches across a few pages and isn't aligned */
mapping = tor_mmap_file(fname2);
test_assert(mapping);
test_eq(mapping->size, buflen);
test_memeq(mapping->data, buf, buflen);
tor_munmap_file(mapping);
/* Now try a big aligned file. */
mapping = tor_mmap_file(fname3);
test_assert(mapping);
test_eq(mapping->size, 16384);
test_memeq(mapping->data, buf, 16384);
/* fname1 got unlinked above */
unlink(fname2);
unlink(fname3);
tor_free(fname1);
tor_free(fname2);
tor_free(fname3);
tor_free(buf);
}
static void static void
test_control_formats(void) test_control_formats(void)
{ {
@ -1771,6 +1823,7 @@ main(int c, char**v)
test_strmap(); test_strmap();
test_control_formats(); test_control_formats();
test_pqueue(); test_pqueue();
test_mmap();
puts("\n========================= Onion Skins ====================="); puts("\n========================= Onion Skins =====================");
test_onion(); test_onion();
test_onion_handshake(); test_onion_handshake();