Emit a variable defined in gcc

Martin Liška mliska@suse.cz
Wed Jul 1 11:14:10 GMT 2020


On 7/1/20 10:54 AM, Harshit Sharma wrote:
> Actually these are two separate things. My callback function to fetch shadow offset from user code is ready. This function is defined in my user code and will be called by compiler (quite similar to how __asan_stack_malloc_ function is implemented in gcc/asan.c).

All right.

> 
> Now in order to make sure that my users have applied this gcc patch to their compilers, I want a have a variable __asan_gcc_patch_present which will be defined inside gcc but will be used by user code. This way if they build the program without applying the patch, they will get a compile error as the reference to this variable will not be resolved.

Note that this detection is fragile as one can do:

new-gcc -fsanitize=address -c foo.c
old-gcc -fsanitize=address -c bar.c
new-gcc foo.o bar.o -fsanitize=address -o a.out

will be happy, but apparently bar.o is compiled with a wrong compiler.

What you want instead is to use ./xgcc -B. /tmp/foo.c -c -fsanitize=address -fdump-tree-optimized=/dev/stdout

...
_sub_I_00099_0 ()
{
   <bb 2> :
   __builtin___asan_init ();
   __builtin___asan_version_mismatch_check_v8 ();
   return;

}
...

as seen constructor of each compilation unit calls __builtin___asan_version_mismatch_check_v8. So you just want to bump
libsanitizer/asan/asan_init_version.h to __builtin___asan_version_mismatch_check_v10 (note that v9 is taken on some targets).
And change the builtin in  gcc/sanitizer.def.

Martin

> 
> This is how I have defined the variable. But I am not sure how I can make this variable accessible by the user code.
> 
>    id = get_identifier ("__asan_gcc_patch_present");
>    decl = build_decl (DECL_SOURCE_LOCATION (current_function_decl), VAR_DECL, id, integer_type_node);
>    DECL_INITIAL(decl) = integer_one_node;
> 
> Please tell me what I am doing wrong.
> 
> -Harshit
> 
> On Wed, Jul 1, 2020 at 12:03 AM Martin Liška <mliska@suse.cz <mailto:mliska@suse.cz>> wrote:
> 
>     On 6/30/20 6:52 PM, Harshit Sharma wrote:
>      > Hey Martin,
>      >
>      > Thanks for your reply. Actually I am trying to have a callback function allowing gcc to fetch shadow offset from runtime code.
> 
>     Ah, all right!
> 
>      >
>      > In order to make sure that my users have applied this patch before using asan feature, I want to define a variable in gcc (could be an integer) which will be referenced by the asan library in our source code. I think I used the wrong word 'emit' in my previous post. The variable say "__asan_gccpatch_present" needs to be defined in gcc and then referenced by our code (just like we do with functions such as __builtin_expect).
> 
>     Then what about adding a new ASAN variable that you will directly access from your source code?
>     What kind of interaction do you need? Or you can add a new ASAN builtin (gcc/sanitizer.def), something like
>     __asan_poison_stack_memory that can be used in a user code?
> 
>     Martin
> 
>      >
>      >
>      > Thanks,
>      > Harshit
>      >
>      > On Tue, Jun 30, 2020 at 12:34 AM Martin Liška <mliska@suse.cz <mailto:mliska@suse.cz> <mailto:mliska@suse.cz <mailto:mliska@suse.cz>>> wrote:
>      >
>      >     On 6/30/20 7:55 AM, Harshit Sharma via Gcc wrote:
>      >      > Hello,
>      >      > I am working on a gcc patch for asan.
>      >
>      >     Hey.
>      >
>      >     Good to hear, what kind of feature is that?
>      >
>      >      > The patch is almost ready except one
>      >      > thing. To make sure that the user has applied this patch before using asan
>      >      > feature, I want to declare an additional variable in gcc which is reference
>      >      > by our source code so that if this patch is missing, the user gets an error
>      >      > compiling the code because the reference to this variable will not be
>      >      > resolved.
>      >
>      >     A nice example can be emission of global variables that are used for -fprofile-generate:
>      >     see gcc/tree-profile.c:194-202.
>      >
>      >     Let me know if it helps?
>      >     Martin
>      >
>      >      >
>      >      > I am still new to gcc development. So, can anyone tell me how can I make
>      >      > gcc emit this variable?
>      >      >
>      >      >
>      >      > Thanks,
>      >      > Harshit
>      >      >
>      >
> 



More information about the Gcc mailing list