ems_hmu.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "ems_gc_internal.h"
  6. #if BH_ENABLE_GC_VERIFY != 0
  7. /**
  8. * Set default value to prefix and suffix
  9. * @param hmu should not be NULL and should have been correctly initilized
  10. * (except prefix and suffix part)
  11. * @param tot_size is offered here because hmu_get_size can not be used
  12. * till now. tot_size should not be smaller than OBJ_EXTRA_SIZE.
  13. * For VO, tot_size should be equal to object total size.
  14. */
  15. void
  16. hmu_init_prefix_and_suffix(hmu_t *hmu, gc_size_t tot_size,
  17. const char *file_name, int line_no)
  18. {
  19. gc_object_prefix_t *prefix = NULL;
  20. gc_object_suffix_t *suffix = NULL;
  21. gc_uint32 i = 0;
  22. bh_assert(hmu);
  23. bh_assert(hmu_get_ut(hmu) == HMU_WO || hmu_get_ut(hmu) == HMU_VO);
  24. bh_assert(tot_size >= OBJ_EXTRA_SIZE);
  25. bh_assert(!(tot_size & 7));
  26. bh_assert(hmu_get_ut(hmu) != HMU_VO || hmu_get_size(hmu) >= tot_size);
  27. prefix = (gc_object_prefix_t *)(hmu + 1);
  28. suffix =
  29. (gc_object_suffix_t *)((gc_uint8 *)hmu + tot_size - OBJ_SUFFIX_SIZE);
  30. prefix->file_name = file_name;
  31. prefix->line_no = line_no;
  32. prefix->size = tot_size;
  33. for (i = 0; i < GC_OBJECT_PREFIX_PADDING_CNT; i++) {
  34. prefix->padding[i] = GC_OBJECT_PADDING_VALUE;
  35. }
  36. for (i = 0; i < GC_OBJECT_SUFFIX_PADDING_CNT; i++) {
  37. suffix->padding[i] = GC_OBJECT_PADDING_VALUE;
  38. }
  39. }
  40. void
  41. hmu_verify(void *vheap, hmu_t *hmu)
  42. {
  43. #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
  44. gc_heap_t *heap = (gc_heap_t *)vheap;
  45. #endif
  46. gc_object_prefix_t *prefix = NULL;
  47. gc_object_suffix_t *suffix = NULL;
  48. gc_uint32 i = 0;
  49. hmu_type_t ut;
  50. gc_size_t size = 0;
  51. int is_padding_ok = 1;
  52. bh_assert(hmu);
  53. ut = hmu_get_ut(hmu);
  54. bh_assert(hmu_is_ut_valid(ut));
  55. prefix = (gc_object_prefix_t *)(hmu + 1);
  56. size = prefix->size;
  57. suffix = (gc_object_suffix_t *)((gc_uint8 *)hmu + size - OBJ_SUFFIX_SIZE);
  58. if (ut == HMU_VO || ut == HMU_WO) {
  59. /* check padding*/
  60. for (i = 0; i < GC_OBJECT_PREFIX_PADDING_CNT; i++) {
  61. if (prefix->padding[i] != GC_OBJECT_PADDING_VALUE) {
  62. is_padding_ok = 0;
  63. break;
  64. }
  65. }
  66. for (i = 0; i < GC_OBJECT_SUFFIX_PADDING_CNT; i++) {
  67. if (suffix->padding[i] != GC_OBJECT_PADDING_VALUE) {
  68. is_padding_ok = 0;
  69. break;
  70. }
  71. }
  72. if (!is_padding_ok) {
  73. LOG_ERROR("Invalid padding for object created at %s:%d\n",
  74. (prefix->file_name ? prefix->file_name : ""),
  75. prefix->line_no);
  76. #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
  77. heap->is_heap_corrupted = true;
  78. #endif
  79. }
  80. }
  81. }
  82. #endif /* end of BH_ENABLE_GC_VERIFY */