debuggers.hg

changeset 20972:eb05347ac47a

tools/xenpaging: fix bug of Segmentation fault

Segmentation fault occurs in two situations:
1. argc is less than 3
2. xenpaging_init() fault

Signed-off-by: Yu Zhiguo <yuzg@cn.fujitsu.com>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Feb 11 19:50:05 2010 +0000 (2010-02-11)
parents f5da4b37f9ca
children b882c558d4e4
files tools/xenpaging/xenpaging.c
line diff
     1.1 --- a/tools/xenpaging/xenpaging.c	Thu Feb 11 19:49:31 2010 +0000
     1.2 +++ b/tools/xenpaging/xenpaging.c	Thu Feb 11 19:50:05 2010 +0000
     1.3 @@ -212,6 +212,9 @@ int xenpaging_teardown(xenpaging_t *pagi
     1.4  {
     1.5      int rc;
     1.6  
     1.7 +    if ( paging == NULL )
     1.8 +        return 0;
     1.9 +
    1.10      /* Tear down domain paging in Xen */
    1.11      rc = xc_mem_event_disable(paging->xc_handle, paging->mem_event.domain_id);
    1.12      if ( rc != 0 )
    1.13 @@ -447,20 +450,29 @@ static int evict_victim(xenpaging_t *pag
    1.14  
    1.15  int main(int argc, char *argv[])
    1.16  {
    1.17 -    domid_t domain_id = atoi(argv[1]);
    1.18 -    int num_pages = atoi(argv[2]);
    1.19 +    domid_t domain_id;
    1.20 +    int num_pages;
    1.21      xenpaging_t *paging;
    1.22 -    xenpaging_victim_t victims[num_pages];
    1.23 +    xenpaging_victim_t *victims;
    1.24      mem_event_request_t req;
    1.25      mem_event_response_t rsp;
    1.26      int i;
    1.27 -    int rc;
    1.28 +    int rc = -1, rc1;
    1.29  
    1.30      int open_flags = O_CREAT | O_TRUNC | O_RDWR;
    1.31      mode_t open_mode = S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH;
    1.32      char filename[80];
    1.33      int fd;
    1.34  
    1.35 +    if ( argc != 3 ) {
    1.36 +        fprintf(stderr, "Usage: %s <domain_id> <num_pages>\n", argv[0]);
    1.37 +	return -1;
    1.38 +    }
    1.39 +    domain_id = atoi(argv[1]);
    1.40 +    num_pages = atoi(argv[2]);
    1.41 +
    1.42 +    victims = calloc(num_pages, sizeof(xenpaging_victim_t));
    1.43 +
    1.44      /* Open file */
    1.45      sprintf(filename, "page_cache_%d", domain_id);
    1.46      fd = open(filename, open_flags, open_mode);
    1.47 @@ -586,15 +598,17 @@ int main(int argc, char *argv[])
    1.48      }
    1.49  
    1.50   out:
    1.51 +    free(victims);
    1.52 +
    1.53      /* Tear down domain paging */
    1.54 -    rc = xenpaging_teardown(paging);
    1.55 -    if ( rc != 0 )
    1.56 -    {
    1.57 +    rc1 = xenpaging_teardown(paging);
    1.58 +    if ( rc1 != 0 )
    1.59          ERROR("Error tearing down paging");
    1.60 -        exit(1);
    1.61 -    }
    1.62  
    1.63 -    return 0;
    1.64 +    if ( rc == 0 )
    1.65 +        rc = rc1;
    1.66 +
    1.67 +    return rc;
    1.68  }
    1.69  
    1.70