summaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/util/sigbushandler/sigbushandler_test.cpp
blob: e687eff3ca7bacc6e3ef3e0d8c98c0e7d359603a (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
132
133
134
135
136
137
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#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 <iostream>
#include <fstream>
#include <vespa/searchlib/test/statefile.h>
#include <vespa/searchlib/test/statestring.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>

#include <vespa/log/log.h>
LOG_SETUP("sigbushandler_test");

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_ABORT("Should never get here");
    }
    EXPECT_TRUE(sbh.fired());
#ifdef __APPLE__
    vespalib::string exp_state = "state=down ts=0.0 operation=sigbus errno=0 code=2 addr=0x0000000000000000\n";
#else
    vespalib::string exp_state = "state=down ts=0.0 operation=sigbus errno=0 code=0\n";
#endif
    {
        vespalib::string act = readState(sf);
        normalizeTimestamp(act);
        normalizeAddr(act, nullptr);
        EXPECT_EQUAL(exp_state, act);
    }
    {
        strvec exp({exp_state});
        std::vector<vespalib::string> act(readHistory("state.history"));
        normalizeTimestamps(act);
        normalizeAddrs(act, nullptr);
        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_ABORT("Should never get here");
    }
    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");
}