public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Get struct function* from function declaration
@ 2021-02-16  8:51 Shuai Wang
  2021-02-16 11:52 ` Martin Liška
  0 siblings, 1 reply; 9+ messages in thread
From: Shuai Wang @ 2021-02-16  8:51 UTC (permalink / raw)
  To: GCC Development

Hello,

I am doing some inter-procedural analysis and suppose I have encountered
the following function calls:

int a = foo(b, c);

And I have accessed a tree representing foo as follows:

              tree func_decl = gimple_call_fndecl(stmt); // stmt is the
function call statement

where func_decl is "foo".

However, can I proceed further to get the function* of "foo"? I checked
relevant code samples (e.g., https://code.woboq.org/gcc/gcc/function.h.html#),
but couldn't find a way of doing so.

Any advice would be appreciated. Thank you.

Best,
Shuai

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Get struct function* from function declaration
  2021-02-16  8:51 Get struct function* from function declaration Shuai Wang
@ 2021-02-16 11:52 ` Martin Liška
  2021-02-16 12:13   ` Shuai Wang
  0 siblings, 1 reply; 9+ messages in thread
From: Martin Liška @ 2021-02-16 11:52 UTC (permalink / raw)
  To: Shuai Wang, GCC Development

On 2/16/21 9:51 AM, Shuai Wang via Gcc wrote:
> However, can I proceed further to get the function* of "foo"? I checked

Hello.

Yes, you can call DECL_STRUCT_FUNCTION(func_decl).

Martin

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Get struct function* from function declaration
  2021-02-16 11:52 ` Martin Liška
@ 2021-02-16 12:13   ` Shuai Wang
  2021-02-16 13:22     ` Shubham Narlawar
  2021-02-16 18:36     ` Martin Liška
  0 siblings, 2 replies; 9+ messages in thread
From: Shuai Wang @ 2021-02-16 12:13 UTC (permalink / raw)
  To: Martin Liška; +Cc: GCC Development

Thank you for your reply!

I tried to use this in the following code:

 tree func_decl = gimple_call_fndecl(stmt); // stmt is the function call
statement
function* f = DECL_STRUCT_FUNCTION(func_decl);
std::cerr << get_name(f->decl) << std::endl;

However, it seems the last line throws an exception as follows:

during GIMPLE pass: XXX  // here XXX is my pass name
internal compiler error: Segmentation fault
...

That seems strange. Does that seem familiar to you by any chance? Thank
you!

Best,
Shuai



On Tue, Feb 16, 2021 at 7:52 PM Martin Liška <mliska@suse.cz> wrote:

> On 2/16/21 9:51 AM, Shuai Wang via Gcc wrote:
> > However, can I proceed further to get the function* of "foo"? I checked
>
> Hello.
>
> Yes, you can call DECL_STRUCT_FUNCTION(func_decl).
>
> Martin
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Get struct function* from function declaration
  2021-02-16 12:13   ` Shuai Wang
@ 2021-02-16 13:22     ` Shubham Narlawar
  2021-02-16 13:37       ` Shuai Wang
  2021-02-16 18:36     ` Martin Liška
  1 sibling, 1 reply; 9+ messages in thread
From: Shubham Narlawar @ 2021-02-16 13:22 UTC (permalink / raw)
  To: Shuai Wang; +Cc: Martin Liška, GCC Development

On Tue, 16 Feb, 2021, 5:55 PM Shuai Wang via Gcc, <gcc@gcc.gnu.org> wrote:

> Thank you for your reply!
>
> I tried to use this in the following code:
>
>  tree func_decl = gimple_call_fndecl(stmt); // stmt is the function call
> statement
> function* f = DECL_STRUCT_FUNCTION(func_decl);
> std::cerr << get_name(f->decl) << std::endl;
>
> However, it seems the last line throws an exception as follows:
>
> during GIMPLE pass: XXX  // here XXX is my pass name
> internal compiler error: Segmentation fault
>

Hi,

Check whether f is a valid function pointer or not. It seems the function
is inlined. You can verify it with -fno-inline.

Macros defined in tree.h are helpful to get a broader view of the problem.

Shubham

...
>
> That seems strange. Does that seem familiar to you by any chance? Thank
> you!
>
> Best,
> Shuai
>
>
>
> On Tue, Feb 16, 2021 at 7:52 PM Martin Liška <mliska@suse.cz> wrote:
>
> > On 2/16/21 9:51 AM, Shuai Wang via Gcc wrote:
> > > However, can I proceed further to get the function* of "foo"? I checked
> >
> > Hello.
> >
> > Yes, you can call DECL_STRUCT_FUNCTION(func_decl).
> >
> > Martin
> >
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Get struct function* from function declaration
  2021-02-16 13:22     ` Shubham Narlawar
@ 2021-02-16 13:37       ` Shuai Wang
  0 siblings, 0 replies; 9+ messages in thread
From: Shuai Wang @ 2021-02-16 13:37 UTC (permalink / raw)
  To: Shubham Narlawar; +Cc: Martin Liška, GCC Development

[-- Attachment #1: Type: text/plain, Size: 1833 bytes --]

Hello Shubham,

Thank you for the reply! I see. That's strange, the `f` pointer turns out
to be invalid. May I ask when would the corresponding struct be
initialized?

And I am attaching the corresponding GIMPLE IR code. As you can see, I am
traversing to the following statement:

r_9 = foo (a_6, d_2);

And trigger the above procedure. Function `foo` has been traversed before
this statement which means the corresponding function* should have been
created already?

Best,
Shuai


On Tue, Feb 16, 2021 at 9:23 PM Shubham Narlawar <gsocshubham@gmail.com>
wrote:

>
>
> On Tue, 16 Feb, 2021, 5:55 PM Shuai Wang via Gcc, <gcc@gcc.gnu.org> wrote:
>
>> Thank you for your reply!
>>
>> I tried to use this in the following code:
>>
>>  tree func_decl = gimple_call_fndecl(stmt); // stmt is the function call
>> statement
>> function* f = DECL_STRUCT_FUNCTION(func_decl);
>> std::cerr << get_name(f->decl) << std::endl;
>>
>> However, it seems the last line throws an exception as follows:
>>
>> during GIMPLE pass: XXX  // here XXX is my pass name
>> internal compiler error: Segmentation fault
>>
>
> Hi,
>
> Check whether f is a valid function pointer or not. It seems the function
> is inlined. You can verify it with -fno-inline.
>
> Macros defined in tree.h are helpful to get a broader view of the problem.
>
> Shubham
>
> ...
>>
>> That seems strange. Does that seem familiar to you by any chance? Thank
>> you!
>>
>> Best,
>> Shuai
>>
>>
>>
>> On Tue, Feb 16, 2021 at 7:52 PM Martin Liška <mliska@suse.cz> wrote:
>>
>> > On 2/16/21 9:51 AM, Shuai Wang via Gcc wrote:
>> > > However, can I proceed further to get the function* of "foo"? I
>> checked
>> >
>> > Hello.
>> >
>> > Yes, you can call DECL_STRUCT_FUNCTION(func_decl).
>> >
>> > Martin
>> >
>>
>

[-- Attachment #2: test.c.234t.optimized --]
[-- Type: application/octet-stream, Size: 1066 bytes --]


;; Function __taint (__taint, funcdef_no=0, decl_uid=2398, cgraph_uid=1, symbol_order=0)

__taint (int a)
{
  int D.2421;
  int _2;

  <bb 2> :
  _2 = a_1(D);

  <bb 3> :
<L0>:
  return _2;

}



;; Function __taint_ptr (__taint_ptr, funcdef_no=1, decl_uid=2401, cgraph_uid=2, symbol_order=1)

__taint_ptr (int * a)
{
  int * D.2423;
  int * _2;

  <bb 2> :
  _2 = a_1(D);

  <bb 3> :
<L0>:
  return _2;

}



;; Function foo (foo, funcdef_no=2, decl_uid=2405, cgraph_uid=3, symbol_order=2)

foo (int a, int b)
{
  int c;
  int d;
  int D.2425;
  int _5;

  <bb 2> :
  d_1 = 12;
  c_4 = a_2(D) + b_3(D);
  _5 = c_4;

  <bb 3> :
<L0>:
  return _5;

}



;; Function main (main, funcdef_no=3, decl_uid=2412, cgraph_uid=4, symbol_order=4)

main (int argc, char * * argv)
{
  int b;
  int * pt1;
  int r;
  int * pt;
  int d;
  int a;
  int D.2427;
  int _13;

  <bb 2> :
  a_1 = 0;
  d_2 = 0;
  pt_3 = &t;
  a_6 = __taint (a_1);
  r_7 = 12;
  r_9 = foo (a_6, d_2);
  *pt_3 = a_6;
  pt1_11 = pt_3 + 16;
  b_12 = *pt1_11;
  _13 = 0;

  <bb 3> :
<L0>:
  return _13;

}



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Get struct function* from function declaration
  2021-02-16 12:13   ` Shuai Wang
  2021-02-16 13:22     ` Shubham Narlawar
@ 2021-02-16 18:36     ` Martin Liška
  2021-02-17  0:24       ` Shuai Wang
  1 sibling, 1 reply; 9+ messages in thread
From: Martin Liška @ 2021-02-16 18:36 UTC (permalink / raw)
  To: Shuai Wang; +Cc: GCC Development

On 2/16/21 1:13 PM, Shuai Wang wrote:
> std::cerr << get_name(f->decl) << std::endl;

You likely want to use cgraph_node::get (f->decl)->name ()

Martin

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Get struct function* from function declaration
  2021-02-16 18:36     ` Martin Liška
@ 2021-02-17  0:24       ` Shuai Wang
  2021-02-17  0:41         ` Shuai Wang
  0 siblings, 1 reply; 9+ messages in thread
From: Shuai Wang @ 2021-02-17  0:24 UTC (permalink / raw)
  To: Martin Liška; +Cc: GCC Development

Hello Martin,

Thanks for the information. I tried but it's the same error. Is it possibly
due to some bugs or something? I use gcc 10.1.0.

Best,
Shuai

On Wed, Feb 17, 2021 at 2:36 AM Martin Liška <mliska@suse.cz> wrote:

> On 2/16/21 1:13 PM, Shuai Wang wrote:
> > std::cerr << get_name(f->decl) << std::endl;
>
> You likely want to use cgraph_node::get (f->decl)->name ()
>
> Martin
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Get struct function* from function declaration
  2021-02-17  0:24       ` Shuai Wang
@ 2021-02-17  0:41         ` Shuai Wang
  2021-02-17  4:22           ` Shuai Wang
  0 siblings, 1 reply; 9+ messages in thread
From: Shuai Wang @ 2021-02-17  0:41 UTC (permalink / raw)
  To: Martin Liška; +Cc: GCC Development

I think the gist is that the recovered pointer itself is somehow NULL:

            tree current_fn_decl = gimple_call_fndecl(stmt);
            if (!current_fn_decl) return false;   // *this won't execute*
            struct function* fs0 = DECL_STRUCT_FUNCTION(current_fn_decl);
            if (!fs0) {
              std::cerr << "error pointer ! " << get_name(fs0->decl) <<
std::endl;   // *this will execute*
            }


On Wed, Feb 17, 2021 at 8:24 AM Shuai Wang <wangshuai901@gmail.com> wrote:

> Hello Martin,
>
> Thanks for the information. I tried but it's the same error. Is it
> possibly due to some bugs or something? I use gcc 10.1.0.
>
> Best,
> Shuai
>
> On Wed, Feb 17, 2021 at 2:36 AM Martin Liška <mliska@suse.cz> wrote:
>
>> On 2/16/21 1:13 PM, Shuai Wang wrote:
>> > std::cerr << get_name(f->decl) << std::endl;
>>
>> You likely want to use cgraph_node::get (f->decl)->name ()
>>
>> Martin
>>
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Get struct function* from function declaration
  2021-02-17  0:41         ` Shuai Wang
@ 2021-02-17  4:22           ` Shuai Wang
  0 siblings, 0 replies; 9+ messages in thread
From: Shuai Wang @ 2021-02-17  4:22 UTC (permalink / raw)
  To: Martin Liška; +Cc: GCC Development

Thank you again for the help. I think the root cause is that instead of
using GIMPLE_PASS, my analysis requires to do IPA. I am struggling in
switching to SIMPLE_IPA_PASS now....

Best,
Shuai

On Wed, Feb 17, 2021 at 8:41 AM Shuai Wang <wangshuai901@gmail.com> wrote:

> I think the gist is that the recovered pointer itself is somehow NULL:
>
>             tree current_fn_decl = gimple_call_fndecl(stmt);
>             if (!current_fn_decl) return false;   // *this won't execute*
>             struct function* fs0 = DECL_STRUCT_FUNCTION(current_fn_decl);
>             if (!fs0) {
>               std::cerr << "error pointer ! " << get_name(fs0->decl) <<
> std::endl;   // *this will execute*
>             }
>
>
> On Wed, Feb 17, 2021 at 8:24 AM Shuai Wang <wangshuai901@gmail.com> wrote:
>
>> Hello Martin,
>>
>> Thanks for the information. I tried but it's the same error. Is it
>> possibly due to some bugs or something? I use gcc 10.1.0.
>>
>> Best,
>> Shuai
>>
>> On Wed, Feb 17, 2021 at 2:36 AM Martin Liška <mliska@suse.cz> wrote:
>>
>>> On 2/16/21 1:13 PM, Shuai Wang wrote:
>>> > std::cerr << get_name(f->decl) << std::endl;
>>>
>>> You likely want to use cgraph_node::get (f->decl)->name ()
>>>
>>> Martin
>>>
>>

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2021-02-17  4:22 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-16  8:51 Get struct function* from function declaration Shuai Wang
2021-02-16 11:52 ` Martin Liška
2021-02-16 12:13   ` Shuai Wang
2021-02-16 13:22     ` Shubham Narlawar
2021-02-16 13:37       ` Shuai Wang
2021-02-16 18:36     ` Martin Liška
2021-02-17  0:24       ` Shuai Wang
2021-02-17  0:41         ` Shuai Wang
2021-02-17  4:22           ` Shuai Wang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).