forked from wasmerio/wasmer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
|
||
int main() { | ||
char command[] = "touch"; | ||
char arg1[] = "foo.txt"; | ||
char* argv[3]; | ||
argv[0] = command; | ||
argv[1] = arg1; | ||
argv[2] = 0; | ||
|
||
printf("_execvp\n"); | ||
int result = execvp(command, argv); | ||
// should not return, and not print this message | ||
printf("error"); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
_execvp |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
|
||
int main() { | ||
char command[] = "C:\\Windows\\System32\\cmd.exe"; | ||
char arg1[] = "echo"; | ||
char arg2[] = "foo"; | ||
char* argv[4]; | ||
argv[0] = command; | ||
argv[1] = arg1; | ||
argv[2] = arg2; | ||
argv[3] = 0; | ||
printf("_execvp\n"); | ||
int result = execvp(command, argv); | ||
// should not return, and not print this message | ||
printf("error"); | ||
return 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use libc::execvp; | ||
use std::cell::Cell; | ||
use std::ffi::CString; | ||
use wasmer_runtime_core::vm::Ctx; | ||
|
||
pub fn _execvp(ctx: &mut Ctx, command_name_offset: u32, argv_offset: u32) -> i32 { | ||
// a single reference to re-use | ||
let emscripten_memory = ctx.memory(0); | ||
|
||
// read command name as string | ||
let command_name_string_vec: Vec<u8> = emscripten_memory.view() | ||
[(command_name_offset as usize)..] | ||
.iter() | ||
.map(|cell| cell.get()) | ||
.take_while(|&byte| byte != 0) | ||
.collect(); | ||
let command_name_string = CString::new(command_name_string_vec).unwrap(); | ||
|
||
// get the array of args | ||
let mut argv: Vec<*const i8> = emscripten_memory.view()[((argv_offset / 4) as usize)..] | ||
.iter() | ||
.map(|cell: &Cell<u32>| cell.get()) | ||
.take_while(|&byte| byte != 0) | ||
.map(|offset| { | ||
let p: *const i8 = (emscripten_memory.view::<u8>()[(offset as usize)..]) | ||
.iter() | ||
.map(|cell| cell.as_ptr() as *const i8) | ||
.collect::<Vec<*const i8>>()[0]; | ||
p | ||
}) | ||
.collect(); | ||
|
||
// push a nullptr on to the end of the args array | ||
argv.push(std::ptr::null()); | ||
|
||
// construct raw pointers and hand them to `execvp` | ||
let command_pointer = command_name_string.as_ptr() as *const i8; | ||
let args_pointer = argv.as_ptr(); | ||
unsafe { execvp(command_pointer, args_pointer) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#[test] | ||
fn test_execvp() { | ||
#[cfg(not(target_os = "windows"))] | ||
assert_emscripten_output!( | ||
"../../emtests/test_execvp.wasm", | ||
"test_execvp", | ||
vec![], | ||
"../../emtests/test_execvp.out" | ||
); | ||
#[cfg(target_os = "windows")] | ||
assert_emscripten_output!( | ||
"../../emtests/test_execvp_windows.wasm", | ||
"test_execvp", | ||
vec![], | ||
"../../emtests/test_execvp.out" | ||
); | ||
} |