Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

program execution is not started at main()

Tags:

c++

I developed many years in C and only now discovered that a program can execute code prior to main() function. Here is a code example

int generateNum(){
    // Some malicious code here...
    return 5;
}

static int someArray[] = {generateNum(),generateNum()}  

int main(){
     // Some code here...
}

The function generateNum() is called twice before main().

My questions are

  1. Who calls generateNum()? I know that on Windows it is crtexe()
  2. Is this behavior standardized on different platforms: Windows/Linux/Android/iOS?
  3. How can I get more information about this behavior? I want to search in Google, but I don't know how to describe it.
  4. Can I do anything I want inside the generateNum()? I mean, can I call malloc()? What about fopen() and fwrite()? Can I open a socket and send information over UDP? Eventually I can abuse this function and even call to main() from it :-)
like image 963
DanielHsH Avatar asked Nov 26 '13 13:11

DanielHsH


People also ask

What is main () in C programming?

Every C program has a primary (main) function that must be named main. If your code adheres to the Unicode programming model, you can use the wide-character version of main, wmain. The main function serves as the starting point for program execution.

Why program execution starts from main method?

The main method is the entry point to your program. If the class that contains the "main" method has static members that need to be initialized or a static code block, this will be executed BEFORE the "main" method. If you put a breakpoint in the object initialization line you will see it runs before the println line.

Can a program run without main () in C++?

The answer is yes. We can write program, that has no main() function. In many places, we have seen that the main() is the entry point of a program execution. Just from the programmers perspective this is true.

Is Main () a process?

It's a process that you spawn when you execute your program. The main function is called at the beginning of the program. It is all a part of the same program (i.e. one process).


1 Answers

  1. C++ guarantees that such initialisations take place before main. This can be taken care of by the operating system loader/linker, or by some special module linked against the object file that contained main. For gcc, this is described here: http://gcc.gnu.org/onlinedocs/gccint/Initialization.html
  2. Not quite. C++11, 3.6.2.4 (basic.start.init): It is implementation-defined whether the dynamic initialization of a non-local variable with static storage duration is done before the first statement of main. Note that initialization takes place before you can ever access that value, though, especially before there is any notion of reference to an entity in the same compilation unit.
  3. [basic.start.init] in the language standard is what you want to have a look at. The behaviour here is dynamic initialization for variables with static storage duration.
like image 111
creichen Avatar answered Oct 19 '22 22:10

creichen