From 0c0a7d5c3b254253c9ab845c1461b96b8e47e9ca Mon Sep 17 00:00:00 2001 From: Triton Library Date: Tue, 13 Aug 2024 09:57:41 +0200 Subject: [PATCH 01/18] Fix #1350: zero extend issue with LDRS instructions --- .build_number | 2 +- src/libtriton/arch/arm/aarch64/aarch64Semantics.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.build_number b/.build_number index 99814cec1..00e37d62b 100644 --- a/.build_number +++ b/.build_number @@ -1 +1 @@ -1595 +1596 diff --git a/src/libtriton/arch/arm/aarch64/aarch64Semantics.cpp b/src/libtriton/arch/arm/aarch64/aarch64Semantics.cpp index dccff3f09..cd6b52eaa 100644 --- a/src/libtriton/arch/arm/aarch64/aarch64Semantics.cpp +++ b/src/libtriton/arch/arm/aarch64/aarch64Semantics.cpp @@ -3298,7 +3298,7 @@ namespace triton { auto op = this->symbolicEngine->getOperandAst(inst, src); /* Create the semantics of the LOAD */ - auto node1 = this->astCtxt->sx(dst.getBitSize() - 8, op); + auto node1 = this->astCtxt->sx(dst.getBitSize() - 8, this->astCtxt->extract(7, 0, op)); /* Create symbolic expression */ auto expr1 = this->symbolicEngine->createSymbolicExpression(inst, node1, dst, "LDRSB operation - LOAD access"); @@ -3353,7 +3353,7 @@ namespace triton { auto op = this->symbolicEngine->getOperandAst(inst, src); /* Create the semantics of the LOAD */ - auto node1 = this->astCtxt->sx(dst.getBitSize() - 16, op); + auto node1 = this->astCtxt->sx(dst.getBitSize() - 16, this->astCtxt->extract(15, 0, op)); /* Create symbolic expression */ auto expr1 = this->symbolicEngine->createSymbolicExpression(inst, node1, dst, "LDRSH operation - LOAD access"); @@ -3413,7 +3413,7 @@ namespace triton { auto op = this->symbolicEngine->getOperandAst(inst, src); /* Create the semantics of the LOAD */ - auto node1 = this->astCtxt->sx(dst.getBitSize() - 32, op); + auto node1 = this->astCtxt->sx(dst.getBitSize() - 32, this->astCtxt->extract(31, 0, op)); /* Create symbolic expression */ auto expr1 = this->symbolicEngine->createSymbolicExpression(inst, node1, dst, "LDRSW operation - LOAD access"); From f53734ebd1ee5af6fbf3f86bb74ef1fbd890b6e5 Mon Sep 17 00:00:00 2001 From: 0x9047 Date: Thu, 15 Aug 2024 18:44:52 +0800 Subject: [PATCH 02/18] Fix LLVM_INCLUDE_DIRS not set when passed with flags --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d8c375d74..5b553fa44 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -187,13 +187,13 @@ if(LLVM_INTERFACE) find_package(LLVM REQUIRED CONFIG) message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") - include_directories(${LLVM_INCLUDE_DIRS}) if(LLVM_LINK_LLVM_DYLIB) set(LLVM_LIBRARIES LLVM) else() set(LLVM_LIBRARIES ${LLVM_AVAILABLE_LIBS}) endif() endif() + include_directories(${LLVM_INCLUDE_DIRS}) set(TRITON_LLVM_INTERFACE ON) endif() From 33c15fd03279a986643a73e9c6cf2a1848701253 Mon Sep 17 00:00:00 2001 From: PsiFunction Date: Mon, 2 Sep 2024 17:22:30 +0300 Subject: [PATCH 03/18] + initial support for fp operand --- src/libtriton/arch/arm/aarch64/aarch64Cpu.cpp | 66 ++++++++++++++++++- src/testers/aarch64/unicorn_test_aarch64.py | 3 + 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/libtriton/arch/arm/aarch64/aarch64Cpu.cpp b/src/libtriton/arch/arm/aarch64/aarch64Cpu.cpp index 18dfd437b..dd68b5cc6 100644 --- a/src/libtriton/arch/arm/aarch64/aarch64Cpu.cpp +++ b/src/libtriton/arch/arm/aarch64/aarch64Cpu.cpp @@ -523,8 +523,72 @@ namespace triton { break; } + case triton::extlibs::capstone::ARM64_OP_FP: { + if (size == 0) { + throw triton::exceptions::Disassembly("Aarch64Cpu::disassembly(): Cannot correctly decode FP operand"); + } + + auto encode_fp_imm = [size](double fp_value) { + static_assert(sizeof(float) == sizeof(triton::uint32), "Unexpected float type size"); + static_assert(sizeof(double) == sizeof(triton::uint64), "Unexpected double type size"); + + auto IEEE754_f32_to_f16 = [](float value) -> uint16_t { + uint32_t f; + std::memcpy(&f, &value, sizeof(uint32_t)); + uint16_t sign = (f >> 16) & 0x8000; + int16_t exponent = ((f >> 23) & 0xff) - 127 + 15; + uint16_t mantissa = (f >> 13) & 0x3ff; + + if (exponent <= 0) { + if (exponent < -10) { + return sign; + } + mantissa = (mantissa | 0x400) >> (1 - exponent); + return sign | mantissa; + } else if (exponent == 0xff - (127 - 15)) { + if (mantissa) { + return sign | 0x7fff; + } else { + return sign | 0x7c00; + } + } else if (exponent > 30) { + return sign | 0x7c00; + } + return sign | (exponent << 10) | mantissa; + }; + + if (size == sizeof(double)) { + triton::uint64 result; + std:memcpy(&result, &fp_value, sizeof(double)); + return result; + } + else if (size == sizeof(float)) { + float converted = static_cast(fp_value); + triton::uint32 conv_repr; + std::memcpy(&conv_repr, &converted, sizeof(float)); + // just zero extended value + return static_cast(conv_repr); + } + else if (size == 2) { // half-precision + float value = static_cast(fp_value); + return static_cast(IEEE754_f32_to_f16(value)); + } + + throw triton::exceptions::Disassembly("AArch64Cpu::disassembly(): Invalid operand."); + }; + + Immediate imm{encode_fp_imm(op->fp), size}; + + /* Set Shift type and value */ + imm.setShiftType(this->capstoneShiftToTritonShift(op->shift.type)); + imm.setShiftValue(op->shift.value); + + inst.operands.push_back(triton::arch::OperandWrapper(imm)); + } + break; + default: - /* NOTE: FP, CIMM, and missing one are not supported yet. */ + /* NOTE: CIMM, and missing one are not supported yet. */ throw triton::exceptions::Disassembly("AArch64Cpu::disassembly(): Invalid operand."); } // switch } // for operand diff --git a/src/testers/aarch64/unicorn_test_aarch64.py b/src/testers/aarch64/unicorn_test_aarch64.py index 39f354c14..b8ee8b150 100644 --- a/src/testers/aarch64/unicorn_test_aarch64.py +++ b/src/testers/aarch64/unicorn_test_aarch64.py @@ -2156,6 +2156,9 @@ #(b"\x00\x00\xaf\x9e", "fmov v0.D[1], x0"), # working on capstone next branch (b"\x40\x03\x67\x9e", "fmov d0, x26"), (b"\x02\x00\x66\x9e", "fmov x2, d0"), + (b"\x04\x90\xe0\x1e", "fmov h4, #2.5"), + (b"\x00\x10\x20\x1e", "fmov s0, #2.0"), + (b"\x03\x10\x2e\x1e", "fmov s3, #1.0"), ] def emu_with_unicorn(opcode, istate): From 381bda73e42db25e0dc8af459c1ffe38bd1d02b8 Mon Sep 17 00:00:00 2001 From: PsiFunction Date: Mon, 2 Sep 2024 18:24:39 +0300 Subject: [PATCH 04/18] + fix unicorn cpu exceptions in tests on f16 aarch64 feature --- src/testers/aarch64/unicorn_test_aarch64.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/testers/aarch64/unicorn_test_aarch64.py b/src/testers/aarch64/unicorn_test_aarch64.py index b8ee8b150..51b0f2ad1 100644 --- a/src/testers/aarch64/unicorn_test_aarch64.py +++ b/src/testers/aarch64/unicorn_test_aarch64.py @@ -2156,9 +2156,9 @@ #(b"\x00\x00\xaf\x9e", "fmov v0.D[1], x0"), # working on capstone next branch (b"\x40\x03\x67\x9e", "fmov d0, x26"), (b"\x02\x00\x66\x9e", "fmov x2, d0"), - (b"\x04\x90\xe0\x1e", "fmov h4, #2.5"), (b"\x00\x10\x20\x1e", "fmov s0, #2.0"), (b"\x03\x10\x2e\x1e", "fmov s3, #1.0"), + #(b"\x04\x90\xe0\x1e", "fmov h4, #2.5"), # unicorn not implement f16 ops ] def emu_with_unicorn(opcode, istate): From 9887eb7271a35bed399e0db121ead49cbebedf0c Mon Sep 17 00:00:00 2001 From: PsiFunction Date: Tue, 3 Sep 2024 09:26:31 +0300 Subject: [PATCH 05/18] + typo, 64-bit fmov test --- src/libtriton/arch/arm/aarch64/aarch64Cpu.cpp | 2 +- src/testers/aarch64/unicorn_test_aarch64.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libtriton/arch/arm/aarch64/aarch64Cpu.cpp b/src/libtriton/arch/arm/aarch64/aarch64Cpu.cpp index c592d748a..d35fb47b2 100644 --- a/src/libtriton/arch/arm/aarch64/aarch64Cpu.cpp +++ b/src/libtriton/arch/arm/aarch64/aarch64Cpu.cpp @@ -567,7 +567,7 @@ namespace triton { if (size == sizeof(double)) { triton::uint64 result; - std:memcpy(&result, &fp_value, sizeof(double)); + std::memcpy(&result, &fp_value, sizeof(double)); return result; } else if (size == sizeof(float)) { diff --git a/src/testers/aarch64/unicorn_test_aarch64.py b/src/testers/aarch64/unicorn_test_aarch64.py index 51b0f2ad1..f78d8fab7 100644 --- a/src/testers/aarch64/unicorn_test_aarch64.py +++ b/src/testers/aarch64/unicorn_test_aarch64.py @@ -2158,6 +2158,7 @@ (b"\x02\x00\x66\x9e", "fmov x2, d0"), (b"\x00\x10\x20\x1e", "fmov s0, #2.0"), (b"\x03\x10\x2e\x1e", "fmov s3, #1.0"), + (b"\x01\x90\x61\x1e", "fmov d1, #3.5"), #(b"\x04\x90\xe0\x1e", "fmov h4, #2.5"), # unicorn not implement f16 ops ] From 2f912069664a9a8b30a6fe9cfbe0bbb605af02c3 Mon Sep 17 00:00:00 2001 From: PsiFunction Date: Thu, 5 Sep 2024 12:23:49 +0300 Subject: [PATCH 06/18] + move fp operand feature to Imm ctor --- src/libtriton/CMakeLists.txt | 6 +++ src/libtriton/arch/arm/aarch64/aarch64Cpu.cpp | 51 +------------------ src/libtriton/arch/immediate.cpp | 45 +++++++++++++++- src/libtriton/includes/triton/coreUtils.hpp | 14 +++++ src/libtriton/includes/triton/immediate.hpp | 3 ++ src/libtriton/includes/triton/softfloat.hpp | 34 +++++++++++++ src/libtriton/utils/softfloat.cpp | 42 +++++++++++++++ 7 files changed, 144 insertions(+), 51 deletions(-) create mode 100644 src/libtriton/includes/triton/softfloat.hpp create mode 100644 src/libtriton/utils/softfloat.cpp diff --git a/src/libtriton/CMakeLists.txt b/src/libtriton/CMakeLists.txt index f8b664618..dc82de0cc 100644 --- a/src/libtriton/CMakeLists.txt +++ b/src/libtriton/CMakeLists.txt @@ -69,6 +69,7 @@ set(LIBTRITON_SOURCE_FILES stubs/x8664-ms-libc.cpp stubs/x8664-systemv-libc.cpp utils/coreUtils.cpp + utils/softfloat.cpp ) # Define all header files @@ -106,6 +107,7 @@ set(LIBTRITON_HEADER_FILES includes/triton/comparableFunctor.hpp includes/triton/context.hpp includes/triton/coreUtils.hpp + includes/triton/softfloat.hpp includes/triton/cpuInterface.hpp includes/triton/cpuSize.hpp includes/triton/dllexport.hpp @@ -157,6 +159,10 @@ set(LIBTRITON_HEADER_FILES includes/triton/z3ToTriton.hpp ) +set_source_files_properties(utils/softfloat.cpp PROPERTIES COMPILE_DEFINITIONS + ${CMAKE_CXX_BYTE_ORDER} +) + # Define all resource files set(LIBTRITON_RESOURCE_FILES includes/triton/version.hpp.in diff --git a/src/libtriton/arch/arm/aarch64/aarch64Cpu.cpp b/src/libtriton/arch/arm/aarch64/aarch64Cpu.cpp index d35fb47b2..e42f2c610 100644 --- a/src/libtriton/arch/arm/aarch64/aarch64Cpu.cpp +++ b/src/libtriton/arch/arm/aarch64/aarch64Cpu.cpp @@ -536,56 +536,7 @@ namespace triton { throw triton::exceptions::Disassembly("Aarch64Cpu::disassembly(): Cannot correctly decode FP operand"); } - auto encode_fp_imm = [size](double fp_value) { - static_assert(sizeof(float) == sizeof(triton::uint32), "Unexpected float type size"); - static_assert(sizeof(double) == sizeof(triton::uint64), "Unexpected double type size"); - - auto IEEE754_f32_to_f16 = [](float value) -> uint16_t { - uint32_t f; - std::memcpy(&f, &value, sizeof(uint32_t)); - uint16_t sign = (f >> 16) & 0x8000; - int16_t exponent = ((f >> 23) & 0xff) - 127 + 15; - uint16_t mantissa = (f >> 13) & 0x3ff; - - if (exponent <= 0) { - if (exponent < -10) { - return sign; - } - mantissa = (mantissa | 0x400) >> (1 - exponent); - return sign | mantissa; - } else if (exponent == 0xff - (127 - 15)) { - if (mantissa) { - return sign | 0x7fff; - } else { - return sign | 0x7c00; - } - } else if (exponent > 30) { - return sign | 0x7c00; - } - return sign | (exponent << 10) | mantissa; - }; - - if (size == sizeof(double)) { - triton::uint64 result; - std::memcpy(&result, &fp_value, sizeof(double)); - return result; - } - else if (size == sizeof(float)) { - float converted = static_cast(fp_value); - triton::uint32 conv_repr; - std::memcpy(&conv_repr, &converted, sizeof(float)); - // just zero extended value - return static_cast(conv_repr); - } - else if (size == 2) { // half-precision - float value = static_cast(fp_value); - return static_cast(IEEE754_f32_to_f16(value)); - } - - throw triton::exceptions::Disassembly("AArch64Cpu::disassembly(): Invalid operand."); - }; - - Immediate imm{encode_fp_imm(op->fp), size}; + Immediate imm{op->fp, size, this->getEndianness()}; /* Set Shift type and value */ imm.setShiftType(this->capstoneShiftToTritonShift(op->shift.type)); diff --git a/src/libtriton/arch/immediate.cpp b/src/libtriton/arch/immediate.cpp index 301e25a8f..a3493d2cc 100644 --- a/src/libtriton/arch/immediate.cpp +++ b/src/libtriton/arch/immediate.cpp @@ -5,10 +5,17 @@ ** This program is under the terms of the Apache License 2.0. */ +#include #include #include -#include +#include +#include +#ifdef LITTLE_ENDIAN // provided by CMake +constexpr auto sys_endianness = triton::arch::LE_ENDIANNESS; +#else +constexpr auto sys_endianness = triton::arch::BE_ENDIANNESS; +#endif namespace triton { @@ -23,6 +30,42 @@ namespace triton { this->setValue(value, size); } + Immediate::Immediate(double value, triton::uint32 size /* bytes */, triton::arch::endianness_e platform_endianness) { + triton::uint64 imm_value; + + auto need_swap = sys_endianness != platform_endianness; + + if (size == sizeof(double)) { + static_assert(sizeof(double) == sizeof(triton::uint64), + "Unexpected double type size"); + std::memcpy(&imm_value, &value, sizeof(double)); + if (need_swap) { + imm_value = utils::byteswap(imm_value); + } + } + else if (size == sizeof(float)) { // single-precision + float fvalue = static_cast(value); + triton::uint32 repr; + static_assert(sizeof(float) == sizeof(uint32_t), + "Unexpected float type size"); + std::memcpy(&repr, &fvalue, sizeof(float)); + + imm_value = need_swap ? static_cast(utils::byteswap(repr)) + : static_cast(repr); + } else if (size == 2) { // half-precision + float fvalue = static_cast(value); + triton::uint16 repr = sf::f32_to_f16(fvalue); + imm_value = need_swap ? static_cast(utils::byteswap(repr)) + : static_cast(repr); + + } + else { + throw triton::exceptions::Immediate("Immediate::Immediate(double): Invalid encoding size."); + } + + this->setValue(imm_value, size); + } + Immediate::Immediate(const Immediate& other) : BitsVector(other), diff --git a/src/libtriton/includes/triton/coreUtils.hpp b/src/libtriton/includes/triton/coreUtils.hpp index 9fdec4d62..660523cb9 100644 --- a/src/libtriton/includes/triton/coreUtils.hpp +++ b/src/libtriton/includes/triton/coreUtils.hpp @@ -10,6 +10,7 @@ #include #include +#include #include #include @@ -81,6 +82,19 @@ namespace triton { template <> TRITON_EXPORT triton::uint80 cast(const triton::uint512& value); template <> TRITON_EXPORT triton::uint512 cast(const triton::uint80& value); + template + std::enable_if_t< + std::is_unsigned_v, + T> + byteswap(T value) { + std::array repr; + std::memcpy(&repr, &value, sizeof(value)); + std::reverse(repr.begin(), repr.end()); + T result; + std::memcpy(&result, &repr, sizeof(result)); + return result; + } + /*! @} End of utils namespace */ }; /*! @} End of triton namespace */ diff --git a/src/libtriton/includes/triton/immediate.hpp b/src/libtriton/includes/triton/immediate.hpp index 9db4a9301..29f94a9c5 100644 --- a/src/libtriton/includes/triton/immediate.hpp +++ b/src/libtriton/includes/triton/immediate.hpp @@ -50,6 +50,9 @@ namespace triton { //! Constructor. TRITON_EXPORT Immediate(triton::uint64 value, triton::uint32 size /* bytes*/); + //! Constructor. + TRITON_EXPORT Immediate(double value, triton::uint32 size /* bytes */, triton::arch::endianness_e platform_endianness); + //! Constructor by copy. TRITON_EXPORT Immediate(const Immediate& other); diff --git a/src/libtriton/includes/triton/softfloat.hpp b/src/libtriton/includes/triton/softfloat.hpp new file mode 100644 index 000000000..6c47fa127 --- /dev/null +++ b/src/libtriton/includes/triton/softfloat.hpp @@ -0,0 +1,34 @@ +//! \file +/* +** Copyright (C) - Triton +** +** This program is under the terms of the Apache License 2.0. +*/ + +#ifndef TRITON_SOFTFLOAT_HPP +#define TRITON_SOFTFLOAT_HPP + +#include + +//! The Triton namespace +namespace triton { +/*! + * \addtogroup triton + * @{ + */ + //! The Softfloat namespace + namespace sf { + /*! + * \ingroup triton + * \addtogroup softfloat + * @{ + */ + + //! Cast 32-bit floating point value to 16-bit according to IEEE-754 + auto f32_to_f16(float value) -> uint16_t; + + } + +} + +#endif /* TRITON_SOFTFLOAT_HPP */ \ No newline at end of file diff --git a/src/libtriton/utils/softfloat.cpp b/src/libtriton/utils/softfloat.cpp new file mode 100644 index 000000000..8781b5d4a --- /dev/null +++ b/src/libtriton/utils/softfloat.cpp @@ -0,0 +1,42 @@ +//! \file +/* +** Copyright (C) - Triton +** +** This program is under the terms of the Apache License 2.0. +*/ + +#include + +#include + +namespace triton { + namespace sf { + + auto f32_to_f16(float value) -> uint16_t { + uint32_t f; + static_assert(sizeof(float) == sizeof(uint32_t), + "Unexpected float type size"); + std::memcpy(&f, &value, sizeof(uint32_t)); + uint16_t sign = (f >> 16) & 0x8000; + int16_t exponent = ((f >> 23) & 0xff) - 127 + 15; + uint16_t mantissa = (f >> 13) & 0x3ff; + if (exponent <= 0) { + if (exponent < -10) { + return sign; + } + mantissa = (mantissa | 0x400) >> (1 - exponent); + return sign | mantissa; + } else if (exponent == 0xff - (127 - 15)) { + if (mantissa) { + return sign | 0x7fff; + } else { + return sign | 0x7c00; + } + } else if (exponent > 30) { + return sign | 0x7c00; + } + return sign | (exponent << 10) | mantissa; + } + + } /* sf namespace */ +} /* triton namespace */ \ No newline at end of file From ae652c8daeb511d5fcc554b5b2e1417e0bdb60d3 Mon Sep 17 00:00:00 2001 From: Triton Library Date: Fri, 6 Sep 2024 09:58:27 +0200 Subject: [PATCH 07/18] Format code --- .build_number | 2 +- src/libtriton/arch/arm/aarch64/aarch64Cpu.cpp | 2 +- src/libtriton/arch/immediate.cpp | 26 +++++++++--------- src/libtriton/includes/triton/coreUtils.hpp | 5 +--- src/libtriton/includes/triton/softfloat.hpp | 13 ++++----- src/libtriton/utils/softfloat.cpp | 27 ++++++++++++------- 6 files changed, 39 insertions(+), 36 deletions(-) diff --git a/.build_number b/.build_number index 00e37d62b..4f6811f46 100644 --- a/.build_number +++ b/.build_number @@ -1 +1 @@ -1596 +1597 diff --git a/src/libtriton/arch/arm/aarch64/aarch64Cpu.cpp b/src/libtriton/arch/arm/aarch64/aarch64Cpu.cpp index e42f2c610..da9f2093c 100644 --- a/src/libtriton/arch/arm/aarch64/aarch64Cpu.cpp +++ b/src/libtriton/arch/arm/aarch64/aarch64Cpu.cpp @@ -543,8 +543,8 @@ namespace triton { imm.setShiftValue(op->shift.value); inst.operands.push_back(triton::arch::OperandWrapper(imm)); - } break; + } default: /* NOTE: CIMM, and missing one are not supported yet. */ diff --git a/src/libtriton/arch/immediate.cpp b/src/libtriton/arch/immediate.cpp index a3493d2cc..40616e0f3 100644 --- a/src/libtriton/arch/immediate.cpp +++ b/src/libtriton/arch/immediate.cpp @@ -30,35 +30,33 @@ namespace triton { this->setValue(value, size); } + Immediate::Immediate(double value, triton::uint32 size /* bytes */, triton::arch::endianness_e platform_endianness) { triton::uint64 imm_value; - auto need_swap = sys_endianness != platform_endianness; - if (size == sizeof(double)) { - static_assert(sizeof(double) == sizeof(triton::uint64), - "Unexpected double type size"); + if (size == sizeof(double)) { + static_assert(sizeof(double) == sizeof(triton::uint64), "Unexpected double type size"); std::memcpy(&imm_value, &value, sizeof(double)); if (need_swap) { imm_value = utils::byteswap(imm_value); } } + else if (size == sizeof(float)) { // single-precision float fvalue = static_cast(value); triton::uint32 repr; - static_assert(sizeof(float) == sizeof(uint32_t), - "Unexpected float type size"); + static_assert(sizeof(float) == sizeof(uint32_t), "Unexpected float type size"); std::memcpy(&repr, &fvalue, sizeof(float)); - - imm_value = need_swap ? static_cast(utils::byteswap(repr)) - : static_cast(repr); - } else if (size == 2) { // half-precision + imm_value = need_swap ? static_cast(utils::byteswap(repr)) : static_cast(repr); + } + + else if (size == 2) { // half-precision float fvalue = static_cast(value); - triton::uint16 repr = sf::f32_to_f16(fvalue); - imm_value = need_swap ? static_cast(utils::byteswap(repr)) - : static_cast(repr); - + triton::uint16 repr = triton::sf::f32_to_f16(fvalue); + imm_value = need_swap ? static_cast(utils::byteswap(repr)) : static_cast(repr); } + else { throw triton::exceptions::Immediate("Immediate::Immediate(double): Invalid encoding size."); } diff --git a/src/libtriton/includes/triton/coreUtils.hpp b/src/libtriton/includes/triton/coreUtils.hpp index 660523cb9..b9aaf1579 100644 --- a/src/libtriton/includes/triton/coreUtils.hpp +++ b/src/libtriton/includes/triton/coreUtils.hpp @@ -83,10 +83,7 @@ namespace triton { template <> TRITON_EXPORT triton::uint512 cast(const triton::uint80& value); template - std::enable_if_t< - std::is_unsigned_v, - T> - byteswap(T value) { + std::enable_if_t, T> byteswap(T value) { std::array repr; std::memcpy(&repr, &value, sizeof(value)); std::reverse(repr.begin(), repr.end()); diff --git a/src/libtriton/includes/triton/softfloat.hpp b/src/libtriton/includes/triton/softfloat.hpp index 6c47fa127..fc2df6abf 100644 --- a/src/libtriton/includes/triton/softfloat.hpp +++ b/src/libtriton/includes/triton/softfloat.hpp @@ -23,12 +23,13 @@ namespace triton { * \addtogroup softfloat * @{ */ - - //! Cast 32-bit floating point value to 16-bit according to IEEE-754 - auto f32_to_f16(float value) -> uint16_t; - } + //! Cast 32-bit floating point value to 16-bit according to IEEE-754 + auto f32_to_f16(float value) -> uint16_t; -} + /*! @} End of softfloat namespace */ + }; +/*! @} End of triton namespace */ +}; -#endif /* TRITON_SOFTFLOAT_HPP */ \ No newline at end of file +#endif /* TRITON_SOFTFLOAT_HPP */ diff --git a/src/libtriton/utils/softfloat.cpp b/src/libtriton/utils/softfloat.cpp index 8781b5d4a..be02c0cbd 100644 --- a/src/libtriton/utils/softfloat.cpp +++ b/src/libtriton/utils/softfloat.cpp @@ -12,31 +12,38 @@ namespace triton { namespace sf { - auto f32_to_f16(float value) -> uint16_t { + uint16_t f32_to_f16(float value) { uint32_t f; - static_assert(sizeof(float) == sizeof(uint32_t), - "Unexpected float type size"); + + static_assert(sizeof(float) == sizeof(uint32_t), "Unexpected float type size"); std::memcpy(&f, &value, sizeof(uint32_t)); - uint16_t sign = (f >> 16) & 0x8000; - int16_t exponent = ((f >> 23) & 0xff) - 127 + 15; - uint16_t mantissa = (f >> 13) & 0x3ff; + + uint16_t sign = ((f >> 16) & 0x8000); + int16_t exponent = ((f >> 23) & 0xff) - 127 + 15; + uint16_t mantissa = ((f >> 13) & 0x3ff); + if (exponent <= 0) { if (exponent < -10) { return sign; } mantissa = (mantissa | 0x400) >> (1 - exponent); return sign | mantissa; - } else if (exponent == 0xff - (127 - 15)) { + } + + else if (exponent == 0xff - (127 - 15)) { if (mantissa) { return sign | 0x7fff; } else { return sign | 0x7c00; } - } else if (exponent > 30) { + } + + else if (exponent > 30) { return sign | 0x7c00; } + return sign | (exponent << 10) | mantissa; } - } /* sf namespace */ -} /* triton namespace */ \ No newline at end of file + }; /* sf namespace */ +}; /* triton namespace */ From e9c72de4385affcd03d517dae2b1bddbf1688314 Mon Sep 17 00:00:00 2001 From: Selim Sandal <49725809+selimsandal@users.noreply.github.com> Date: Tue, 1 Oct 2024 10:51:58 +0300 Subject: [PATCH 08/18] Fix build error on macos --- CMakeModules/FindBITWUZLA.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeModules/FindBITWUZLA.cmake b/CMakeModules/FindBITWUZLA.cmake index 518ea4708..e3784d966 100644 --- a/CMakeModules/FindBITWUZLA.cmake +++ b/CMakeModules/FindBITWUZLA.cmake @@ -23,7 +23,7 @@ endif() if(NOT BITWUZLA_INCLUDE_DIRS AND NOT BITWUZLA_LIBRARIES) find_path(BITWUZLA_INCLUDE_DIR - NAMES bitwuzla/bitwuzla.h + NAMES bitwuzla/bitwuzla.h bitwuzla/cpp/bitwuzla.h PATHS ${BITWUZLA_PKGCONF_INCLUDE_DIRS} ) From ade1773a67a3cb9e3b61d5a692de49ced629adbe Mon Sep 17 00:00:00 2001 From: Christian Heitman Date: Mon, 15 Jul 2024 16:59:31 -0300 Subject: [PATCH 09/18] Update Python workflow --- .github/workflows/python.yml | 12 +++++++++++- src/scripts/docker/Dockerfile | 4 ++-- src/scripts/docker/build-wheel-linux.sh | 18 ++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index 5354db03f..21172d910 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -62,7 +62,7 @@ jobs: runs-on: windows-latest strategy: matrix: - python-version: ['3.8', '3.9', '3.10', '3.11'] + python-version: ['3.8', '3.9', '3.10', '3.11', '3.12', '3.13'] include: - python-version: 3.8 pycp: cp38-cp38 @@ -76,6 +76,12 @@ jobs: - python-version: 3.11 pycp: cp311-cp311 pylib: python311.lib + - python-version: 3.12 + pycp: cp312-cp312 + pylib: python312.lib + - python-version: 3.13 + pycp: cp313-cp313 + pylib: python313.lib steps: - name: Checkout uses: actions/checkout@v3 @@ -155,6 +161,10 @@ jobs: pycp: cp39-cp39 - python-version: 3.10 pycp: cp310-cp310 + # - python-version: 3.11 + # pycp: cp311-cp311 + # - python-version: 3.12 + # pycp: cp312-cp312 steps: - name: Checkout uses: actions/checkout@v3 diff --git a/src/scripts/docker/Dockerfile b/src/scripts/docker/Dockerfile index cf08bac3b..bae7bd2a1 100644 --- a/src/scripts/docker/Dockerfile +++ b/src/scripts/docker/Dockerfile @@ -1,4 +1,4 @@ -ARG BASE_IMG +ARG BASE_IMG=quay.io/pypa/manylinux_2_28_x86_64 FROM $BASE_IMG @@ -41,7 +41,7 @@ RUN echo "[+] Download, build and install Bitwuzla" && \ git clone https://github.com/bitwuzla/bitwuzla.git && \ cd bitwuzla && \ git checkout -b 0.4.0 0.4.0 && \ - export PY310_PATH=$(dirname $(realpath -L $(which python3.10))) && \ + export PY310_PATH=$(python3.10 -c 'import sys, os; print(os.path.dirname(sys.executable))') && \ CC=clang CXX=clang++ PATH=$PATH:$PY310_PATH python3.10 ./configure.py --shared --prefix $(pwd)/install && \ cd build && \ PATH=$PATH:$PY310_PATH ninja -j $(nproc) install diff --git a/src/scripts/docker/build-wheel-linux.sh b/src/scripts/docker/build-wheel-linux.sh index fab3e7794..72879e714 100755 --- a/src/scripts/docker/build-wheel-linux.sh +++ b/src/scripts/docker/build-wheel-linux.sh @@ -71,6 +71,24 @@ export PYTHON_LIBRARY=$($PYTHON_BINARY -c "from sysconfig import get_paths; prin $PYTHON_BINARY -m build --wheel --outdir $WHEEL_DIR/linux_x86_64 +# Build Triton Python wheel package for Python 3.12. +echo "[+] Build Triton wheel package for Python 3.12" +cd $SOURCE_DIR +export PYTHON_BINARY=/opt/_internal/cpython-3.12.*/bin/python +export PYTHON_INCLUDE_DIRS=$($PYTHON_BINARY -c "from sysconfig import get_paths; print(get_paths()['include'])") +export PYTHON_LIBRARY=$($PYTHON_BINARY -c "from sysconfig import get_paths; print(get_paths()['include'])") + +$PYTHON_BINARY -m build --wheel --outdir $WHEEL_DIR/linux_x86_64 + +# Build Triton Python wheel package for Python 3.13. +echo "[+] Build Triton wheel package for Python 3.13" +cd $SOURCE_DIR +export PYTHON_BINARY=/opt/_internal/cpython-3.13.0/bin/python +export PYTHON_INCLUDE_DIRS=$($PYTHON_BINARY -c "from sysconfig import get_paths; print(get_paths()['include'])") +export PYTHON_LIBRARY=$($PYTHON_BINARY -c "from sysconfig import get_paths; print(get_paths()['include'])") + +$PYTHON_BINARY -m build --wheel --outdir $WHEEL_DIR/linux_x86_64 + # Repair wheels. echo "[+] Repair wheel packages" cd $SOURCE_DIR From 3dbe302679ce6df50d7c6df45a0a8fd34acf5d27 Mon Sep 17 00:00:00 2001 From: Christian Heitman Date: Tue, 15 Oct 2024 14:36:46 -0300 Subject: [PATCH 10/18] Fix Linux workflow --- .github/workflows/linux.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index 523b711af..aaa4e0935 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -26,7 +26,7 @@ jobs: - name: Install dependencies run: | - sudo apt-get install python-setuptools libboost-dev libgmp-dev + sudo apt-get install python3-setuptools libboost-dev libgmp-dev - name: Install LLVM and Clang uses: KyleMayes/install-llvm-action@v1 From 8cfdc16d08ca73ac436cf4aae6574c1257201df5 Mon Sep 17 00:00:00 2001 From: Christian Heitman Date: Tue, 15 Oct 2024 15:39:37 -0300 Subject: [PATCH 11/18] Fix CodeCov workflow --- .github/workflows/codecov.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index d329f91d6..b9cea758a 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -23,7 +23,7 @@ jobs: - name: Install dependencies run: | - sudo apt-get install python-setuptools lcov libboost-dev libgmp-dev + sudo apt-get install python3-setuptools lcov libboost-dev libgmp-dev - name: Install LLVM and Clang uses: KyleMayes/install-llvm-action@v1 From 80168f68955bb2d2f4c22e62cf1b9a07fa9979ad Mon Sep 17 00:00:00 2001 From: Christian Heitman Date: Wed, 16 Oct 2024 14:07:08 -0300 Subject: [PATCH 12/18] Fix CodeCov workflow --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5b553fa44..c4625c5cd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -128,7 +128,7 @@ if(${CMAKE_SYSTEM_NAME} MATCHES "Linux" AND GCOV) COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target test-python COMMAND ctest --output-on-failure COMMAND lcov --no-checksum --directory $ --capture --output-file coverage.info - COMMAND lcov --remove coverage.info '/usr*' --remove coverage.info 'pintools*' --remove coverage.info 'examples*' -o coverage.info + COMMAND lcov --ignore-errors unused --remove coverage.info '/usr*' --remove coverage.info 'pintools*' --remove coverage.info 'examples*' -o coverage.info COMMAND genhtml coverage.info -o coverage COMMAND ${CMAKE_COMMAND} -E echo "-- Report generated in ${CMAKE_CURRENT_BINARY_DIR}/coverage/index.html" ) From 6fc90db0c8045473ed956a4c34ad9c12cf6594fe Mon Sep 17 00:00:00 2001 From: llaubin <20232176+llaubin@users.noreply.github.com> Date: Sun, 13 Oct 2024 15:55:56 +0200 Subject: [PATCH 13/18] Fix unsupported instruction for some bics variant --- src/libtriton/arch/arm/aarch64/aarch64Specifications.cpp | 1 + src/testers/aarch64/unicorn_test_aarch64.py | 3 +++ 2 files changed, 4 insertions(+) diff --git a/src/libtriton/arch/arm/aarch64/aarch64Specifications.cpp b/src/libtriton/arch/arm/aarch64/aarch64Specifications.cpp index 04a3a799b..3359ce142 100644 --- a/src/libtriton/arch/arm/aarch64/aarch64Specifications.cpp +++ b/src/libtriton/arch/arm/aarch64/aarch64Specifications.cpp @@ -405,6 +405,7 @@ namespace triton { break; case triton::extlibs::capstone::ARM64_INS_BIC: + case triton::extlibs::capstone::ARM64_INS_BICS: tritonId = triton::arch::arm::aarch64::ID_INS_BIC; break; diff --git a/src/testers/aarch64/unicorn_test_aarch64.py b/src/testers/aarch64/unicorn_test_aarch64.py index 39f354c14..ed4c67307 100644 --- a/src/testers/aarch64/unicorn_test_aarch64.py +++ b/src/testers/aarch64/unicorn_test_aarch64.py @@ -2071,6 +2071,9 @@ (b"\x22\x70\x1d\x72", "bics w2, w1, #7"), (b"\x22\x78\x1c\x72", "bics w2, w1, #8"), + (b"\x02\x00\x21\x6a", "bics w2, w0, w1"), + (b"\x1f\x00\x21\x6a", "bics wzr, w0, w1"), + (b"\x28\x00\x00\x37", "tbnz w8, #0, #4"), (b"\x48\x00\x00\x36", "tbz w8, #0, #0"), From 2a34605e9b156c4e8a296bc525a9bc2f63c0a571 Mon Sep 17 00:00:00 2001 From: Christian Heitman Date: Mon, 15 Jul 2024 16:59:31 -0300 Subject: [PATCH 14/18] Update Python workflow --- .github/workflows/python.yml | 12 +++++++++++- src/scripts/docker/Dockerfile | 4 ++-- src/scripts/docker/build-wheel-linux.sh | 18 ++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index 5354db03f..21172d910 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -62,7 +62,7 @@ jobs: runs-on: windows-latest strategy: matrix: - python-version: ['3.8', '3.9', '3.10', '3.11'] + python-version: ['3.8', '3.9', '3.10', '3.11', '3.12', '3.13'] include: - python-version: 3.8 pycp: cp38-cp38 @@ -76,6 +76,12 @@ jobs: - python-version: 3.11 pycp: cp311-cp311 pylib: python311.lib + - python-version: 3.12 + pycp: cp312-cp312 + pylib: python312.lib + - python-version: 3.13 + pycp: cp313-cp313 + pylib: python313.lib steps: - name: Checkout uses: actions/checkout@v3 @@ -155,6 +161,10 @@ jobs: pycp: cp39-cp39 - python-version: 3.10 pycp: cp310-cp310 + # - python-version: 3.11 + # pycp: cp311-cp311 + # - python-version: 3.12 + # pycp: cp312-cp312 steps: - name: Checkout uses: actions/checkout@v3 diff --git a/src/scripts/docker/Dockerfile b/src/scripts/docker/Dockerfile index cf08bac3b..bae7bd2a1 100644 --- a/src/scripts/docker/Dockerfile +++ b/src/scripts/docker/Dockerfile @@ -1,4 +1,4 @@ -ARG BASE_IMG +ARG BASE_IMG=quay.io/pypa/manylinux_2_28_x86_64 FROM $BASE_IMG @@ -41,7 +41,7 @@ RUN echo "[+] Download, build and install Bitwuzla" && \ git clone https://github.com/bitwuzla/bitwuzla.git && \ cd bitwuzla && \ git checkout -b 0.4.0 0.4.0 && \ - export PY310_PATH=$(dirname $(realpath -L $(which python3.10))) && \ + export PY310_PATH=$(python3.10 -c 'import sys, os; print(os.path.dirname(sys.executable))') && \ CC=clang CXX=clang++ PATH=$PATH:$PY310_PATH python3.10 ./configure.py --shared --prefix $(pwd)/install && \ cd build && \ PATH=$PATH:$PY310_PATH ninja -j $(nproc) install diff --git a/src/scripts/docker/build-wheel-linux.sh b/src/scripts/docker/build-wheel-linux.sh index fab3e7794..72879e714 100755 --- a/src/scripts/docker/build-wheel-linux.sh +++ b/src/scripts/docker/build-wheel-linux.sh @@ -71,6 +71,24 @@ export PYTHON_LIBRARY=$($PYTHON_BINARY -c "from sysconfig import get_paths; prin $PYTHON_BINARY -m build --wheel --outdir $WHEEL_DIR/linux_x86_64 +# Build Triton Python wheel package for Python 3.12. +echo "[+] Build Triton wheel package for Python 3.12" +cd $SOURCE_DIR +export PYTHON_BINARY=/opt/_internal/cpython-3.12.*/bin/python +export PYTHON_INCLUDE_DIRS=$($PYTHON_BINARY -c "from sysconfig import get_paths; print(get_paths()['include'])") +export PYTHON_LIBRARY=$($PYTHON_BINARY -c "from sysconfig import get_paths; print(get_paths()['include'])") + +$PYTHON_BINARY -m build --wheel --outdir $WHEEL_DIR/linux_x86_64 + +# Build Triton Python wheel package for Python 3.13. +echo "[+] Build Triton wheel package for Python 3.13" +cd $SOURCE_DIR +export PYTHON_BINARY=/opt/_internal/cpython-3.13.0/bin/python +export PYTHON_INCLUDE_DIRS=$($PYTHON_BINARY -c "from sysconfig import get_paths; print(get_paths()['include'])") +export PYTHON_LIBRARY=$($PYTHON_BINARY -c "from sysconfig import get_paths; print(get_paths()['include'])") + +$PYTHON_BINARY -m build --wheel --outdir $WHEEL_DIR/linux_x86_64 + # Repair wheels. echo "[+] Repair wheel packages" cd $SOURCE_DIR From 180761b9bbcb71673ac1d0a6d8507d7cec7cd892 Mon Sep 17 00:00:00 2001 From: Christian Heitman Date: Tue, 15 Oct 2024 14:36:46 -0300 Subject: [PATCH 15/18] Fix Linux workflow --- .github/workflows/linux.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index 523b711af..aaa4e0935 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -26,7 +26,7 @@ jobs: - name: Install dependencies run: | - sudo apt-get install python-setuptools libboost-dev libgmp-dev + sudo apt-get install python3-setuptools libboost-dev libgmp-dev - name: Install LLVM and Clang uses: KyleMayes/install-llvm-action@v1 From 2076b0f982729b7303318277c1f02aecbe699b24 Mon Sep 17 00:00:00 2001 From: Christian Heitman Date: Tue, 15 Oct 2024 15:39:37 -0300 Subject: [PATCH 16/18] Fix CodeCov workflow --- .github/workflows/codecov.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index d329f91d6..b9cea758a 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -23,7 +23,7 @@ jobs: - name: Install dependencies run: | - sudo apt-get install python-setuptools lcov libboost-dev libgmp-dev + sudo apt-get install python3-setuptools lcov libboost-dev libgmp-dev - name: Install LLVM and Clang uses: KyleMayes/install-llvm-action@v1 From 46b75c9b673c2243f78bc3c74d1669771216ef26 Mon Sep 17 00:00:00 2001 From: Christian Heitman Date: Wed, 16 Oct 2024 14:07:08 -0300 Subject: [PATCH 17/18] Fix CodeCov workflow --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5b553fa44..c4625c5cd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -128,7 +128,7 @@ if(${CMAKE_SYSTEM_NAME} MATCHES "Linux" AND GCOV) COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target test-python COMMAND ctest --output-on-failure COMMAND lcov --no-checksum --directory $ --capture --output-file coverage.info - COMMAND lcov --remove coverage.info '/usr*' --remove coverage.info 'pintools*' --remove coverage.info 'examples*' -o coverage.info + COMMAND lcov --ignore-errors unused --remove coverage.info '/usr*' --remove coverage.info 'pintools*' --remove coverage.info 'examples*' -o coverage.info COMMAND genhtml coverage.info -o coverage COMMAND ${CMAKE_COMMAND} -E echo "-- Report generated in ${CMAKE_CURRENT_BINARY_DIR}/coverage/index.html" ) From ec187d996d117640462088bbcdebc197658c5a60 Mon Sep 17 00:00:00 2001 From: Selim Sandal <49725809+selimsandal@users.noreply.github.com> Date: Tue, 1 Oct 2024 10:51:58 +0300 Subject: [PATCH 18/18] Fix build error on macos --- CMakeModules/FindBITWUZLA.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeModules/FindBITWUZLA.cmake b/CMakeModules/FindBITWUZLA.cmake index 518ea4708..e3784d966 100644 --- a/CMakeModules/FindBITWUZLA.cmake +++ b/CMakeModules/FindBITWUZLA.cmake @@ -23,7 +23,7 @@ endif() if(NOT BITWUZLA_INCLUDE_DIRS AND NOT BITWUZLA_LIBRARIES) find_path(BITWUZLA_INCLUDE_DIR - NAMES bitwuzla/bitwuzla.h + NAMES bitwuzla/bitwuzla.h bitwuzla/cpp/bitwuzla.h PATHS ${BITWUZLA_PKGCONF_INCLUDE_DIRS} )