mirror of
https://github.com/Decompollaborate/rabbitizer.git
synced 2025-01-13 09:38:55 +00:00
Avoid heap-allocating memory for calculating required buffer size on RabbitizerOperandType_getBufferSize
This commit is contained in:
parent
5f4ff4d2e5
commit
f5d811f18e
@ -10,6 +10,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
- Consider r5900's `paddub` as a possible move instruction.
|
- Consider r5900's `paddub` as a possible move instruction.
|
||||||
|
- Internal rework to avoid allocating memory when calculating required buffer
|
||||||
|
size for disassembly.
|
||||||
|
- This is part of the `RabbitizerInstruction_getSizeForBuffer` function.
|
||||||
|
- This change may help recent Windows specific issues.
|
||||||
|
|
||||||
## [1.9.4] - 2024-03-18
|
## [1.9.4] - 2024-03-18
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "rabbitizer"
|
name = "rabbitizer"
|
||||||
# Version should be synced with include/common/RabbitizerVersion.h
|
# Version should be synced with include/common/RabbitizerVersion.h
|
||||||
version = "1.9.4"
|
version = "1.9.5"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = ["Anghelo Carvajal <angheloalf95@gmail.com>"]
|
authors = ["Anghelo Carvajal <angheloalf95@gmail.com>"]
|
||||||
description = "MIPS instruction decoder"
|
description = "MIPS instruction decoder"
|
||||||
|
1
Makefile
1
Makefile
@ -22,6 +22,7 @@ WARNINGS += -Wformat=2 -Wundef
|
|||||||
WARNINGS += -Werror=vla -Werror=switch -Werror=implicit-fallthrough -Werror=unused-function
|
WARNINGS += -Werror=vla -Werror=switch -Werror=implicit-fallthrough -Werror=unused-function
|
||||||
WARNINGS += -Werror=unused-parameter -Werror=shadow -Werror=switch -Werror=double-promotion
|
WARNINGS += -Werror=unused-parameter -Werror=shadow -Werror=switch -Werror=double-promotion
|
||||||
WARNINGS_C := -Werror=implicit-function-declaration -Werror=incompatible-pointer-types
|
WARNINGS_C := -Werror=implicit-function-declaration -Werror=incompatible-pointer-types
|
||||||
|
WARNINGS_C += -Wno-nonnull-compare
|
||||||
WARNINGS += -Werror=type-limits
|
WARNINGS += -Werror=type-limits
|
||||||
WARNINGS_CXX :=
|
WARNINGS_CXX :=
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ extern "C" {
|
|||||||
// Header version
|
// Header version
|
||||||
#define RAB_VERSION_MAJOR 1
|
#define RAB_VERSION_MAJOR 1
|
||||||
#define RAB_VERSION_MINOR 9
|
#define RAB_VERSION_MINOR 9
|
||||||
#define RAB_VERSION_PATCH 4
|
#define RAB_VERSION_PATCH 5
|
||||||
|
|
||||||
#define RAB_VERSION_STR RAB_STRINGIFY(RAB_VERSION_MAJOR) "." RAB_STRINGIFY(RAB_VERSION_MINOR) "." RAB_STRINGIFY(RAB_VERSION_PATCH)
|
#define RAB_VERSION_STR RAB_STRINGIFY(RAB_VERSION_MAJOR) "." RAB_STRINGIFY(RAB_VERSION_MINOR) "." RAB_STRINGIFY(RAB_VERSION_PATCH)
|
||||||
|
|
||||||
|
@ -84,19 +84,28 @@ typedef enum RabTrinaryValue {
|
|||||||
#define RABUTILS_BUFFER_ADVANCE(buffer, totalSize, expression) \
|
#define RABUTILS_BUFFER_ADVANCE(buffer, totalSize, expression) \
|
||||||
do { \
|
do { \
|
||||||
size_t __tempSize = (size_t)(expression); \
|
size_t __tempSize = (size_t)(expression); \
|
||||||
(buffer) += __tempSize; \
|
if (buffer != NULL) { \
|
||||||
|
(buffer) += __tempSize; \
|
||||||
|
} \
|
||||||
(totalSize) += __tempSize; \
|
(totalSize) += __tempSize; \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
#define RABUTILS_BUFFER_WRITE_CHAR(buffer, totalSize, character) \
|
#define RABUTILS_BUFFER_WRITE_CHAR(buffer, totalSize, character) \
|
||||||
do { \
|
do { \
|
||||||
*(buffer) = (character); \
|
if (buffer != NULL) { \
|
||||||
|
*(buffer) = (character); \
|
||||||
|
} \
|
||||||
RABUTILS_BUFFER_ADVANCE(buffer, totalSize, 1); \
|
RABUTILS_BUFFER_ADVANCE(buffer, totalSize, 1); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
#define RABUTILS_BUFFER_SPRINTF(buffer, totalSize, format, ...) \
|
#define RABUTILS_BUFFER_SPRINTF(buffer, totalSize, format, ...) \
|
||||||
do { \
|
do { \
|
||||||
int _len = sprintf(buffer, format, __VA_ARGS__); \
|
int _len; \
|
||||||
|
if (buffer != NULL) { \
|
||||||
|
_len = sprintf(buffer, format, __VA_ARGS__); \
|
||||||
|
} else { \
|
||||||
|
_len = snprintf(NULL, 0, format, __VA_ARGS__); \
|
||||||
|
} \
|
||||||
assert(_len > 0); \
|
assert(_len > 0); \
|
||||||
RABUTILS_BUFFER_ADVANCE(buffer, totalSize, _len); \
|
RABUTILS_BUFFER_ADVANCE(buffer, totalSize, _len); \
|
||||||
} while (0)
|
} while (0)
|
||||||
@ -104,7 +113,9 @@ typedef enum RabTrinaryValue {
|
|||||||
#define RABUTILS_BUFFER_CPY(buffer, totalSize, string) \
|
#define RABUTILS_BUFFER_CPY(buffer, totalSize, string) \
|
||||||
do { \
|
do { \
|
||||||
size_t _tempSize = strlen(string); \
|
size_t _tempSize = strlen(string); \
|
||||||
memcpy(buffer, string, _tempSize); \
|
if (buffer != NULL) { \
|
||||||
|
memcpy(buffer, string, _tempSize); \
|
||||||
|
} \
|
||||||
RABUTILS_BUFFER_ADVANCE(buffer, totalSize, _tempSize); \
|
RABUTILS_BUFFER_ADVANCE(buffer, totalSize, _tempSize); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "rabbitizer"
|
name = "rabbitizer"
|
||||||
# Version should be synced with include/common/RabbitizerVersion.h
|
# Version should be synced with include/common/RabbitizerVersion.h
|
||||||
version = "1.9.4"
|
version = "1.9.5"
|
||||||
description = "MIPS instruction decoder"
|
description = "MIPS instruction decoder"
|
||||||
# license = "MIT"
|
# license = "MIT"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
1
setup.py
1
setup.py
@ -17,6 +17,7 @@ if platform.system() == "Linux":
|
|||||||
extraCompileArgs += ["-Werror=vla", "-Werror=switch", "-Werror=implicit-fallthrough", "-Werror=unused-function", "-Werror=unused-parameter", "-Werror=shadow", "-Werror=switch"]
|
extraCompileArgs += ["-Werror=vla", "-Werror=switch", "-Werror=implicit-fallthrough", "-Werror=unused-function", "-Werror=unused-parameter", "-Werror=shadow", "-Werror=switch"]
|
||||||
extraCompileArgs += ["-Werror=implicit-function-declaration", "-Werror=incompatible-pointer-types"]
|
extraCompileArgs += ["-Werror=implicit-function-declaration", "-Werror=incompatible-pointer-types"]
|
||||||
extraCompileArgs += ["-Werror"]
|
extraCompileArgs += ["-Werror"]
|
||||||
|
extraCompileArgs += ["-Wno-nonnull-compare"]
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
ext_modules=[
|
ext_modules=[
|
||||||
|
@ -10,10 +10,7 @@
|
|||||||
|
|
||||||
size_t RabbitizerOperandType_getBufferSize(RabbitizerOperandType operand, const RabbitizerInstruction *instr,
|
size_t RabbitizerOperandType_getBufferSize(RabbitizerOperandType operand, const RabbitizerInstruction *instr,
|
||||||
size_t immOverrideLength) {
|
size_t immOverrideLength) {
|
||||||
char *auxBuffer = calloc(immOverrideLength * 2 + 2, sizeof(char));
|
|
||||||
char *immOverride = calloc(immOverrideLength + 2, sizeof(char));
|
|
||||||
OperandCallback callback;
|
OperandCallback callback;
|
||||||
size_t size;
|
|
||||||
|
|
||||||
assert(operand > RAB_OPERAND_ALL_INVALID);
|
assert(operand > RAB_OPERAND_ALL_INVALID);
|
||||||
assert(operand < RAB_OPERAND_ALL_MAX);
|
assert(operand < RAB_OPERAND_ALL_MAX);
|
||||||
@ -21,12 +18,7 @@ size_t RabbitizerOperandType_getBufferSize(RabbitizerOperandType operand, const
|
|||||||
callback = instrOpercandCallbacks[operand];
|
callback = instrOpercandCallbacks[operand];
|
||||||
assert(callback != NULL);
|
assert(callback != NULL);
|
||||||
|
|
||||||
size = callback(instr, auxBuffer, immOverride, immOverrideLength);
|
return callback(instr, NULL, NULL, immOverrideLength);
|
||||||
|
|
||||||
free(auxBuffer);
|
|
||||||
free(immOverride);
|
|
||||||
|
|
||||||
return size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t RabbitizerInstruction_getSizeForBufferOperandsDisasm(const RabbitizerInstruction *self,
|
size_t RabbitizerInstruction_getSizeForBufferOperandsDisasm(const RabbitizerInstruction *self,
|
||||||
|
@ -195,6 +195,10 @@ size_t RabbitizerOperandType_process_cpu_label(const RabbitizerInstruction *self
|
|||||||
size_t immOverrideLength) {
|
size_t immOverrideLength) {
|
||||||
size_t totalSize = 0;
|
size_t totalSize = 0;
|
||||||
|
|
||||||
|
if ((dst == NULL) && (immOverrideLength > 0)) {
|
||||||
|
return immOverrideLength;
|
||||||
|
}
|
||||||
|
|
||||||
if ((immOverride != NULL) && (immOverrideLength > 0)) {
|
if ((immOverride != NULL) && (immOverrideLength > 0)) {
|
||||||
memcpy(dst, immOverride, immOverrideLength);
|
memcpy(dst, immOverride, immOverrideLength);
|
||||||
return immOverrideLength;
|
return immOverrideLength;
|
||||||
@ -209,6 +213,10 @@ size_t RabbitizerOperandType_process_cpu_immediate(const RabbitizerInstruction *
|
|||||||
size_t totalSize = 0;
|
size_t totalSize = 0;
|
||||||
int32_t number;
|
int32_t number;
|
||||||
|
|
||||||
|
if ((dst == NULL) && (immOverrideLength > 0)) {
|
||||||
|
return immOverrideLength;
|
||||||
|
}
|
||||||
|
|
||||||
if ((immOverride != NULL) && (immOverrideLength > 0)) {
|
if ((immOverride != NULL) && (immOverrideLength > 0)) {
|
||||||
memcpy(dst, immOverride, immOverrideLength);
|
memcpy(dst, immOverride, immOverrideLength);
|
||||||
return immOverrideLength;
|
return immOverrideLength;
|
||||||
@ -241,6 +249,10 @@ size_t RabbitizerOperandType_process_cpu_branch_target_label(const RabbitizerIns
|
|||||||
const char *immOverride, size_t immOverrideLength) {
|
const char *immOverride, size_t immOverrideLength) {
|
||||||
size_t totalSize = 0;
|
size_t totalSize = 0;
|
||||||
|
|
||||||
|
if ((dst == NULL) && (immOverrideLength > 0)) {
|
||||||
|
return immOverrideLength;
|
||||||
|
}
|
||||||
|
|
||||||
if ((immOverride != NULL) && (immOverrideLength > 0)) {
|
if ((immOverride != NULL) && (immOverrideLength > 0)) {
|
||||||
memcpy(dst, immOverride, immOverrideLength);
|
memcpy(dst, immOverride, immOverrideLength);
|
||||||
return immOverrideLength;
|
return immOverrideLength;
|
||||||
|
@ -492,6 +492,10 @@ size_t RabbitizerOperandType_process_r5900_immediate5(const RabbitizerInstructio
|
|||||||
size_t totalSize = 0;
|
size_t totalSize = 0;
|
||||||
int32_t number;
|
int32_t number;
|
||||||
|
|
||||||
|
if ((dst == NULL) && (immOverrideLength > 0)) {
|
||||||
|
return immOverrideLength;
|
||||||
|
}
|
||||||
|
|
||||||
if ((immOverride != NULL) && (immOverrideLength > 0)) {
|
if ((immOverride != NULL) && (immOverrideLength > 0)) {
|
||||||
memcpy(dst, immOverride, immOverrideLength);
|
memcpy(dst, immOverride, immOverrideLength);
|
||||||
return immOverrideLength;
|
return immOverrideLength;
|
||||||
@ -525,6 +529,10 @@ size_t RabbitizerOperandType_process_r5900_immediate15(const RabbitizerInstructi
|
|||||||
size_t totalSize = 0;
|
size_t totalSize = 0;
|
||||||
int32_t number;
|
int32_t number;
|
||||||
|
|
||||||
|
if ((dst == NULL) && (immOverrideLength > 0)) {
|
||||||
|
return immOverrideLength;
|
||||||
|
}
|
||||||
|
|
||||||
if ((immOverride != NULL) && (immOverrideLength > 0)) {
|
if ((immOverride != NULL) && (immOverrideLength > 0)) {
|
||||||
memcpy(dst, immOverride, immOverrideLength);
|
memcpy(dst, immOverride, immOverrideLength);
|
||||||
return immOverrideLength;
|
return immOverrideLength;
|
||||||
|
@ -115,7 +115,7 @@ int main() {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
fread(buffer, sizeof(char), fileSize, file);
|
assert(fread(buffer, sizeof(char), fileSize, file) == (size_t)fileSize);
|
||||||
|
|
||||||
errorCount += doVersionCheck(path, buffer);
|
errorCount += doVersionCheck(path, buffer);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user