summaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/util/sigbushandler/sigbushandler_test.cpp
blob: af657420575b1c90734835ac30df8e1acc6c7e18 (plain) (blame)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/fastos/fastos.h>
#include <vespa/log/log.h>
LOG_SETUP("sigbushandler_test");
#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/vespalib/stllike/string.h>
#include <vespa/searchlib/util/statefile.h>
#include <vespa/searchlib/util/sigbushandler.h>
#include <atomic>
#include <iostream>
#include <fstream>
#include <string>
#include <vespa/searchlib/test/statefile.h>
#include <vespa/searchlib/test/statestring.h>

using namespace search::test::statefile;
using namespace search::test::statestring;

namespace search
{

using strvec = std::vector<vespalib::string>;

namespace
{

bool
assertHistory(std::vector<vespalib::string> &exp,
              std::vector<vespalib::string> &act)
{
    if (!EXPECT_EQUAL(exp.size(), act.size())) {
        return false;
    }
    for (size_t i = 0; i < exp.size(); ++i) {
        if (!EXPECT_EQUAL(exp[i], act[i])) {
            return false;
        }
    }
    return true;
}

}


TEST("Test that sigbus handler can be instantated")
{
    StateFile::erase("state");
    StateFile sf("state");
    SigBusHandler sbh(&sf);
    EXPECT_FALSE(sbh.fired());
}


TEST("Test that sigbus handler can trap synthetic sigbus")
{
    StateFile::erase("state");
    StateFile sf("state");
    SigBusHandler sbh(&sf);
    EXPECT_FALSE(sbh.fired());
    sigjmp_buf sjb;
    if (sigsetjmp(sjb, 1) == 0) {
        sbh.setUnwind(&sjb);
        kill(getpid(), SIGBUS);
        LOG(error, "Should never get here");
        abort();
    }
    EXPECT_TRUE(sbh.fired());
    {
        vespalib::string act = readState(sf);
        normalizeTimestamp(act);
        EXPECT_EQUAL("state=down ts=0.0 operation=sigbus errno=0 code=0\n",
                     act);
    }
    {
        strvec exp({"state=down ts=0.0 operation=sigbus errno=0 code=0\n" });
        std::vector<vespalib::string> act(readHistory("state.history"));
        normalizeTimestamps(act);
        TEST_DO(assertHistory(exp, act));
    }
}

TEST("Test that sigbus handler can trap normal sigbus")
{
    StateFile::erase("state");
    StateFile sf("state");
    SigBusHandler sbh(&sf);
    EXPECT_FALSE(sbh.fired());

    int fd = open("mmapfile", O_CREAT | O_TRUNC | O_RDWR, 0644);
    assert(fd >= 0);
    void *mmapres = mmap(nullptr, 4096, PROT_READ | PROT_WRITE,
                         MAP_SHARED, fd, 0);
    assert(mmapres != nullptr);
    assert(mmapres != reinterpret_cast<void *>(-1l));
    char *p = reinterpret_cast<char *>(mmapres) + 42;
    volatile char r = 0;
    sigjmp_buf sjb;
    if (sigsetjmp(sjb, 1) == 0) {
        sbh.setUnwind(&sjb);
        r = *p;
        LOG(error, "Should never get here");
        abort();
    }
    EXPECT_TRUE(sbh.fired());
    EXPECT_TRUE(r == '\0');
    {
        vespalib::string act = readState(sf);
        vespalib::string exp ="state=down ts=0.0 operation=sigbus errno=0 "
                         "code=2 addr=0x0000000000000000\n";
        normalizeAddr(exp, p);
        normalizeTimestamp(act);
        EXPECT_EQUAL(exp, act);
    }
    {
        strvec exp({"state=down ts=0.0 operation=sigbus errno=0 code=2 "
                            "addr=0x0000000000000000\n" });
        normalizeAddrs(exp, p);
        std::vector<vespalib::string> act(readHistory("state.history"));
        normalizeTimestamps(act);
        TEST_DO(assertHistory(exp, act));
    }
}

}

TEST_MAIN()
{
    TEST_RUN_ALL();
    search::StateFile::erase("state");
    unlink("mmapfile");
}