1 //===-- chunk_test.cpp ------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "tests/scudo_unit_test.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
) {
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
) +
31 scudo::Chunk::storeHeader(Cookie
, P
, &Header
);
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
), "");
40 TEST(ScudoChunkDeathTest
, CorruptHeader
) {
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
) +
47 scudo::Chunk::storeHeader(Cookie
, P
, &Header
);
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
;