"Illegal Instruction: 0xC000001D" while a bcc64x (Modern C++) application
Very often this runtime error shows when a function is called which SHOULD return a type but no return value was specified.
The Modern C++ compiler has a very useful feature in that it injects an UD2
as the last instruction in such a function.
The UD2
instruction:
Generates an invalid opcode. This instruction is provided for software testing to explicitly generate an invalid opcode. The opcode for this instruction is reserved for this purpose. Other than raising the invalid opcode exception, this instruction is the same as the NOP instruction.
For example this code:
int Test()
{
printf("Test()\n");
}
The assembler looks like this:
File1.cpp.16: printf("Test()\n");
00007FF669421654 488D0D4D2A0000 lea rcx, [rip + 0x2a4d]; .refptr._newmode + 8
00007FF66942165B E810030000 call 0x7ff669421970; printf
00007FF669421660 0F0B ud2
00007FF669421662 66666666662E0F1F840000000000 nop word ptr cs:[rax + rax]
At runtime you will get:
Project Project4.exe raised exception class 0xc000001d with message 'Exception 0xc000001d encountered at address 0x7ff669421660'.
Which means:
0xC000001D
STATUS_ILLEGAL_INSTRUCTION ntstatus.h
# {EXCEPTION}
# Illegal Instruction
# An attempt was made to execute an illegal instruction.
The compiler should also warn you if you have warnings switched ON:
[bcc64x Warning] File1.cpp(17): non-void function does not return a value [-Wreturn-type]
If you prefer to catch these warnings as errors at compile time you can do the following:
1) In the IDE go to:
Project | Options | Building | "C++ Compiler" | Advanced | "Other options" | "Additional options to pass to the compiler"
and add
-Werror=return-type
2) Or add this line to your code:
#pragma clang diagnostic error "-Wreturn-type"