Friday 31 January 2014

Explain the action how C program works

1. Write a basic C program which prints a message on the screen, Hello World!-style -- something like this:
 
        #include <stdio.h>

       int main() {
           printf("Hello World!\n");
       }
 
2. Compile the program using the GCC compiler. Include these compiler options (refer to the gcc manpage for details):
-g               # enable debugging information
-O0              # do not optimize (that's a capital letter and then the digit zero)
-fno-builtin     # do not use builtin function optimizations

3. The resulting binary is an ELF (Executable and Linkable Format) file, which contains multiple sections. These sections may contain object code, link tables, debugging symbols, program data (such as constants and the initial values of variables), metadata about the program and ELF sections, and comments.
Examine the binary produced by the previous step using the objdump program. These options may be useful -- see the manpage for objdump for other options:
-f          # display header information for the entire file
-s          # display per-section summary information
-d          # disassemble sections containing code
--source    # (implies -d) show source code, if available, along with disassembly

4. Try to gain a basic understanding of what the compiled code is doing.
5. Recompile the code with these changes:
(1) Add the compiler option -static. Note and explain the change in size, section headers, and the function call.
 files' size of compiling originally:


the files' size of compiling with -static:




We can see the per-section summary information, disassemble sections and source code for static one are much more than original one.
 
 The static one include all the libraries in the compiled file, section headers 
are much bigger than original one. 
Function call for original:
 
function call for -static:
 
we can see the static directly call function from IO, because it includes everything it need.
 

(2) Remove the compiler option -fno-builtin. Note and explain the change in the function call.
original function call:

remove no-builtin:





We can see when remove no-builtin, the function call is optimized and directly call the 'puts' which is the parent of printf when you don't add any parameters.

(3) Remove the compiler option -g. Note and explain the change in size, section headers, and disassembly output.
original files size:


remove -g:
 

We can see when remove the -g, the pre-section info is smaller, because it doesn't include debug information any more.

(4) Add additional arguments to the printf() function in your program. Note which register each argument is placed in. (Tip: Use sequential integer arguments after the first string argument. Go up to 10 arguments and note the pattern).
 
 We can see the integer arguments is place in the registers one by one together. And they are moved backward. Here we can also find how stack works in the when compiler is working with registers.

(5) Move the printf() call to a separate function, and call that function from main(). Explain the changes in the object code.
original code:




When separate function:


We can see when separate the printf to another function, it will go to the function first, and then call printf.

(6) Remove -O0 and add -O3 to the gcc options. Note and explain the difference in the compiled code.
 when add -o3 optimization,






the code goes to the front, and the section info become inline.

Thats it.

Friday 17 January 2014

About Open Source Code Review

   
There are many open source software packages on the internet, and they are under different Licenses. I take a look at 2 of them and discuss something about them.

1. Bitcoin

Bitcoin is released under the MIT License. The contents of MIT License is below:

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

Their code contributing process is that developers work in their own trees, then submit pull requests when they think their feature or bug fix is ready. If it is a simple/trivial/non-controversial change, then one of the Bitcoin development team members simply pulls it.If it is a more complicated or potentially controversial change, then the patch submitter will be asked to start a discussion (if they haven't already) on the mailing list.
The patch will be accepted if there is broad consensus that it is a good thing. Developers should expect to rework and resubmit patches if the code doesn't match the project's coding conventions (see doc/coding.md) or are controversial.
The master branch is regularly built and tested, but is not guaranteed to be completely stable. Tags are created regularly to indicate new official, stable release versions of Bitcoin.

Bitcoin also suggests contributors to create unit test cases which will help the code review period shorter. They review codes very carefully cause this products cant be anything wrong, or it will cause users losing a lot of money. However, it will take long time to apply a patch.

2. RethinkDB
RethinkDB is an open-source distributed database. It has an intuitive query language, automatically parallelized queries, and simple administration. It is released under the terms of the GNU Affero General Public License, version 3.

The content is in the link: https://github.com/rethinkdb/rethinkdb/blob/next/COPYRIGHT

You can check out the source code from Github, make a fork and contribute. you need to add the mailing list to communicate with them, or go to IRC to chat with them.

There are many other open source package based on different Licenses, and code reviewing process is also different.