Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. /*-
  2. * Copyright (c) 2014 EMC / Isilon Storage Division
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  15. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  16. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  18. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. * POSSIBILITY OF SUCH DAMAGE.
  25. */
  26.  
  27. #include <atf-c.h>
  28.  
  29. #include <assert.h>
  30. #include <dirent.h>
  31. #include <err.h>
  32. #include <fcntl.h>
  33. #include <malloc_np.h>
  34. #include <stdio.h>
  35. #include <stdint.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <unistd.h>
  39.  
  40. ATF_TC(cgetstr_leak);
  41. ATF_TC_HEAD(cgetstr_leak, tc)
  42. {
  43.  
  44. atf_tc_set_md_var(tc, "descr",
  45. "Check cgetstr(3) for memory leakage (Bug 195128)");
  46. }
  47.  
  48. void
  49. malgetuint64(char *name, uint64_t *val)
  50. {
  51. size_t len;
  52.  
  53. len = sizeof(*val);
  54. if (mallctl(name, val, &len, NULL, 0) == -1)
  55. err(2, "mallctl failed");
  56. if (len != sizeof(*val))
  57. err(2, "bad size from mallctl");
  58. }
  59.  
  60. int64_t
  61. malallocated(void)
  62. {
  63. uint64_t allocated, deallocated;
  64.  
  65. malgetuint64("thread.allocated", &allocated);
  66. malgetuint64("thread.deallocated", &deallocated);
  67. printf("alloc: %jd dealloc: %jd delta: %jd\n", (uintmax_t)allocated, (uintmax_t)deallocated, (uintmax_t)(allocated - deallocated));
  68. return ((int64_t)(allocated - deallocated));
  69. }
  70.  
  71. ATF_TC_BODY(cgetstr_leak, tc)
  72. {
  73. char *buf, *str, *dbarray[] = { "/etc/login.conf" };
  74. int errcode, i;
  75. int64_t allocated, start;
  76.  
  77. start = malallocated();
  78. printf("Start %jd\n", (intmax_t)start);
  79.  
  80. i = cgetent(&buf, dbarray, "default");
  81. if (i == -1)
  82. atf_tc_skip("Unable to find default class in login.conf");
  83. if (i == -2)
  84. atf_tc_skip("Error reading login.conf");
  85.  
  86. allocated = malallocated();
  87. printf("cgetent %jd\n", (intmax_t)allocated);
  88. for (i = 0; i < 1000; i++) {
  89. if ((errcode = cgetstr(buf, "path", &str)) < 0)
  90. atf_tc_skip("cgetstr failed with %d", errcode);
  91. }
  92. allocated = malallocated();
  93. printf("cgetstr %jd\n", (intmax_t)allocated);
  94. free(buf);
  95. printf("free %jd\n", (intmax_t)allocated);
  96. cgetclose();
  97.  
  98. allocated = malallocated();
  99. printf("cgetclose %jd\n", (intmax_t)allocated);
  100. if (allocated - start > 4096)
  101. atf_tc_fail("Failure: leaked %jd bytes", allocated - start);
  102. else
  103. (void)printf("OK: used %jd bytes\n", allocated - start);
  104. }
  105.  
  106. ATF_TP_ADD_TCS(tp)
  107. {
  108.  
  109. ATF_TP_ADD_TC(tp, cgetstr_leak);
  110.  
  111. return atf_no_error();
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement