From a6abbb818c3c523dfb806cf4e8a0211f3a8d56e4 Mon Sep 17 00:00:00 2001 From: 2over12 Date: Mon, 10 Jul 2023 12:07:42 -0400 Subject: [PATCH] Mirror Sleigh's Byte-width Flags (#668) * add sleigh state * remove bad include * add volatile * dont run tests in docker deploys * remove ccache * Revert "remove ccache" This reverts commit 221b70a1bc81805c1d56442c5bb65c54d1c55bb3. * Revert "dont run tests in docker deploys" This reverts commit 72229acd6e27d53971c2881578e5b54f092d16ce. * disable packaging in dockerfile * fix block packagingA * fix syntax --- Dockerfile | 3 +- include/remill/Arch/AArch64/Runtime/State.h | 31 ++++++++++++++++++++- lib/Arch/Sleigh/AArch64Arch.cpp | 30 ++++++++++++++++++-- lib/Arch/Sleigh/AArch64Arch.h | 2 ++ scripts/build.sh | 20 ++++++++++--- 5 files changed, 78 insertions(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index 064f6224..83575278 100644 --- a/Dockerfile +++ b/Dockerfile @@ -36,7 +36,8 @@ RUN git config --global user.email "41898282+github-actions[bot]@users.noreply.g RUN ./scripts/build.sh \ --llvm-version ${LLVM_VERSION} \ --prefix /opt/trailofbits \ - --extra-cmake-args "-DCMAKE_BUILD_TYPE=Release" + --extra-cmake-args "-DCMAKE_BUILD_TYPE=Release" \ + --disable-package RUN pip3 install ./scripts/diff_tester_export_insns diff --git a/include/remill/Arch/AArch64/Runtime/State.h b/include/remill/Arch/AArch64/Runtime/State.h index 18dd7b17..b9ee995d 100644 --- a/include/remill/Arch/AArch64/Runtime/State.h +++ b/include/remill/Arch/AArch64/Runtime/State.h @@ -279,6 +279,31 @@ struct alignas(16) SIMD { static_assert(512 == sizeof(SIMD), "Invalid packing of `struct SIMD`."); +struct alignas(8) SleighFlagState { + uint8_t NG; + volatile uint8_t _1; + uint8_t ZR; + volatile uint8_t _2; + uint8_t CY; + volatile uint8_t _3; + uint8_t OV; + volatile uint8_t _4; + uint8_t shift_carry; + volatile uint8_t _5; + uint8_t tmpCY; + volatile uint8_t _6; + uint8_t tmpOV; + volatile uint8_t _7; + uint8_t tmpNG; + volatile uint8_t _8; + uint8_t tmpZR; + volatile uint8_t _9; + uint8_t padding[6]; +} __attribute__((packed)); + +static_assert(24 == sizeof(SleighFlagState), + "Invalid packing of `struct SleighFlagState`."); + struct alignas(16) AArch64State : public ArchState { SIMD simd; // 512 bytes. @@ -298,9 +323,13 @@ struct alignas(16) AArch64State : public ArchState { uint64_t _3; + SleighFlagState sleigh_flags; + + uint8_t padding[8]; + } __attribute__((packed)); -static_assert((1152 + 16) == sizeof(AArch64State), +static_assert((1152 + 16 + 24 + 8) == sizeof(AArch64State), "Invalid packing of `struct State`"); struct State : public AArch64State {}; diff --git a/lib/Arch/Sleigh/AArch64Arch.cpp b/lib/Arch/Sleigh/AArch64Arch.cpp index 4d933601..0e1518be 100644 --- a/lib/Arch/Sleigh/AArch64Arch.cpp +++ b/lib/Arch/Sleigh/AArch64Arch.cpp @@ -4,6 +4,7 @@ #include #include +#include "remill/Arch/AArch64/AArch64Base.h" #include "remill/Arch/Instruction.h" #include "remill/Arch/Name.h" #include "remill/BC/ABI.h" @@ -24,8 +25,7 @@ namespace remill { // TODO(Ian): support different arm versions SleighAArch64Decoder::SleighAArch64Decoder(const remill::Arch &arch) : SleighDecoder(arch, "AARCH64.sla", "AARCH64.pspec", - sleigh::ContextRegMappings({}, {}), - {{"CY", "C"}, {"NG", "N"}, {"ZR", "Z"}, {"OV", "V"}}) {} + sleigh::ContextRegMappings({}, {}), {}) {} void SleighAArch64Decoder::InitializeSleighContext( @@ -76,6 +76,32 @@ DecodingContext AArch64Arch::CreateInitialContext(void) const { return DecodingContext(); } +void AArch64Arch::PopulateRegisterTable(void) const { + AArch64ArchBase::PopulateRegisterTable(); + +#define OFFSET_OF(type, access) \ + (reinterpret_cast(&reinterpret_cast( \ + static_cast(nullptr)->access))) + +#define REG(name, access, type) \ + AddRegister(#name, type, OFFSET_OF(AArch64State, access), nullptr) + +#define SUB_REG(name, access, type, parent_reg_name) \ + AddRegister(#name, type, OFFSET_OF(AArch64State, access), #parent_reg_name) + + auto u8 = llvm::Type::getInt8Ty(*context); + + REG(NG, sleigh_flags.NG, u8); + REG(ZR, sleigh_flags.ZR, u8); + REG(CY, sleigh_flags.CY, u8); + REG(OV, sleigh_flags.OV, u8); + REG(SHIFT_CARRY, sleigh_flags.shift_carry, u8); + REG(TMPCY, sleigh_flags.tmpCY, u8); + REG(TMPOV, sleigh_flags.tmpOV, u8); + REG(TMPZR, sleigh_flags.tmpZR, u8); + REG(TMPNG, sleigh_flags.tmpNG, u8); +} + // TODO(pag): We pretend that these are singletons, but they aren't really! Arch::ArchPtr Arch::GetAArch64Sleigh(llvm::LLVMContext *context_, diff --git a/lib/Arch/Sleigh/AArch64Arch.h b/lib/Arch/Sleigh/AArch64Arch.h index c2a8e9e8..abe29eef 100644 --- a/lib/Arch/Sleigh/AArch64Arch.h +++ b/lib/Arch/Sleigh/AArch64Arch.h @@ -27,6 +27,8 @@ class AArch64Arch final : public AArch64ArchBase { virtual ~AArch64Arch(void); + void PopulateRegisterTable(void) const override; + virtual DecodingContext CreateInitialContext(void) const override; bool DecodeInstruction(uint64_t address, std::string_view instr_bytes, diff --git a/scripts/build.sh b/scripts/build.sh index a8999538..2da55623 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -28,6 +28,7 @@ OS_VERSION= ARCH_VERSION= BUILD_FLAGS= CXX_COMMON_VERSION="0.3.1" +CREATE_PACKAGES=true # There are pre-build versions of various libraries for specific # Ubuntu releases. @@ -275,9 +276,12 @@ function Package cmake --build . \ --target install - cpack -D REMILL_DATA_PATH="${DESTDIR}" \ - -R ${remill_version} \ - --config "${SRC_DIR}/packaging/main.cmake" + + if [ "$CREATE_PACKAGES" = true ]; then + cpack -D REMILL_DATA_PATH="${DESTDIR}" \ + -R ${remill_version} \ + --config "${SRC_DIR}/packaging/main.cmake" + fi ) || return $? return $? @@ -366,6 +370,14 @@ function main shift # past argument ;; + # Disable packages + --disable-package) + CREATE_PACKAGES=false + echo "[+] Disabled building packages" + shift # past argument + ;; + + # Make the build type to be a debug build. --debug) BUILD_FLAGS="${BUILD_FLAGS} -DCMAKE_BUILD_TYPE=Debug" @@ -407,7 +419,7 @@ function main mkdir -p "${BUILD_DIR}" cd "${BUILD_DIR}" || exit 1 - if ! (DownloadLibraries && Configure && Build && Package); then + if ! (DownloadLibraries && Configure && Build && Package ); then echo "[x] Build aborted." exit 1 fi