1414
1515//! Contains all information related to the execution environment of the binary, mainly the calling conventions used
1616
17- use std:: { borrow:: Borrow , ffi, ptr} ;
18-
1917use binaryninjacore_sys:: * ;
18+ use std:: ptr:: NonNull ;
19+ use std:: { borrow:: Borrow , ffi, ptr} ;
2020
21+ use crate :: typecontainer:: TypeContainer ;
22+ use crate :: typeparser:: { TypeParserError , TypeParserErrorSeverity , TypeParserResult } ;
2123use crate :: {
2224 architecture:: { Architecture , CoreArchitecture } ,
2325 callingconvention:: CallingConvention ,
2426 rc:: * ,
2527 string:: * ,
2628 typelibrary:: TypeLibrary ,
27- types:: { QualifiedName , QualifiedNameAndType , Type } ,
29+ types:: QualifiedNameAndType ,
2830} ;
29- use crate :: typeparser:: { TypeParserError , TypeParserErrorSeverity , TypeParserResult } ;
3031
3132#[ derive( PartialEq , Eq , Hash ) ]
3233pub struct Platform {
@@ -169,6 +170,14 @@ impl Platform {
169170 unsafe { CoreArchitecture :: from_raw ( BNGetPlatformArchitecture ( self . handle ) ) }
170171 }
171172
173+ pub fn type_container ( & self ) -> TypeContainer {
174+ let type_container_ptr = NonNull :: new ( unsafe { BNGetPlatformTypeContainer ( self . handle ) } ) ;
175+ // NOTE: I have no idea how this isn't a UAF, see the note in `TypeContainer::from_raw`
176+ // TODO: We are cloning here for platforms but we dont need to do this for [BinaryViewExt::type_container]
177+ // TODO: Why does this require that we, construct a TypeContainer, duplicate the type container, then drop the original.
178+ unsafe { TypeContainer :: from_raw ( type_container_ptr. unwrap ( ) ) . clone ( ) }
179+ }
180+
172181 pub fn get_type_libraries_by_name < T : BnStrCompatible > ( & self , name : T ) -> Array < TypeLibrary > {
173182 let mut count = 0 ;
174183 let name = name. into_bytes_with_nul ( ) ;
@@ -262,6 +271,7 @@ impl Platform {
262271 }
263272 }
264273
274+ // TODO: Documentation, specifically how this differs from the TypeParser impl
265275 pub fn preprocess_source (
266276 & self ,
267277 source : & str ,
@@ -283,21 +293,23 @@ impl Platform {
283293 include_dirs. len ( ) ,
284294 )
285295 } ;
286-
296+
287297 if success {
288298 assert ! ( !result. is_null( ) ) ;
289299 Ok ( unsafe { BnString :: from_raw ( result) } )
290300 } else {
291301 assert ! ( !error_string. is_null( ) ) ;
292302 Err ( TypeParserError :: new (
293303 TypeParserErrorSeverity :: FatalSeverity ,
294- unsafe { BnString :: from_raw ( error_string) } ,
295- file_name,
304+ unsafe { BnString :: from_raw ( error_string) } . to_string ( ) ,
305+ file_name. to_string ( ) ,
296306 0 ,
297307 0 ,
298308 ) )
299309 }
300310 }
311+
312+ // TODO: Documentation, specifically how this differs from the TypeParser impl
301313 pub fn parse_types_from_source (
302314 & self ,
303315 src : & str ,
@@ -309,14 +321,14 @@ impl Platform {
309321 let file_name_cstr = BnString :: new ( filename) ;
310322 let auto_type_source = BnString :: new ( auto_type_source) ;
311323
312- let mut result = BNTypeParserResult :: default ( ) ;
324+ let mut raw_result = BNTypeParserResult :: default ( ) ;
313325 let mut error_string = ptr:: null_mut ( ) ;
314326 let success = unsafe {
315327 BNParseTypesFromSource (
316328 self . handle ,
317329 source_cstr. as_ptr ( ) ,
318330 file_name_cstr. as_ptr ( ) ,
319- & mut result ,
331+ & mut raw_result ,
320332 & mut error_string,
321333 include_dirs. as_ptr ( ) as * mut * const ffi:: c_char ,
322334 include_dirs. len ( ) ,
@@ -325,19 +337,20 @@ impl Platform {
325337 } ;
326338
327339 if success {
328- Ok ( unsafe { TypeParserResult :: from_raw ( result ) } )
340+ Ok ( raw_result . into ( ) )
329341 } else {
330342 assert ! ( !error_string. is_null( ) ) ;
331343 Err ( TypeParserError :: new (
332344 TypeParserErrorSeverity :: FatalSeverity ,
333- unsafe { BnString :: from_raw ( error_string) } ,
334- filename,
345+ unsafe { BnString :: from_raw ( error_string) } . to_string ( ) ,
346+ filename. to_string ( ) ,
335347 0 ,
336348 0 ,
337349 ) )
338350 }
339351 }
340352
353+ // TODO: Documentation, specifically how this differs from the TypeParser impl
341354 pub fn parse_types_from_source_file (
342355 & self ,
343356 filename : & str ,
@@ -362,13 +375,13 @@ impl Platform {
362375 } ;
363376
364377 if success {
365- Ok ( unsafe { TypeParserResult :: from_raw ( result) } )
378+ Ok ( result. into ( ) )
366379 } else {
367380 assert ! ( !error_string. is_null( ) ) ;
368381 Err ( TypeParserError :: new (
369382 TypeParserErrorSeverity :: FatalSeverity ,
370- unsafe { BnString :: from_raw ( error_string) } ,
371- filename,
383+ unsafe { BnString :: from_raw ( error_string) } . to_string ( ) ,
384+ filename. to_string ( ) ,
372385 0 ,
373386 0 ,
374387 ) )
0 commit comments