Skip to content

Commit

Permalink
Merge pull request #3044 from torusrxxx/patch000000d9
Browse files Browse the repository at this point in the history
Misc Maintenance
  • Loading branch information
mrexodia committed Apr 5, 2023
2 parents 93ad736 + 096db3b commit 55930a5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 55 deletions.
91 changes: 43 additions & 48 deletions src/dbg/stringformat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,90 +23,85 @@ enum class StringValueType
FloatingPointDouble
};

template<class T> String printFloatValue(FormatValueType value)
// get an offset of REGDUMP structure, or 0 when the input is not an SSE register.
// value: string like "xmm0"
// elementSize: 4 or 8 for float and double respectively
static size_t getSSERegisterOffset(FormatValueType value, size_t elementSize)
{
static_assert(std::is_same<T, double>::value || std::is_same<T, float>::value, "This function is used to print float and double values.");
String result;
char buf[16]; // a safe buffer with sufficient length to prevent buffer overflow while parsing
memset(buf, 0, sizeof(buf));
strcpy_s(buf, value); // copy value into buf
_strlwr_s(buf); // convert "XMM" to "xmm"
size_t offset = 0;
bool bad = false;
if(buf[1] == 'm' && buf[2] == 'm' && (buf[0] == 'x' || buf[0] == 'y')) // begins with /[xy]mm/
if(buf[1] == 'm' && buf[2] == 'm' && (buf[0] == 'x' || buf[0] == 'y')) // begins with /[xy]mm/
{
int index = 0; // the index of XMM/YMM register
int bufptr = 0; // where is the character after the XMM register string
if(buf[3] >= '0' && buf[3] <= '9' && buf[4] >= '0' && buf[4] <= '9')
{
index = (buf[3] - '0') * 10 + (buf[4] - '0'); // convert "10" to 10
if(index >= ArchValue(8, 16)) // limit to available XMM registers (32bit: XMM0~XMM7, 64bit: XMM0~XMM15)
bad = true;
if(index >= ArchValue(8, 16)) // limit to available XMM registers (32bit: XMM0~XMM7, 64bit: XMM0~XMM15)
return 0;
bufptr = 5;
}
else if(buf[3] >= '0' && buf[3] <= '9')
{
index = buf[3] - '0'; // convert "7" to 7
if(index >= ArchValue(8, 16)) // limit to available XMM registers (32bit: XMM0~XMM7, 64bit: XMM0~XMM15)
bad = true;
if(index >= ArchValue(8, 16)) // limit to available XMM registers (32bit: XMM0~XMM7, 64bit: XMM0~XMM15)
return 0;
bufptr = 4;
}
else
bad = true;
if(!bad)
return 0; // return value 0 is EAX which is not an SSE register, and represents in general the input value is not an SSE register.

if(buf[bufptr] == '\0') // [xy]mm\d{1,2}
return offsetof(REGDUMP, regcontext.XmmRegisters[index]);
else if(elementSize == 8 && buf[0] == 'x' && buf[bufptr] == 'h' && buf[bufptr + 1] == '\0') // xmm\d{1,2}h
return offsetof(REGDUMP, regcontext.XmmRegisters[index].High);
else if(buf[bufptr] == '[')
{
if(buf[bufptr] == '\0') // [xy]mm\d{1,2}
offset = offsetof(REGDUMP, regcontext.XmmRegisters[index]);
else if(std::is_same<T, double>() && buf[0] == 'x' && buf[bufptr] == 'h' && buf[bufptr + 1] == '\0') // xmm\d{1,2}h
offset = offsetof(REGDUMP, regcontext.XmmRegisters[index].High);
else if(buf[bufptr] == '[')
if(buf[bufptr + 1] >= '0' && buf[bufptr + 1] <= '9' && buf[bufptr + 2] == ']' && buf[bufptr + 3] == '\0') // [xy]mm\d{1,2}\[\d\]
{
if(buf[bufptr + 1] >= '0' && buf[bufptr + 1] <= '9' && buf[bufptr + 2] == ']' && buf[bufptr + 3] == '\0') // [xy]mm\d{1,2}\[\d\]
{
int item = buf[bufptr + 1] - '0';
if(buf[0] == 'x' && item >= 0 && item < 16 / sizeof(T)) // xmm
offset = offsetof(REGDUMP, regcontext.XmmRegisters[index]) + item * sizeof(T);
else if(buf[0] == 'y' && item >= 0 && item < 32 / sizeof(T)) // ymm
offset = offsetof(REGDUMP, regcontext.YmmRegisters[index]) + item * sizeof(T);
else
bad = true;
}
int item = buf[bufptr + 1] - '0';
if(buf[0] == 'x' && item >= 0 && item < 16 / elementSize) // xmm
return offsetof(REGDUMP, regcontext.XmmRegisters[index]) + item * elementSize;
else if(buf[0] == 'y' && item >= 0 && item < 32 / elementSize) // ymm
return offsetof(REGDUMP, regcontext.YmmRegisters[index]) + item * elementSize;
else
bad = true;
return 0;
}
else
bad = true;
return 0;
}
else
return 0;
}
else
bad = true; // TO DO: ST(...)
return 0; // TO DO: ST(...)
}

template<class T> String printFloatValue(FormatValueType value)
{
static_assert(std::is_same<T, double>::value || std::is_same<T, float>::value, "This function is used to print float and double values.");
size_t offset = getSSERegisterOffset(value, sizeof(T));
REGDUMP registers;
if(!bad) // prints an FPU register
T data;
if(offset != 0) // prints an FPU register
{
assert((offset + sizeof(T)) <= sizeof(REGDUMP));
if(DbgGetRegDumpEx(&registers, sizeof(registers)))
{
T* ptr = (T*)((char*)&registers + offset);
std::stringstream wFloatingStr;
wFloatingStr << std::setprecision(std::numeric_limits<T>::digits10) << *ptr;
result = wFloatingStr.str();
}
data = *(T*)((char*)&registers + offset);
else
result = "???";
return "???";
}
else // prints a memory pointer
{
T data;
duint valuint = 0;
if(valfromstring(value, &valuint) && DbgMemRead(valuint, &data, sizeof(data)))
{
std::stringstream wFloatingStr;
wFloatingStr << std::setprecision(std::numeric_limits<T>::digits10) << data;
result = wFloatingStr.str();
}
else
result = "???";
if(!(valfromstring(value, &valuint) && DbgMemRead(valuint, &data, sizeof(data))))
return "???";
}
return result;
std::stringstream wFloatingStr;
wFloatingStr << std::setprecision(std::numeric_limits<T>::digits10) << data;
return wFloatingStr.str();
}

static String printValue(FormatValueType value, StringValueType type)
Expand Down
14 changes: 8 additions & 6 deletions src/dbg/variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,9 @@ bool varset(const char* Name, const char* Value, bool ReadOnly)
\brief Deletes a variable.
\param Name The name of the variable to delete. Cannot be null.
\param DelSystem true to allow deleting system variables.
\return true if the variable was deleted successfully, false otherwise.
\return 0 if the variable was deleted successfully, -1 when variable doesn't exist, -2 when a user could not delete a system variable, -3 when unknown reason caused a variable couldn't be deleted
*/
bool vardel(const char* Name, bool DelSystem)
int vardel(const char* Name, bool DelSystem)
{
EXCLUSIVE_ACQUIRE(LockVariables);

Expand All @@ -309,7 +309,7 @@ bool vardel(const char* Name, bool DelSystem)
name_ += Name;
auto found = variables.find(name_);
if(found == variables.end()) //not found
return false;
return -1;
if(found->second.alias.length())
{
// Release the lock (potential deadlock here)
Expand All @@ -319,19 +319,21 @@ bool vardel(const char* Name, bool DelSystem)
}

if(!DelSystem && found->second.type != VAR_USER)
return false;
return -2;
found = variables.begin();
String NameString(Name);
String NameString(name_);
bool deleted = false;
while(found != variables.end())
{
if(found->first == NameString || found->second.alias == NameString)
{
found = variables.erase(found); // Invalidate iterators
deleted = true;
}
else
found++;
}
return true;
return deleted ? 0 : -3; //We should have deleted a variable, failing at here is a bug
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/dbg/variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ bool varget(const char* Name, duint* Value, int* Size, VAR_TYPE* Type);
bool varget(const char* Name, char* String, int* Size, VAR_TYPE* Type);
bool varset(const char* Name, duint Value, bool ReadOnly);
bool varset(const char* Name, const char* Value, bool ReadOnly);
bool vardel(const char* Name, bool DelSystem);
int vardel(const char* Name, bool DelSystem);
bool vargettype(const char* Name, VAR_TYPE* Type = nullptr, VAR_VALUE_TYPE* ValueType = nullptr);
bool varenum(VAR* List, size_t* Size);

Expand Down
1 change: 1 addition & 0 deletions src/gui/Src/Gui/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class QMutex;
class QDragEnterEvent;
class QDropEvent;
class QMutex;
class CloseDialog;
class CommandLineEdit;
class MHTabWidget;
Expand Down

0 comments on commit 55930a5

Please sign in to comment.