parent_process.cpp 6.87 KB
Newer Older
Tijana Vukovic's avatar
Tijana Vukovic committed
1
#include <Windows.h>
2
#include <tchar.h>
Tijana Vukovic's avatar
Tijana Vukovic committed
3
#include <iostream>
4
#include <cstring>
Tijana Vukovic's avatar
Tijana Vukovic committed
5
6

#include "test.hpp"
7
8
9
#include <migraphx/errors.hpp>
#include <migraphx/file_buffer.hpp>
#include <migraphx/filesystem.hpp>
Tijana Vukovic's avatar
Tijana Vukovic committed
10
11
#include <migraphx/msgpack.hpp>
#include <migraphx/process.hpp>
12
13

#define BUFSIZE MAX_PATH
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
constexpr std::size_t BUFFER_SIZE = 4096;
STARTUPINFO info;
PROCESS_INFORMATION process_info;

enum class direction
{
    input,
    output
};

template <direction dir>
class pipe
{
    public:
    explicit pipe()
    {
        SECURITY_ATTRIBUTES attrs;
        attrs.nLength              = sizeof(SECURITY_ATTRIBUTES);
        attrs.bInheritHandle       = TRUE;
        attrs.lpSecurityDescriptor = nullptr;

        if(CreatePipe(&m_read, &m_write, &attrs, 0) == FALSE)
            throw GetLastError();

        if(dir == direction::output)
        {
            // Do not inherit the read handle for the output pipe
            if(SetHandleInformation(m_read, HANDLE_FLAG_INHERIT, 0) == 0)
                throw GetLastError();
        }
        else
        {
            // Do not inherit the write handle for the input pipe
            if(SetHandleInformation(m_write, HANDLE_FLAG_INHERIT, 0) == 0)
                throw GetLastError();
        }
    }

    pipe(const pipe&)            = delete;
    pipe& operator=(const pipe&) = delete;

    pipe(pipe&&) = default;

    ~pipe()
    {
        if(m_write != nullptr)
        {
            CloseHandle(m_write);
        }
        if(m_read != nullptr)
        {
            CloseHandle(m_read);
        }
    }

    bool close_write_handle()
    {
        auto result = true;
        if(m_write != nullptr)
        {
            result  = CloseHandle(m_write) == TRUE;
            m_write = nullptr;
        }
        return result;
    }

    bool close_read_handle()
    {
        auto result = true;
        if(m_read != nullptr)
        {
            result = CloseHandle(m_read) == TRUE;
            m_read = nullptr;
        }
        return result;
    }

    std::pair<bool, DWORD> read(LPVOID buffer, DWORD length) const
    {
        DWORD bytes_read;
        if(ReadFile(m_read, buffer, length, &bytes_read, nullptr) == FALSE and
            GetLastError() == ERROR_MORE_DATA)
        {
            return {true, bytes_read};
        }
        return {false, bytes_read};
    }

    HANDLE get_read_handle() const { return m_read; }

    bool write(LPCVOID buffer, DWORD length) const
    {
        DWORD bytes_written;
        return WriteFile(m_write, buffer, length, &bytes_written, nullptr) == TRUE;
    }

    HANDLE get_write_handle() const { return m_write; }

        
    HANDLE m_write = nullptr, m_read = nullptr;
};

116
117
118
119
120
121
122
123
124
std::string get_cwd()
{
    char Buffer[BUFSIZE];
    DWORD dwRet;
    dwRet = GetCurrentDirectory(BUFSIZE, Buffer);
    if(dwRet == 0)
        MIGRAPHX_THROW("GetCurrentDirectory failed (" + std::to_string(GetLastError()) + ")");
    return std::string(Buffer);
}
Tijana Vukovic's avatar
Tijana Vukovic committed
125

126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
void CreateProcess(pipe<direction::input>& input,
                pipe<direction::output>& output,
                const std::string& child_process_name,
                const std::string& cwd)
{
    ZeroMemory(&info, sizeof(STARTUPINFO));
    info.cb         = sizeof(STARTUPINFO);
    info.hStdError  = output.get_write_handle();
    info.hStdOutput = output.get_write_handle();
    info.hStdInput  = input.get_read_handle();
    info.dwFlags |= STARTF_USESTDHANDLES;

    TCHAR cmdline[MAX_PATH];
    std::strncpy(cmdline, child_process_name.c_str(), MAX_PATH);

    ZeroMemory(&process_info, sizeof(process_info));

    if(CreateProcess(nullptr,
                        cmdline,
                        nullptr,
                        nullptr,
                        TRUE,
                        0,
                        nullptr,
                        cwd.empty() ? nullptr : static_cast<LPCSTR>(cwd.c_str()),
                        &info,
                        &process_info) == FALSE)
    {
        MIGRAPHX_THROW("Error creating process (" + std::to_string(GetLastError()) + ")");
    }

    if(not output.close_write_handle())
        MIGRAPHX_THROW("Error closing STDOUT handle for writing (" +
                        std::to_string(GetLastError()) + ")");

    if(not input.close_read_handle())
        MIGRAPHX_THROW("Error closing STDIN handle for reading (" +
                        std::to_string(GetLastError()) + ")");
}

void write_to_child(LPCVOID buffer, std::size_t n, pipe<direction::input>& input)
{
    DWORD bytes_written;
    if(WriteFile(input.m_write, buffer, n, &bytes_written, nullptr) ==
    FALSE) 
    {
        MIGRAPHX_THROW("Error writing to child STDIN (" + std::to_string(GetLastError()) + ")");
    }

    if(not input.close_write_handle())
        MIGRAPHX_THROW("Error closing STDIN handle for writing (" + std::to_string(GetLastError()) +
                        ")");
}

std::vector<char> read_from_child(pipe<direction::output>& output) {
    std::vector<char> result;
    DWORD bytes_read;
    TCHAR buffer[BUFFER_SIZE];

    for(;;)
    {
        BOOL status = ReadFile(output.m_read, buffer, BUFFER_SIZE, &bytes_read, nullptr);
        if(status == FALSE or bytes_read == 0)
            break;

        result.insert(result.end(), buffer, buffer + bytes_read);
    }
    return result;
}


Tijana Vukovic's avatar
Tijana Vukovic committed
197
198
TEST_CASE(string_data)
{
199
    std::string cwd = get_cwd();
Tijana Vukovic's avatar
Tijana Vukovic committed
200

201
    std::string string_data = "Parent string";
Tijana Vukovic's avatar
Tijana Vukovic committed
202

203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
    std::string child_process_name = "test_child.exe";

    pipe<direction::input> input{};
    pipe<direction::output> output{};

    CreateProcess(input, output, child_process_name, cwd);

    // write to child process
    TCHAR buffer[BUFFER_SIZE];
    std::strncpy(buffer, string_data.c_str(), BUFFER_SIZE);  
    write_to_child(buffer, BUFFER_SIZE, input);

    // read from child stdout
    std::vector<char> result = read_from_child(output);
        
    WaitForSingleObject(process_info.hProcess, INFINITE);

    DWORD status{};
    GetExitCodeProcess(process_info.hProcess, &status);

    CloseHandle(process_info.hProcess);
    CloseHandle(process_info.hThread);
    
    //compare input parent and output child process
    EXPECT(result.data() == string_data);
Tijana Vukovic's avatar
Tijana Vukovic committed
228
229
230
231
}

TEST_CASE(binary_data)
{
232
    std::string cwd = get_cwd();
Tijana Vukovic's avatar
Tijana Vukovic committed
233
234

    std::vector<char> binary_data = {'B', 'i', 'n', 'a', 'r', 'y'};
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257

    std::string child_process_name = "test_child.exe";

    pipe<direction::input> input{};
    pipe<direction::output> output{};

    CreateProcess(input, output, child_process_name, cwd);

    write_to_child(binary_data.data(), binary_data.size(), input);

    // read from child stdout
    std::vector<char> result = read_from_child(output);

    WaitForSingleObject(process_info.hProcess, INFINITE);

    DWORD status{};
    GetExitCodeProcess(process_info.hProcess, &status);

    CloseHandle(process_info.hProcess);
    CloseHandle(process_info.hThread);

    // compare input parent and output child process
    EXPECT(result == binary_data);
Tijana Vukovic's avatar
Tijana Vukovic committed
258
259
260
}

int main(int argc, const char* argv[]) { test::run(argc, argv); }