Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I've been busy the last few weeks, but now I am back with a bigger PR.
This PR contains a rewrite of
middle::Type
. I started to rewrite it because I found it hard to follow how the oldType
worked. I realize that I might have just replaced something that I did not understand with something everybody else does not understand, but I have at least tried to separate out safe functionality (such as just describing libffi type signatures) from unsafe code to handle allocation of memory.In addition, I have made some breaking changes. None of the changes should require major changes downstream (hopefully), but I am not the best judge over what breaking changes are acceptable and not. Please let me know if you have any input on changes that should be made or changes that I've made that should be undone.
Breaking changes
middle::Type
is now an enum. It does not manage the memory offfi_type
s forCif
s, which is handled by newFfiTypeArray
andFfiType
structs.FfiType
s passes tests inmiddle::types
with miri. The tests should probably be modified to useFfiTypeAray
as well.middle::Type
does not have avoid
variant asffi_type_void
cannot be used for argument types in libffi. Instead, when creating aCif
, the result type is anOption<Type>
whereNone
represents avoid
return type.()
no longer implementsCType
. Instead, it implementsCRetType
, a new trait which is implemented for()
and all types that implementCType
.RetType
from theCType
trait as it is no longer needed. It was previously needed, but become obsolete after Fixed out-of-bounds write inlow::call
#108 was merged.Other changes
There are a few other changes I have made that I think it is worth to highlight to get eventual feedback.
Memory management
This crate previously used
libc::malloc
andlibc::free
to manage memory forffi_type
. This PR exchanges that for creatingBox
es,Box::into_raw
andBox::from_raw
. With this PR, there is no longer any use oflibc
and it can be removed as a dependency.Box
es come with their own set of problems, but this change should avoid problems with uniqueness by not creatingBox
es except for allocating memory andfrom_raw
when dropping them. This has the advantage that it allocates memory using the global allocator configured in projects using libffi-rs. In the future this should also make it fairly easy to allow the usage of custom (non-global) allocators if anyone should wish to do so.I did consider using
alloc::alloc
to allocate memory, but was hesitant to do so as the documentation states that it "is expected to be deprecated".Documentation of unsafe code
I have tried to add comments where it is useful to understand the memory management and why/when it is safe to do everything needed to manage memory for
ffi_type
s. I am sure there are places where things could be documented better, and there are probably comments that are superfluous and only add clutter.I will try to review the comments later, but currently I am blind on my own code.
Deprecation of
Type
functionsPreviously,
Type
s had to be constructed from functions such asType::u8()
. Since the newType
is an enum, this is no longer needed (for all types except structs). I chose to keep the old functions, but deprecate them as I find it better to refer directly to theType
enum variants when possible.Unresolved issues
Does not currently compile with
system
feature flag on Mac due to missinglong double
symbols.