brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.2 KiB · 1b2c1eb Raw
58 lines · cpp
1//===-- chunk_test.cpp ------------------------------------------*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "tests/scudo_unit_test.h"10 11#include "chunk.h"12 13#include <stdlib.h>14 15static constexpr scudo::uptr HeaderSize = scudo::Chunk::getHeaderSize();16static constexpr scudo::u32 Cookie = 0x41424344U;17static constexpr scudo::u32 InvalidCookie = 0x11223344U;18 19static void initChecksum(void) {20  if (&scudo::computeHardwareCRC32 && scudo::hasHardwareCRC32())21    scudo::HashAlgorithm = scudo::Checksum::HardwareCRC32;22}23 24TEST(ScudoChunkDeathTest, ChunkBasic) {25  initChecksum();26  const scudo::uptr Size = 0x100U;27  scudo::Chunk::UnpackedHeader Header = {};28  void *Block = malloc(HeaderSize + Size);29  void *P = reinterpret_cast<void *>(reinterpret_cast<scudo::uptr>(Block) +30                                     HeaderSize);31  scudo::Chunk::storeHeader(Cookie, P, &Header);32  memset(P, 'A', Size);33  scudo::Chunk::loadHeader(Cookie, P, &Header);34  EXPECT_TRUE(scudo::Chunk::isValid(Cookie, P, &Header));35  EXPECT_FALSE(scudo::Chunk::isValid(InvalidCookie, P, &Header));36  EXPECT_DEATH(scudo::Chunk::loadHeader(InvalidCookie, P, &Header), "");37  free(Block);38}39 40TEST(ScudoChunkDeathTest, CorruptHeader) {41  initChecksum();42  const scudo::uptr Size = 0x100U;43  scudo::Chunk::UnpackedHeader Header = {};44  void *Block = malloc(HeaderSize + Size);45  void *P = reinterpret_cast<void *>(reinterpret_cast<scudo::uptr>(Block) +46                                     HeaderSize);47  scudo::Chunk::storeHeader(Cookie, P, &Header);48  memset(P, 'A', Size);49  scudo::Chunk::loadHeader(Cookie, P, &Header);50  // Simulate a couple of corrupted bits per byte of header data.51  for (scudo::uptr I = 0; I < sizeof(scudo::Chunk::PackedHeader); I++) {52    *(reinterpret_cast<scudo::u8 *>(Block) + I) ^= 0x42U;53    EXPECT_DEATH(scudo::Chunk::loadHeader(Cookie, P, &Header), "");54    *(reinterpret_cast<scudo::u8 *>(Block) + I) ^= 0x42U;55  }56  free(Block);57}58