From 8d621c51b2797fda7b1dc22243dde611cfc04f68 Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Tue, 23 Dec 2025 13:12:02 -0700 Subject: Refactor calling conventions to support correct representation of structures --- function.cpp | 169 +++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 125 insertions(+), 44 deletions(-) (limited to 'function.cpp') diff --git a/function.cpp b/function.cpp index 094a50db..bd7003e0 100644 --- a/function.cpp +++ b/function.cpp @@ -41,6 +41,24 @@ Variable Variable::FromIdentifier(uint64_t id) } +Variable Variable::Register(uint32_t reg) +{ + return Variable(RegisterVariableSourceType, reg); +} + + +Variable Variable::Flag(uint32_t flag) +{ + return Variable(FlagVariableSourceType, flag); +} + + +Variable Variable::StackOffset(int64_t offset) +{ + return Variable(StackVariableSourceType, offset); +} + + RegisterValue::RegisterValue() : state(UndeterminedValue), value(0), offset(0), size(0) {} @@ -63,6 +81,12 @@ bool RegisterValue::operator==(const RegisterValue& a) const case StackFrameOffset: return (state == StackFrameOffset) && (a.value == value); + case ResultPointerValue: + return (state == ResultPointerValue) && (a.value == value); + + case ParameterPointerValue: + return (state == ParameterPointerValue) && (a.value == value) && (a.offset == offset); + case UndeterminedValue: return state == UndeterminedValue; @@ -729,6 +753,30 @@ Confidence> Function::GetReturnType() const } +ReturnValue Function::GetReturnValue() const +{ + BNReturnValue ret = BNGetFunctionReturnValue(m_object); + ReturnValue result = ReturnValue::FromAPIObject(&ret); + BNFreeReturnValue(&ret); + return result; +} + + +bool Function::IsReturnValueDefaultLocation() const +{ + return BNIsFunctionReturnValueDefaultLocation(m_object); +} + + +Confidence Function::GetReturnValueLocation() const +{ + auto location = BNGetFunctionReturnValueLocation(m_object); + Confidence result(ValueLocation::FromAPIObject(&location.location), location.confidence); + BNFreeValueLocation(&location.location); + return result; +} + + Confidence> Function::GetReturnRegisters() const { BNRegisterSetWithConfidence regs = BNGetFunctionReturnRegisters(m_object); @@ -763,6 +811,19 @@ Confidence> Function::GetParameterVariables() const } +Confidence> Function::GetParameterLocations() const +{ + BNValueLocationListWithConfidence locations = BNGetFunctionParameterLocations(m_object); + vector locationList; + locationList.reserve(locations.count); + for (size_t i = 0; i < locations.count; i++) + locationList.push_back(ValueLocation::FromAPIObject(&locations.locations[i])); + Confidence> result(locationList, locations.confidence); + BNFreeParameterLocations(&locations); + return result; +} + + Confidence Function::HasVariableArguments() const { BNBoolWithConfidence bc = BNFunctionHasVariableArguments(m_object); @@ -817,16 +878,27 @@ void Function::SetAutoReturnType(const Confidence>& type) } -void Function::SetAutoReturnRegisters(const Confidence>& returnRegs) +void Function::SetAutoReturnValue(const ReturnValue& rv) { - BNRegisterSetWithConfidence regs; - regs.regs = new uint32_t[returnRegs.GetValue().size()]; - regs.count = returnRegs.GetValue().size(); - for (size_t i = 0; i < regs.count; i++) - regs.regs[i] = returnRegs.GetValue()[i]; - regs.confidence = returnRegs.GetConfidence(); - BNSetAutoFunctionReturnRegisters(m_object, ®s); - delete[] regs.regs; + BNReturnValue ret = rv.ToAPIObject(); + BNSetAutoFunctionReturnValue(m_object, &ret); + ReturnValue::FreeAPIObject(&ret); +} + + +void Function::SetAutoIsReturnValueDefaultLocation(bool defaultLocation) +{ + BNSetAutoIsFunctionReturnValueDefaultLocation(m_object, defaultLocation); +} + + +void Function::SetAutoReturnValueLocation(const Confidence& location) +{ + BNValueLocationWithConfidence loc; + loc.location = location->ToAPIObject(); + loc.confidence = location.GetConfidence(); + BNSetAutoFunctionReturnValueLocation(m_object, &loc); + ValueLocation::FreeAPIObject(&loc.location); } @@ -839,22 +911,21 @@ void Function::SetAutoCallingConvention(const Confidence> } -void Function::SetAutoParameterVariables(const Confidence>& vars) +void Function::SetAutoParameterLocations(const Confidence>& locations) { - BNParameterVariablesWithConfidence varConf; - varConf.vars = new BNVariable[vars->size()]; - varConf.count = vars->size(); + BNValueLocationListWithConfidence varConf; + varConf.locations = new BNValueLocation[locations->size()]; + varConf.count = locations->size(); size_t i = 0; - for (auto it = vars->begin(); it != vars->end(); ++it, ++i) - { - varConf.vars[i].type = it->type; - varConf.vars[i].index = it->index; - varConf.vars[i].storage = it->storage; - } - varConf.confidence = vars.GetConfidence(); + for (auto it = locations->begin(); it != locations->end(); ++it, ++i) + varConf.locations[i] = it->ToAPIObject(); + varConf.confidence = locations.GetConfidence(); - BNSetAutoFunctionParameterVariables(m_object, &varConf); - delete[] varConf.vars; + BNSetAutoFunctionParameterLocations(m_object, &varConf); + + for (i = 0; i < locations->size(); i++) + ValueLocation::FreeAPIObject(&varConf.locations[i]); + delete[] varConf.locations; } @@ -946,16 +1017,27 @@ void Function::SetReturnType(const Confidence>& type) } -void Function::SetReturnRegisters(const Confidence>& returnRegs) +void Function::SetReturnValue(const ReturnValue& rv) { - BNRegisterSetWithConfidence regs; - regs.regs = new uint32_t[returnRegs.GetValue().size()]; - regs.count = returnRegs.GetValue().size(); - for (size_t i = 0; i < regs.count; i++) - regs.regs[i] = returnRegs.GetValue()[i]; - regs.confidence = returnRegs.GetConfidence(); - BNSetUserFunctionReturnRegisters(m_object, ®s); - delete[] regs.regs; + BNReturnValue ret = rv.ToAPIObject(); + BNSetUserFunctionReturnValue(m_object, &ret); + ReturnValue::FreeAPIObject(&ret); +} + + +void Function::SetIsReturnValueDefaultLocation(bool defaultLocation) +{ + BNSetUserIsFunctionReturnValueDefaultLocation(m_object, defaultLocation); +} + + +void Function::SetReturnValueLocation(const Confidence& location) +{ + BNValueLocationWithConfidence loc; + loc.location = location->ToAPIObject(); + loc.confidence = location.GetConfidence(); + BNSetUserFunctionReturnValueLocation(m_object, &loc); + ValueLocation::FreeAPIObject(&loc.location); } @@ -968,22 +1050,21 @@ void Function::SetCallingConvention(const Confidence>& co } -void Function::SetParameterVariables(const Confidence>& vars) +void Function::SetParameterLocations(const Confidence>& locations) { - BNParameterVariablesWithConfidence varConf; - varConf.vars = new BNVariable[vars->size()]; - varConf.count = vars->size(); + BNValueLocationListWithConfidence varConf; + varConf.locations = new BNValueLocation[locations->size()]; + varConf.count = locations->size(); size_t i = 0; - for (auto it = vars->begin(); it != vars->end(); ++it, ++i) - { - varConf.vars[i].type = it->type; - varConf.vars[i].index = it->index; - varConf.vars[i].storage = it->storage; - } - varConf.confidence = vars.GetConfidence(); + for (auto it = locations->begin(); it != locations->end(); ++it, ++i) + varConf.locations[i] = it->ToAPIObject(); + varConf.confidence = locations.GetConfidence(); + + BNSetUserFunctionParameterLocations(m_object, &varConf); - BNSetUserFunctionParameterVariables(m_object, &varConf); - delete[] varConf.vars; + for (i = 0; i < locations->size(); i++) + ValueLocation::FreeAPIObject(&varConf.locations[i]); + delete[] varConf.locations; } -- cgit v1.3.1