Skip to content

Commit

Permalink
Mirror Sleigh's Byte-width Flags (#668)
Browse files Browse the repository at this point in the history
* add sleigh state

* remove bad include

* add volatile

* dont run tests in docker deploys

* remove ccache

* Revert "remove ccache"

This reverts commit 221b70a.

* Revert "dont run tests in docker deploys"

This reverts commit 72229ac.

* disable packaging in dockerfile

* fix block packagingA

* fix syntax
  • Loading branch information
2over12 authored Jul 10, 2023
1 parent 047c628 commit a6abbb8
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 8 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 30 additions & 1 deletion include/remill/Arch/AArch64/Runtime/State.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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 {};
Expand Down
30 changes: 28 additions & 2 deletions lib/Arch/Sleigh/AArch64Arch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <sstream>
#include <string>

#include "remill/Arch/AArch64/AArch64Base.h"
#include "remill/Arch/Instruction.h"
#include "remill/Arch/Name.h"
#include "remill/BC/ABI.h"
Expand All @@ -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(
Expand Down Expand Up @@ -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<uintptr_t>(&reinterpret_cast<const volatile char &>( \
static_cast<type *>(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_,
Expand Down
2 changes: 2 additions & 0 deletions lib/Arch/Sleigh/AArch64Arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 16 additions & 4 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 $?
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a6abbb8

Please sign in to comment.