Commit d46723ae authored by Samuel Groß's avatar Samuel Groß Committed by Commit Bot

Updated libreprl from Fuzzilli and improved Fuzzilli test

The test now verifies that JavaScript programs can be executed
over the REPRL interface, that runtime exceptions can be detected,
and that the engine's state is properly reset between executions.

Change-Id: Ic8032c07e222307cbb4d332e7eaec61936a10ccd
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2396082Reviewed-by: 's avatarClemens Backes <clemensb@chromium.org>
Reviewed-by: 's avatarMichael Stanton <mvstanton@chromium.org>
Commit-Queue: Samuel Groß <saelo@google.com>
Cr-Commit-Position: refs/heads/master@{#69883}
parent 345518a0
......@@ -100,7 +100,7 @@ namespace {
const int kMB = 1024 * 1024;
#ifdef V8_FUZZILLI
// REPRL = read-eval-print-loop
// REPRL = read-eval-print-reset-loop
// These file descriptors are being opened when Fuzzilli uses fork & execve to
// run V8.
#define REPRL_CRFD 100 // Control read file decriptor
......@@ -2821,6 +2821,7 @@ bool ends_with(const char* input, const char* suffix) {
bool SourceGroup::Execute(Isolate* isolate) {
bool success = true;
#ifdef V8_FUZZILLI
if (fuzzilli_reprl) {
HandleScope handle_scope(isolate);
Local<String> file_name =
String::NewFromUtf8(isolate, "fuzzcode.js", NewStringType::kNormal)
......@@ -2849,6 +2850,7 @@ bool SourceGroup::Execute(Isolate* isolate) {
Shell::kNoProcessMessageQueue)) {
return false;
}
}
#endif // V8_FUZZILLI
for (int i = begin_offset_; i < end_offset_; ++i) {
const char* arg = argv_[i];
......@@ -4258,6 +4260,10 @@ int Shell::Main(int argc, char* argv[]) {
<< bitmap.size() << std::endl;
iteration_counter++;
}
// In REPRL mode, stdout and stderr can be regular files, so they need
// to be flushed after every execution
fflush(stdout);
fflush(stderr);
CHECK_EQ(write(REPRL_CWFD, &status, 4), 4);
sanitizer_cov_reset_edgeguards();
if (options.fuzzilli_enable_builtins_coverage) {
......
......@@ -15,14 +15,19 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <poll.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
......@@ -31,14 +36,13 @@
#include "libreprl.h"
// Well-known file descriptor numbers for fuzzer <-> fuzzee communication on child process side.
#define CRFD 100
#define CWFD 101
#define DRFD 102
#define DWFD 103
// Well-known file descriptor numbers for reprl <-> child communication, child process side
#define REPRL_CHILD_CTRL_IN 100
#define REPRL_CHILD_CTRL_OUT 101
#define REPRL_CHILD_DATA_IN 102
#define REPRL_CHILD_DATA_OUT 103
#define CHECK_SUCCESS(cond) if((cond) < 0) { perror(#cond); abort(); }
#define CHECK(cond) if(!(cond)) { fprintf(stderr, "(" #cond ") failed!"); abort(); }
#define MIN(x, y) ((x) < (y) ? (x) : (y))
static uint64_t current_millis()
{
......@@ -47,173 +51,430 @@ static uint64_t current_millis()
return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
}
int reprl_spawn_child(char** argv, char** envp, struct reprl_child_process* child)
static char** copy_string_array(const char** orig)
{
// We need to make sure that our fds don't end up being 100 - 104.
if (fcntl(CRFD, F_GETFD) == -1) {
int devnull = open("/dev/null", O_RDWR);
dup2(devnull, CRFD);
dup2(devnull, CWFD);
dup2(devnull, DRFD);
dup2(devnull, DWFD);
close(devnull);
size_t num_entries = 0;
for (const char** current = orig; *current; current++) {
num_entries += 1;
}
char** copy = calloc(num_entries + 1, sizeof(char*));
for (size_t i = 0; i < num_entries; i++) {
copy[i] = strdup(orig[i]);
}
return copy;
}
static void free_string_array(char** arr)
{
if (!arr) return;
for (char** current = arr; *current; current++) {
free(*current);
}
free(arr);
}
// A unidirectional communication channel for larger amounts of data, up to a maximum size (REPRL_MAX_DATA_SIZE).
// Implemented as a (RAM-backed) file for which the file descriptor is shared with the child process and which is mapped into our address space.
struct data_channel {
// File descriptor of the underlying file. Directly shared with the child process.
int fd;
// Memory mapping of the file, always of size REPRL_MAX_DATA_SIZE.
char* mapping;
};
struct reprl_context {
// Whether reprl_initialize has been successfully performed on this context.
int initialized;
// Read file descriptor of the control pipe. Only valid if a child process is running (i.e. pid is nonzero).
int ctrl_in;
// Write file descriptor of the control pipe. Only valid if a child process is running (i.e. pid is nonzero).
int ctrl_out;
// Data channel REPRL -> Child
struct data_channel* data_in;
// Data channel Child -> REPRL
struct data_channel* data_out;
// Optional data channel for the child's stdout and stderr.
struct data_channel* stdout;
struct data_channel* stderr;
// PID of the child process. Will be zero if no child process is currently running.
int pid;
// Arguments and environment for the child process.
char** argv;
char** envp;
int crpipe[2] = { 0, 0 }; // control channel child -> fuzzer
int cwpipe[2] = { 0, 0 }; // control channel fuzzer -> child
int drpipe[2] = { 0, 0 }; // data channel child -> fuzzer
int dwpipe[2] = { 0, 0 }; // data channel fuzzer -> child
int res = 0;
res |= pipe(crpipe);
res |= pipe(cwpipe);
res |= pipe(drpipe);
res |= pipe(dwpipe);
if (res != 0) {
if (crpipe[0] != 0) { close(crpipe[0]); close(crpipe[1]); }
if (cwpipe[0] != 0) { close(cwpipe[0]); close(cwpipe[1]); }
if (drpipe[0] != 0) { close(drpipe[0]); close(drpipe[1]); }
if (dwpipe[0] != 0) { close(dwpipe[0]); close(dwpipe[1]); }
fprintf(stderr, "[REPRL] Could not setup pipes for communication with child: %s\n", strerror(errno));
// A malloc'd string containing a description of the last error that occurred.
char* last_error;
};
static int reprl_error(struct reprl_context* ctx, const char *format, ...)
{
va_list args;
va_start(args, format);
free(ctx->last_error);
vasprintf(&ctx->last_error, format, args);
return -1;
}
static struct data_channel* reprl_create_data_channel(struct reprl_context* ctx)
{
#ifdef __linux__
int fd = memfd_create("REPRL_DATA_CHANNEL", MFD_CLOEXEC);
#else
char path[] = "/tmp/reprl_data_channel_XXXXXXXX";
if (mktemp(path) < 0) {
reprl_error(ctx, "Failed to create temporary filename for data channel: %s", strerror(errno));
return NULL;
}
int fd = open(path, O_RDWR | O_CREAT| O_CLOEXEC);
unlink(path);
#endif
if (fd == -1 || ftruncate(fd, REPRL_MAX_DATA_SIZE) != 0) {
reprl_error(ctx, "Failed to create data channel file: %s", strerror(errno));
return NULL;
}
char* mapping = mmap(0, REPRL_MAX_DATA_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (mapping == MAP_FAILED) {
reprl_error(ctx, "Failed to mmap data channel file: %s", strerror(errno));
return NULL;
}
struct data_channel* channel = malloc(sizeof(struct data_channel));
channel->fd = fd;
channel->mapping = mapping;
return channel;
}
static void reprl_destroy_data_channel(struct reprl_context* ctx, struct data_channel* channel)
{
if (!channel) return;
close(channel->fd);
munmap(channel->mapping, REPRL_MAX_DATA_SIZE);
free(channel);
}
static void reprl_child_terminated(struct reprl_context* ctx)
{
if (!ctx->pid) return;
ctx->pid = 0;
close(ctx->ctrl_in);
close(ctx->ctrl_out);
}
child->crfd = crpipe[0];
child->cwfd = cwpipe[1];
child->drfd = drpipe[0];
child->dwfd = dwpipe[1];
static void reprl_terminate_child(struct reprl_context* ctx)
{
if (!ctx->pid) return;
int status;
kill(ctx->pid, SIGKILL);
waitpid(ctx->pid, &status, 0);
reprl_child_terminated(ctx);
}
static int reprl_spawn_child(struct reprl_context* ctx)
{
// This is also a good time to ensure the data channel backing files don't grow too large.
ftruncate(ctx->data_in->fd, REPRL_MAX_DATA_SIZE);
ftruncate(ctx->data_out->fd, REPRL_MAX_DATA_SIZE);
if (ctx->stdout) ftruncate(ctx->stdout->fd, REPRL_MAX_DATA_SIZE);
if (ctx->stderr) ftruncate(ctx->stderr->fd, REPRL_MAX_DATA_SIZE);
int crpipe[2] = { 0, 0 }; // control pipe child -> reprl
int cwpipe[2] = { 0, 0 }; // control pipe reprl -> child
int flags;
flags = fcntl(child->drfd, F_GETFL, 0);
fcntl(child->drfd, F_SETFL, flags | O_NONBLOCK);
if (pipe(crpipe) != 0) {
return reprl_error(ctx, "Could not create pipe for REPRL communication: %s", strerror(errno));
}
if (pipe(cwpipe) != 0) {
close(crpipe[0]);
close(crpipe[1]);
return reprl_error(ctx, "Could not create pipe for REPRL communication: %s", strerror(errno));
}
fcntl(child->crfd, F_SETFD, FD_CLOEXEC);
fcntl(child->cwfd, F_SETFD, FD_CLOEXEC);
fcntl(child->drfd, F_SETFD, FD_CLOEXEC);
fcntl(child->dwfd, F_SETFD, FD_CLOEXEC);
ctx->ctrl_in = crpipe[0];
ctx->ctrl_out = cwpipe[1];
fcntl(ctx->ctrl_in, F_SETFD, FD_CLOEXEC);
fcntl(ctx->ctrl_out, F_SETFD, FD_CLOEXEC);
int pid = fork();
if (pid == 0) {
dup2(cwpipe[0], CRFD);
dup2(crpipe[1], CWFD);
dup2(dwpipe[0], DRFD);
dup2(drpipe[1], DWFD);
dup2(cwpipe[0], REPRL_CHILD_CTRL_IN);
dup2(crpipe[1], REPRL_CHILD_CTRL_OUT);
close(cwpipe[0]);
close(crpipe[1]);
close(dwpipe[0]);
close(drpipe[1]);
dup2(ctx->data_out->fd, REPRL_CHILD_DATA_IN);
dup2(ctx->data_in->fd, REPRL_CHILD_DATA_OUT);
int devnull = open("/dev/null", O_RDWR);
dup2(devnull, 0);
dup2(devnull, 1);
dup2(devnull, 2);
if (ctx->stdout) dup2(ctx->stdout->fd, 1);
else dup2(devnull, 1);
if (ctx->stderr) dup2(ctx->stderr->fd, 2);
else dup2(devnull, 2);
close(devnull);
execve(argv[0], argv, envp);
fprintf(stderr, "[REPRL] Failed to spawn child process\n");
// close all other FDs. We try to use FD_CLOEXEC everywhere, but let's be extra sure we don't leak any fds to the child.
int tablesize = getdtablesize();
for (int i = 3; i < tablesize; i++) {
if (i == REPRL_CHILD_CTRL_IN || i == REPRL_CHILD_CTRL_OUT || i == REPRL_CHILD_DATA_IN || i == REPRL_CHILD_DATA_OUT) {
continue;
}
close(i);
}
execve(ctx->argv[0], ctx->argv, ctx->envp);
fprintf(stderr, "Failed to execute child process %s: %s\n", ctx->argv[0], strerror(errno));
fflush(stderr);
_exit(-1);
} else if (pid < 0) {
fprintf(stderr, "[REPRL] Failed to fork\n");
return -1;
}
close(crpipe[1]);
close(cwpipe[0]);
close(drpipe[1]);
close(dwpipe[0]);
child->pid = pid;
int helo;
if (read(child->crfd, &helo, 4) != 4 || write(child->cwfd, &helo, 4) != 4) {
fprintf(stderr, "[REPRL] Failed to communicate with child process\n");
close(child->crfd);
close(child->cwfd);
close(child->drfd);
close(child->dwfd);
int status;
kill(pid, SIGKILL);
waitpid(pid, &status, 0);
return -1;
if (pid < 0) {
close(ctx->ctrl_in);
close(ctx->ctrl_out);
return reprl_error(ctx, "Failed to fork: %s", strerror(errno));
}
ctx->pid = pid;
char helo[4] = { 0 };
if (read(ctx->ctrl_in, helo, 4) != 4) {
reprl_terminate_child(ctx);
return reprl_error(ctx, "Did not receive HELO message from child");
}
if (strncmp(helo, "HELO", 4) != 0) {
reprl_terminate_child(ctx);
return reprl_error(ctx, "Received invalid HELO message from child");
}
if (write(ctx->ctrl_out, helo, 4) != 4) {
reprl_terminate_child(ctx);
return reprl_error(ctx, "Failed to send HELO reply message to child");
}
return 0;
}
static char* fetch_output(int fd, size_t* outsize)
struct reprl_context* reprl_create_context()
{
ssize_t rv;
*outsize = 0;
size_t remaining = 0x1000;
char* outbuf = malloc(remaining + 1);
struct reprl_context* ctx = malloc(sizeof(struct reprl_context));
memset(ctx, 0, sizeof(struct reprl_context));
return ctx;
}
do {
rv = read(fd, outbuf + *outsize, remaining);
if (rv == -1) {
if (errno != EAGAIN && errno != EWOULDBLOCK) {
fprintf(stderr, "[REPRL] Error while receiving data: %s\n", strerror(errno));
}
break;
int reprl_initialize_context(struct reprl_context* ctx, const char** argv, const char** envp, int capture_stdout, int capture_stderr)
{
if (ctx->initialized) {
return reprl_error(ctx, "Context is already initialized");
}
*outsize += rv;
remaining -= rv;
// We need to ignore SIGPIPE since we could end up writing to a pipe after our child process has exited.
signal(SIGPIPE, SIG_IGN);
if (remaining == 0) {
remaining = *outsize;
outbuf = realloc(outbuf, *outsize * 2 + 1);
if (!outbuf) {
fprintf(stderr, "[REPRL] Could not allocate output buffer");
_exit(-1);
ctx->argv = copy_string_array(argv);
ctx->envp = copy_string_array(envp);
ctx->data_in = reprl_create_data_channel(ctx);
ctx->data_out = reprl_create_data_channel(ctx);
if (capture_stdout) {
ctx->stdout = reprl_create_data_channel(ctx);
}
if (capture_stderr) {
ctx->stderr = reprl_create_data_channel(ctx);
}
} while (rv > 0);
if (!ctx->data_in || !ctx->data_out || (capture_stdout && !ctx->stdout) || (capture_stderr && !ctx->stderr)) {
// Proper error message will have been set by reprl_create_data_channel
return -1;
}
ctx->initialized = 1;
return 0;
}
outbuf[*outsize] = 0;
void reprl_destroy_context(struct reprl_context* ctx)
{
reprl_terminate_child(ctx);
free_string_array(ctx->argv);
free_string_array(ctx->envp);
return outbuf;
reprl_destroy_data_channel(ctx, ctx->data_in);
reprl_destroy_data_channel(ctx, ctx->data_out);
reprl_destroy_data_channel(ctx, ctx->stdout);
reprl_destroy_data_channel(ctx, ctx->stderr);
free(ctx->last_error);
free(ctx);
}
// Execute one script, wait for its completion, and return the result.
int reprl_execute_script(int pid, int crfd, int cwfd, int drfd, int dwfd, int timeout, const char* script, int64_t script_length, struct reprl_result* result)
int reprl_execute(struct reprl_context* ctx, const char* script, uint64_t script_length, uint64_t timeout, uint64_t* execution_time, int fresh_instance)
{
uint64_t start_time = current_millis();
if (!ctx->initialized) {
return reprl_error(ctx, "REPRL context is not initialized");
}
if (script_length > REPRL_MAX_DATA_SIZE) {
return reprl_error(ctx, "Script too large");
}
if (write(cwfd, "exec", 4) != 4 ||
write(cwfd, &script_length, 8) != 8) {
fprintf(stderr, "[REPRL] Failed to send command to child process\n");
return -1;
// Terminate any existing instance if requested.
if (fresh_instance && ctx->pid) {
reprl_terminate_child(ctx);
}
int64_t remaining = script_length;
while (remaining > 0) {
ssize_t rv = write(dwfd, script, remaining);
if (rv <= 0) {
fprintf(stderr, "[REPRL] Failed to send script to child process\n");
return -1;
// Reset file position so the child can simply read(2) and write(2) to these fds.
lseek(ctx->data_out->fd, 0, SEEK_SET);
lseek(ctx->data_in->fd, 0, SEEK_SET);
if (ctx->stdout) {
lseek(ctx->stdout->fd, 0, SEEK_SET);
}
remaining -= rv;
script += rv;
if (ctx->stderr) {
lseek(ctx->stderr->fd, 0, SEEK_SET);
}
struct pollfd fds = {.fd = crfd, .events = POLLIN, .revents = 0};
if (poll(&fds, 1, timeout) != 1) {
kill(pid, SIGKILL);
waitpid(pid, &result->status, 0);
result->child_died = 1;
// Spawn a new instance if necessary.
if (!ctx->pid) {
int r = reprl_spawn_child(ctx);
if (r != 0) return r;
}
// Copy the script to the data channel.
memcpy(ctx->data_out->mapping, script, script_length);
// Tell child to execute the script.
if (write(ctx->ctrl_out, "exec", 4) != 4 ||
write(ctx->ctrl_out, &script_length, 8) != 8) {
// These can fail if the child unexpectedly terminated between executions.
// Check for that here to be able to provide a better error message.
int status;
if (waitpid(ctx->pid, &status, WNOHANG) == ctx->pid) {
reprl_child_terminated(ctx);
if (WIFEXITED(status)) {
return reprl_error(ctx, "Child unexpectedly exited with status %i between executions", WEXITSTATUS(status));
} else {
result->child_died = 0;
ssize_t rv = read(crfd, &result->status, 4);
if (rv != 4) {
// This should not happen...
kill(pid, SIGKILL);
waitpid(pid, &result->status, 0);
result->child_died = 1;
return reprl_error(ctx, "Child unexpectedly terminated with signal %i between executions", WTERMSIG(status));
}
}
return reprl_error(ctx, "Failed to send command to child process: %s", strerror(errno));
}
result->output = fetch_output(drfd, &result->output_size);
result->exec_time = current_millis() - start_time;
// Wait for child to finish execution (or crash).
uint64_t start_time = current_millis();
struct pollfd fds = {.fd = ctx->ctrl_in, .events = POLLIN, .revents = 0};
int res = poll(&fds, 1, (int)timeout);
*execution_time = current_millis() - start_time;
if (res == 0) {
// Execution timed out. Kill child and return a timeout status.
reprl_terminate_child(ctx);
return 1 << 16;
} else if (res != 1) {
// An error occurred.
// We expect all signal handlers to be installed with SA_RESTART, so receiving EINTR here is unexpected and thus also an error.
return reprl_error(ctx, "Failed to poll: %s", strerror(errno));
}
return 0;
// Poll succeeded, so there must be something to read now (either the status or EOF).
int status;
ssize_t rv = read(ctx->ctrl_in, &status, 4);
if (rv < 0) {
return reprl_error(ctx, "Failed to read from control pipe: %s", strerror(errno));
} else if (rv != 4) {
// Most likely, the child process crashed and closed the write end of the control pipe.
// Unfortunately, there probably is nothing that guarantees that waitpid() will immediately succeed now,
// and we also don't want to block here. So just retry waitpid() a few times...
int success = 0;
do {
success = waitpid(ctx->pid, &status, WNOHANG) == ctx->pid;
if (!success) usleep(10);
} while (!success && current_millis() - start_time < timeout);
if (!success) {
// Wait failed, so something weird must have happened. Maybe somehow the control pipe was closed without the child exiting?
// Probably the best we can do is kill the child and return an error.
reprl_terminate_child(ctx);
return reprl_error(ctx, "Child in weird state after execution");
}
// Cleanup any state related to this child process.
reprl_child_terminated(ctx);
if (WIFEXITED(status)) {
status = WEXITSTATUS(status) << 8;
} else if (WIFSIGNALED(status)) {
status = WTERMSIG(status);
} else {
// This shouldn't happen, since we don't specify WUNTRACED for waitpid...
return reprl_error(ctx, "Waitpid returned unexpected child state %i", status);
}
}
// The status must be a positive number, see the status encoding format below.
// We also don't allow the child process to indicate a timeout. If we wanted,
// we could treat it as an error if the upper bits are set.
status &= 0xffff;
return status;
}
/// The 32bit REPRL exit status as returned by reprl_execute has the following format:
/// [ 00000000 | did_timeout | exit_code | terminating_signal ]
/// Only one of did_timeout, exit_code, or terminating_signal may be set at one time.
int RIFSIGNALED(int status)
{
return (status & 0xff) != 0;
}
int RIFEXITED(int status)
{
return !RIFSIGNALED(status) && !RIFTIMEDOUT(status);
}
int RIFTIMEDOUT(int status)
{
return (status & 0xff0000) != 0;
}
int RTERMSIG(int status)
{
return status & 0xff;
}
int REXITSTATUS(int status)
{
return (status >> 8) & 0xff;
}
static const char* fetch_data_channel_content(struct data_channel* channel)
{
if (!channel) return "";
size_t pos = lseek(channel->fd, 0, SEEK_CUR);
pos = MIN(pos, REPRL_MAX_DATA_SIZE - 1);
channel->mapping[pos] = 0;
return channel->mapping;
}
const char* reprl_fetch_fuzzout(struct reprl_context* ctx)
{
return fetch_data_channel_content(ctx->data_in);
}
const char* reprl_fetch_stdout(struct reprl_context* ctx)
{
return fetch_data_channel_content(ctx->stdout);
}
const char* reprl_fetch_stderr(struct reprl_context* ctx)
{
return fetch_data_channel_content(ctx->stderr);
}
const char* reprl_get_last_error(struct reprl_context* ctx)
{
return ctx->last_error;
}
......@@ -18,37 +18,100 @@
#ifndef __LIBREPRL_H__
#define __LIBREPRL_H__
#include <stdint.h>
#include <sys/types.h>
struct reprl_child_process {
// Read file descriptor of the control pipe.
int crfd;
// Write file descriptor of the control pipe.
int cwfd;
// Read file descriptor of the data pipe.
int drfd;
// Write file descriptor of the data pipe.
int dwfd;
// PID of the child process.
int pid;
};
struct reprl_result {
int child_died;
int status;
unsigned long exec_time;
char* output;
size_t output_size;
};
// Spawn a child process implementing the REPRL protocol.
int reprl_spawn_child(char** argv, char** envp,
struct reprl_child_process* child);
// Execute the provided script in the child process, wait for its completion,
// and return the result.
int reprl_execute_script(int pid, int crfd, int cwfd, int drfd, int dwfd,
int timeout, const char* script, int64_t script_length,
struct reprl_result* result);
/// Maximum size for data transferred through REPRL. In particular, this is the
/// maximum size of scripts that can be executed. Currently, this is 16MB.
/// Executing a 16MB script file is very likely to take longer than the typical
/// timeout, so the limit on script size shouldn't be a problem in practice.
#define REPRL_MAX_DATA_SIZE (16 << 20)
/// Opaque struct representing a REPRL execution context.
struct reprl_context;
/// Allocates a new REPRL context.
/// @return an uninitialzed REPRL context
struct reprl_context* reprl_create_context();
/// Initializes a REPRL context.
/// @param ctx An uninitialized context
/// @param argv The argv vector for the child processes
/// @param envp The envp vector for the child processes
/// @param capture_stdout Whether this REPRL context should capture the child's
/// stdout
/// @param capture_stderr Whether this REPRL context should capture the child's
/// stderr
/// @return zero in case of no errors, otherwise a negative value
int reprl_initialize_context(struct reprl_context* ctx, const char** argv,
const char** envp, int capture_stdout,
int capture_stderr);
/// Destroys a REPRL context, freeing all resources held by it.
/// @param ctx The context to destroy
void reprl_destroy_context(struct reprl_context* ctx);
/// Executes the provided script in the target process, wait for its completion,
/// and return the result. If necessary, or if fresh_instance is true, this will
/// automatically spawn a new instance of the target process.
///
/// @param ctx The REPRL context
/// @param script The script to execute
/// @param script_length The size of the script in bytes
/// @param timeout The maximum allowed execution time in milliseconds
/// @param execution_time A pointer to which, if execution succeeds, the
/// execution time in milliseconds is written to
/// @param fresh_instance if true, forces the creation of a new instance of the
/// target
/// @return A REPRL exit status (see below) or a negative number in case of an
/// error
int reprl_execute(struct reprl_context* ctx, const char* script,
uint64_t script_length, uint64_t timeout,
uint64_t* execution_time, int fresh_instance);
/// Returns true if the execution terminated due to a signal.
int RIFSIGNALED(int status);
/// Returns true if the execution finished normally.
int RIFEXITED(int status);
/// Returns true if the execution terminated due to a timeout.
int RIFTIMEDOUT(int status);
/// Returns the terminating signal in case RIFSIGNALED is true.
int RTERMSIG(int status);
/// Returns the exit status in case RIFEXITED is true.
int REXITSTATUS(int status);
/// Returns the stdout data of the last successful execution if the context is
/// capturing stdout, otherwise an empty string. The output is limited to
/// REPRL_MAX_FAST_IO_SIZE (currently 16MB).
/// @param ctx The REPRL context
/// @return A string pointer which is owned by the REPRL context and thus should
/// not be freed by the caller
const char* reprl_fetch_stdout(struct reprl_context* ctx);
/// Returns the stderr data of the last successful execution if the context is
/// capturing stderr, otherwise an empty string. The output is limited to
/// REPRL_MAX_FAST_IO_SIZE (currently 16MB).
/// @param ctx The REPRL context
/// @return A string pointer which is owned by the REPRL context and thus should
/// not be freed by the caller
const char* reprl_fetch_stderr(struct reprl_context* ctx);
/// Returns the fuzzout data of the last successful execution.
/// The output is limited to REPRL_MAX_FAST_IO_SIZE (currently 16MB).
/// @param ctx The REPRL context
/// @return A string pointer which is owned by the REPRL context and thus should
/// not be freed by the caller
const char* reprl_fetch_fuzzout(struct reprl_context* ctx);
/// Returns a string describing the last error that occurred in the given
/// context.
/// @param ctx The REPRL context
/// @return A string pointer which is owned by the REPRL context and thus should
/// not be freed by the caller
const char* reprl_get_last_error(struct reprl_context* ctx);
#endif
......@@ -3,16 +3,58 @@
// found in the LICENSE file.
extern "C" {
#include <stdio.h>
#include <string.h>
#include "libreprl.h"
}
int main() {
struct reprl_child_process child;
char* env[] = {nullptr};
char prog[] = "./out.gn/x64.debug/d8";
char*(argv[]) = {prog, nullptr};
if (reprl_spawn_child(argv, env, &child) == -1) return -1;
// struct reprl_result res;
// reprl_execute_script(child.pid, child.crfd, child.cwfd, child.drfd,
// child.dwfd, 1, ,,&res);
int main(int argc, char** argv) {
struct reprl_context* ctx = reprl_create_context();
const char* env[] = {nullptr};
const char* prog = argc > 1 ? argv[1] : "./out.gn/x64.debug/d8";
const char* args[] = {prog, nullptr};
if (reprl_initialize_context(ctx, args, env, 1, 1) != 0) {
printf("REPRL initialization failed\n");
return -1;
}
uint64_t exec_time;
// Basic functionality test
const char* code = "let greeting = \"Hello World!\";";
if (reprl_execute(ctx, code, strlen(code), 1000, &exec_time, 0) != 0) {
printf("Execution of \"%s\" failed\n", code);
printf("Is %s the path to d8 built with v8_fuzzilli=true?\n", prog);
return -1;
}
// Verify that runtime exceptions can be detected
code = "throw 'failure';";
if (reprl_execute(ctx, code, strlen(code), 1000, &exec_time, 0) == 0) {
printf("Execution of \"%s\" unexpectedly succeeded\n", code);
return -1;
}
// Verify that existing state is property reset between executions
code = "globalProp = 42; Object.prototype.foo = \"bar\";";
if (reprl_execute(ctx, code, strlen(code), 1000, &exec_time, 0) != 0) {
printf("Execution of \"%s\" failed\n", code);
return -1;
}
code = "if (typeof(globalProp) !== 'undefined') throw 'failure'";
if (reprl_execute(ctx, code, strlen(code), 1000, &exec_time, 0) != 0) {
printf("Execution of \"%s\" failed\n", code);
return -1;
}
code = "if (typeof(Object.prototype.foo) !== 'undefined') throw 'failure'";
if (reprl_execute(ctx, code, strlen(code), 1000, &exec_time, 0) != 0) {
printf("Execution of \"%s\" failed\n", code);
return -1;
}
puts("OK");
return 0;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment