Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a C compatible interface #43

Merged
merged 6 commits into from
Jan 24, 2020
Merged

Conversation

rageagainsthepc
Copy link
Collaborator

No description provided.

@rageagainsthepc rageagainsthepc force-pushed the add-ffi branch 5 times, most recently from ac11fe4 to ef83fc4 Compare January 8, 2020 14:30
@rageagainsthepc rageagainsthepc marked this pull request as ready for review January 10, 2020 13:55
Copy link
Owner

@Wenzel Wenzel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some comments over your work so far.
I've tested it on my machine and it compiled and worked for me, except for the mem-dump.

We should also provide a Makefile in c_examples, automatically compiling and linking with libmicrovmi in $root/target/debug.

A c_examples/README will be useful as well, explaining how to generate the libmicrovmi.h here, and how to set your LD_LIBRARY_PATH.

Congrats for all your work @rageagainsthepc ! 🎉 👍

c_examples/libmicrovmi.h Outdated Show resolved Hide resolved
c_examples/mem-dump.c Show resolved Hide resolved
c_examples/pause.c Show resolved Hide resolved
c_examples/regs-dump.c Show resolved Hide resolved
src/capi.rs Outdated Show resolved Hide resolved
@rageagainsthepc rageagainsthepc force-pushed the add-ffi branch 2 times, most recently from ab6dd53 to a7c2f8f Compare January 21, 2020 15:35
Copy link
Owner

@Wenzel Wenzel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comments

Command to execute an example as an unprivileged user:

~~~
sudo LD_LIBRARY_PATH="$LD_LIBRARY_PATH:../target/debug" <example> <vm_name>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sudo 🤔

If you are working with Xen, you need to be root, that's correct.
But in the general case, I would prefer to remove this indication, since it's not needed for KVM and VirtualBox.

@@ -64,4 +67,6 @@ pub trait Introspectable {
fn resume(&mut self) -> Result<(), Box<dyn Error>> {
unimplemented!();
}

fn get_driver_type(&self) -> DriverType;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rageagainsthepc can you explain why you had to introduce a new API here, it's hard to follow for me ?
I would like to avoid changing the core of the Rust library just to provide a C compatibility layer.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, if we initialize the driver with None, we don't know the type of the driver in advance. But we need to know since we are casting back and forth between void* and the actual type of the driver. As far as I know it's not possible to cast directly from void* to a trait type. There are probably different ways to approach this, but this one seemed pretty straight forward to me. I'm open to alternatives though ;)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the issue was that pointers to traits are fat pointers. Maybe there is way to pass fat pointers to C code and cast them back to traits afterwards. I'll look into that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There exists a std::raw::TraitObject but it's marked as unstable because its representation may change in the future rust-lang/rust#27751 . Also cbindgen reports TraitObject types as unsupported. In theory we could split the fat pointer into two separate pointers, store them in a struct and transmute the memory back to an Introspectable, but imho that sounds super ugly and unstable.

Copy link
Collaborator Author

@rageagainsthepc rageagainsthepc Jan 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the other hand, we could just loose the support for passing None instead of the DriverType, at least for the C-API ;)

Copy link
Collaborator Author

@rageagainsthepc rageagainsthepc Jan 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've found the crate downcast-rs. With this crate you are able to check a trait object for its concrete type.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, thanks for the update.

I'm okay to merge this as long as you add a comment that this API has been introduced for the solve purpose of the C interoperability, and that it's a workaround, to be deprecated in the future when new better solutions will emerge to deal with this, either from Rust unstable, or simply by switching to dynamic library loading.

@@ -64,4 +67,6 @@ pub trait Introspectable {
fn resume(&mut self) -> Result<(), Box<dyn Error>> {
unimplemented!();
}

fn get_driver_type(&self) -> DriverType;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, thanks for the update.

I'm okay to merge this as long as you add a comment that this API has been introduced for the solve purpose of the C interoperability, and that it's a workaround, to be deprecated in the future when new better solutions will emerge to deal with this, either from Rust unstable, or simply by switching to dynamic library loading.

@Wenzel Wenzel merged commit 3e0ab8c into Wenzel:master Jan 24, 2020
@Wenzel Wenzel mentioned this pull request Jan 24, 2020
@rageagainsthepc rageagainsthepc deleted the add-ffi branch January 25, 2020 09:42
@Wenzel Wenzel mentioned this pull request Apr 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants