Skip to content

Commit

Permalink
Merge pull request #1318 from Antwy/riscv-support
Browse files Browse the repository at this point in the history
RISCV basic support
  • Loading branch information
JonathanSalwan committed Jul 25, 2024
2 parents 3a39d48 + ea19db3 commit 6ee4e2c
Show file tree
Hide file tree
Showing 42 changed files with 10,696 additions and 51 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/vcpkg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
name: Get vcpkg
with:
repository: 'microsoft/vcpkg'
ref: '2022.04.12'
ref: '2024.05.24'
path: '${{ github.workspace }}/_vcpkg'
fetch-depth: 0

Expand All @@ -64,7 +64,7 @@ jobs:
- name: Configure (Windows)
if: ${{ matrix.platform == 'windows' }}
run: |
cmake --preset=${{ matrix.cmake-preset }} -DBUILD_SHARED_LIBS:BOOL=ON -DVCPKG_OVERLAY_TRIPLETS:PATH=${{ github.workspace }}\vcpkg\triplets -DVCPKG_TARGET_TRIPLET:STRING=${{ matrix.vcpkg-triplet }}
cmake --preset=${{ matrix.cmake-preset }} -DBUILD_SHARED_LIBS:BOOL=ON -DCS_VERSION_MAJOR:STRING=5 -DVCPKG_OVERLAY_TRIPLETS:PATH=${{ github.workspace }}\vcpkg\triplets -DVCPKG_TARGET_TRIPLET:STRING=${{ matrix.vcpkg-triplet }}
env:
# capstone 4.0.2 does not produce a CMake Config, hence the need to point directly to include directories and libraries
CAPSTONE_INCLUDE_DIRS: ${{ github.workspace }}/build/${{ matrix.cmake-preset }}/vcpkg_installed/${{ matrix.vcpkg-triplet }}/include
Expand All @@ -76,7 +76,7 @@ jobs:
if: ${{ matrix.platform != 'windows' }}
run: |
python -m pip install importlib-resources
cmake --preset=${{ matrix.cmake-preset }} -DBUILD_SHARED_LIBS:BOOL=ON -DVCPKG_OVERLAY_TRIPLETS:PATH=${{ github.workspace }}/vcpkg/triplets -DVCPKG_TARGET_TRIPLET:STRING=${{ matrix.vcpkg-triplet }}
cmake --preset=${{ matrix.cmake-preset }} -DBUILD_SHARED_LIBS:BOOL=ON -DCS_VERSION_MAJOR:STRING=5 -DVCPKG_OVERLAY_TRIPLETS:PATH=${{ github.workspace }}/vcpkg/triplets -DVCPKG_TARGET_TRIPLET:STRING=${{ matrix.vcpkg-triplet }}
env:
# capstone 4.0.2 does not produce a CMake Config, hence the need to point directly to include directories and libraries
CAPSTONE_INCLUDE_DIRS: ${{ github.workspace }}/build/${{ matrix.cmake-preset }}/vcpkg_installed/${{ matrix.vcpkg-triplet }}/include
Expand All @@ -86,7 +86,7 @@ jobs:

- name: Build
run: |
cmake --build --preset build-${{ matrix.cmake-preset }} --config Release
cmake --build --preset build-${{ matrix.cmake-preset }} --config Release -DCS_VERSION_MAJOR:STRING=5
- name: Test
# Test disabled on Windows `pip install lief` fails as there is no ready to use .whl and compilation fails
Expand All @@ -98,7 +98,7 @@ jobs:
- name: Install
run: |
cmake --build --preset build-${{ matrix.cmake-preset }} --config Release --target install
cmake --build --preset build-${{ matrix.cmake-preset }} --config Release --target install -DCS_VERSION_MAJOR:STRING=5
- name: Upload
uses: actions/upload-artifact@v3
Expand Down
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ endif()
message(STATUS "Compiling with Capstone")
find_package(CAPSTONE 5 REQUIRED)
message(STATUS "CAPSTONE version: ${CAPSTONE_VERSION}")
if(${CS_VERSION_MAJOR} GREATER_EQUAL 5)
add_definitions(-DCOMPILE_RISCV)
endif()
if(TARGET capstone::capstone)
link_libraries(capstone::capstone)
elseif(DEFINED CAPSTONE_INCLUDE_DIRS)
Expand Down
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ COPY . /Triton
# cmake >= 3.20
RUN apt update && apt upgrade -y && apt install -y build-essential clang curl git libboost-all-dev libgmp-dev libpython3-dev libpython3-stdlib llvm-12 llvm-12-dev python3-pip tar ninja-build pkg-config && apt-get clean && pip install --upgrade pip && pip3 install Cython lief cmake meson

# libcapstone >= 4.0.x
# libcapstone >= 5.0.x
RUN cd /tmp && \
curl -o cap.tgz -L https://github.com/aquynh/capstone/archive/5.0.1.tar.gz && \
tar xvf cap.tgz && cd capstone-5.0.1/ && ./make.sh && make install && rm -rf /tmp/cap* \
tar xvf cap.tgz && cd capstone-5.0.1/ && CAPSTONE_ARCHS="arm aarch64 riscv x86" ./make.sh && \
make install && rm -rf /tmp/cap* \
&& ln -s /usr/lib/libcapstone.so.5 /usr/lib/x86_64-linux-gnu/libcapstone.so

# libbitwuzla >= 0.4.0
Expand Down
46 changes: 40 additions & 6 deletions doc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,54 @@ add_custom_target(gen_arm32_doc_from_spec
DEPENDS ${TRITON_ROOT}/src/libtriton/includes/triton/arm32.spec
)

add_custom_target(doc
COMMAND doxygen ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
DEPENDS gen_x86_doc_from_spec
DEPENDS gen_aarch64_doc_from_spec
DEPENDS gen_arm32_doc_from_spec
add_custom_target(gen_rv64_doc_from_spec
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/extract_doc.py ${TRITON_ROOT}/src/libtriton/includes/triton/riscv64.spec "${CMAKE_CURRENT_BINARY_DIR}"
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/extract_doc.py
DEPENDS ${TRITON_ROOT}/src/libtriton/includes/triton/riscv64.spec
)

add_custom_target(gen_rv32_doc_from_spec
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/extract_doc.py ${TRITON_ROOT}/src/libtriton/includes/triton/riscv32.spec "${CMAKE_CURRENT_BINARY_DIR}"
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/extract_doc.py
DEPENDS ${TRITON_ROOT}/src/libtriton/includes/triton/riscv32.spec
)

if(${CS_VERSION_MAJOR} GREATER_EQUAL 5)
add_custom_target(doc
COMMAND doxygen ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
DEPENDS gen_x86_doc_from_spec
DEPENDS gen_aarch64_doc_from_spec
DEPENDS gen_arm32_doc_from_spec
DEPENDS gen_rv64_doc_from_spec
DEPENDS gen_rv32_doc_from_spec
)
else()
add_custom_target(doc
COMMAND doxygen ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
DEPENDS gen_x86_doc_from_spec
DEPENDS gen_aarch64_doc_from_spec
DEPENDS gen_arm32_doc_from_spec
)
endif()

if(PYTHON_BINDINGS_AUTOCOMPLETE)
add_custom_target(python_autocomplete
if(${CS_VERSION_MAJOR} GREATER_EQUAL 5)
add_custom_target(python_autocomplete
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_LIST_DIR}/autocomplete/generate_autocomplete.py "--basedir" "${CMAKE_CURRENT_BINARY_DIR}" "$<$<BOOL:${BITWUZLA_INTERFACE}>:--bitwuzla>" "$<$<BOOL:${Z3_INTERFACE}>:--z3>"
DEPENDS gen_x86_doc_from_spec
DEPENDS gen_aarch64_doc_from_spec
DEPENDS gen_arm32_doc_from_spec
DEPENDS gen_rv64_doc_from_spec
DEPENDS gen_rv32_doc_from_spec
)
else()
add_custom_target(python_autocomplete
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_LIST_DIR}/autocomplete/generate_autocomplete.py "--basedir" "${CMAKE_CURRENT_BINARY_DIR}" "$<$<BOOL:${BITWUZLA_INTERFACE}>:--bitwuzla>" "$<$<BOOL:${Z3_INTERFACE}>:--z3>"
DEPENDS gen_x86_doc_from_spec
DEPENDS gen_aarch64_doc_from_spec
DEPENDS gen_arm32_doc_from_spec
)
endif()
if (NOT DEFINED PYTHON_SITE_PACKAGES)
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "from sysconfig import get_path; print(get_path('platlib'))" OUTPUT_VARIABLE PYTHON_SITE_PACKAGES OUTPUT_STRIP_TRAILING_WHITESPACE)
endif()
Expand Down
32 changes: 32 additions & 0 deletions doc/extract_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
x86_regs = list()
aarch64_regs = list()
arm32_regs = list()
rv64_regs = list()
rv32_regs = list()

##############################################################################

Expand Down Expand Up @@ -58,3 +60,33 @@
for name in arm32_regs:
out.write("<li><b>REG.ARM32.{}</b></li>\n".format(name))
out.write('</ul>\n')

##############################################################################

elif SPEC.find('riscv64') >= 0:
with open(SPEC, "r") as f:
for line in f.readlines():
if line.startswith("REG_SPEC") or line.startswith("SYS_REG_SPEC"):
args = line[line.find("(") + 1: line.find(")")].split(", ")
rv64_regs.append(args[0])

with open(os.path.join(BUILD_DIR, "rv64_reg"), "w") as out:
out.write('<ul>\n')
for name in rv64_regs:
out.write("<li><b>REG.RISCV64.{}</b></li>\n".format(name))
out.write('</ul>\n')

##############################################################################

elif SPEC.find('riscv32') >= 0:
with open(SPEC, "r") as f:
for line in f.readlines():
if line.startswith("REG_SPEC") or line.startswith("SYS_REG_SPEC"):
args = line[line.find("(") + 1: line.find(")")].split(", ")
rv32_regs.append(args[0])

with open(os.path.join(BUILD_DIR, "rv32_reg"), "w") as out:
out.write('<ul>\n')
for name in rv32_regs:
out.write("<li><b>REG.RISCV32.{}</b></li>\n".format(name))
out.write('</ul>\n')
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

#include <stdio.h>
#include <stdlib.h>

char *serial = "\x31\x3e\x3d\x26\x31";

int check(char *ptr)
{
int i;
int hash = 0xABCD;

for (i = 0; ptr[i]; i++)
hash += ptr[i] ^ serial[i % 5];

return hash;
}

int main(int ac, char **av)
{
int ret;

if (ac != 2)
return -1;

ret = check(av[1]);
if (ret == 0xad6d)
printf("Win\n");
else
printf("fail\n");

return 0;
}

Loading

0 comments on commit 6ee4e2c

Please sign in to comment.