From 5d66fab3c2b36a14f9efd119a818c48c0950394a Mon Sep 17 00:00:00 2001 From: Peter LaFosse Date: Tue, 18 Jan 2022 09:16:25 -0500 Subject: Make enumeration's signed property BoolWithConfidence --- binaryninjaapi.h | 8 ++++---- binaryninjacore.h | 8 ++++---- python/types.py | 4 +++- rust/src/types.rs | 9 ++++----- type.cpp | 28 ++++++++++++++++++++-------- 5 files changed, 35 insertions(+), 22 deletions(-) diff --git a/binaryninjaapi.h b/binaryninjaapi.h index e16158cc..a852290d 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -2834,8 +2834,8 @@ __attribute__ ((format (printf, 1, 2))) static Ref NamedType(const QualifiedName& name, Type* type); static Ref NamedType(const std::string& id, const QualifiedName& name, Type* type); static Ref NamedType(BinaryView* view, const QualifiedName& name); - static Ref EnumerationType(Architecture* arch, Enumeration* enm, size_t width = 0, bool isSigned = false); - static Ref EnumerationType(Enumeration* enm, size_t width, bool isSigned = false); + static Ref EnumerationType(Architecture* arch, Enumeration* enm, size_t width = 0, const Confidence& isSigned = Confidence(false, 0)); + static Ref EnumerationType(Enumeration* enm, size_t width, const Confidence& isSigned = Confidence(false, 0)); static Ref PointerType(Architecture* arch, const Confidence>& type, const Confidence& cnst = Confidence(false, 0), const Confidence& vltl = Confidence(false, 0), BNReferenceType refType = PointerReferenceType); @@ -2967,8 +2967,8 @@ __attribute__ ((format (printf, 1, 2))) static TypeBuilder NamedType(const QualifiedName& name, Type* type); static TypeBuilder NamedType(const std::string& id, const QualifiedName& name, Type* type); static TypeBuilder NamedType(BinaryView* view, const QualifiedName& name); - static TypeBuilder EnumerationType(Architecture* arch, Enumeration* enm, size_t width = 0, bool issigned = false); - static TypeBuilder EnumerationType(Architecture* arch, EnumerationBuilder* enm, size_t width = 0, bool issigned = false); + static TypeBuilder EnumerationType(Architecture* arch, Enumeration* enm, size_t width = 0, const Confidence& issigned = Confidence(false, 0)); + static TypeBuilder EnumerationType(Architecture* arch, EnumerationBuilder* enm, size_t width = 0, const Confidence& issigned = Confidence(false, 0)); static TypeBuilder PointerType(Architecture* arch, const Confidence>& type, const Confidence& cnst = Confidence(false, 0), const Confidence& vltl = Confidence(false, 0), BNReferenceType refType = PointerReferenceType); diff --git a/binaryninjacore.h b/binaryninjacore.h index f1505ad1..f1353c10 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -4757,8 +4757,8 @@ __attribute__ ((format (printf, 1, 2))) BINARYNINJACOREAPI BNType* BNCreateFloatType(size_t width, const char* altName); BINARYNINJACOREAPI BNType* BNCreateWideCharType(size_t width, const char* altName); BINARYNINJACOREAPI BNType* BNCreateStructureType(BNStructure* s); - BINARYNINJACOREAPI BNType* BNCreateEnumerationType(BNArchitecture* arch, BNEnumeration* e, size_t width, bool isSigned); - BINARYNINJACOREAPI BNType* BNCreateEnumerationTypeOfWidth(BNEnumeration* e, size_t width, bool isSigned); + BINARYNINJACOREAPI BNType* BNCreateEnumerationType(BNArchitecture* arch, BNEnumeration* e, size_t width, BNBoolWithConfidence* isSigned); + BINARYNINJACOREAPI BNType* BNCreateEnumerationTypeOfWidth(BNEnumeration* e, size_t width, BNBoolWithConfidence* isSigned); BINARYNINJACOREAPI BNType* BNCreatePointerType(BNArchitecture* arch, const BNTypeWithConfidence* const type, BNBoolWithConfidence* cnst, BNBoolWithConfidence* vltl, BNReferenceType refType); BINARYNINJACOREAPI BNType* BNCreatePointerTypeOfWidth(size_t width, const BNTypeWithConfidence* const type, @@ -4780,8 +4780,8 @@ __attribute__ ((format (printf, 1, 2))) BINARYNINJACOREAPI BNTypeBuilder* BNCreateWideCharTypeBuilder(size_t width, const char* altName); BINARYNINJACOREAPI BNTypeBuilder* BNCreateStructureTypeBuilder(BNStructure* s); BINARYNINJACOREAPI BNTypeBuilder* BNCreateStructureTypeBuilderWithBuilder(BNStructureBuilder* s); - BINARYNINJACOREAPI BNTypeBuilder* BNCreateEnumerationTypeBuilder(BNArchitecture* arch, BNEnumeration* e, size_t width, bool isSigned); - BINARYNINJACOREAPI BNTypeBuilder* BNCreateEnumerationTypeBuilderWithBuilder(BNArchitecture* arch, BNEnumerationBuilder* e, size_t width, bool isSigned); + BINARYNINJACOREAPI BNTypeBuilder* BNCreateEnumerationTypeBuilder(BNArchitecture* arch, BNEnumeration* e, size_t width, BNBoolWithConfidence* isSigned); + BINARYNINJACOREAPI BNTypeBuilder* BNCreateEnumerationTypeBuilderWithBuilder(BNArchitecture* arch, BNEnumerationBuilder* e, size_t width, BNBoolWithConfidence* isSigned); BINARYNINJACOREAPI BNTypeBuilder* BNCreatePointerTypeBuilder(BNArchitecture* arch, const BNTypeWithConfidence* const type, BNBoolWithConfidence* cnst, BNBoolWithConfidence* vltl, BNReferenceType refType); BINARYNINJACOREAPI BNTypeBuilder* BNCreatePointerTypeBuilderOfWidth(size_t width, const BNTypeWithConfidence* const type, diff --git a/python/types.py b/python/types.py index 1faec34d..8ca8e260 100644 --- a/python/types.py +++ b/python/types.py @@ -2048,7 +2048,9 @@ class EnumerationType(IntegerType): assert type_builder_handle is not None, "core.BNCreateTypeBuilderFromType returned None" enumeration_handle = core.BNGetTypeEnumeration(self._handle) assert enumeration_handle is not None, "core.BNGetTypeStructure returned None" - enumeration_builder_handle = core.BNCreateEnumerationTypeBuilder(self.platform.arch.handle, enumeration_handle, self.width, self.signed.value) + + _sign = BoolWithConfidence.get_core_struct(self.signed) + enumeration_builder_handle = core.BNCreateEnumerationTypeBuilder(self.platform.arch.handle, enumeration_handle, self.width, _sign) assert enumeration_builder_handle is not None, "core.BNCreateEnumerationTypeBuilder returned None" return EnumerationBuilder(type_builder_handle, enumeration_builder_handle, self.platform, self.confidence) diff --git a/rust/src/types.rs b/rust/src/types.rs index ec36e5d9..17721361 100644 --- a/rust/src/types.rs +++ b/rust/src/types.rs @@ -376,7 +376,7 @@ impl TypeBuilder { unsafe { Self::from_raw(BNCreateArrayTypeBuilder(&t.into().into(), count)) } } - pub fn enumeration(enumeration: &Enumeration, width: usize, is_signed: bool) -> Self { + pub fn enumeration(enumeration: &Enumeration, width: usize, is_signed: Conf) -> Self { //! The C/C++ APIs require an associated architecture, but in the core we only query the default_int_size if the given width is 0 //! For simplicity's sake, that convention isn't followed and you can query the default_int_size from an arch, if you have it, if you need to @@ -387,7 +387,7 @@ impl TypeBuilder { &mut fake_arch, enumeration.handle, width, - is_signed, + &mut is_signed.into(), )) } } @@ -727,10 +727,9 @@ impl Type { unsafe { Self::ref_from_raw(BNCreateArrayType(&t.into().into(), count)) } } - pub fn enumeration(enumeration: &Enumeration, width: usize, is_signed: bool) -> Ref { + pub fn enumeration(enumeration: &Enumeration, width: usize, is_signed: Conf) -> Ref { //! The C/C++ APIs require an associated architecture, but in the core we only query the default_int_size if the given width is 0 //! For simplicity's sake, that convention isn't followed and you can query the default_int_size from an arch, if you have it, if you need to - unsafe { // TODO : This is _extremely fragile_, we should change the internals of BNCreateEnumerationType instead of doing this let mut fake_arch: BNArchitecture = mem::zeroed(); @@ -738,7 +737,7 @@ impl Type { &mut fake_arch, enumeration.handle, width, - is_signed, + &mut is_signed.into(), )) } } diff --git a/type.cpp b/type.cpp index 6762a21b..e641b130 100644 --- a/type.cpp +++ b/type.cpp @@ -746,15 +746,21 @@ Ref Type::NamedType(BinaryView* view, const QualifiedName& name) } -Ref Type::EnumerationType(Architecture* arch, Enumeration* enm, size_t width, bool isSigned) +Ref Type::EnumerationType(Architecture* arch, Enumeration* enm, size_t width, const Confidence& isSigned) { - return new Type(BNCreateEnumerationType(arch->GetObject(), enm->GetObject(), width, isSigned)); + BNBoolWithConfidence isSignedConf; + isSignedConf.value = isSigned.GetValue(); + isSignedConf.confidence = isSigned.GetConfidence(); + return new Type(BNCreateEnumerationType(arch->GetObject(), enm->GetObject(), width, &isSignedConf)); } -Ref Type::EnumerationType(Enumeration* enm, size_t width, bool isSigned) +Ref Type::EnumerationType(Enumeration* enm, size_t width, const Confidence& isSigned) { - return new Type(BNCreateEnumerationTypeOfWidth(enm->GetObject(), width, isSigned)); + BNBoolWithConfidence isSignedConf; + isSignedConf.value = isSigned.GetValue(); + isSignedConf.confidence = isSigned.GetConfidence(); + return new Type(BNCreateEnumerationTypeOfWidth(enm->GetObject(), width, &isSignedConf)); } @@ -1428,15 +1434,21 @@ TypeBuilder TypeBuilder::NamedType(BinaryView* view, const QualifiedName& name) } -TypeBuilder TypeBuilder::EnumerationType(Architecture* arch, Enumeration* enm, size_t width, bool isSigned) +TypeBuilder TypeBuilder::EnumerationType(Architecture* arch, Enumeration* enm, size_t width, const Confidence& isSigned) { - return TypeBuilder(BNCreateEnumerationTypeBuilder(arch ? arch->GetObject() : nullptr, enm->GetObject(), width, isSigned)); + BNBoolWithConfidence isSignedConf; + isSignedConf.value = isSigned.GetValue(); + isSignedConf.confidence = isSigned.GetConfidence(); + return TypeBuilder(BNCreateEnumerationTypeBuilder(arch ? arch->GetObject() : nullptr, enm->GetObject(), width, &isSignedConf)); } -TypeBuilder TypeBuilder::EnumerationType(Architecture* arch, EnumerationBuilder* enm, size_t width, bool isSigned) +TypeBuilder TypeBuilder::EnumerationType(Architecture* arch, EnumerationBuilder* enm, size_t width, const Confidence& isSigned) { - return TypeBuilder(BNCreateEnumerationTypeBuilderWithBuilder(arch->GetObject(), enm->GetObject(), width, isSigned)); + BNBoolWithConfidence isSignedConf; + isSignedConf.value = isSigned.GetValue(); + isSignedConf.confidence = isSigned.GetConfidence(); + return TypeBuilder(BNCreateEnumerationTypeBuilderWithBuilder(arch->GetObject(), enm->GetObject(), width, &isSignedConf)); } TypeBuilder TypeBuilder::PointerType(Architecture* arch, const Confidence>& type, -- cgit v1.3.1