linux-user: Fix the computation of the requested heap size

There were several remaining bugs in the previous implementation of
do_brk():

    1. the value of "new_alloc_size" was one page too large when the
       requested brk was aligned on a host page boundary.

    2. no new pages should be (re-)allocated when the requested brk is
       in the range of the pages that were already allocated
       previsouly (for the same purpose).  Technically these pages are
       never unmapped in the current implementation.

The problem/fix can be reproduced/validated with the test-suite above:

    #include <unistd.h>       /* syscall(2),      */
    #include <sys/syscall.h>  /* SYS_brk,         */
    #include <stdio.h>        /* puts(3),         */
    #include <stdlib.h>       /* exit(3), EXIT_*, */
    #include <stdint.h>       /* uint*_t,         */
    #include <sys/mman.h>     /* mmap(2), MAP_*,  */
    #include <string.h>       /* memset(3), */

    int main()
    {
        int exit_status = EXIT_SUCCESS;
        uint8_t *current_brk = 0;
        uint8_t *initial_brk;
        uint8_t *new_brk;
        uint8_t *old_brk;
        int failure = 0;
        int i;

        void test_brk(int increment, int expected_result) {
            new_brk = (uint8_t *)syscall(SYS_brk, current_brk + increment);
            if ((new_brk == current_brk) == expected_result)
                failure = 1;
            current_brk = (uint8_t *)syscall(SYS_brk, 0);
        }

        void test_result() {
            if (!failure)
                puts("OK");
            else {
                puts("failure");
                exit_status = EXIT_FAILURE;
            }
        }

        void test_title(const char *title) {
            failure = 0;
            printf("%-45s : ", title);
            fflush(stdout);
        }

        test_title("Initialization");
        test_brk(0, 1);
        initial_brk = current_brk;
        test_result();

        test_title("Don't overlap \"brk\" pages");
        test_brk(HOST_PAGE_SIZE, 1);
        test_brk(HOST_PAGE_SIZE, 1);
        test_result();

        /* Preparation for the test "Re-allocated heap is initialized".  */
        old_brk = current_brk - HOST_PAGE_SIZE;
        memset(old_brk, 0xFF, HOST_PAGE_SIZE);

        test_title("Don't allocate the same \"brk\" page twice");
        test_brk(-HOST_PAGE_SIZE, 1);
        test_brk(HOST_PAGE_SIZE, 1);
        test_result();

        test_title("Re-allocated \"brk\" pages are initialized");
        for (i = 0; i < HOST_PAGE_SIZE; i++) {
            if (old_brk[i] != 0) {
                printf("(index = %d, value = 0x%x) ", i, old_brk[i]);
                failure = 1;
                break;
            }
        }
        test_result();

        test_title("Don't allocate \"brk\" pages over \"mmap\" pages");
        new_brk = mmap(current_brk, HOST_PAGE_SIZE / 2, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
        if (new_brk == (void *) -1)
            puts("unknown");
        else {
            test_brk(HOST_PAGE_SIZE, 0);
            test_result();
        }

        test_title("All \"brk\" pages are writable (please wait)");
        if (munmap(current_brk, HOST_PAGE_SIZE / 2) != 0)
            puts("unknown");
        else {
            while (current_brk - initial_brk < 2*1024*1024*1024UL) {
                old_brk = current_brk;

                test_brk(HOST_PAGE_SIZE, -1);
                if (old_brk == current_brk)
                    break;

                for (i = 0; i < HOST_PAGE_SIZE; i++)
                    old_brk[i] = 0xAA;
            }
            puts("OK");
        }

        test_title("Maximum size of the heap > 16MB");
        failure = (current_brk - initial_brk) < 16*1024*1024;
        test_result();

        exit(exit_status);
    }

Changes introduced in patch v2:

    * extend the "brk" test-suite embedded within the commit message;

    * heap contents have to be initialized to zero, this bug was
      exposed by "tst-calloc.c" from the GNU C library;

    * don't [try to] allocate a new host page if the new "brk" is
      equal to the latest allocated host page ("brk_page"); and

    * print some debug information when DEBUGF_BRK is defined.

Signed-off-by: Cédric VINCENT <cedric.vincent@st.com>
Reviewed-by: Christophe Guillon <christophe.guillon@st.com>
Cc: Riku Voipio <riku.voipio@iki.fi>
Signed-off-by: Riku Voipio <riku.voipio@iki.fi>
1 file changed
tree: 49f6e0735bdbb78292f7a8953c6a4592885f0dfa
  1. audio/
  2. block/
  3. bsd-user/
  4. darwin-user/
  5. default-configs/
  6. docs/
  7. fpu/
  8. fsdev/
  9. gdb-xml/
  10. hw/
  11. libcacard/
  12. linux-user/
  13. net/
  14. pc-bios/
  15. QMP/
  16. roms/
  17. scripts/
  18. slirp/
  19. sysconfigs/
  20. target-alpha/
  21. target-arm/
  22. target-cris/
  23. target-i386/
  24. target-lm32/
  25. target-m68k/
  26. target-microblaze/
  27. target-mips/
  28. target-ppc/
  29. target-s390x/
  30. target-sh4/
  31. target-sparc/
  32. target-unicore32/
  33. tcg/
  34. tests/
  35. ui/
  36. .gitignore
  37. .gitmodules
  38. a.out.h
  39. acl.c
  40. acl.h
  41. aes.c
  42. aes.h
  43. aio.c
  44. alpha-dis.c
  45. alpha.ld
  46. arch_init.c
  47. arch_init.h
  48. arm-dis.c
  49. arm-semi.c
  50. arm.ld
  51. async.c
  52. balloon.c
  53. balloon.h
  54. bitmap.c
  55. bitmap.h
  56. bitops.c
  57. bitops.h
  58. block-migration.c
  59. block-migration.h
  60. block.c
  61. block.h
  62. block_int.h
  63. blockdev.c
  64. blockdev.h
  65. bswap.h
  66. bt-host.c
  67. bt-host.h
  68. bt-vhci.c
  69. buffered_file.c
  70. buffered_file.h
  71. cache-utils.c
  72. cache-utils.h
  73. Changelog
  74. check-qdict.c
  75. check-qfloat.c
  76. check-qint.c
  77. check-qjson.c
  78. check-qlist.c
  79. check-qstring.c
  80. cmd.c
  81. cmd.h
  82. CODING_STYLE
  83. compatfd.c
  84. compatfd.h
  85. config.h
  86. configure
  87. console.c
  88. console.h
  89. COPYING
  90. COPYING.LIB
  91. cpu-all.h
  92. cpu-common.h
  93. cpu-defs.h
  94. cpu-exec.c
  95. cpus.c
  96. cpus.h
  97. cris-dis.c
  98. cursor.c
  99. cursor_hidden.xpm
  100. cursor_left_ptr.xpm
  101. cutils.c
  102. def-helper.h
  103. device_tree.c
  104. device_tree.h
  105. dis-asm.h
  106. disas.c
  107. disas.h
  108. dma-helpers.c
  109. dma.h
  110. dyngen-exec.h
  111. elf.h
  112. envlist.c
  113. envlist.h
  114. error.c
  115. error.h
  116. error_int.h
  117. exec-all.h
  118. exec.c
  119. gdbstub.c
  120. gdbstub.h
  121. gen-icount.h
  122. HACKING
  123. hmp-commands.hx
  124. host-utils.c
  125. host-utils.h
  126. hpet.h
  127. hppa-dis.c
  128. hppa.ld
  129. i386-dis.c
  130. i386.ld
  131. ia64-dis.c
  132. ia64.ld
  133. input.c
  134. iohandler.c
  135. ioport-user.c
  136. ioport.c
  137. ioport.h
  138. iorange.h
  139. iov.c
  140. iov.h
  141. json-lexer.c
  142. json-lexer.h
  143. json-parser.c
  144. json-parser.h
  145. json-streamer.c
  146. json-streamer.h
  147. kvm-all.c
  148. kvm-stub.c
  149. kvm.h
  150. libfdt_env.h
  151. LICENSE
  152. linux-aio.c
  153. m68k-dis.c
  154. m68k-semi.c
  155. m68k.ld
  156. MAINTAINERS
  157. Makefile
  158. Makefile.dis
  159. Makefile.hw
  160. Makefile.objs
  161. Makefile.target
  162. Makefile.user
  163. microblaze-dis.c
  164. migration-exec.c
  165. migration-fd.c
  166. migration-tcp.c
  167. migration-unix.c
  168. migration.c
  169. migration.h
  170. mips-dis.c
  171. mips.ld
  172. module.c
  173. module.h
  174. monitor.c
  175. monitor.h
  176. nbd.c
  177. nbd.h
  178. net.c
  179. net.h
  180. notify.c
  181. notify.h
  182. os-posix.c
  183. os-win32.c
  184. osdep.c
  185. osdep.h
  186. oslib-posix.c
  187. oslib-win32.c
  188. path.c
  189. pci-ids.txt
  190. pflib.c
  191. pflib.h
  192. poison.h
  193. posix-aio-compat.c
  194. ppc-dis.c
  195. ppc.ld
  196. ppc64.ld
  197. qbool.c
  198. qbool.h
  199. qdict-test-data.txt
  200. qdict.c
  201. qdict.h
  202. qemu-aio.h
  203. qemu-barrier.h
  204. qemu-char.c
  205. qemu-char.h
  206. qemu-common.h
  207. qemu-config.c
  208. qemu-config.h
  209. qemu-doc.texi
  210. qemu-error.c
  211. qemu-error.h
  212. qemu-img-cmds.hx
  213. qemu-img.c
  214. qemu-img.texi
  215. qemu-io.c
  216. qemu-lock.h
  217. qemu-log.h
  218. qemu-malloc.c
  219. qemu-nbd.c
  220. qemu-nbd.texi
  221. qemu-objects.h
  222. qemu-option.c
  223. qemu-option.h
  224. qemu-options.h
  225. qemu-options.hx
  226. qemu-os-posix.h
  227. qemu-os-win32.h
  228. qemu-progress.c
  229. qemu-queue.h
  230. qemu-sockets.c
  231. qemu-tech.texi
  232. qemu-thread-posix.c
  233. qemu-thread-posix.h
  234. qemu-thread-win32.c
  235. qemu-thread-win32.h
  236. qemu-thread.h
  237. qemu-timer-common.c
  238. qemu-timer.c
  239. qemu-timer.h
  240. qemu-tool.c
  241. qemu-x509.h
  242. qemu.sasl
  243. qemu_socket.h
  244. qerror.c
  245. qerror.h
  246. qfloat.c
  247. qfloat.h
  248. qint.c
  249. qint.h
  250. qjson.c
  251. qjson.h
  252. qlist.c
  253. qlist.h
  254. qmp-commands.hx
  255. qobject.h
  256. qstring.c
  257. qstring.h
  258. range.h
  259. readline.c
  260. readline.h
  261. README
  262. rules.mak
  263. rwhandler.c
  264. rwhandler.h
  265. s390-dis.c
  266. s390.ld
  267. savevm.c
  268. sh4-dis.c
  269. simpletrace.c
  270. simpletrace.h
  271. softmmu-semi.h
  272. softmmu_defs.h
  273. softmmu_exec.h
  274. softmmu_header.h
  275. softmmu_template.h
  276. sparc-dis.c
  277. sparc.ld
  278. sparc64.ld
  279. spice-qemu-char.c
  280. sysemu.h
  281. targphys.h
  282. tcg-runtime.c
  283. thunk.c
  284. thunk.h
  285. TODO
  286. trace-events
  287. translate-all.c
  288. uboot_image.h
  289. usb-bsd.c
  290. usb-linux.c
  291. usb-stub.c
  292. user-exec.c
  293. VERSION
  294. version.rc
  295. vgafont.h
  296. vl.c
  297. x86_64.ld
  298. xen-all.c
  299. xen-mapcache-stub.c
  300. xen-mapcache.c
  301. xen-mapcache.h
  302. xen-stub.c