Skip to content

Commit

Permalink
Merge pull request #3 from Decompollaborate/develop
Browse files Browse the repository at this point in the history
1.0.1
  • Loading branch information
AngheloAlf authored Jul 12, 2022
2 parents 937c7b7 + 9300da7 commit ebb4b84
Show file tree
Hide file tree
Showing 33 changed files with 507 additions and 220 deletions.
23 changes: 23 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
IndentWidth: 4
Language: Cpp
UseTab: Never
ColumnLimit: 160
PointerAlignment: Right
BreakBeforeBraces: Attach
SpaceAfterCStyleCast: false
Cpp11BracedListStyle: false
IndentCaseLabels: true
BinPackArguments: true
BinPackParameters: true
AlignAfterOpenBracket: Align
AlignOperands: true
BreakBeforeTernaryOperators: true
BreakBeforeBinaryOperators: None
AllowShortBlocksOnASingleLine: true
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AlignEscapedNewlines: Left
AlignTrailingComments: true
SortIncludes: false
9 changes: 9 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Checks: 'readability-*,-readability-magic-numbers,clang-diagnostic-*,clang-analyzer-*,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling,bugprone*,-bugprone-branch-clone,modernize*,performance*,portability*,diagnostic-*,analyzer-*,misc*,-misc-no-recursion'
WarningsAsErrors: ''
HeaderFilterRegex: '(src|include)\/.*\.h$'
FormatStyle: 'file'
CheckOptions:
# Require argument names to match exactly (instead of allowing a name to be a prefix/suffix of another)
# Note: 'true' is expected by clang-tidy 12+ but '1' is used for compatibility with older versions
- key: readability-inconsistent-declaration-parameter-name.Strict
value: 1
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-20.04, macos-10.15, windows-2019]
os: [ubuntu-latest, macos-latest, windows-latest]

steps:
- uses: actions/checkout@v2

- name: Build wheels
uses: pypa/cibuildwheel@v2.5.0
env:
CIBW_ARCHS_WINDOWS: "auto"
CIBW_ARCHS_LINUX: "auto"
CIBW_ARCHS_MACOS: "all"

- uses: actions/upload-artifact@v2
with:
Expand Down
84 changes: 84 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Build options can be changed by modifying the makefile or by building with 'make SETTING=value'.
DEBUG ?= 0
WERROR ?= 0
ASAN ?= 0
EXPERIMENTAL ?= 0

CC := clang
IINC := -I include
CSTD := -std=c11
CFLAGS :=
LDFLAGS :=
WARNINGS := -Wall -Wextra
# WARNINGS := -Wall -Wextra -Wpedantic -Wpadded # binary constants :s
WARNINGS += -Werror=implicit-function-declaration -Werror=incompatible-pointer-types -Werror=vla -Werror=switch -Werror=implicit-fallthrough -Werror=unused-function -Werror=unused-parameter -Werror=shadow

ifeq ($(CC),gcc)
WARNINGS += -Wno-cast-function-type
endif

ifeq ($(DEBUG),0)
OPTFLAGS := -O2 -g
else
OPTFLAGS := -O0 -g3
CFLAGS += -DDEVELOPMENT=1
endif

ifneq ($(WERROR),0)
WARNINGS += -Werror
endif

ifneq ($(ASAN),0)
CFLAGS += -fsanitize=address -fsanitize=pointer-compare -fsanitize=pointer-subtract -fsanitize=undefined
endif

ifneq ($(EXPERIMENTAL),0)
CFLAGS += -DEXPERIMENTAL
endif


SRC_DIRS := $(shell find src -type d)
C_FILES := $(foreach dir,$(SRC_DIRS),$(wildcard $(dir)/*.c))
H_FILES := $(foreach dir,$(IINC),$(wildcard $(dir)/**/*.h))
O_FILES := $(foreach f,$(C_FILES:.c=.o),build/$f)
DEP_FILES := $(O_FILES:%.o=%.d)


# create build directories
$(shell mkdir -p $(foreach dir,$(SRC_DIRS),build/$(dir)))


#### Main Targets ###

all: tests

clean:
$(RM) -rf build

distclean: clean
$(RM) -rf dist rabbitizer.egg-info .mypy_cache

format:
clang-format-11 -i -style=file $(C_FILES)

tidy:
clang-tidy-11 -p . --fix --fix-errors $(C_FILES) $(H_FILES) -- $(CSTD) $(OPTFLAGS) $(IINC) $(WARNINGS) $(CFLAGS)

tests: build/test.elf

.PHONY: all clean distclean format tidy tests
.DEFAULT_GOAL := all
.SECONDARY:


#### Various Recipes ####

build/%.elf: %.c $(O_FILES)
$(CC) -MMD $(CSTD) $(OPTFLAGS) $(IINC) $(WARNINGS) $(CFLAGS) $(LDFLAGS) -o $@ $^

build/%.o: %.c
# The -MMD flags additionaly creates a .d file with the same name as the .o file.
$(CC) -MMD -c $(CSTD) $(OPTFLAGS) $(IINC) $(WARNINGS) $(CFLAGS) -o $@ $<


-include $(DEP_FILES)
22 changes: 18 additions & 4 deletions include/analysis/RabbitizerRegistersTracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#define RABBITIZER_REGISTERS_TRACKER_H
#pragma once

#include "common/Utils.h"
#include "RabbitizerTrackedRegisterState.h"
#include "instructions/RabbitizerInstruction.h"

Expand All @@ -14,22 +15,35 @@ typedef struct RabbitizerRegistersTracker {
} RabbitizerRegistersTracker;


NON_NULL(1)
void RabbitizerRegistersTracker_init(RabbitizerRegistersTracker *self, const RabbitizerRegistersTracker *other);
NON_NULL(1)
void RabbitizerRegistersTracker_destroy(RabbitizerRegistersTracker *self);

NON_NULL(1, 2)
bool RabbitizerRegistersTracker_moveRegisters(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr);
NON_NULL(1, 2)
void RabbitizerRegistersTracker_overwriteRegisters(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int instrOffset);
NON_NULL(1, 2, 3)
void RabbitizerRegistersTracker_unsetRegistersAfterFuncCall(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, const RabbitizerInstruction *prevInstr);
bool RabbitizerRegistersTracker_getAddressIfCanSetType(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int instrOffset, uint32_t *dstAddress);
bool RabbitizerRegistersTracker_getJrInfo(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int *dstOffset, uint32_t *dstAddress);
NON_NULL(1, 2, 4)
bool RabbitizerRegistersTracker_getAddressIfCanSetType(const RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int instrOffset, uint32_t *dstAddress);
NON_NULL(1, 2, 3, 4)
bool RabbitizerRegistersTracker_getJrInfo(const RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int *dstOffset, uint32_t *dstAddress);

// prevInstr can be NULL
NON_NULL(1, 2)
void RabbitizerRegistersTracker_processLui(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int instrOffset, const RabbitizerInstruction *prevInstr);
bool RabbitizerRegistersTracker_getLuiOffsetForConstant(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int *dstOffset);
NON_NULL(1, 2, 3)
bool RabbitizerRegistersTracker_getLuiOffsetForConstant(const RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int *dstOffset);
NON_NULL(1, 2)
void RabbitizerRegistersTracker_processConstant(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, uint32_t value, int offset);
NON_NULL(1, 2, 4, 5)
bool RabbitizerRegistersTracker_getLuiOffsetForLo(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int instrOffset, int *dstOffset, bool *dstIsGp);
NON_NULL(1, 2)
void RabbitizerRegistersTracker_processLo(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, uint32_t value, int offset);
bool RabbitizerRegistersTracker_hasLoButNoHi(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr);
NON_NULL(1, 2)
bool RabbitizerRegistersTracker_hasLoButNoHi(const RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr);


#endif
14 changes: 14 additions & 0 deletions include/analysis/RabbitizerTrackedRegisterState.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <stdbool.h>
#include <stdint.h>

#include "common/Utils.h"


typedef struct RabbitizerTrackedRegisterState {
int registerNum;
Expand All @@ -25,22 +27,34 @@ typedef struct RabbitizerTrackedRegisterState {
} RabbitizerTrackedRegisterState;


NON_NULL(1)
void RabbitizerTrackedRegisterState_init(RabbitizerTrackedRegisterState *self, int registerNum);
NON_NULL(1)
void RabbitizerTrackedRegisterState_destroy(RabbitizerTrackedRegisterState *self);

NON_NULL(1)
void RabbitizerTrackedRegisterState_clear(RabbitizerTrackedRegisterState *self);
NON_NULL(1)
void RabbitizerTrackedRegisterState_clearHi(RabbitizerTrackedRegisterState *self);
NON_NULL(1)
void RabbitizerTrackedRegisterState_clearLo(RabbitizerTrackedRegisterState *self);

NON_NULL(1, 2)
void RabbitizerTrackedRegisterState_copyState(RabbitizerTrackedRegisterState *self, const RabbitizerTrackedRegisterState *other);

NON_NULL(1)
void RabbitizerTrackedRegisterState_setHi(RabbitizerTrackedRegisterState *self, uint32_t value, int offset);
NON_NULL(1)
void RabbitizerTrackedRegisterState_setLo(RabbitizerTrackedRegisterState *self, uint32_t value, int offset);

NON_NULL(1)
void RabbitizerTrackedRegisterState_deref(RabbitizerTrackedRegisterState *self, int offset);
NON_NULL(1, 2)
void RabbitizerTrackedRegisterState_dereferenceState(RabbitizerTrackedRegisterState *self, const RabbitizerTrackedRegisterState *other, int offset);

NODISCARD NON_NULL(1) PURE
bool RabbitizerTrackedRegisterState_hasAnyValue(const RabbitizerTrackedRegisterState *self);
NODISCARD NON_NULL(1) PURE
bool RabbitizerTrackedRegisterState_wasSetInCurrentOffset(const RabbitizerTrackedRegisterState *self, int offset);


Expand Down
55 changes: 48 additions & 7 deletions include/common/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,39 @@
#define __attribute__(x)
#endif

#if __STDC_VERSION__ >= 202300L
#if __STDC_VERSION__ >= 202000L
#define CONST [[gnu::const]]
#define DEPRECATED(reason) [[deprecated (reason)]]
#define FALLTHROUGH [[fallthrough]]
#define NODISCARD(reason) [[nodiscard (reason)]]
#define NODISCARD [[nodiscard]]
#define NORETURN [[noreturn]]
#define NON_NULL(...) [[gnu::nonnull (__VA_ARGS__)]]
#define PURE [[gnu::pure]]
#define RETURNS_NON_NULL [[gnu::returns_nonnull]]
#define UNUSED [[maybe_unused]]
#else
#define CONST __attribute__((const))
#define DEPRECATED(reason) __attribute__((deprecated (reason)))
#define FALLTHROUGH __attribute__((fallthrough))
#define NODISCARD(reason) __attribute__((warn_unused_result))
#define NODISCARD __attribute__((warn_unused_result))
#define NORETURN _Noreturn
#define NON_NULL(...) __attribute__((nonnull (__VA_ARGS__)))
#define PURE __attribute__((pure))
#define RETURNS_NON_NULL __attribute__((returns_nonnull))
#define UNUSED __attribute__((unused))
#endif


#if defined(_MSC_VER)
# define UNREACHABLE __assume(0)
#else
#elif defined(__GNUC__) || defined(__clang__)
# define UNREACHABLE __builtin_unreachable()
#else
# define UNREACHABLE
#endif

#define PURE __attribute__((pure))


#define ARRAY_COUNT(arr) (sizeof(arr) / sizeof(arr[0]))
#define ARRAY_COUNT(arr) (sizeof(arr) / sizeof((arr)[0]))

#define MASK(v, w) ((v) & ((1 << (w)) - 1))

Expand All @@ -58,7 +67,39 @@
#define BITREPACK_RIGHT(fullword, v, s, w) (SHIFTL((v), (s), (w)) | MASK((fullword), (s)))


#define RABUTILS_BUFFER_ADVANCE(buffer, totalSize, expression) \
do { \
size_t __tempSize = expression; \
(buffer) += __tempSize; \
(totalSize) += __tempSize; \
} while (0)

#define RABUTILS_BUFFER_WRITE_CHAR(buffer, totalSize, character) \
do { \
*(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__); \
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); \
RABUTILS_BUFFER_ADVANCE(buffer, totalSize, _tempSize); \
} while (0)


CONST NODISCARD
int32_t RabbitizerUtils_From2Complement(uint32_t number, int bits);
NON_NULL(1)
size_t RabbitizerUtils_CharFill(char *dst, int count, char fillchar);
NON_NULL(1, 3)
size_t RabbitizerUtils_escapeString(char *dst, size_t dstSize, const char *src, size_t srcSize);

#endif
25 changes: 25 additions & 0 deletions include/instructions/RabbitizerInstrDescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <stdbool.h>

#include "common/Utils.h"
#include "RabbitizerOperandType.h"
#include "RabbitizerInstrId.h"

Expand Down Expand Up @@ -64,37 +65,61 @@ typedef struct RabbitizerInstrDescriptor {
extern const RabbitizerInstrDescriptor RabbitizerInstrDescriptor_Descriptors[];


NODISCARD NON_NULL(1) PURE
bool RabbitizerInstrDescriptor_isUnknownType(const RabbitizerInstrDescriptor *self);
NODISCARD NON_NULL(1) PURE
bool RabbitizerInstrDescriptor_isJType(const RabbitizerInstrDescriptor *self);
NODISCARD NON_NULL(1) PURE
bool RabbitizerInstrDescriptor_isIType(const RabbitizerInstrDescriptor *self);
NODISCARD NON_NULL(1) PURE
bool RabbitizerInstrDescriptor_isRType(const RabbitizerInstrDescriptor *self);
NODISCARD NON_NULL(1) PURE
bool RabbitizerInstrDescriptor_isRegimmType(const RabbitizerInstrDescriptor *self);

NODISCARD NON_NULL(1) PURE
bool RabbitizerInstrDescriptor_isBranch(const RabbitizerInstrDescriptor *self);
NODISCARD NON_NULL(1) PURE
bool RabbitizerInstrDescriptor_isBranchLikely(const RabbitizerInstrDescriptor *self);
NODISCARD NON_NULL(1) PURE
bool RabbitizerInstrDescriptor_isJump(const RabbitizerInstrDescriptor *self);
NODISCARD NON_NULL(1) PURE
bool RabbitizerInstrDescriptor_isTrap(const RabbitizerInstrDescriptor *self);

NODISCARD NON_NULL(1) PURE
bool RabbitizerInstrDescriptor_isFloat(const RabbitizerInstrDescriptor *self);
NODISCARD NON_NULL(1) PURE
bool RabbitizerInstrDescriptor_isDouble(const RabbitizerInstrDescriptor *self);

NODISCARD NON_NULL(1) PURE
bool RabbitizerInstrDescriptor_isUnsigned(const RabbitizerInstrDescriptor *self);

NODISCARD NON_NULL(1) PURE
bool RabbitizerInstrDescriptor_modifiesRt(const RabbitizerInstrDescriptor *self);
NODISCARD NON_NULL(1) PURE
bool RabbitizerInstrDescriptor_modifiesRd(const RabbitizerInstrDescriptor *self);

NODISCARD NON_NULL(1) PURE
bool RabbitizerInstrDescriptor_notEmitedByCompilers(const RabbitizerInstrDescriptor *self);

NODISCARD NON_NULL(1) PURE
bool RabbitizerInstrDescriptor_canBeHi(const RabbitizerInstrDescriptor *self);
NODISCARD NON_NULL(1) PURE
bool RabbitizerInstrDescriptor_canBeLo(const RabbitizerInstrDescriptor *self);
NODISCARD NON_NULL(1) PURE
bool RabbitizerInstrDescriptor_doesLink(const RabbitizerInstrDescriptor *self);
NODISCARD NON_NULL(1) PURE
bool RabbitizerInstrDescriptor_doesDereference(const RabbitizerInstrDescriptor *self);
NODISCARD NON_NULL(1) PURE
bool RabbitizerInstrDescriptor_doesLoad(const RabbitizerInstrDescriptor *self);
NODISCARD NON_NULL(1) PURE
bool RabbitizerInstrDescriptor_doesStore(const RabbitizerInstrDescriptor *self);
NODISCARD NON_NULL(1) PURE
bool RabbitizerInstrDescriptor_maybeIsMove(const RabbitizerInstrDescriptor *self);

NODISCARD NON_NULL(1) PURE
bool RabbitizerInstrDescriptor_isPseudo(const RabbitizerInstrDescriptor *self);

NODISCARD NON_NULL(1) PURE
RabbitizerArchitectureVersion RabbitizerInstrDescriptor_getArchitectureVersion(const RabbitizerInstrDescriptor *self);

#endif
Loading

0 comments on commit ebb4b84

Please sign in to comment.