summaryrefslogtreecommitdiffstats
path: root/llvm/unittests/Support/SignalsTest.cpp
blob: 6dfa4bf996de7980e6335bc399d662598bb6b793 (plain)
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
//========- unittests/Support/SignalsTest.cpp - Signal handling test =========//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#if !defined(_WIN32)
#include <unistd.h>
#include <sysexits.h>
#endif // !defined(_WIN32)

#include "llvm/Support/Signals.h"

#include "gtest/gtest.h"

using namespace llvm;

#if !defined(_WIN32)
TEST(SignalTest, IgnoreMultipleSIGPIPEs) {
  // Ignore SIGPIPE.
  signal(SIGPIPE, SIG_IGN);

  // Disable exit-on-SIGPIPE.
  sys::SetPipeSignalFunction(nullptr);

  // Create unidirectional read/write pipes.
  int fds[2];
  int err = pipe(fds);
  if (err != 0)
    return; // If we can't make pipes, this isn't testing anything.

  // Close the read pipe.
  close(fds[0]);

  // Attempt to write to the write pipe. Currently we're asserting that the
  // write fails, which isn't great.
  //
  // What we really want is a death test that checks that this block exits
  // with a special exit "success" code, as opposed to unexpectedly exiting due
  // to a kill-by-SIGNAL or due to the default SIGPIPE handler.
  //
  // Unfortunately llvm's unit tests aren't set up to support death tests well.
  // For one, death tests are flaky in a multithreaded context. And sigactions
  // inherited from llvm-lit interfere with what's being tested.
  const void *buf = (const void *)&fds;
  err = write(fds[1], buf, 1);
  ASSERT_EQ(err, -1);
  err = write(fds[1], buf, 1);
  ASSERT_EQ(err, -1);
}
#endif // !defined(_WIN32)
OpenPOWER on IntegriCloud