improve how we deal with the expect memory buffer

This commit is contained in:
Folkert 2022-08-13 00:01:05 +02:00
parent 3aa4ebb02f
commit f8fba56b0a
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
3 changed files with 112 additions and 157 deletions

View file

@ -8,12 +8,10 @@ use roc_build::link::{LinkType, LinkingStrategy};
use roc_collections::VecMap;
use roc_error_macros::{internal_error, user_error};
use roc_gen_llvm::llvm::build::LlvmBackendMode;
use roc_gen_llvm::run_roc::RocCallResult;
use roc_gen_llvm::run_roc_dylib;
use roc_load::{ExecutionMode, Expectations, LoadConfig, LoadingProblem, Threading};
use roc_module::symbol::{Interns, ModuleId};
use roc_mono::ir::OptLevel;
use roc_repl_expect::run::{expect_mono_module_to_dylib, roc_dev_expect};
use roc_repl_expect::run::expect_mono_module_to_dylib;
use roc_target::TargetInfo;
use std::env;
use std::ffi::{CString, OsStr};
@ -314,8 +312,6 @@ pub enum FormatMode {
CheckOnly,
}
const SHM_SIZE: i64 = 1024;
pub fn test(matches: &ArgMatches, triple: Triple) -> io::Result<i32> {
let start_time = Instant::now();
let arena = Bump::new();
@ -401,32 +397,6 @@ pub fn test(matches: &ArgMatches, triple: Triple) -> io::Result<i32> {
let mut writer = std::io::stdout();
// let mut shared_buffer = vec![0u8; SHM_SIZE as usize];
let shared_buffer_pointer = unsafe {
// IMPORTANT: shared memory object names must begin with / and contain no other slashes!
let name = "/roc_expect_buffer"; // format!("/roc_expect_buffer", std::process::id());
let cstring = CString::new(name).unwrap();
let shared_fd =
libc::shm_open(cstring.as_ptr().cast(), libc::O_RDWR | libc::O_CREAT, 0o777);
let shared_ptr = libc::mmap(
std::ptr::null_mut(),
SHM_SIZE as usize,
libc::PROT_WRITE | libc::PROT_READ,
libc::MAP_SHARED,
shared_fd,
0,
);
shared_ptr.cast()
};
let set_shared_buffer = run_roc_dylib!(lib, "set_shared_buffer", (*mut u8, usize), ());
let mut result = RocCallResult::default();
let slice = (shared_buffer_pointer, SHM_SIZE as usize);
unsafe { set_shared_buffer(slice, &mut result) };
let (failed, passed) = roc_repl_expect::run::run_expects(
&mut writer,
roc_reporting::report::RenderTarget::ColorTerminal,
@ -434,7 +404,6 @@ pub fn test(matches: &ArgMatches, triple: Triple) -> io::Result<i32> {
interns,
&lib,
&mut expectations,
shared_buffer_pointer,
expects,
)
.unwrap();
@ -953,94 +922,13 @@ impl ExecutableFile {
// with Expect
#[cfg(target_family = "unix")]
unsafe fn roc_run_native_debug(
executable: ExecutableFile,
argv: &[*const c_char],
envp: &[*const c_char],
mut expectations: VecMap<ModuleId, Expectations>,
interns: Interns,
_executable: ExecutableFile,
_argv: &[*const c_char],
_envp: &[*const c_char],
_expectations: VecMap<ModuleId, Expectations>,
_interns: Interns,
) {
use signal_hook::{consts::signal::SIGCHLD, consts::signal::SIGUSR1, iterator::Signals};
let mut signals = Signals::new(&[SIGCHLD, SIGUSR1]).unwrap();
match libc::fork() {
0 => {
// we are the child
if executable.execve(argv, envp) < 0 {
// Get the current value of errno
let e = errno::errno();
// Extract the error code as an i32
let code = e.0;
// Display a human-friendly error message
println!("💥 Error {}: {}", code, e);
}
}
-1 => {
// something failed
// Get the current value of errno
let e = errno::errno();
// Extract the error code as an i32
let code = e.0;
// Display a human-friendly error message
println!("Error {}: {}", code, e);
process::exit(1)
}
1.. => {
let name = "/roc_expect_buffer"; // IMPORTANT: shared memory object names must begin with / and contain no other slashes!
let cstring = CString::new(name).unwrap();
let arena = &bumpalo::Bump::new();
let interns = arena.alloc(interns);
for sig in &mut signals {
match sig {
SIGCHLD => {
// clean up
libc::shm_unlink(cstring.as_ptr().cast());
// done!
process::exit(0);
}
SIGUSR1 => {
// this is the signal we use for an expect failure. Let's see what the child told us
let shared_fd =
libc::shm_open(cstring.as_ptr().cast(), libc::O_RDONLY, 0o666);
libc::ftruncate(shared_fd, SHM_SIZE);
let shared_ptr = libc::mmap(
std::ptr::null_mut(),
SHM_SIZE as usize,
libc::PROT_READ,
libc::MAP_SHARED,
shared_fd,
0,
);
let shared_memory_ptr: *mut u8 = shared_ptr.cast();
roc_dev_expect(
&mut std::io::stdout(),
arena,
&mut expectations,
interns,
shared_memory_ptr,
)
.unwrap();
}
_ => println!("received signal {}", sig),
}
}
}
_ => unreachable!(),
}
todo!()
}
#[cfg(target_os = "linux")]