Skip to content

Commit af6a717

Browse files
authored
librerpl: port to Windows (googleprojectzero#276)
1 parent b8d3a2a commit af6a717

File tree

2 files changed

+458
-12
lines changed

2 files changed

+458
-12
lines changed

Sources/libreprl/libreprl.c renamed to Sources/libreprl/libreprl-posix.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#if !defined(_WIN32)
16+
17+
#include "libreprl.h"
18+
1519
#ifndef _GNU_SOURCE
1620
#define _GNU_SOURCE
1721
#endif
1822

19-
#include "libreprl.h"
20-
23+
#include <assert.h>
2124
#include <errno.h>
2225
#include <fcntl.h>
2326
#include <poll.h>
@@ -33,7 +36,8 @@
3336
#include <time.h>
3437
#include <unistd.h>
3538

36-
// Well-known file descriptor numbers for reprl <-> child communication, child process side
39+
// Well-known file descriptor numbers for reprl <-> child communication, child
40+
// process side
3741
#define REPRL_CHILD_CTRL_IN 100
3842
#define REPRL_CHILD_CTRL_OUT 101
3943
#define REPRL_CHILD_DATA_IN 102
@@ -82,12 +86,12 @@ struct data_channel {
8286
struct reprl_context {
8387
// Whether reprl_initialize has been successfully performed on this context.
8488
int initialized;
85-
89+
8690
// Read file descriptor of the control pipe. Only valid if a child process is running (i.e. pid is nonzero).
8791
int ctrl_in;
8892
// Write file descriptor of the control pipe. Only valid if a child process is running (i.e. pid is nonzero).
8993
int ctrl_out;
90-
94+
9195
// Data channel REPRL -> Child
9296
struct data_channel* data_in;
9397
// Data channel Child -> REPRL
@@ -98,8 +102,8 @@ struct reprl_context {
98102
struct data_channel* child_stderr;
99103

100104
// PID of the child process. Will be zero if no child process is currently running.
101-
int pid;
102-
105+
pid_t pid;
106+
103107
// Arguments and environment for the child process.
104108
char** argv;
105109
char** envp;
@@ -199,9 +203,9 @@ static int reprl_spawn_child(struct reprl_context* ctx)
199203
#ifdef __linux__
200204
// Use vfork() on Linux as that considerably improves the fuzzer performance. See also https://github.com/googleprojectzero/fuzzilli/issues/174
201205
// Due to vfork, the code executed in the child process *must not* modify any memory apart from its stack, as it will share the page table of its parent.
202-
int pid = vfork();
206+
pid_t pid = vfork();
203207
#else
204-
int pid = fork();
208+
pid_t pid = fork();
205209
#endif
206210
if (pid == 0) {
207211
if (dup2(cwpipe[0], REPRL_CHILD_CTRL_IN) < 0 ||
@@ -300,7 +304,7 @@ int reprl_initialize_context(struct reprl_context* ctx, const char** argv, const
300304

301305
// We need to ignore SIGPIPE since we could end up writing to a pipe after our child process has exited.
302306
signal(SIGPIPE, SIG_IGN);
303-
307+
304308
ctx->argv = copy_string_array(argv);
305309
ctx->envp = copy_string_array(envp);
306310

@@ -345,12 +349,12 @@ int reprl_execute(struct reprl_context* ctx, const char* script, uint64_t script
345349
if (script_length > REPRL_MAX_DATA_SIZE) {
346350
return reprl_error(ctx, "Script too large");
347351
}
348-
352+
349353
// Terminate any existing instance if requested.
350354
if (fresh_instance && ctx->pid) {
351355
reprl_terminate_child(ctx);
352356
}
353-
357+
354358
// Reset file position so the child can simply read(2) and write(2) to these fds.
355359
lseek(ctx->data_out->fd, 0, SEEK_SET);
356360
lseek(ctx->data_in->fd, 0, SEEK_SET);
@@ -503,3 +507,4 @@ const char* reprl_get_last_error(struct reprl_context* ctx)
503507
return ctx->last_error;
504508
}
505509

510+
#endif

0 commit comments

Comments
 (0)