Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this mean?: *(int32 *) 0 = 0;

Tags:

c

In the following piece of code, what does *(int32 *) 0 = 0; mean?

void function (void) {   ...    for (;;)      *(int32 *) 0 = 0;     /* What does this line do? */ } 

A few notes:

  • The code seems to not be reachable, as there is an exit statement before that particular piece of code.
  • int32 is typedef'ed but you shouldn't care too much about it.
  • This piece of code is from a language's runtime in a compiler, for anyone interested.
like image 677
NlightNFotis Avatar asked Aug 23 '13 16:08

NlightNFotis


People also ask

What is Int32 in C?

Int32: This Struct is used to represents 32-bit signed integer. The Int32 can store both types of values including negative and positive between the ranges of -2147483648 to +2147483647. Example : C#

What does 0 mean in code?

'\0' is referred to as NULL character or NULL terminator It is the character equivalent of integer 0(zero) as it refers to nothing In C language it is generally used to mark an end of a string.


2 Answers

The code is doing the following:

   for (;;) // while(true)      *(int32 *) 0 = 0; // Treat 0 as an address, de-reference the 0 address and try and store 0 into it. 

This should segfault, null pointer de-reference.

EDIT

Compiled and ran for further information:

#include <stdio.h> #include <stdlib.h> #include <stdint.h>  int main(void){   *(int32_t *) 0 = 0;   printf("done\n");   return 0; } 

gcc -g null.c; ./a.out

Program received signal SIGSEGV, Segmentation fault. 0x00000000004004cd in main () at null.c:7 7         *(int32_t *) 0 = 0; 
like image 121
Scotty Bauer Avatar answered Oct 03 '22 23:10

Scotty Bauer


Since the OP states the code was written by experienced compiler engineers, it is possible this is the intent of the code:

  • *(int32 *) 0 = 0; is recognized by this specific C implementation as code that causes behavior not defined by the C standard and known to this implementation to be illegal.
  • The for (;;) additionally indicates that this code is never exited.
  • The compiler engineers know that the optimizer will recognize this code and deduce that it may be “optimized away”, because any program that reaches this code is permitted to have any behavior, so the optimizer may choose to give it the behavior as if the code is never reached.1

This sort of reasoning is possible only if you have specific knowledge of the internal operation of a C implementation. It is the sort of thing a compiler engineer might include in special headers for a C implementation, perhaps to mark that certain code (such as code after an abort call) is never reached. It should never be used in normal programming.


1 For example, consider this code:

if (a)     for (;;)         *(int 32 *) 0 = 0; else     foo(); 

The compiler can recognize that the then-clause is permitted to have any behavior. Therefore, the compiler is free to choose what behavior it has. For simplicity, it chooses it to have the same behavior as foo();. Then the code becomes:

if (a)     foo(); else     foo(); 

and can be further simplified to:

foo(); 
like image 33
Eric Postpischil Avatar answered Oct 03 '22 22:10

Eric Postpischil