Skip to content

Commit

Permalink
adds documentation for porting, resolves PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
null-cell committed May 15, 2023
1 parent 00bd22a commit 2a611d9
Show file tree
Hide file tree
Showing 14 changed files with 430 additions and 40 deletions.
10 changes: 5 additions & 5 deletions bindings/go/keystone/keystone_const.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ const (
ARCH_MIPS Architecture = 3
ARCH_X86 Architecture = 4
ARCH_PPC Architecture = 5
ARCH_RISCV Architecture = 6
ARCH_SPARC Architecture = 7
ARCH_SYSTEMZ Architecture = 8
ARCH_HEXAGON Architecture = 9
ARCH_EVM Architecture = 10
ARCH_SPARC Architecture = 6
ARCH_SYSTEMZ Architecture = 7
ARCH_HEXAGON Architecture = 8
ARCH_EVM Architecture = 9
ARCH_RISCV Architecture = 10
ARCH_MAX Architecture = 11
)

Expand Down
12 changes: 6 additions & 6 deletions bindings/nodejs/consts/keystone.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ module.exports.API_MAJOR = 0
module.exports.API_MINOR = 9
module.exports.VERSION_MAJOR = 0
module.exports.VERSION_MINOR = 9
module.exports.VERSION_EXTRA = 3
module.exports.VERSION_EXTRA = 2
module.exports.ARCH_ARM = 1
module.exports.ARCH_ARM64 = 2
module.exports.ARCH_MIPS = 3
module.exports.ARCH_X86 = 4
module.exports.ARCH_PPC = 5
module.exports.ARCH_RISCV = 6
module.exports.ARCH_SPARC = 7
module.exports.ARCH_SYSTEMZ = 8
module.exports.ARCH_HEXAGON = 9
module.exports.ARCH_EVM = 10
module.exports.ARCH_SPARC = 6
module.exports.ARCH_SYSTEMZ = 7
module.exports.ARCH_HEXAGON = 8
module.exports.ARCH_EVM = 9
module.exports.ARCH_RISCV = 10
module.exports.ARCH_MAX = 11
module.exports.MODE_LITTLE_ENDIAN = 0
module.exports.MODE_BIG_ENDIAN = 1073741824
Expand Down
12 changes: 6 additions & 6 deletions bindings/powershell/Keystone/Const/keystone_h.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
KS_API_MINOR = 9,
KS_VERSION_MAJOR = 0,
KS_VERSION_MINOR = 9,
KS_VERSION_EXTRA = 3,
KS_VERSION_EXTRA = 2,
KS_ARCH_ARM = 1,
KS_ARCH_ARM64 = 2,
KS_ARCH_MIPS = 3,
KS_ARCH_X86 = 4,
KS_ARCH_PPC = 5,
KS_ARCH_RISCV = 6,
KS_ARCH_SPARC = 7,
KS_ARCH_SYSTEMZ = 8,
KS_ARCH_HEXAGON = 9,
KS_ARCH_EVM = 10,
KS_ARCH_SPARC = 6,
KS_ARCH_SYSTEMZ = 7,
KS_ARCH_HEXAGON = 8,
KS_ARCH_EVM = 9,
KS_ARCH_RISCV = 10,
KS_ARCH_MAX = 11,
KS_MODE_LITTLE_ENDIAN = 0,
KS_MODE_BIG_ENDIAN = 1073741824,
Expand Down
1 change: 1 addition & 0 deletions bindings/python/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ OBJDIR = ./build
else
OBJDIR = $(abspath $(BUILDDIR))/obj/bindings/python
endif


.PHONY: gen_const install install3 clean

Expand Down
12 changes: 6 additions & 6 deletions bindings/python/keystone/keystone_const.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
KS_API_MINOR = 9
KS_VERSION_MAJOR = 0
KS_VERSION_MINOR = 9
KS_VERSION_EXTRA = 3
KS_VERSION_EXTRA = 2
KS_ARCH_ARM = 1
KS_ARCH_ARM64 = 2
KS_ARCH_MIPS = 3
KS_ARCH_X86 = 4
KS_ARCH_PPC = 5
KS_ARCH_RISCV = 6
KS_ARCH_SPARC = 7
KS_ARCH_SYSTEMZ = 8
KS_ARCH_HEXAGON = 9
KS_ARCH_EVM = 10
KS_ARCH_SPARC = 6
KS_ARCH_SYSTEMZ = 7
KS_ARCH_HEXAGON = 8
KS_ARCH_EVM = 9
KS_ARCH_RISCV = 10
KS_ARCH_MAX = 11
KS_MODE_LITTLE_ENDIAN = 0
KS_MODE_BIG_ENDIAN = 1073741824
Expand Down
12 changes: 6 additions & 6 deletions bindings/ruby/keystone_gem/lib/keystone/keystone_const.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ module Keystone
KS_API_MINOR = 9
KS_VERSION_MAJOR = 0
KS_VERSION_MINOR = 9
KS_VERSION_EXTRA = 3
KS_VERSION_EXTRA = 2
KS_ARCH_ARM = 1
KS_ARCH_ARM64 = 2
KS_ARCH_MIPS = 3
KS_ARCH_X86 = 4
KS_ARCH_PPC = 5
KS_ARCH_RISCV = 6
KS_ARCH_SPARC = 7
KS_ARCH_SYSTEMZ = 8
KS_ARCH_HEXAGON = 9
KS_ARCH_EVM = 10
KS_ARCH_SPARC = 6
KS_ARCH_SYSTEMZ = 7
KS_ARCH_HEXAGON = 8
KS_ARCH_EVM = 9
KS_ARCH_RISCV = 10
KS_ARCH_MAX = 11
KS_MODE_LITTLE_ENDIAN = 0
KS_MODE_BIG_ENDIAN = 1073741824
Expand Down
10 changes: 5 additions & 5 deletions bindings/rust/keystone-sys/src/keystone_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ pub enum Arch {
MIPS = 3,
X86 = 4,
PPC = 5,
RISCV = 6,
SPARC = 7,
SYSTEMZ = 8,
HEXAGON = 9,
EVM = 10,
SPARC = 6,
SYSTEMZ = 7,
HEXAGON = 8,
EVM = 9,
RISCV = 10,
MAX = 11,
}

Expand Down
239 changes: 239 additions & 0 deletions docs/intro_to_porting.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions include/keystone/keystone.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ typedef struct ks_struct ks_engine;
// Package version
#define KS_VERSION_MAJOR KS_API_MAJOR
#define KS_VERSION_MINOR KS_API_MINOR
#define KS_VERSION_EXTRA 3
#define KS_VERSION_EXTRA 2

/*
Macro to create combined version which can be compared to
Expand All @@ -60,11 +60,11 @@ typedef enum ks_arch {
KS_ARCH_MIPS, // Mips architecture
KS_ARCH_X86, // X86 architecture (including x86 & x86-64)
KS_ARCH_PPC, // PowerPC architecture (currently unsupported)
KS_ARCH_RISCV, // RISC-V architecture
KS_ARCH_SPARC, // Sparc architecture
KS_ARCH_SYSTEMZ, // SystemZ architecture (S390X)
KS_ARCH_HEXAGON, // Hexagon architecture
KS_ARCH_EVM, // Ethereum Virtual Machine architecture
KS_ARCH_RISCV, // RISC-V architecture
KS_ARCH_MAX,
} ks_arch;

Expand Down
12 changes: 8 additions & 4 deletions llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ bool RISCVAsmBackend::writeNopData(uint64_t Count, MCObjectWriter * OW) const {
return true;
}

static uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value) {
static uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value, unsigned int KsError) {

unsigned Kind = Fixup.getKind();
switch (Kind) {
Expand Down Expand Up @@ -200,11 +200,13 @@ static uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value) {
case RISCV::fixup_riscv_jal: {
if (!isInt<21>(Value))
//Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
// FIXME: report an error to keystone
// FIXME: report a more specific error to keystone
KsError = KS_ERR_ASM_FIXUP_INVALID;
return -1;
if (Value & 0x1)
//Ctx.reportError(Fixup.getLoc(), "fixup value must be 2-byte aligned");
// FIXME: report an error to keystone
// FIXME: report a more specific error to keystone
KsError = KS_ERR_ASM_FIXUP_INVALID;
return -1;
// Need to produce imm[19|10:1|11|19:12] from the 21-bit Value.
unsigned Sbit = (Value >> 20) & 0x1;
Expand All @@ -221,9 +223,11 @@ static uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value) {
case RISCV::fixup_riscv_branch: {
if (!isInt<13>(Value))
//Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
KsError = KS_ERR_ASM_FIXUP_INVALID;
return -1;
if (Value & 0x1)
//Ctx.reportError(Fixup.getLoc(), "fixup value must be 2-byte aligned");
KsError = KS_ERR_ASM_FIXUP_INVALID;
return -1;
// Need to extract imm[12], imm[10:5], imm[4:1], imm[11] from the 13-bit
// Value.
Expand Down Expand Up @@ -283,7 +287,7 @@ void RISCVAsmBackend::applyFixup(const MCFixup &Fixup, char *Data, unsigned Data
if (!Value)
return; // Doesn't change encoding.
// Apply any target-specific value adjustments.
Value = adjustFixupValue(Fixup, Value);
Value = adjustFixupValue(Fixup, Value, KsError);

// Shift the value into position.
Value <<= Info.TargetOffset;
Expand Down
4 changes: 4 additions & 0 deletions samples/sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ int main(int argc, char **argv)
test_ks(KS_ARCH_PPC, KS_MODE_PPC64, "add 1, 2, 3", 0);
test_ks(KS_ARCH_PPC, KS_MODE_PPC64 + KS_MODE_BIG_ENDIAN, "add 1, 2, 3", 0);

// RISCV
test_ks(KS_ARCH_RISCV, KS_MODE_RISCV32 + KS_MODE_LITTLE_ENDIAN, "addi x0, x0, 10", 0);
test_ks(KS_ARCH_RISCV, KS_MODE_RISCV64 + KS_MODE_LITTLE_ENDIAN, "addiw x0, x0, 10", 0);

// Sparc
test_ks(KS_ARCH_SPARC, KS_MODE_SPARC32 + KS_MODE_LITTLE_ENDIAN, "add %g1, %g2, %g3", 0);
test_ks(KS_ARCH_SPARC, KS_MODE_SPARC32 + KS_MODE_BIG_ENDIAN, "add %g1, %g2, %g3", 0);
Expand Down
66 changes: 66 additions & 0 deletions suite/fuzz/fuzz_asm_riscv32.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <keystone/keystone.h>

#include <string.h>

FILE * outfile = NULL;


int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
ks_engine *ks;
ks_err err;
size_t count;
unsigned char *encode = NULL;
size_t size;
char * assembler;

if (outfile == NULL) {
// we compute the output
outfile = fopen("/dev/null", "w");
if (outfile == NULL) {
printf("failed opening /dev/null\n");
abort();
}
}

if (Size < 1) {
return 0;
}

err = ks_open(KS_ARCH_RISCV, KS_MODE_RISCV32 + KS_MODE_LITTLE_ENDIAN, &ks);
if (err != KS_ERR_OK) {
printf("ERROR: failed on ks_open(), quit error = %u\n", err);
abort();
}

ks_option(ks, KS_OPT_SYNTAX, Data[Size-1]);

assembler = malloc(Size);
memcpy(assembler, Data, Size-1);
//null terminate string
assembler[Size-1] = 0;

if (ks_asm(ks, assembler, 0, &encode, &size, &count) != KS_ERR_OK) {
fprintf(outfile, "ERROR: ks_asm() failed & count = %lu, error = %u\n",
count, ks_errno(ks));
} else {
size_t i;

fprintf(outfile, "%s = ", assembler);
for (i = 0; i < size; i++) {
fprintf(outfile, "%02x ", encode[i]);
}
fprintf(outfile, "\n");
fprintf(outfile, "Compiled: %lu bytes, statements: %lu\n", size, count);
}

free(assembler);
// NOTE: free encode after usage to avoid leaking memory
if (encode != NULL) {
ks_free(encode);
}

// close Keystone instance when done
ks_close(ks);

return 0;
}
66 changes: 66 additions & 0 deletions suite/fuzz/fuzz_asm_riscv64.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <keystone/keystone.h>

#include <string.h>

FILE * outfile = NULL;


int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
ks_engine *ks;
ks_err err;
size_t count;
unsigned char *encode = NULL;
size_t size;
char * assembler;

if (outfile == NULL) {
// we compute the output
outfile = fopen("/dev/null", "w");
if (outfile == NULL) {
printf("failed opening /dev/null\n");
abort();
}
}

if (Size < 1) {
return 0;
}

err = ks_open(KS_ARCH_RISCV, KS_MODE_RISCV64 + KS_MODE_LITTLE_ENDIAN, &ks);
if (err != KS_ERR_OK) {
printf("ERROR: failed on ks_open(), quit error = %u\n", err);
abort();
}

ks_option(ks, KS_OPT_SYNTAX, Data[Size-1]);

assembler = malloc(Size);
memcpy(assembler, Data, Size-1);
//null terminate string
assembler[Size-1] = 0;

if (ks_asm(ks, assembler, 0, &encode, &size, &count) != KS_ERR_OK) {
fprintf(outfile, "ERROR: ks_asm() failed & count = %lu, error = %u\n",
count, ks_errno(ks));
} else {
size_t i;

fprintf(outfile, "%s = ", assembler);
for (i = 0; i < size; i++) {
fprintf(outfile, "%02x ", encode[i]);
}
fprintf(outfile, "\n");
fprintf(outfile, "Compiled: %lu bytes, statements: %lu\n", size, count);
}

free(assembler);
// NOTE: free encode after usage to avoid leaking memory
if (encode != NULL) {
ks_free(encode);
}

// close Keystone instance when done
ks_close(ks);

return 0;
}
10 changes: 10 additions & 0 deletions suite/test-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,13 @@ echo "::PPC64 BE"
kstool ppc64be "add 1, 2, 3"
#encoding: [0x7c,0x22,0x1a,0x14]
echo

echo "::RISCV32"
kstool riscv "addi x0, x0, 10"
#encoding: [0x13,0x00,0xa0,0x00]
echo

echo "::RISCV64"
kstool riscv64 "addiw x0, x0, 10"
#encoding: [0x1b,0x00,0xa0,0x00]
echo

0 comments on commit 2a611d9

Please sign in to comment.