main.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright (C) 2023 Midokura Japan KK. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <stdio.h>
  6. #include <stdint.h>
  7. #include <stdlib.h>
  8. #include "mem_alloc.h"
  9. char store[1000];
  10. int
  11. main(int argc, char **argv)
  12. {
  13. mem_allocator_t a = mem_allocator_create(store, sizeof(store));
  14. uint8_t *p;
  15. uint8_t *p2;
  16. p = mem_allocator_malloc(a, 256);
  17. printf("%p\n", p);
  18. if (p == NULL) {
  19. exit(1);
  20. }
  21. p = mem_allocator_realloc(a, p, 256 + 12);
  22. printf("%p\n", p);
  23. if (p == NULL) {
  24. exit(1);
  25. }
  26. /*
  27. * write some values to confuse the ems allocator.
  28. *
  29. * hmu = p + 256
  30. * hmu_set_ut(hmu, HMU_FC)
  31. * hmu_set_size(hmu, 256)
  32. * hmu_set_free_size(hmu)
  33. */
  34. *(uint32_t *)(p + 256) = (1 << 30) | 0x20;
  35. *(uint32_t *)(p + 256 + 12 - 4) = 12;
  36. p2 = mem_allocator_malloc(a, 256);
  37. printf("%p\n", p2);
  38. if (p2 == NULL) {
  39. exit(1);
  40. }
  41. mem_allocator_free(a, p2);
  42. p2 = mem_allocator_malloc(a, 256);
  43. printf("%p\n", p2);
  44. if (p2 == NULL) {
  45. exit(1);
  46. }
  47. mem_allocator_free(a, p2);
  48. mem_allocator_free(a, p);
  49. }