Hello,

I would like to announce that I have been able to compile GCC while checking it with a simple MELT plugin that I have wrote. This plugin is integrated into GCC by using the MELT plugin. I guess this is the first successfull use of MELT with a huge real program like GCC.

For those who are not really aware about MELT and how it works:
        MELT is available into 2 forms:
                -A GCC branch
                -A GCC plugin

        Here I have use MELT as a GCC plugin.

This GCC plugin loads a melt.so library which has severals modes. Firstly I have use the translatetomodule mode. This mode allows me to translate my plugin (which is a file "test_fopen.melt") into several C files and then into a dynamic test_fopen.so.

The command is the following:
gcc -fplugin=/usr/lib64/gcc/x86_64-mandriva-linux-gnu/4.6.0/plugin/melt.so \
        -fplugin-arg-melt-mode=translatetomodule \
        
-fplugin-arg-melt-arg=/home/mpierre/programmation/melt/test_fopen/test_fopen.melt
 \
    -c empty.c 2>fopen_compil_debug.txt



The second step is to compile GCC using CFLAGS which define that we use the MELT plugin with the mode test_fopen which is defined in my plugin and precising the location of the test_fopen.so. This allows me to execute the pass defined in my plugin.

The command is the following:
make CFLAGS="-fplugin=/usr/lib64/gcc/x86_64-mandriva-linux- gnu/4.6.0/plugin/melt.so -fplugin-arg-melt-mode=test_fopen -fplugin-arg-melt-init=@@:test_fopen -fplugin-arg-melt-module-path=$build -fplugin-arg-melt-source-path=$build -Wno-error"

What does my test_fopen.melt plugin?
It searchs each call to the fopen function and get the lhs (under the form of a tree). After the fopen calls, it looks at the next gimple. If this not a gimple_cond_equal or gimple_cond_notequal between the lhs of the call and NULL, it returns a warning, explaining that the call was not tested.

This is not perfect for now, it can find some false positifs, in some special cases, like this one:
s->file = fd < 0 ? F_OPEN(path, fmode) : (FILE*)fdopen(fd, fmode);
if (s->file == NULL) {
        ...
}
...

It comes from the fact, that at GIMPLE state, the expression is separate into severals and the test is not made immediatly but after a few gimples.
        
        However, it sends also some pertinents warnings:
        -in gcc/gcc.c line 3422, function driver_handle_option:
There is a call to fopen without test, I don't know much about what it does but can we be sure that the fopen always return somethings not NULL. note: in reality this is not the fopen function but the fopen_unlocked function which is called at this step (using a #define). I have adapted my plugin to search also for fopen_unlocked which call fopen without testing more the returned value.

        -in libcpp/files.c line 1468, function read_name_map:
                there is another call to the fopen function.

The full log of my GCC compilation can be found here: http://pvittet.com/GSOC/log.txt . The plugin that I have written can be found here: https://github.com/Piervit/GMWarn/tree/master/test_fopen .

If you have any questions or remarks, I would be glad to read them :)

Reply via email to