orccodemem.c 1.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12

#include "config.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>

#include <orc/orcprogram.h>
13
#include <orc/orcdebug.h>
14 15 16 17

#define SIZE 65536


18
#ifdef HAVE_CODEMEM_MMAP
19 20 21 22 23
void
orc_program_allocate_codemem (OrcProgram *program)
{
  char filename[32] = "/tmp/orcexecXXXXXX";
  int fd;
24
  int n;
25 26 27 28

  fd = mkstemp (filename);
  if (fd == -1) {
    /* FIXME oh crap */
29
    ORC_ERROR ("failed to create temp file");
30 31 32 33 34
    program->error = TRUE;
    return;
  }
  unlink (filename);

35
  n = ftruncate (fd, SIZE);
36 37 38 39

  program->code = mmap (NULL, SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  if (program->code == MAP_FAILED) {
    /* FIXME oh crap */
40
    ORC_ERROR ("failed to create write map");
41 42 43 44 45 46
    program->error = TRUE;
    return;
  }
  program->code_exec = mmap (NULL, SIZE, PROT_READ|PROT_EXEC, MAP_SHARED, fd, 0);
  if (program->code_exec == MAP_FAILED) {
    /* FIXME oh crap */
47
    ORC_ERROR ("failed to create exec map");
48 49 50 51 52 53 54 55 56
    program->error = TRUE;
    return;
  }

  close (fd);

  program->code_size = SIZE;
  program->codeptr = program->code;
}
57 58 59 60 61 62 63 64 65 66 67 68 69 70
#endif

#ifdef HAVE_CODEMEM_MALLOC
void
orc_program_allocate_codemem (OrcProgram *program)
{
  /* Now you know why Windows has viruses */

  program->code = malloc(SIZE);
  program->code_exec = program->code;
  program->code_size = SIZE;
  program->codeptr = program->code;
}
#endif
71