More Rust FFI adventures

We can now convert Rust Errors to C++ exceptions. At the Rust->C++ FFI
boundary, Result<T, Error> will cause Error to be converted to and
thrown as a C++ exception.
This commit is contained in:
Eelco Dolstra 2019-09-11 01:15:20 +02:00
parent 8110b4ebb2
commit f738cd4d97
6 changed files with 133 additions and 18 deletions

View File

@ -1,5 +1,30 @@
#[derive(Debug)]
pub enum Error {
IOError(std::io::Error),
Misc(String),
Foreign(libc::c_void), // == std::exception_ptr
Foreign(CppException),
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Error::IOError(err)
}
}
impl From<Error> for CppException {
fn from(err: Error) -> Self {
match err {
Error::Foreign(ex) => ex,
Error::Misc(s) => unsafe { make_error(&s) },
Error::IOError(err) => unsafe { make_error(&err.to_string()) },
}
}
}
#[repr(C)]
#[derive(Debug)]
pub struct CppException(*const libc::c_void); // == std::exception_ptr*
extern "C" {
fn make_error(s: &str) -> CppException;
}

View File

@ -4,7 +4,30 @@ mod tarfile;
pub use error::Error;
#[no_mangle]
pub extern "C" fn unpack_tarfile(source: foreign::Source, dest_dir: &str) {
tarfile::unpack_tarfile(source, dest_dir).unwrap();
pub struct CBox<T> {
ptr: *mut libc::c_void,
phantom: std::marker::PhantomData<T>,
}
impl<T> CBox<T> {
fn new(t: T) -> Self {
unsafe {
let size = std::mem::size_of::<T>();
let ptr = libc::malloc(size);
eprintln!("PTR = {:?}, SIZE = {}", ptr, size);
*(ptr as *mut T) = t; // FIXME: probably UB
Self {
ptr,
phantom: std::marker::PhantomData,
}
}
}
}
#[no_mangle]
pub extern "C" fn unpack_tarfile(
source: foreign::Source,
dest_dir: &str,
) -> CBox<Result<(), error::CppException>> {
CBox::new(tarfile::unpack_tarfile(source, dest_dir).map_err(|err| err.into()))
}

View File

@ -10,19 +10,19 @@ pub fn unpack_tarfile(source: Source, dest_dir: &str) -> Result<(), Error> {
let mut tar = Archive::new(source);
for file in tar.entries().unwrap() {
let mut file = file.unwrap();
for file in tar.entries()? {
let mut file = file?;
let dest_file = dest_dir.join(file.path().unwrap());
let dest_file = dest_dir.join(file.path()?);
fs::create_dir_all(dest_file.parent().unwrap()).unwrap();
fs::create_dir_all(dest_file.parent().unwrap())?;
match file.header().entry_type() {
tar::EntryType::Directory => {
fs::create_dir(dest_file).unwrap();
fs::create_dir(dest_file)?;
}
tar::EntryType::Regular => {
let mode = if file.header().mode().unwrap() & libc::S_IXUSR == 0 {
let mode = if file.header().mode()? & libc::S_IXUSR == 0 {
0o666
} else {
0o777
@ -31,13 +31,11 @@ pub fn unpack_tarfile(source: Source, dest_dir: &str) -> Result<(), Error> {
.create(true)
.write(true)
.mode(mode)
.open(dest_file)
.unwrap();
io::copy(&mut file, &mut f).unwrap();
.open(dest_file)?;
io::copy(&mut file, &mut f)?;
}
tar::EntryType::Symlink => {
std::os::unix::fs::symlink(file.header().link_name().unwrap().unwrap(), dest_file)
.unwrap();
std::os::unix::fs::symlink(file.header().link_name()?.unwrap(), dest_file)?;
}
t => return Err(Error::Misc(format!("unsupported tar entry type '{:?}'", t))),
}

View File

@ -27,7 +27,7 @@ void builtinUnpackChannel(const BasicDerivation & drv)
decompressor->finish();
});
unpack_tarfile(*source, out);
unpack_tarfile(*source, out).use()->unwrap();
auto entries = readDirectory(out);
if (entries.size() != 1)

12
src/libstore/rust.cc Normal file
View File

@ -0,0 +1,12 @@
#include "logging.hh"
#include "rust.hh"
namespace nix {
extern "C" std::exception_ptr * make_error(rust::StringSlice s)
{
// FIXME: leak
return new std::exception_ptr(std::make_exception_ptr(Error(std::string(s.ptr, s.size))));
}
}

View File

@ -4,7 +4,8 @@ namespace rust {
// Depending on the internal representation of Rust slices is slightly
// evil...
template<typename T> struct Slice
template<typename T>
struct Slice
{
T * ptr;
size_t size;
@ -37,8 +38,64 @@ struct Source
}
};
/* C++ representation of Rust's Result<T, CppException>. */
template<typename T>
struct Result
{
unsigned int tag;
union {
T data;
std::exception_ptr * exc;
};
/* Rethrow the wrapped exception or return the wrapped value. */
T unwrap()
{
if (tag == 0)
return data;
else if (tag == 1)
std::rethrow_exception(*exc);
else
abort();
}
};
template<typename T>
struct CBox
{
T * ptr;
T * operator ->()
{
return ptr;
}
CBox(T * ptr) : ptr(ptr) { }
CBox(const CBox &) = delete;
CBox(CBox &&) = delete;
~CBox()
{
free(ptr);
}
};
// Grrr, this is only needed because 'extern "C"' functions don't
// support non-POD return types (and CBox has a destructor so it's not
// POD).
template<typename T>
struct CBox2
{
T * ptr;
CBox<T> use()
{
return CBox(ptr);
}
};
}
extern "C" {
void unpack_tarfile(rust::Source source, rust::StringSlice dest_dir);
rust::CBox2<rust::Result<std::tuple<>>> unpack_tarfile(rust::Source source, rust::StringSlice dest_dir);
}