diff --git a/rust/src/platform.rs b/rust/src/platform.rs index 9de125a1c3..b32dec8afd 100644 --- a/rust/src/platform.rs +++ b/rust/src/platform.rs @@ -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 { unsafe { let mut count = 0; diff --git a/rust/tests/platform.rs b/rust/tests/platform.rs index 09022ec1c3..fa2136e103 100644 --- a/rust/tests/platform.rs +++ b/rust/tests/platform.rs @@ -1,3 +1,4 @@ +use binaryninja::architecture::{ArchitectureExt, CoreArchitecture}; use binaryninja::headless::Session; use binaryninja::platform::Platform; @@ -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"); +}