Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / lib / scudo / standalone / tests / chunk_test.cpp
blob1b2c1eb5a7d0c5cd90bde73628487b0df21c3784
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-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "tests/scudo_unit_test.h"
11 #include "chunk.h"
13 #include <stdlib.h>
15 static constexpr scudo::uptr HeaderSize = scudo::Chunk::getHeaderSize();
16 static constexpr scudo::u32 Cookie = 0x41424344U;
17 static constexpr scudo::u32 InvalidCookie = 0x11223344U;
19 static void initChecksum(void) {
20 if (&scudo::computeHardwareCRC32 && scudo::hasHardwareCRC32())
21 scudo::HashAlgorithm = scudo::Checksum::HardwareCRC32;
24 TEST(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);
40 TEST(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;
56 free(Block);