Skip to content

Commit

Permalink
Avoid heap-allocating memory for calculating required buffer size on …
Browse files Browse the repository at this point in the history
…`RabbitizerOperandType_getBufferSize`
  • Loading branch information
AngheloAlf committed Apr 3, 2024
1 parent 5f4ff4d commit f5d811f
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- 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

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[package]
name = "rabbitizer"
# Version should be synced with include/common/RabbitizerVersion.h
version = "1.9.4"
version = "1.9.5"
edition = "2021"
authors = ["Anghelo Carvajal <[email protected]>"]
description = "MIPS instruction decoder"
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ WARNINGS += -Wformat=2 -Wundef
WARNINGS += -Werror=vla -Werror=switch -Werror=implicit-fallthrough -Werror=unused-function
WARNINGS += -Werror=unused-parameter -Werror=shadow -Werror=switch -Werror=double-promotion
WARNINGS_C := -Werror=implicit-function-declaration -Werror=incompatible-pointer-types
WARNINGS_C += -Wno-nonnull-compare
WARNINGS += -Werror=type-limits
WARNINGS_CXX :=

Expand Down
2 changes: 1 addition & 1 deletion include/common/RabbitizerVersion.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extern "C" {
// Header version
#define RAB_VERSION_MAJOR 1
#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)

Expand Down
19 changes: 15 additions & 4 deletions include/common/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,27 +84,38 @@ typedef enum RabTrinaryValue {
#define RABUTILS_BUFFER_ADVANCE(buffer, totalSize, expression) \
do { \
size_t __tempSize = (size_t)(expression); \
(buffer) += __tempSize; \
if (buffer != NULL) { \
(buffer) += __tempSize; \
} \
(totalSize) += __tempSize; \
} while (0)

#define RABUTILS_BUFFER_WRITE_CHAR(buffer, totalSize, character) \
do { \
*(buffer) = (character); \
if (buffer != NULL) { \
*(buffer) = (character); \
} \
RABUTILS_BUFFER_ADVANCE(buffer, totalSize, 1); \
} while (0)

#define RABUTILS_BUFFER_SPRINTF(buffer, totalSize, format, ...) \
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); \
RABUTILS_BUFFER_ADVANCE(buffer, totalSize, _len); \
} while (0)

#define RABUTILS_BUFFER_CPY(buffer, totalSize, string) \
do { \
size_t _tempSize = strlen(string); \
memcpy(buffer, string, _tempSize); \
if (buffer != NULL) { \
memcpy(buffer, string, _tempSize); \
} \
RABUTILS_BUFFER_ADVANCE(buffer, totalSize, _tempSize); \
} while (0)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[project]
name = "rabbitizer"
# Version should be synced with include/common/RabbitizerVersion.h
version = "1.9.4"
version = "1.9.5"
description = "MIPS instruction decoder"
# license = "MIT"
readme = "README.md"
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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"]
extraCompileArgs += ["-Wno-nonnull-compare"]

setup(
ext_modules=[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,15 @@

size_t RabbitizerOperandType_getBufferSize(RabbitizerOperandType operand, const RabbitizerInstruction *instr,
size_t immOverrideLength) {
char *auxBuffer = calloc(immOverrideLength * 2 + 2, sizeof(char));
char *immOverride = calloc(immOverrideLength + 2, sizeof(char));
OperandCallback callback;
size_t size;

assert(operand > RAB_OPERAND_ALL_INVALID);
assert(operand < RAB_OPERAND_ALL_MAX);

callback = instrOpercandCallbacks[operand];
assert(callback != NULL);

size = callback(instr, auxBuffer, immOverride, immOverrideLength);

free(auxBuffer);
free(immOverride);

return size;
return callback(instr, NULL, NULL, immOverrideLength);
}

size_t RabbitizerInstruction_getSizeForBufferOperandsDisasm(const RabbitizerInstruction *self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ size_t RabbitizerOperandType_process_cpu_label(const RabbitizerInstruction *self
size_t immOverrideLength) {
size_t totalSize = 0;

if ((dst == NULL) && (immOverrideLength > 0)) {
return immOverrideLength;
}

if ((immOverride != NULL) && (immOverrideLength > 0)) {
memcpy(dst, immOverride, immOverrideLength);
return immOverrideLength;
Expand All @@ -209,6 +213,10 @@ size_t RabbitizerOperandType_process_cpu_immediate(const RabbitizerInstruction *
size_t totalSize = 0;
int32_t number;

if ((dst == NULL) && (immOverrideLength > 0)) {
return immOverrideLength;
}

if ((immOverride != NULL) && (immOverrideLength > 0)) {
memcpy(dst, immOverride, immOverrideLength);
return immOverrideLength;
Expand Down Expand Up @@ -241,6 +249,10 @@ size_t RabbitizerOperandType_process_cpu_branch_target_label(const RabbitizerIns
const char *immOverride, size_t immOverrideLength) {
size_t totalSize = 0;

if ((dst == NULL) && (immOverrideLength > 0)) {
return immOverrideLength;
}

if ((immOverride != NULL) && (immOverrideLength > 0)) {
memcpy(dst, immOverride, immOverrideLength);
return immOverrideLength;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,10 @@ size_t RabbitizerOperandType_process_r5900_immediate5(const RabbitizerInstructio
size_t totalSize = 0;
int32_t number;

if ((dst == NULL) && (immOverrideLength > 0)) {
return immOverrideLength;
}

if ((immOverride != NULL) && (immOverrideLength > 0)) {
memcpy(dst, immOverride, immOverrideLength);
return immOverrideLength;
Expand Down Expand Up @@ -525,6 +529,10 @@ size_t RabbitizerOperandType_process_r5900_immediate15(const RabbitizerInstructi
size_t totalSize = 0;
int32_t number;

if ((dst == NULL) && (immOverrideLength > 0)) {
return immOverrideLength;
}

if ((immOverride != NULL) && (immOverrideLength > 0)) {
memcpy(dst, immOverride, immOverrideLength);
return immOverrideLength;
Expand Down
2 changes: 1 addition & 1 deletion tests/c/build_info_checks/version_number.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ int main() {
continue;
}

fread(buffer, sizeof(char), fileSize, file);
assert(fread(buffer, sizeof(char), fileSize, file) == (size_t)fileSize);

errorCount += doVersionCheck(path, buffer);

Expand Down

0 comments on commit f5d811f

Please sign in to comment.