xref: /aosp_15_r20/external/scudo/standalone/tests/report_test.cpp (revision 76559068c068bd27e82aff38fac3bfc865233bca)
1 //===-- report_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 //===----------------------------------------------------------------------===//
8 
9 #include "tests/scudo_unit_test.h"
10 
11 #include "report.h"
12 
TEST(ScudoReportDeathTest,Check)13 TEST(ScudoReportDeathTest, Check) {
14   CHECK_LT(-1, 1);
15   EXPECT_DEATH(CHECK_GT(-1, 1),
16                "\\(-1\\) > \\(1\\) \\(\\(u64\\)op1=18446744073709551615, "
17                "\\(u64\\)op2=1");
18 }
19 
TEST(ScudoReportDeathTest,Generic)20 TEST(ScudoReportDeathTest, Generic) {
21   // Potentially unused if EXPECT_DEATH isn't defined.
22   UNUSED void *P = reinterpret_cast<void *>(0x42424242U);
23   EXPECT_DEATH(scudo::reportError("TEST123"), "Scudo ERROR.*TEST123");
24   EXPECT_DEATH(scudo::reportInvalidFlag("ABC", "DEF"), "Scudo ERROR.*ABC.*DEF");
25   EXPECT_DEATH(scudo::reportHeaderCorruption(P), "Scudo ERROR.*42424242");
26   EXPECT_DEATH(scudo::reportSanityCheckError("XYZ"), "Scudo ERROR.*XYZ");
27   EXPECT_DEATH(scudo::reportAlignmentTooBig(123, 456), "Scudo ERROR.*123.*456");
28   EXPECT_DEATH(scudo::reportAllocationSizeTooBig(123, 456, 789),
29                "Scudo ERROR.*123.*456.*789");
30   EXPECT_DEATH(scudo::reportOutOfMemory(4242), "Scudo ERROR.*4242");
31   EXPECT_DEATH(
32       scudo::reportInvalidChunkState(scudo::AllocatorAction::Recycling, P),
33       "Scudo ERROR.*recycling.*42424242");
34   EXPECT_DEATH(
35       scudo::reportInvalidChunkState(scudo::AllocatorAction::Sizing, P),
36       "Scudo ERROR.*sizing.*42424242");
37   EXPECT_DEATH(
38       scudo::reportMisalignedPointer(scudo::AllocatorAction::Deallocating, P),
39       "Scudo ERROR.*deallocating.*42424242");
40   EXPECT_DEATH(scudo::reportDeallocTypeMismatch(
41                    scudo::AllocatorAction::Reallocating, P, 0, 1),
42                "Scudo ERROR.*reallocating.*42424242");
43   EXPECT_DEATH(scudo::reportDeleteSizeMismatch(P, 123, 456),
44                "Scudo ERROR.*42424242.*123.*456");
45 }
46 
TEST(ScudoReportDeathTest,CSpecific)47 TEST(ScudoReportDeathTest, CSpecific) {
48   EXPECT_DEATH(scudo::reportAlignmentNotPowerOfTwo(123), "Scudo ERROR.*123");
49   EXPECT_DEATH(scudo::reportCallocOverflow(123, 456), "Scudo ERROR.*123.*456");
50   EXPECT_DEATH(scudo::reportInvalidPosixMemalignAlignment(789),
51                "Scudo ERROR.*789");
52   EXPECT_DEATH(scudo::reportPvallocOverflow(123), "Scudo ERROR.*123");
53   EXPECT_DEATH(scudo::reportInvalidAlignedAllocAlignment(123, 456),
54                "Scudo ERROR.*123.*456");
55 }
56 
57 #if SCUDO_LINUX || SCUDO_TRUSTY || SCUDO_ANDROID
58 #include "report_linux.h"
59 
60 #include <errno.h>
61 #include <sys/mman.h>
62 
TEST(ScudoReportDeathTest,Linux)63 TEST(ScudoReportDeathTest, Linux) {
64   errno = ENOMEM;
65   EXPECT_DEATH(scudo::reportMapError(),
66                "Scudo ERROR:.*internal map failure \\(error desc=.*\\)");
67   errno = ENOMEM;
68   EXPECT_DEATH(scudo::reportMapError(1024U),
69                "Scudo ERROR:.*internal map failure \\(error desc=.*\\) "
70                "requesting 1KB");
71   errno = ENOMEM;
72   EXPECT_DEATH(scudo::reportUnmapError(0x1000U, 100U),
73                "Scudo ERROR:.*internal unmap failure \\(error desc=.*\\) Addr "
74                "0x1000 Size 100");
75   errno = ENOMEM;
76   EXPECT_DEATH(scudo::reportProtectError(0x1000U, 100U, PROT_READ),
77                "Scudo ERROR:.*internal protect failure \\(error desc=.*\\) "
78                "Addr 0x1000 Size 100 Prot 1");
79 }
80 #endif
81