Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 1 | //=- RemoteTargetExternal.inc - LLVM out-of-process JIT execution for Unix --=// |
| 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 | // |
| 10 | // Implementation of the Unix-specific parts of the RemoteTargetExternal class |
| 11 | // which executes JITed code in a separate process from where it was built. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <sys/wait.h> |
Chandler Carruth | 07baed5 | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 18 | #include <unistd.h> |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 19 | |
| 20 | namespace { |
| 21 | |
| 22 | struct ConnectionData_t { |
| 23 | int InputPipe; |
| 24 | int OutputPipe; |
| 25 | |
| 26 | ConnectionData_t(int in, int out) : InputPipe(in), OutputPipe(out) {} |
| 27 | }; |
| 28 | |
| 29 | } // namespace |
| 30 | |
| 31 | namespace llvm { |
| 32 | |
Renato Golin | 695895c | 2014-01-14 22:43:43 +0000 | [diff] [blame^] | 33 | bool RemoteTargetExternal::create() { |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 34 | int PipeFD[2][2]; |
| 35 | pid_t ChildPID; |
| 36 | |
Benjamin Kramer | 3cfafb8 | 2013-10-04 19:10:03 +0000 | [diff] [blame] | 37 | // Create two pipes. |
| 38 | if (pipe(PipeFD[0]) != 0 || pipe(PipeFD[1]) != 0) |
| 39 | perror("Error creating pipe: "); |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 40 | |
| 41 | ChildPID = fork(); |
| 42 | |
| 43 | if (ChildPID == 0) { |
| 44 | // In the child... |
| 45 | |
| 46 | // Close the parent ends of the pipes |
| 47 | close(PipeFD[0][1]); |
| 48 | close(PipeFD[1][0]); |
| 49 | |
| 50 | // Use our pipes as stdin and stdout |
| 51 | if (PipeFD[0][0] != STDIN_FILENO) { |
| 52 | dup2(PipeFD[0][0], STDIN_FILENO); |
| 53 | close(PipeFD[0][0]); |
| 54 | } |
| 55 | if (PipeFD[1][1] != STDOUT_FILENO) { |
| 56 | dup2(PipeFD[1][1], STDOUT_FILENO); |
| 57 | close(PipeFD[1][1]); |
| 58 | } |
| 59 | |
| 60 | // Execute the child process. |
| 61 | char *args[1] = { NULL }; |
| 62 | int rc = execv(ChildName.c_str(), args); |
| 63 | if (rc != 0) |
| 64 | perror("Error executing child process: "); |
| 65 | } |
| 66 | else { |
| 67 | // In the parent... |
| 68 | |
| 69 | // Close the child ends of the pipes |
| 70 | close(PipeFD[0][0]); |
| 71 | close(PipeFD[1][1]); |
| 72 | |
| 73 | // Store the parent ends of the pipes |
| 74 | ConnectionData = (void*)new ConnectionData_t(PipeFD[1][0], PipeFD[0][1]); |
| 75 | |
Renato Golin | 695895c | 2014-01-14 22:43:43 +0000 | [diff] [blame^] | 76 | // We must get Ack from the client (blocking read) |
| 77 | if (!Receive(LLI_ChildActive)) { |
| 78 | ErrorMsg += ", (RemoteTargetExternal::create) - Stopping process!"; |
| 79 | stop(); |
| 80 | return false; |
| 81 | } |
| 82 | } |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | static void ReportError(int rc, size_t Size, std::string &ErrorMsg) { |
| 87 | if (rc == -1) { |
| 88 | if (errno == EPIPE) |
| 89 | ErrorMsg += "pipe closed"; |
| 90 | else if (errno == EINTR) |
| 91 | ErrorMsg += "interrupted"; |
| 92 | else |
| 93 | ErrorMsg += "file descriptor error"; |
| 94 | } else { |
| 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; |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Renato Golin | 695895c | 2014-01-14 22:43:43 +0000 | [diff] [blame^] | 105 | bool RemoteTargetExternal::WriteBytes(const void *Data, size_t Size) { |
| 106 | int rc = write(((ConnectionData_t*)ConnectionData)->OutputPipe, Data, Size); |
| 107 | if (rc != -1 && (size_t)rc == Size) |
| 108 | return true; |
| 109 | |
| 110 | ErrorMsg = "WriteBytes: "; |
| 111 | ReportError(rc, Size, ErrorMsg); |
| 112 | return false; |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Renato Golin | 695895c | 2014-01-14 22:43:43 +0000 | [diff] [blame^] | 115 | bool RemoteTargetExternal::ReadBytes(void *Data, size_t Size) { |
| 116 | int rc = read(((ConnectionData_t*)ConnectionData)->InputPipe, Data, Size); |
| 117 | if (rc != -1 && (size_t)rc == Size) |
| 118 | return true; |
| 119 | |
| 120 | ErrorMsg = "ReadBytes: "; |
| 121 | ReportError(rc, Size, ErrorMsg); |
| 122 | return false; |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void RemoteTargetExternal::Wait() { |
| 126 | wait(NULL); |
| 127 | } |
| 128 | |
Benjamin Kramer | c83946f | 2013-10-05 11:53:20 +0000 | [diff] [blame] | 129 | RemoteTargetExternal::~RemoteTargetExternal() { |
| 130 | delete static_cast<ConnectionData_t *>(ConnectionData); |
| 131 | } |
| 132 | |
Benjamin Kramer | c12c7d0 | 2013-10-02 21:58:02 +0000 | [diff] [blame] | 133 | } // namespace llvm |