collab/polyglot-riscv-arm

what if the same machine code was valid riscv and arm asm

git clone https://git.julia.blue/collab/polyglot-riscv-arm

Jonathan Dönszelmannwebsite start885cc4e

main
1.2 KiB44 linesraw
1#include <fcntl.h>
2#include <capstone/capstone.h>
3#include <string.h>
4#include <unistd.h>
5#include <stdint.h>
6#include <sys/mman.h>
7
8int main(int argc, char* argv[]) {
9    csh handle;
10    if (cs_open(CS_ARCH_AARCH64, CS_MODE_ARM, &handle) != CS_ERR_OK) {
11        fprintf(stderr, "could not open arm64 disassembler\n");
12        return 1;
13    }
14    size_t n_verify = 0x0ffffffffULL + 1;
15    int fd = open(argv[1], O_RDWR);
16    if (fd < 0) {
17        perror("open");
18    }
19
20    int16_t* data = mmap(NULL, n_verify * sizeof(int16_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
21    if (data == (int16_t*) -1) {
22        perror("mmap");
23    }
24
25    size_t size = sizeof(uint32_t);
26    for (size_t i = 0; i < n_verify; i++) {
27        if (i % 10000000 == 0) {
28            printf("%.3f\n", (float) i / (float) n_verify * 100);
29        }
30        if (data[i] != -1) {
31            cs_insn* insn;
32            uint32_t idat = (uint32_t) i;
33            size_t count = cs_disasm(handle, (const uint8_t*) &idat, size, 0, 1, &insn);
34            if (count != 1) {
35                data[i] = -1;
36                continue;
37            }
38            cs_free(insn, 1);
39        }
40    }
41    close(fd);
42
43    return 0;
44}