aboutsummaryrefslogtreecommitdiff
path: root/compiler-rt/lib/scudo/standalone/tests/chunk_test.cpp
blob: 1b2c1eb5a7d0c5cd90bde73628487b0df21c3784 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//===-- chunk_test.cpp ------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "tests/scudo_unit_test.h"

#include "chunk.h"

#include <stdlib.h>

static constexpr scudo::uptr HeaderSize = scudo::Chunk::getHeaderSize();
static constexpr scudo::u32 Cookie = 0x41424344U;
static constexpr scudo::u32 InvalidCookie = 0x11223344U;

static void initChecksum(void) {
  if (&scudo::computeHardwareCRC32 && scudo::hasHardwareCRC32())
    scudo::HashAlgorithm = scudo::Checksum::HardwareCRC32;
}

TEST(ScudoChunkDeathTest, ChunkBasic) {
  initChecksum();
  const scudo::uptr Size = 0x100U;
  scudo::Chunk::UnpackedHeader Header = {};
  void *Block = malloc(HeaderSize + Size);
  void *P = reinterpret_cast<void *>(reinterpret_cast<scudo::uptr>(Block) +
                                     HeaderSize);
  scudo::Chunk::storeHeader(Cookie, P, &Header);
  memset(P, 'A', Size);
  scudo::Chunk::loadHeader(Cookie, P, &Header);
  EXPECT_TRUE(scudo::Chunk::isValid(Cookie, P, &Header));
  EXPECT_FALSE(scudo::Chunk::isValid(InvalidCookie, P, &Header));
  EXPECT_DEATH(scudo::Chunk::loadHeader(InvalidCookie, P, &Header), "");
  free(Block);
}

TEST(ScudoChunkDeathTest, CorruptHeader) {
  initChecksum();
  const scudo::uptr Size = 0x100U;
  scudo::Chunk::UnpackedHeader Header = {};
  void *Block = malloc(HeaderSize + Size);
  void *P = reinterpret_cast<void *>(reinterpret_cast<scudo::uptr>(Block) +
                                     HeaderSize);
  scudo::Chunk::storeHeader(Cookie, P, &Header);
  memset(P, 'A', Size);
  scudo::Chunk::loadHeader(Cookie, P, &Header);
  // Simulate a couple of corrupted bits per byte of header data.
  for (scudo::uptr I = 0; I < sizeof(scudo::Chunk::PackedHeader); I++) {
    *(reinterpret_cast<scudo::u8 *>(Block) + I) ^= 0x42U;
    EXPECT_DEATH(scudo::Chunk::loadHeader(Cookie, P, &Header), "");
    *(reinterpret_cast<scudo::u8 *>(Block) + I) ^= 0x42U;
  }
  free(Block);
}