int main(int ac,
         char **av) {
  struct NaClDescIoDesc *src;
  struct NaClDescIoDesc *dst;
  struct NaClGioNaClDesc gsrc;
  struct NaClGioNaClDesc gdst;
  int num_errors = 0;

  if (ac != 3) {
    Usage();
    return -1;
  }

  NaClLogModuleInit();
  NaClTimeInit();
  NaClSecureRngModuleInit();
  NaClGlobalSecureRngInit();

  src = NaClDescIoDescOpen(av[1], NACL_ABI_O_RDONLY, 0);
  if (NULL == src) {
    fprintf(stderr, "could not open %s for read\n", av[1]);
    Usage();
    return -2;
  }
  dst = NaClDescIoDescOpen(av[2],
                           NACL_ABI_O_WRONLY|
                           NACL_ABI_O_TRUNC|
                           NACL_ABI_O_CREAT,
                           0666);
  if (NULL == dst) {
    fprintf(stderr, "could not open %s for write\n", av[2]);
    Usage();
    return -3;
  }
  if (!NaClGioNaClDescCtor(&gsrc, (struct NaClDesc *) src)) {
    fprintf(stderr, "NaClGioNaClDescCtor faied for source file\n");
    return -4;
  }
  if (!NaClGioNaClDescCtor(&gdst, (struct NaClDesc *) dst)) {
    fprintf(stderr, "NaClGioNaClDescCtor faied for destination file\n");
    return -5;
  }

  num_errors += GioCopy((struct Gio *) &gsrc, (struct Gio *) &gdst);
  num_errors += ComparePosixFiles(av[1], av[2]);

  (*NACL_VTBL(Gio, &gdst)->Close)(&gdst.base);
  (*NACL_VTBL(Gio, &gdst)->Dtor)(&gdst.base);

  if (0 < num_errors) {
    return num_errors;
  }

  RemoveFile(av[2]);

  /* reverse copy; reuse gsrc */

  dst = NaClDescIoDescOpen(av[2],
                           NACL_ABI_O_WRONLY|
                           NACL_ABI_O_TRUNC|
                           NACL_ABI_O_CREAT,
                           0666);
  if (NULL == dst) {
    fprintf(stderr, "could not open %s for write\n", av[2]);
    Usage();
    return -6;
  }
  if (!NaClGioNaClDescCtor(&gdst, (struct NaClDesc *) dst)) {
    fprintf(stderr, "NaClGioNaClDescCtor faied for destination file\n");
    return -7;
  }

  /*
   * We run GioRevCopy twice because if Seek failed to move the file
   * pointer but reported the file size correctly, it would still
   * result in a correct output.  By running it twice, the destination
   * data is just overwritten if the implementation is correct, and if
   * it isn't, we would end up with a file that is twice the source
   * file size.
   */
  num_errors += GioRevCopy((struct Gio *) &gsrc, (struct Gio *) &gdst);
  num_errors += GioRevCopy((struct Gio *) &gsrc, (struct Gio *) &gdst);
  num_errors += ComparePosixFiles(av[1], av[2]);

  (*NACL_VTBL(Gio, &gdst)->Close)((struct Gio *) &gdst);
  (*NACL_VTBL(Gio, &gdst)->Dtor)((struct Gio *) &gdst);

  (*NACL_VTBL(Gio, &gsrc)->Close)((struct Gio *) &gsrc);
  (*NACL_VTBL(Gio, &gsrc)->Dtor)((struct Gio *) &gsrc);

  if (0 < num_errors) {
    return num_errors;
  }

  RemoveFile(av[2]);

  return num_errors;
}
int main(int argc, char **argv) {
  struct NaClApp app[2];
  struct NaClDesc *nd;
  NaClHandle handle_pair[2];
  int i;
  char *domain1_args[] = {"prog", "domain1"};
  char *domain2_args[] = {"prog", "domain2"};
  int return_code;

  if (argc != 2)
    NaClLog(LOG_FATAL, "Expected 1 argument: executable filename\n");

  NaClAllModulesInit();

  NaClFileNameForValgrind(argv[1]);
  nd = (struct NaClDesc *) NaClDescIoDescOpen(argv[1], NACL_ABI_O_RDONLY, 0);
  CHECK(NULL != nd);

  for (i = 0; i < 2; i++) {
    CHECK(NaClAppCtor(&app[i]));

    /* Use a smaller guest address space size, because 32-bit Windows
       does not let us allocate 2GB of address space.  We don't do this
       for x86-64 because there is an assertion in NaClAllocateSpace()
       that requires 4GB. */
#if NACL_ARCH(NACL_BUILD_ARCH) == NACL_x86 && NACL_BUILD_SUBARCH == 32
    app[i].addr_bits = 29; /* 512MB per process */
#endif

    /*
     * On x86-32, we cannot enable ASLR even when the address space is
     * 512MB.  In particular, when on Windows where the available
     * contiguous address space is tight, if the random choice for the
     * base of the first 512MB address space puts that address space
     * in the middle of the available address space region, the
     * address space allocation code might not be able to (randomly)
     * find another contiguous 512MB region for the second NaCl
     * module.
     */
    CHECK(NaClAppLoadFileAslr(nd, &app[i],
                              NACL_DISABLE_ASLR) == LOAD_OK);
    NaClAppInitialDescriptorHookup(&app[i]);
    CHECK(NaClAppPrepareToLaunch(&app[i]) == LOAD_OK);
  }

  /* Set up an IMC connection between the two guests.  This allows us
     to test communications between the two and also synchronise the
     output for the purpose of checking against the golden file. */
  CHECK(NaClSocketPair(handle_pair) == 0);
  NaClAddImcHandle(&app[0], handle_pair[0], SEND_DESC);
  NaClAddImcHandle(&app[1], handle_pair[1], RECEIVE_DESC);

  CHECK(NaClCreateMainThread(&app[0], 2, domain1_args, NULL));
  CHECK(NaClCreateMainThread(&app[1], 2, domain2_args, NULL));

  return_code = NaClWaitForMainThreadToExit(&app[0]);
  CHECK(return_code == 101);
  return_code = NaClWaitForMainThreadToExit(&app[1]);
  CHECK(return_code == 102);

  /*
   * Avoid calling exit() because it runs process-global destructors
   * which might break code that is running in our unjoined threads.
   */
  NaClExit(0);
}
Beispiel #3
0
int main(int argc, char **argv) {
  
  struct NaClApp app;
  struct NaClDesc *nd;
  NaClHandle handle_pair[2];
  char *parse_args[] = {"prog", "parse"};
  int return_code;
  char buffer[100];
  NaClMessageHeader header;
  NaClIOVec vec;

  struct ncwebserver_t server;
  struct ncwebserver_conf_t conf;

  if (argc < 2){
    usage(ncwebserver_usage_string);
  }

  NaClAllModulesInit();
  NaClFileNameForValgrind(argv[1]);
  nd = (struct NaClDesc *) NaClDescIoDescOpen(argv[1], NACL_ABI_O_RDONLY, 0);
  CHECK(NULL != nd);
  CHECK(NaClAppCtor(&app));
  /* Use a smaller guest address space size, because 32-bit Windows
     does not let us allocate 2GB of address space.  We don't do this
     for x86-64 because there is an assertion in NaClAllocateSpace()
     that requires 4GB. */
#if NACL_ARCH(NACL_BUILD_ARCH) == NACL_x86 && NACL_BUILD_SUBARCH == 32
  app.addr_bits = 29; /* 512MB per process */
#endif
  /*
   * On x86-32, we cannot enable ASLR even when the address space is
   * 512MB.  In particular, when on Windows where the available
   * contiguous address space is tight, if the random choice for the
   * base of the first 512MB address space puts that address space
   * in the middle of the available address space region, the
   * address space allocation code might not be able to (randomly)
   * find another contiguous 512MB region for the second NaCl
   * module.
   */
  CHECK(NaClAppLoadFileAslr(nd, &app,
                            NACL_DISABLE_ASLR) == LOAD_OK);
  NaClAppInitialDescriptorHookup(&app);
  CHECK(NaClAppPrepareToLaunch(&app) == LOAD_OK);
  /* Set up an IMC connection between the host and guest. */
  CHECK(NaClSocketPair(handle_pair) == 0);
  NaClAddImcHandle(&app, handle_pair[0], SEND_DESC);
  CHECK(NaClCreateMainThread(&app, 2, parse_args, NULL));
  return_code = NaClWaitForMainThreadToExit(&app);
  CHECK(return_code == 101);

  // receive message sent from HTML parser
  vec.base = buffer;
  vec.length = sizeof buffer;
  memset(buffer, 0, sizeof buffer);
  header.iov = &vec;
  header.iov_length = 1;
  header.handles = NULL;
  header.handle_count = 0;
  return_code = NaClReceiveDatagram(handle_pair[1], &header, 0);
  assert(return_code == sizeof buffer);
  printf("Server received \"%s\" (with size = %d)\n", buffer+16, return_code);

  // run web server
  memset(&conf, 0, sizeof(conf));
  parse_argv(argc, argv, &conf);
  ncwebserver_init(&server, &conf);
  ncwebserver_start(&server);

  /*
   * Avoid calling exit() because it runs process-global destructors
   * which might break code that is running in our unjoined threads.
   */
  NaClExit(0);
}