2022-02-17 22:55:42 +00:00
|
|
|
#ifndef LAIKA_VM_H
|
|
|
|
#define LAIKA_VM_H
|
|
|
|
|
|
|
|
/* Laika VM:
|
|
|
|
This is an obfuscation technique where vital code can be executed in a
|
|
|
|
stack-based VM, inlined into the function. The VM instruction-set is fairly
|
|
|
|
simple, see the OP_* for avaliable opcodes and their expected arguments.
|
|
|
|
*/
|
|
|
|
|
2022-03-21 19:59:04 +00:00
|
|
|
#include <inttypes.h>
|
|
|
|
|
|
|
|
#include "lerror.h"
|
|
|
|
|
2022-02-17 22:55:42 +00:00
|
|
|
#define LAIKA_VM_CODESIZE 512
|
|
|
|
#define LAIKA_VM_STACKSIZE 64
|
|
|
|
#define LAIKA_VM_CONSTSIZE 32
|
|
|
|
|
2022-03-21 22:47:46 +00:00
|
|
|
struct sLaikaV_vm_val {
|
2022-02-17 22:55:42 +00:00
|
|
|
union {
|
2022-03-21 19:59:04 +00:00
|
|
|
uint8_t i;
|
2022-02-17 22:55:42 +00:00
|
|
|
void *ptr;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-03-21 22:47:46 +00:00
|
|
|
struct sLaikaV_vm {
|
|
|
|
struct sLaikaV_vm_val stack[LAIKA_VM_STACKSIZE];
|
|
|
|
struct sLaikaV_vm_val constList[LAIKA_VM_CONSTSIZE];
|
2022-02-17 22:55:42 +00:00
|
|
|
uint8_t code[LAIKA_VM_CODESIZE];
|
|
|
|
int pc;
|
|
|
|
};
|
|
|
|
|
2022-03-21 22:47:46 +00:00
|
|
|
#define LAIKA_MAKE_VM(consts, code) (struct sLaikaV_vm)({.constList = consts, .code = code, .pc = 0})
|
2022-02-17 22:55:42 +00:00
|
|
|
|
2022-03-21 20:24:05 +00:00
|
|
|
/* constants */
|
2022-03-21 22:47:46 +00:00
|
|
|
#define LAIKA_MAKE_VM_INT(i) (struct sLaikaV_vm_val)({.i = i})
|
|
|
|
#define LAIKA_MAKE_VM_PTR(ptr) (struct sLaikaV_vm_val)({.ptr = ptr})
|
2022-03-21 20:24:05 +00:00
|
|
|
/* instructions */
|
2022-03-21 19:59:04 +00:00
|
|
|
#define LAIKA_MAKE_VM_IA(opcode, a) opcode, a
|
|
|
|
#define LAIKA_MAKE_VM_IAB(opcode, a, b) opcode, a, b
|
|
|
|
#define LAIKA_MAKE_VM_IABC(opcode, a, b, c) opcode, a, b, c
|
|
|
|
|
2022-02-17 22:55:42 +00:00
|
|
|
enum {
|
|
|
|
OP_EXIT,
|
|
|
|
OP_LOADCONST, /* stk_indx[uint8_t] = const_indx[uint8_t] */
|
2022-03-21 19:59:04 +00:00
|
|
|
OP_READ, /* stk_indx[uint8_t] = *(int8_t*)stk_indx[uint8_t] */
|
|
|
|
OP_WRITE, /* *(uint8_t*)stk_indx[uint8_t] = stk_indx[uint8_t] */
|
2022-02-17 22:55:42 +00:00
|
|
|
|
|
|
|
/* arithmetic */
|
|
|
|
OP_ADD, /* stk_indx[uint8_t] = stk_indx[uint8_t] + stk_indx[uint8_t] */
|
|
|
|
OP_SUB, /* stk_indx[uint8_t] = stk_indx[uint8_t] - stk_indx[uint8_t] */
|
|
|
|
OP_MUL, /* stk_indx[uint8_t] = stk_indx[uint8_t] * stk_indx[uint8_t] */
|
|
|
|
OP_DIV, /* stk_indx[uint8_t] = stk_indx[uint8_t] / stk_indx[uint8_t] */
|
|
|
|
OP_AND, /* stk_indx[uint8_t] = stk_indx[uint8_t] & stk_indx[uint8_t] */
|
|
|
|
OP_OR, /* stk_indx[uint8_t] = stk_indx[uint8_t] | stk_indx[uint8_t] */
|
|
|
|
OP_XOR, /* stk_indx[uint8_t] = stk_indx[uint8_t] ^ stk_indx[uint8_t] */
|
|
|
|
|
|
|
|
/* control-flow */
|
2022-03-21 19:59:04 +00:00
|
|
|
OP_TESTJMP, /* if stk_indx[uint8_t] != 0, pc += [uint8_t] */
|
2022-02-17 22:55:42 +00:00
|
|
|
};
|
|
|
|
|
2022-03-21 22:47:46 +00:00
|
|
|
inline void laikaV_execute(struct sLaikaV_vm *vm) {
|
2022-03-21 19:59:04 +00:00
|
|
|
|
|
|
|
#define READBYTE (vm->code[vm->pc++])
|
|
|
|
#define BINOP(x) { \
|
|
|
|
uint8_t a = READBYTE; \
|
|
|
|
uint8_t b = READBYTE; \
|
|
|
|
uint8_t c = READBYTE; \
|
2022-03-21 20:24:05 +00:00
|
|
|
vm->stack[a].i = vm->stack[b].i x vm->stack[c].i; \
|
2022-03-21 19:59:04 +00:00
|
|
|
break; \
|
|
|
|
}
|
|
|
|
|
|
|
|
while (vm->code[vm->pc]) {
|
|
|
|
switch (vm->code[vm->pc++]) {
|
|
|
|
case OP_LOADCONST: {
|
|
|
|
uint8_t indx = READBYTE;
|
|
|
|
uint8_t constIndx = READBYTE;
|
|
|
|
vm->stack[indx] = vm->constList[constIndx];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OP_READ: {
|
|
|
|
uint8_t indx = READBYTE;
|
|
|
|
uint8_t ptr = READBYTE;
|
|
|
|
vm->stack[indx].i = *(uint8_t*)vm->stack[ptr].ptr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OP_WRITE: {
|
|
|
|
uint8_t ptr = READBYTE;
|
|
|
|
uint8_t indx = READBYTE;
|
|
|
|
*(uint8_t*)vm->stack[ptr].ptr = vm->stack[indx].i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OP_ADD: BINOP(+);
|
|
|
|
case OP_SUB: BINOP(-);
|
|
|
|
case OP_MUL: BINOP(*);
|
|
|
|
case OP_DIV: BINOP(/);
|
|
|
|
case OP_AND: BINOP(&);
|
|
|
|
case OP_OR: BINOP(|);
|
|
|
|
case OP_XOR: BINOP(^);
|
|
|
|
default:
|
|
|
|
LAIKA_ERROR("laikaV_execute: unknown opcode [%d]!", vm->code[vm->pc])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef READBYTE
|
|
|
|
#undef BINOP
|
|
|
|
}
|
|
|
|
|
2022-02-17 22:55:42 +00:00
|
|
|
#endif
|