CanaryAllocator_test.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "memory/Allocator.h"
  16. #include "memory/BufferAllocator.h"
  17. #include "memory/CanaryAllocator_pvt.h"
  18. #include "util/Assert.h"
  19. void tryOverflowLow(struct Allocator* alloc, int bytes)
  20. {
  21. uint8_t* buff = Allocator_malloc(alloc, bytes);
  22. buff[-1] = 0;
  23. Assert_always(CanaryAllocator_isOverflow((struct CanaryAllocator_pvt*) alloc));
  24. }
  25. void tryOverflowHigh(struct Allocator* alloc, int bytes)
  26. {
  27. uint8_t* buff = Allocator_malloc(alloc, bytes);
  28. buff[bytes + 3] = 0;
  29. Assert_always(CanaryAllocator_isOverflow((struct CanaryAllocator_pvt*) alloc));
  30. }
  31. void tryOverflow(struct Allocator* alloc, int bytes)
  32. {
  33. // can't free these child allocators or they will explode!
  34. struct Allocator* child = Allocator_child(alloc);
  35. tryOverflowLow(child, bytes);
  36. child = Allocator_child(alloc);
  37. tryOverflowHigh(child, bytes);
  38. }
  39. void tryCalloc(struct Allocator* alloc, int bytes)
  40. {
  41. Allocator_calloc(alloc, bytes, 1);
  42. CanaryAllocator_check(alloc);
  43. }
  44. int main()
  45. {
  46. struct Allocator* bAlloc;
  47. BufferAllocator_STACK(bAlloc, 20000);
  48. struct Allocator* alloc = CanaryAllocator_new(bAlloc, NULL);
  49. tryCalloc(alloc, 0);
  50. tryCalloc(alloc, 1);
  51. tryCalloc(alloc, 2);
  52. tryCalloc(alloc, 3);
  53. tryCalloc(alloc, 4);
  54. tryCalloc(alloc, 5);
  55. tryCalloc(alloc, 6);
  56. tryCalloc(alloc, 7);
  57. tryCalloc(alloc, 8);
  58. tryCalloc(alloc, 9);
  59. tryCalloc(alloc, 10);
  60. tryCalloc(alloc, 11);
  61. tryCalloc(alloc, 12);
  62. void* buff = Allocator_calloc(alloc, 1, 1);
  63. CanaryAllocator_check(alloc);
  64. Allocator_realloc(alloc, buff, 3);
  65. CanaryAllocator_check(alloc);
  66. tryOverflow(alloc, 0);
  67. tryOverflow(alloc, 1);
  68. tryOverflow(alloc, 2);
  69. tryOverflow(alloc, 3);
  70. tryOverflow(alloc, 4);
  71. tryOverflow(alloc, 5);
  72. tryOverflow(alloc, 6);
  73. tryOverflow(alloc, 7);
  74. tryOverflow(alloc, 8);
  75. tryOverflow(alloc, 9);
  76. tryOverflow(alloc, 10);
  77. tryOverflow(alloc, 11);
  78. tryOverflow(alloc, 12);
  79. }