Reputation: 4980
Can I execute valid native executable assembly instruction by generating it from within running C code?
void execute_single_asm_instruction(char * ptr_asm, int length)
{
// ptr_asm[] = valid assembly instruction
execute_asm(ptr, length);
}
Is it possible to write execute_asm
?
This is running on bare-metal ARM i.e. the RTOS is custom and is not Linux, QNX, Windows, etc.
This is related to my previous question:
how to single-step code on-target with no jtag, breakpoints, simulator, emulator
Upvotes: 0
Views: 686
Reputation: 22440
It is technically impossible to do what you want safely; execute a single assembler instruction. The issue is 'context' or machine state. You need to extend the API like this,
extern void init_asm_context(void* context);
extern void execute_asm(void* context, char * ptr_asm, int length);
// context; global or declared.
// ptr_asm[] = valid assembly instruction
init_asm_context(&context);
execute_asm(&context, ptr, length);
The issue is that any assembler instruction can randomly change registers that the 'C' code depends on. For this reason, most people make up their own 'virtual machine' and make the machine language easier to decode. As you have no OS, it is difficult to think of a use case where you are trying to execute off the shelf code and have it work when there is no existing OS. To do this, you will have constantly context switch with every invocation. A solution that executes several instructions at a time will perform much quicker as you would not have to save/restore context with every instruction.
If you have a security application and performance is critical, I suggest you investigate proof carrying code which is a concept that tries keep performance and make guarantees about memory access. It is also not as patent encumber as virtual machines (at the moment).
Upvotes: 2