se_memory.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (C) 2011-2018 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include "se_memory.h"
  32. #include "se_trace.h"
  33. #include "util.h"
  34. void* se_virtual_alloc(void* address, size_t size, uint32_t type)
  35. {
  36. UNUSED(type);
  37. void* pRet = mmap(address, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  38. if(MAP_FAILED == pRet)
  39. return NULL;
  40. return pRet;
  41. }
  42. int se_virtual_free(void* address, size_t size, uint32_t type)
  43. {
  44. UNUSED(type);
  45. return !(munmap(address, size));
  46. }
  47. int se_virtual_lock(void* address, size_t size)
  48. {
  49. return !mlock(address, size);
  50. }
  51. static unsigned int get_prot(uint64_t flags)
  52. {
  53. if ((flags & SI_FLAG_PT_MASK) == SI_FLAG_TCS)
  54. return PROT_READ|PROT_WRITE|PROT_EXEC;
  55. switch (flags & (SI_FLAG_R | SI_FLAG_W | SI_FLAG_X))
  56. {
  57. case SI_FLAG_X: return PROT_EXEC; break;
  58. case SI_FLAG_R | SI_FLAG_X: return PROT_READ|PROT_EXEC; break;
  59. case SI_FLAG_R | SI_FLAG_W | SI_FLAG_X: return PROT_READ|PROT_WRITE|PROT_EXEC; break;
  60. case SI_FLAG_R: return PROT_READ; break;
  61. case SI_FLAG_R | SI_FLAG_W: return PROT_READ|PROT_WRITE; break;
  62. /* This covers no access, W and WX */
  63. default: return PROT_NONE; break;
  64. }
  65. }
  66. int se_virtual_protect(void* address, size_t size, uint32_t prot)
  67. {
  68. return !mprotect(address, size, (int)get_prot(prot));
  69. }
  70. se_proc_t get_self_proc()
  71. {
  72. return getpid();
  73. }
  74. int put_self_proc(se_proc_t proc)
  75. {
  76. UNUSED(proc);
  77. return 1;
  78. }
  79. int se_read_process_mem(se_proc_t proc, void* base_addr, void* buffer, size_t size, size_t* read_nr)
  80. {
  81. char filename[64];
  82. int fd = -1;
  83. int ret = FALSE;
  84. ssize_t len = 0;
  85. off64_t offset = (off64_t)(size_t) base_addr;
  86. snprintf (filename, 64, "/proc/%d/mem", (int)proc);
  87. fd = open(filename, O_RDONLY | O_LARGEFILE);
  88. if(fd == -1)
  89. return FALSE;
  90. if(lseek64(fd, offset, SEEK_SET) == -1)
  91. {
  92. goto out;
  93. }
  94. if((len = read(fd, buffer, size)) < 0)
  95. {
  96. goto out;
  97. }
  98. else if(read_nr)
  99. *read_nr = (size_t)len; /* len is a non-negative number */
  100. ret = TRUE;
  101. out:
  102. close (fd);
  103. return ret;
  104. }
  105. int se_write_process_mem(se_proc_t proc, void* base_addr, void* buffer, size_t size, size_t* write_nr)
  106. {
  107. char filename[64];
  108. int fd = -1;
  109. int ret = FALSE;
  110. ssize_t len = 0;
  111. off64_t offset = (off64_t)(size_t)base_addr;
  112. snprintf (filename, 64, "/proc/%d/mem", (int)proc);
  113. fd = open(filename, O_RDWR | O_LARGEFILE);
  114. if(fd == -1)
  115. return FALSE;
  116. if(lseek64(fd, offset, SEEK_SET) == -1)
  117. {
  118. goto out;
  119. }
  120. if((len = write(fd, buffer, size)) < 0)
  121. {
  122. goto out;
  123. }
  124. else if(write_nr)
  125. *write_nr = (size_t)len; /* len is a non-negative number */
  126. ret = TRUE;
  127. out:
  128. close (fd);
  129. return ret;
  130. }