mlock-random-test.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * It tests the mlock/mlock2() when they are invoked
  3. * on randomly memory region.
  4. */
  5. #include <unistd.h>
  6. #include <sys/resource.h>
  7. #include <sys/capability.h>
  8. #include <sys/mman.h>
  9. #include <fcntl.h>
  10. #include <string.h>
  11. #include <sys/ipc.h>
  12. #include <sys/shm.h>
  13. #include <time.h>
  14. #include "mlock2.h"
  15. #define CHUNK_UNIT (128 * 1024)
  16. #define MLOCK_RLIMIT_SIZE (CHUNK_UNIT * 2)
  17. #define MLOCK_WITHIN_LIMIT_SIZE CHUNK_UNIT
  18. #define MLOCK_OUTOF_LIMIT_SIZE (CHUNK_UNIT * 3)
  19. #define TEST_LOOP 100
  20. #define PAGE_ALIGN(size, ps) (((size) + ((ps) - 1)) & ~((ps) - 1))
  21. int set_cap_limits(rlim_t max)
  22. {
  23. struct rlimit new;
  24. cap_t cap = cap_init();
  25. new.rlim_cur = max;
  26. new.rlim_max = max;
  27. if (setrlimit(RLIMIT_MEMLOCK, &new)) {
  28. perror("setrlimit() returns error\n");
  29. return -1;
  30. }
  31. /* drop capabilities including CAP_IPC_LOCK */
  32. if (cap_set_proc(cap)) {
  33. perror("cap_set_proc() returns error\n");
  34. return -2;
  35. }
  36. return 0;
  37. }
  38. int get_proc_locked_vm_size(void)
  39. {
  40. FILE *f;
  41. int ret = -1;
  42. char line[1024] = {0};
  43. unsigned long lock_size = 0;
  44. f = fopen("/proc/self/status", "r");
  45. if (!f) {
  46. perror("fopen");
  47. return -1;
  48. }
  49. while (fgets(line, 1024, f)) {
  50. if (strstr(line, "VmLck")) {
  51. ret = sscanf(line, "VmLck:\t%8lu kB", &lock_size);
  52. if (ret <= 0) {
  53. printf("sscanf() on VmLck error: %s: %d\n",
  54. line, ret);
  55. fclose(f);
  56. return -1;
  57. }
  58. fclose(f);
  59. return (int)(lock_size << 10);
  60. }
  61. }
  62. perror("cann't parse VmLck in /proc/self/status\n");
  63. fclose(f);
  64. return -1;
  65. }
  66. /*
  67. * Get the MMUPageSize of the memory region including input
  68. * address from proc file.
  69. *
  70. * return value: on error case, 0 will be returned.
  71. * Otherwise the page size(in bytes) is returned.
  72. */
  73. int get_proc_page_size(unsigned long addr)
  74. {
  75. FILE *smaps;
  76. char *line;
  77. unsigned long mmupage_size = 0;
  78. size_t size;
  79. smaps = seek_to_smaps_entry(addr);
  80. if (!smaps) {
  81. printf("Unable to parse /proc/self/smaps\n");
  82. return 0;
  83. }
  84. while (getline(&line, &size, smaps) > 0) {
  85. if (!strstr(line, "MMUPageSize")) {
  86. free(line);
  87. line = NULL;
  88. size = 0;
  89. continue;
  90. }
  91. /* found the MMUPageSize of this section */
  92. if (sscanf(line, "MMUPageSize: %8lu kB",
  93. &mmupage_size) < 1) {
  94. printf("Unable to parse smaps entry for Size:%s\n",
  95. line);
  96. break;
  97. }
  98. }
  99. free(line);
  100. if (smaps)
  101. fclose(smaps);
  102. return mmupage_size << 10;
  103. }
  104. /*
  105. * Test mlock/mlock2() on provided memory chunk.
  106. * It expects the mlock/mlock2() to be successful (within rlimit)
  107. *
  108. * With allocated memory chunk [p, p + alloc_size), this
  109. * test will choose start/len randomly to perform mlock/mlock2
  110. * [start, start + len] memory range. The range is within range
  111. * of the allocated chunk.
  112. *
  113. * The memory region size alloc_size is within the rlimit.
  114. * So we always expect a success of mlock/mlock2.
  115. *
  116. * VmLck is assumed to be 0 before this test.
  117. *
  118. * return value: 0 - success
  119. * else: failure
  120. */
  121. int test_mlock_within_limit(char *p, int alloc_size)
  122. {
  123. int i;
  124. int ret = 0;
  125. int locked_vm_size = 0;
  126. struct rlimit cur;
  127. int page_size = 0;
  128. getrlimit(RLIMIT_MEMLOCK, &cur);
  129. if (cur.rlim_cur < alloc_size) {
  130. printf("alloc_size[%d] < %u rlimit,lead to mlock failure\n",
  131. alloc_size, (unsigned int)cur.rlim_cur);
  132. return -1;
  133. }
  134. srand(time(NULL));
  135. for (i = 0; i < TEST_LOOP; i++) {
  136. /*
  137. * - choose mlock/mlock2 randomly
  138. * - choose lock_size randomly but lock_size < alloc_size
  139. * - choose start_offset randomly but p+start_offset+lock_size
  140. * < p+alloc_size
  141. */
  142. int is_mlock = !!(rand() % 2);
  143. int lock_size = rand() % alloc_size;
  144. int start_offset = rand() % (alloc_size - lock_size);
  145. if (is_mlock)
  146. ret = mlock(p + start_offset, lock_size);
  147. else
  148. ret = mlock2_(p + start_offset, lock_size,
  149. MLOCK_ONFAULT);
  150. if (ret) {
  151. printf("%s() failure at |%p(%d)| mlock:|%p(%d)|\n",
  152. is_mlock ? "mlock" : "mlock2",
  153. p, alloc_size,
  154. p + start_offset, lock_size);
  155. return ret;
  156. }
  157. }
  158. /*
  159. * Check VmLck left by the tests.
  160. */
  161. locked_vm_size = get_proc_locked_vm_size();
  162. page_size = get_proc_page_size((unsigned long)p);
  163. if (page_size == 0) {
  164. printf("cannot get proc MMUPageSize\n");
  165. return -1;
  166. }
  167. if (locked_vm_size > PAGE_ALIGN(alloc_size, page_size) + page_size) {
  168. printf("test_mlock_within_limit() left VmLck:%d on %d chunk\n",
  169. locked_vm_size, alloc_size);
  170. return -1;
  171. }
  172. return 0;
  173. }
  174. /*
  175. * We expect the mlock/mlock2() to be fail (outof limitation)
  176. *
  177. * With allocated memory chunk [p, p + alloc_size), this
  178. * test will randomly choose start/len and perform mlock/mlock2
  179. * on [start, start+len] range.
  180. *
  181. * The memory region size alloc_size is above the rlimit.
  182. * And the len to be locked is higher than rlimit.
  183. * So we always expect a failure of mlock/mlock2.
  184. * No locked page number should be increased as a side effect.
  185. *
  186. * return value: 0 - success
  187. * else: failure
  188. */
  189. int test_mlock_outof_limit(char *p, int alloc_size)
  190. {
  191. int i;
  192. int ret = 0;
  193. int locked_vm_size = 0, old_locked_vm_size = 0;
  194. struct rlimit cur;
  195. getrlimit(RLIMIT_MEMLOCK, &cur);
  196. if (cur.rlim_cur >= alloc_size) {
  197. printf("alloc_size[%d] >%u rlimit, violates test condition\n",
  198. alloc_size, (unsigned int)cur.rlim_cur);
  199. return -1;
  200. }
  201. old_locked_vm_size = get_proc_locked_vm_size();
  202. srand(time(NULL));
  203. for (i = 0; i < TEST_LOOP; i++) {
  204. int is_mlock = !!(rand() % 2);
  205. int lock_size = (rand() % (alloc_size - cur.rlim_cur))
  206. + cur.rlim_cur;
  207. int start_offset = rand() % (alloc_size - lock_size);
  208. if (is_mlock)
  209. ret = mlock(p + start_offset, lock_size);
  210. else
  211. ret = mlock2_(p + start_offset, lock_size,
  212. MLOCK_ONFAULT);
  213. if (ret == 0) {
  214. printf("%s() succeeds? on %p(%d) mlock%p(%d)\n",
  215. is_mlock ? "mlock" : "mlock2",
  216. p, alloc_size,
  217. p + start_offset, lock_size);
  218. return -1;
  219. }
  220. }
  221. locked_vm_size = get_proc_locked_vm_size();
  222. if (locked_vm_size != old_locked_vm_size) {
  223. printf("tests leads to new mlocked page: old[%d], new[%d]\n",
  224. old_locked_vm_size,
  225. locked_vm_size);
  226. return -1;
  227. }
  228. return 0;
  229. }
  230. int main(int argc, char **argv)
  231. {
  232. char *p = NULL;
  233. int ret = 0;
  234. if (set_cap_limits(MLOCK_RLIMIT_SIZE))
  235. return -1;
  236. p = malloc(MLOCK_WITHIN_LIMIT_SIZE);
  237. if (p == NULL) {
  238. perror("malloc() failure\n");
  239. return -1;
  240. }
  241. ret = test_mlock_within_limit(p, MLOCK_WITHIN_LIMIT_SIZE);
  242. if (ret)
  243. return ret;
  244. munlock(p, MLOCK_WITHIN_LIMIT_SIZE);
  245. free(p);
  246. p = malloc(MLOCK_OUTOF_LIMIT_SIZE);
  247. if (p == NULL) {
  248. perror("malloc() failure\n");
  249. return -1;
  250. }
  251. ret = test_mlock_outof_limit(p, MLOCK_OUTOF_LIMIT_SIZE);
  252. if (ret)
  253. return ret;
  254. munlock(p, MLOCK_OUTOF_LIMIT_SIZE);
  255. free(p);
  256. return 0;
  257. }