Alp Toker | 632c6cd | 2014-01-23 22:19:45 +0000 | [diff] [blame] | 1 | //=- RPCChannel.inc - LLVM out-of-process JIT execution for Unix --=// |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Alp Toker | 632c6cd | 2014-01-23 22:19:45 +0000 | [diff] [blame] | 10 | // Implementation of the Unix-specific parts of the RPCChannel class |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 11 | // which executes JITed code in a separate process from where it was built. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Alp Toker | ad6aa47 | 2014-01-24 17:18:52 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Errno.h" |
| 16 | #include "llvm/Support/raw_ostream.h" |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <sys/wait.h> |
Chandler Carruth | 07baed5 | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 20 | #include <unistd.h> |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 21 | |
| 22 | namespace { |
| 23 | |
| 24 | struct ConnectionData_t { |
| 25 | int InputPipe; |
| 26 | int OutputPipe; |
| 27 | |
| 28 | ConnectionData_t(int in, int out) : InputPipe(in), OutputPipe(out) {} |
| 29 | }; |
| 30 | |
| 31 | } // namespace |
| 32 | |
| 33 | namespace llvm { |
| 34 | |
Alp Toker | 632c6cd | 2014-01-23 22:19:45 +0000 | [diff] [blame] | 35 | bool RPCChannel::createServer() { |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 36 | int PipeFD[2][2]; |
| 37 | pid_t ChildPID; |
| 38 | |
Benjamin Kramer | 3cfafb8 | 2013-10-04 19:10:03 +0000 | [diff] [blame] | 39 | // Create two pipes. |
| 40 | if (pipe(PipeFD[0]) != 0 || pipe(PipeFD[1]) != 0) |
| 41 | perror("Error creating pipe: "); |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 42 | |
| 43 | ChildPID = fork(); |
| 44 | |
| 45 | if (ChildPID == 0) { |
| 46 | // In the child... |
| 47 | |
| 48 | // Close the parent ends of the pipes |
| 49 | close(PipeFD[0][1]); |
| 50 | close(PipeFD[1][0]); |
| 51 | |
| 52 | // Use our pipes as stdin and stdout |
| 53 | if (PipeFD[0][0] != STDIN_FILENO) { |
| 54 | dup2(PipeFD[0][0], STDIN_FILENO); |
| 55 | close(PipeFD[0][0]); |
| 56 | } |
| 57 | if (PipeFD[1][1] != STDOUT_FILENO) { |
| 58 | dup2(PipeFD[1][1], STDOUT_FILENO); |
| 59 | close(PipeFD[1][1]); |
| 60 | } |
| 61 | |
| 62 | // Execute the child process. |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 63 | char *args[1] = { nullptr }; |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 64 | int rc = execv(ChildName.c_str(), args); |
| 65 | if (rc != 0) |
| 66 | perror("Error executing child process: "); |
Alp Toker | 632c6cd | 2014-01-23 22:19:45 +0000 | [diff] [blame] | 67 | } else { |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 68 | // In the parent... |
| 69 | |
| 70 | // Close the child ends of the pipes |
| 71 | close(PipeFD[0][0]); |
| 72 | close(PipeFD[1][1]); |
| 73 | |
| 74 | // Store the parent ends of the pipes |
Alp Toker | 632c6cd | 2014-01-23 22:19:45 +0000 | [diff] [blame] | 75 | ConnectionData = (void *)new ConnectionData_t(PipeFD[1][0], PipeFD[0][1]); |
| 76 | return true; |
Renato Golin | 695895c | 2014-01-14 22:43:43 +0000 | [diff] [blame] | 77 | } |
Alp Toker | 632c6cd | 2014-01-23 22:19:45 +0000 | [diff] [blame] | 78 | return false; |
| 79 | } |
| 80 | |
| 81 | bool RPCChannel::createClient() { |
| 82 | // Store the parent ends of the pipes |
| 83 | ConnectionData = (void *)new ConnectionData_t(STDIN_FILENO, STDOUT_FILENO); |
Renato Golin | 695895c | 2014-01-14 22:43:43 +0000 | [diff] [blame] | 84 | return true; |
| 85 | } |
| 86 | |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 87 | void RPCChannel::Wait() { wait(nullptr); } |
Alp Toker | ad6aa47 | 2014-01-24 17:18:52 +0000 | [diff] [blame] | 88 | |
| 89 | static bool CheckError(int rc, size_t Size, const char *Desc) { |
| 90 | if (rc < 0) { |
| 91 | llvm::errs() << "IO Error: " << Desc << ": " << sys::StrError() << '\n'; |
| 92 | return false; |
| 93 | } else if ((size_t)rc != Size) { |
| 94 | std::string ErrorMsg; |
Renato Golin | 695895c | 2014-01-14 22:43:43 +0000 | [diff] [blame] | 95 | char Number[10] = { 0 }; |
| 96 | ErrorMsg += "Expecting "; |
| 97 | sprintf(Number, "%d", (uint32_t)Size); |
| 98 | ErrorMsg += Number; |
| 99 | ErrorMsg += " bytes, Got "; |
| 100 | sprintf(Number, "%d", rc); |
| 101 | ErrorMsg += Number; |
Alp Toker | ad6aa47 | 2014-01-24 17:18:52 +0000 | [diff] [blame] | 102 | llvm::errs() << "RPC Error: " << Desc << ": " << ErrorMsg << '\n'; |
| 103 | return false; |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 104 | } |
Alp Toker | ad6aa47 | 2014-01-24 17:18:52 +0000 | [diff] [blame] | 105 | return true; |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Alp Toker | ad6aa47 | 2014-01-24 17:18:52 +0000 | [diff] [blame] | 108 | bool RPCChannel::WriteBytes(const void *Data, size_t Size) { |
| 109 | int rc = write(((ConnectionData_t *)ConnectionData)->OutputPipe, Data, Size); |
| 110 | return CheckError(rc, Size, "WriteBytes"); |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Alp Toker | ad6aa47 | 2014-01-24 17:18:52 +0000 | [diff] [blame] | 113 | bool RPCChannel::ReadBytes(void *Data, size_t Size) { |
| 114 | int rc = read(((ConnectionData_t *)ConnectionData)->InputPipe, Data, Size); |
| 115 | return CheckError(rc, Size, "ReadBytes"); |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Alp Toker | 632c6cd | 2014-01-23 22:19:45 +0000 | [diff] [blame] | 118 | RPCChannel::~RPCChannel() { |
Benjamin Kramer | c83946f | 2013-10-05 11:53:20 +0000 | [diff] [blame] | 119 | delete static_cast<ConnectionData_t *>(ConnectionData); |
| 120 | } |
| 121 | |
Benjamin Kramer | c12c7d0 | 2013-10-02 21:58:02 +0000 | [diff] [blame] | 122 | } // namespace llvm |