Merged
Conversation
ab1f597 to
856bbd5
Compare
Contributor
Author
|
Some updates: I've gotten arbitrary function argument passing to work on a test, I still am figuring out return type inference however. use std::ffi::c_void;
use std::path::Path;
use libffi::middle as libffi;
use libloading::Symbol;
type FP = unsafe extern "C" fn ();
fn main() {
let path = "C:\\Windows\\System32\\kernel32.dll";
let path = Path::new(path);
let lib = unsafe { libloading::Library::new(path) }.unwrap();
let function_bytes = b"Sleep\0";
let function: Symbol<FP> = unsafe { lib.get(function_bytes) }.unwrap();
let function = *function;
let args = vec![libffi::Type::u32()];
let cif = libffi::Cif::new(args.into_iter(), libffi::Type::void());
let ms_wait: u32 = 5000;
println!("Calling function");
let now = std::time::Instant::now();
let _res: *const c_void = unsafe { cif.call(libffi::CodePtr(function as *mut _), &[libffi::arg(&ms_wait)]) };
println!("Function returned in {}ms", now.elapsed().as_millis());
println!("Function called");
} |
Contributor
Author
|
Nevermind, it seems like return type needs to be manually specified and the default is cint |
Contributor
Author
|
Ready for review. (this is from the extra test) libc = cdll.msvcrt
print("rand", libc.rand())/cc @youknowone |
28 tasks
3115d8a to
06dbae4
Compare
Signed-off-by: Ashwin Naren <[email protected]>
Signed-off-by: Ashwin Naren <[email protected]>
Signed-off-by: Ashwin Naren <[email protected]>
Signed-off-by: Ashwin Naren <[email protected]>
Signed-off-by: Ashwin Naren <[email protected]>
Signed-off-by: Ashwin Naren <[email protected]>
youknowone
reviewed
Mar 3, 2025
Member
youknowone
left a comment
There was a problem hiding this comment.
Looks good, thank you so much!
Please check if the extra indent is intended or not.
Contributor
Author
|
Done. |
youknowone
requested changes
Mar 3, 2025
Co-authored-by: Jeong, YunWon <[email protected]>
youknowone
approved these changes
Mar 3, 2025
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Here comes the meat of the code and the hardest part implementation-wise.
The main goal is to implement (atleast partially)
CFuncPtr. By partially, I mean that not supporting arguments to functions is probably fine. At any rate the cpython implementation can be used as a reference since they use libffi. The main challenge is determining the return type of the loaded function from thecvoidit'll return (unless we don't have to do that for some reason).CFuncPtrCFuncPtrthat encapsulated the entire thingThe next pr can probably just do some cleanup on this and get the basics for struct and union return types working.
Known issues
c_intand cannot be changedimport ctypesfails