| 
 | 1 | +#include <assert.h>  | 
 | 2 | +#include <stdlib.h>  | 
 | 3 | +#include <stddef.h>  | 
 | 4 | +#include <stdio.h>  | 
 | 5 | +#include <string.h>  | 
 | 6 | +#include <sys/mman.h>  | 
 | 7 | +#include <unistd.h>  | 
 | 8 | + | 
 | 9 | +#include "plasma.h"  | 
 | 10 | +#include "uthash.h"  | 
 | 11 | + | 
 | 12 | +void *fake_mmap(size_t);  | 
 | 13 | +int fake_munmap(void *, size_t);  | 
 | 14 | + | 
 | 15 | +#define MMAP(s) fake_mmap(s)  | 
 | 16 | +#define MUNMAP(a, s) fake_munmap(a, s)  | 
 | 17 | +#define DIRECT_MMAP(s) fake_mmap(s)  | 
 | 18 | +#define DIRECT_MUNMAP(a, s) fake_munmap(a, s)  | 
 | 19 | +#define USE_DL_PREFIX  | 
 | 20 | +#define HAVE_MORECORE 0  | 
 | 21 | + | 
 | 22 | +#include "third_party/dlmalloc.c"  | 
 | 23 | + | 
 | 24 | +#undef MMAP  | 
 | 25 | +#undef MUNMAP  | 
 | 26 | +#undef DIRECT_MMAP  | 
 | 27 | +#undef DIRECT_MUNMAP  | 
 | 28 | +#undef USE_DL_PREFIX  | 
 | 29 | +#undef HAVE_MORECORE  | 
 | 30 | + | 
 | 31 | +struct mmap_record {  | 
 | 32 | +  int fd;  | 
 | 33 | +  void *pointer;  | 
 | 34 | +  int64_t size;  | 
 | 35 | +  UT_hash_handle hh_fd;  | 
 | 36 | +  UT_hash_handle hh_pointer;  | 
 | 37 | +};  | 
 | 38 | + | 
 | 39 | +struct mmap_record *records_by_fd = NULL;  | 
 | 40 | +struct mmap_record *records_by_pointer = NULL;  | 
 | 41 | + | 
 | 42 | +/* Create a buffer. This is creating a temporary file and then  | 
 | 43 | + * immediately unlinking it so we do not leave traces in the system. */  | 
 | 44 | +int create_buffer(int64_t size) {  | 
 | 45 | +  static char template[] = "/tmp/plasmaXXXXXX";  | 
 | 46 | +  char file_name[32];  | 
 | 47 | +  strncpy(file_name, template, 32);  | 
 | 48 | +  int fd = mkstemp(file_name);  | 
 | 49 | +  if (fd < 0)  | 
 | 50 | +    return -1;  | 
 | 51 | +  FILE *file = fdopen(fd, "a+");  | 
 | 52 | +  if (!file) {  | 
 | 53 | +    close(fd);  | 
 | 54 | +    return -1;  | 
 | 55 | +  }  | 
 | 56 | +  if (unlink(file_name) != 0) {  | 
 | 57 | +    LOG_ERR("unlink error");  | 
 | 58 | +    return -1;  | 
 | 59 | +  }  | 
 | 60 | +  if (ftruncate(fd, (off_t) size) != 0) {  | 
 | 61 | +    LOG_ERR("ftruncate error");  | 
 | 62 | +    return -1;  | 
 | 63 | +  }  | 
 | 64 | +  return fd;  | 
 | 65 | +}  | 
 | 66 | + | 
 | 67 | +void *fake_mmap(size_t size) {  | 
 | 68 | +  // Add sizeof(size_t) so that the returned pointer is deliberately not  | 
 | 69 | +  // page-aligned. This ensures that the segments of memory returned by  | 
 | 70 | +  // fake_mmap are never contiguous.  | 
 | 71 | +  int fd = create_buffer(size + sizeof(size_t));  | 
 | 72 | +  void *pointer = mmap(NULL, size + sizeof(size_t), PROT_READ | PROT_WRITE,  | 
 | 73 | +                       MAP_SHARED, fd, 0);  | 
 | 74 | +  if (pointer == MAP_FAILED) {  | 
 | 75 | +    return pointer;  | 
 | 76 | +  }  | 
 | 77 | +  pointer += sizeof(size_t);  | 
 | 78 | + | 
 | 79 | +  struct mmap_record *record = malloc(sizeof(struct mmap_record));  | 
 | 80 | +  record->fd = fd;  | 
 | 81 | +  record->pointer = pointer;  | 
 | 82 | +  record->size = size;  | 
 | 83 | +  HASH_ADD(hh_fd, records_by_fd, fd, sizeof(fd), record);  | 
 | 84 | +  HASH_ADD(hh_pointer, records_by_pointer, pointer, sizeof(pointer), record);  | 
 | 85 | + | 
 | 86 | +  LOG_DEBUG("%p = fake_mmap(%lu)", pointer, size);  | 
 | 87 | +  return pointer;  | 
 | 88 | +}  | 
 | 89 | + | 
 | 90 | +int fake_munmap(void *addr, size_t size) {  | 
 | 91 | +  LOG_DEBUG("fake_munmap(%p, %lu)", addr, size);  | 
 | 92 | + | 
 | 93 | +  struct mmap_record *record;  | 
 | 94 | + | 
 | 95 | +  addr -= sizeof(size_t);  | 
 | 96 | +  HASH_FIND(hh_pointer, records_by_pointer, &addr, sizeof(addr), record);  | 
 | 97 | +  assert(record != NULL);  | 
 | 98 | +  close(record->fd);  | 
 | 99 | + | 
 | 100 | +  HASH_DELETE(hh_fd, records_by_fd, record);  | 
 | 101 | +  HASH_DELETE(hh_pointer, records_by_pointer, record);  | 
 | 102 | + | 
 | 103 | +  return munmap(addr, size + sizeof(size_t));  | 
 | 104 | +}  | 
 | 105 | + | 
 | 106 | +void get_malloc_mapinfo(void *addr,  | 
 | 107 | +                        int *fd,  | 
 | 108 | +                        int64_t *map_size,  | 
 | 109 | +                        ptrdiff_t *offset) {  | 
 | 110 | +  struct mmap_record *record;  | 
 | 111 | +  // TODO(rshin): Implement a more efficient search through records_by_fd.  | 
 | 112 | +  for (record = records_by_fd; record != NULL; record = record->hh_fd.next) {  | 
 | 113 | +    if (addr >= record->pointer && addr < record->pointer + record->size) {  | 
 | 114 | +      *fd = record->fd;  | 
 | 115 | +      *map_size = record->size;  | 
 | 116 | +      *offset = addr - record->pointer;  | 
 | 117 | +      return;  | 
 | 118 | +    }  | 
 | 119 | +  }  | 
 | 120 | +  *fd = -1;  | 
 | 121 | +  *map_size = 0;  | 
 | 122 | +  *offset = 0;  | 
 | 123 | +}  | 
0 commit comments