Search code examples
frama-c

Looking for idea on overwrite a function during frama-c


I'm looking for idea on how to overwrite a function without modify the source. Like if I have foo() in the original source, I want to overwrite it with my own version with the same function name by adding it in a C file, which may also contains other overwrite functions. Sort of like strong/weak compilation. Currently I have to go in the source files and ifdef with __FRAMAC__. I don't want to touch the source files. Is there some kernel option to not use the second instance of foo() function?


Solution

  • Your question does not specify whether you want to replace a function declaration or a function definition. Since they are handled differently by Frama-C, I'm going to detail both.

    Duplicate definitions at the kernel level

    Currently, at the parsing level, there is no option in Frama-C to completely ignore the definition of a function that is present in one of the files given for parsing. The Frama-C AST will incorporate the definition of all functions it finds. There is no exact equivalent for strong/weak symbols.

    If a second definition for the same function is found, one of the following will happen:

    1. If both definitions occur in the same compilation unit, there is an error.

    2. If each definition happens in a different compilation unit, Frama-C will try to find a plausible solution:

      1. If both occurrences have the same signature, Frama-C will emit a warning such as:

        [kernel] b.c:2: Warning: 
          dropping duplicate def'n of func f at b.c:2 in favor of that at a.c:1
        

        In this case, you just need to ensure the definition you want appears later in the list of sources to be parsed.

      2. If the occurrences have different signatures, but one of the functions is never actually used, you may have a warning such as:

        [kernel:linker:drop-conflicting-unused] Warning: 
          Incompatible declaration for f:
          different number of arguments
          First declaration was at a.c:1
          Current declaration is at b.c:2
          Current declaration is unused, silently removing it
        

        However, if both occurrences are used, then you have an error:

        [kernel] User Error: Incompatible declaration for f:
          different type constructors: int vs. int *
          First declaration was at a.c:1
          Current declaration is at b.c:2
        

    Duplicate declarations at the kernel level

    Considering function declarations, Frama-C will, in accordance with the C standard, accept as many of them as are given, provided they are compatible. If they have ACSL specifications, those specifications will be merged.

    Multiple incompatible declarations are handled as before, with warnings or errors depending on whether both versions are used (in which case Frama-C is unable to choose).

    Plugin-specific options

    Plug-ins may have specific options to override the default behavior of functions in the AST. For instance, Eva has option -eva-use-spec <fns>, which tells the analysis to ignore the definitions of functions <fns>, using only their specifications instead.