brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.2 KiB · b318641 Raw
38 lines · c
1// Test malloc alignment.2// RUN: %clang_hwasan -mllvm -hwasan-globals=0 %s -o %t3// RUN: %run %t4 5#include <assert.h>6#include <sanitizer/allocator_interface.h>7#include <sanitizer/hwasan_interface.h>8#include <stdio.h>9#include <stdlib.h>10 11static const size_t sizes[] = {12    1, 3, 7, 8, 9, 16, 17, 31, 32, 33,13    63, 64, 65, 127, 128, 129, 511, 512, 513, 2047,14    2048, 2049, 65535, 65536, 65537, 1048575, 1048576, 1048577};15static const size_t alignments[] = {8, 16, 64, 256, 1024, 4096, 65536, 131072};16 17__attribute__((no_sanitize("hwaddress"))) int main() {18  for (unsigned i = 0; i < sizeof(sizes) / sizeof(*sizes); ++i) {19    for (unsigned j = 0; j < sizeof(alignments) / sizeof(*alignments); ++j) {20      size_t size = sizes[i];21      size_t alignment = alignments[j];22      fprintf(stderr, "size %zu, alignment %zu (0x%zx)\n", size, alignment,23              alignment);24      const int cnt = 10;25      void *ptrs[cnt];26      for (int k = 0; k < cnt; ++k) {27        int res = posix_memalign(&ptrs[k], alignment, size);28        assert(res == 0);29        fprintf(stderr, "... addr 0x%zx\n", (size_t)ptrs[k]);30        assert(((size_t)ptrs[k] & (alignment - 1)) == 0);31      }32      for (int k = 0; k < cnt; ++k)33        free(ptrs[k]);34    }35  }36  return 0;37}38