Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions rust/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,27 @@ impl Platform {
BNSetPlatformSystemCallConvention
);

/// Register a calling convention with the platform, adding it to [`Platform::calling_conventions`].
///
/// Unlike [`Platform::set_default_calling_convention`] and the other role setters, this does not
/// assign the calling convention to a role, it only makes it available on the platform.
///
/// # Panics
///
/// Panics if the calling convention's architecture does not match the platform's architecture.
pub fn register_calling_convention(&self, cc: &CoreCallingConvention) {
let arch = self.arch();

assert_eq!(
cc.arch_handle.handle, arch.handle,
"use of calling convention with non-matching Platform architecture!"
);

unsafe {
BNRegisterPlatformCallingConvention(self.handle, cc.handle);
}
}

pub fn calling_conventions(&self) -> Array<CoreCallingConvention> {
unsafe {
let mut count = 0;
Expand Down
18 changes: 18 additions & 0 deletions rust/tests/platform.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use binaryninja::architecture::{ArchitectureExt, CoreArchitecture};
use binaryninja::headless::Session;
use binaryninja::platform::Platform;

Expand Down Expand Up @@ -38,3 +39,20 @@ fn test_platform_calling_conventions() {
let platform = Platform::by_name("windows-x86_64").expect("windows-x86_64 exists");
assert_eq!(platform.calling_conventions().len(), 1);
}

#[test]
fn test_platform_register_calling_convention() {
let _session = Session::new().expect("Failed to initialize session");
let arch = CoreArchitecture::by_name("x86_64").expect("x86_64 exists");
let platform = Platform::new(&arch, "test-register-cc-x86_64");
assert_eq!(platform.calling_conventions().len(), 0);

let cc = arch
.calling_convention_by_name("win64")
.expect("win64 calling convention exists");
platform.register_calling_convention(&cc);

let calling_conventions = platform.calling_conventions();
assert_eq!(calling_conventions.len(), 1);
assert_eq!(calling_conventions.get(0).name(), "win64");
}
Loading