Search code examples
memorycpu-registersinstructions

Can the CPU mistake data for instruction in Von Neumann architecture?


Since Von Neumann model stores both instructions and data in the same block of memory, I was wondering what can happen during the fetch-decode-execute cycle. Some points I am especially worried about:

  1. Is it possible that the IR will receive and store data instead of an instruction?

  2. Will the CPU try to execute data as if it were an instruction?

  3. What consequences will follow?

Thank you!


Solution

  • Yes, if there is a logic error in the program, then it can easily fetch data and attempt to execute that as instructions.

    Depending on the computer system, there may be protections against executing data — by keeping data and code separate, the processor can protect them by address.  Such protections would force the program to stop with an exception.

    If such protections are not present, or the data in question is actually in the code section, then yes, the processor will attempt to execute data as an instruction.  Data will generally not appear as sensible instructions, though sometimes they can execute without causing a fault or exception, in which case the processor will move on to the next data following and execute that, and so on.

    Common data patterns include all 0's and all 1's, so some processors like RISC V are designed so as to make those patterns illegal (cause exceptions).

    The results of executing data could be anything — all bets are off when that happens, but usually the program will simply end with a crash, though it could also go into an infinite loop (hang), or any other unpredictable behavior.

    However, computer viruses sometimes feed some malicious code into data (because data is mutable whereas code is not1) and make the processor execute that — some buffer overflow attacks do that.  Let's note also that buffer overflows are logic errors in the program that can be exploited.  In a buffer overflow attack, the program is reading data from an external source, so that external source could feed it data that happens to be machine code instructions, and also feed the vulnerable program too much data so as to corrupt a stacked return address — return addresses are involved in transfer of control of the processor to another memory location for further instruction execution.


    1Let's go the other way around — could the processor write data into code?  Yes, it could attempt that, but for this reason code is generally write protected so that attempt will stop the program with an exception.  But if it isn't protected, this writing data to code will corrupt the code (unless the program intends such self modification).  Later attempts to execute that corrupted code will involve the processor executing data as instructions.