Skip to contentSkip to conversation

Small help on trivial issue

DISCUSSION Vijay Khemka 4 participants 5 messages Aug 1, 2019 – Aug 2, 2019

lore
Share & tools
lore
#0 lore raw

Team,
I am having small issue of global json object corruption and I am unable to find reason for it. I am defining a global json object (nlohmann::json appSelData) at the start of file. I am initializing this object in register function and accessing this in different functions defined in same shared library. While accessing in different function, it doesn’t hold initialized data. This is failing in my storagecommands.cpp for IPMI.

Then I created a separate new file selcommands.cpp and did the same operation, it works here as expected. So I am wondering why it is failing in existing file vs new one. Looks like there are some header included in existing file is messing it up but I have no clue yet. This only happens with json object.

For your reference, I am attaching both below files Storagecommands.cpp – where this fails Selcommands.cpp – where it works.

I know it is a debug issue and I have been struggling last 2 days so thought of asking for help to team. Any help here would be appreciated.

Regards
-Vijay

#1 lore raw

I did not check the code details, but from the description, it is likely due to the global variable's initialization order in a shared library.

Basically, you need to specify __attribute__((init_priority(101))) for your global variables.

Please check below commit for details and how to manually specify the init order for such variables.
https://github.com/openbmc/phosphor-host-ipmid/commit/4b0ddb68b4e76420358589213bc400155fa12e43

Quoted 26 lines · Vijay Khemka

Team,

I am having small issue of global json object corruption and I am unable to find reason for it. I am defining a global json object (nlohmann::json appSelData) at the start of file. I am initializing this object in register function and accessing this in different functions defined in same shared library. While accessing in different function, it doesn’t hold initialized data. This is failing in my storagecommands.cpp for IPMI.

Then I created a separate new file selcommands.cpp and did the same operation, it works here as expected. So I am wondering why it is failing in existing file vs new one. Looks like there are some header included in existing file is messing it up but I have no clue yet. This only happens with json object.

For your reference, I am attaching both below files

Storagecommands.cpp – where this fails

Selcommands.cpp – where it works.

I know it is a debug issue and I have been struggling last 2 days so thought of asking for help to team. Any help here would be appreciated.

Regards

-Vijay

#2 lore raw

Here is another instance: from one of my old commits.

https://github.com/openbmc/phosphor-host-ipmid/blob/master/systemintfcmds.cpp#L131

!! Vishwa !!

Quoted 37 lines · Lei YU

I did not check the code details, but from the description, it is likely due to the global variable's initialization order in a shared library.

Basically, you need to specify __attribute__((init_priority(101))) for your global variables.

Please check below commit for details and how to manually specify the init order for such variables.
https://github.com/openbmc/phosphor-host-ipmid/commit/4b0ddb68b4e76420358589213bc400155fa12e43

Quoted 25 lines · Vijay Khemka

Team,

I am having small issue of global json object corruption and I am unable to find reason for it. I am defining a global json object (nlohmann::json appSelData) at the start of file. I am initializing this object in register function and accessing this in different functions defined in same shared library. While accessing in different function, it doesn’t hold initialized data. This is failing in my storagecommands.cpp for IPMI.

Then I created a separate new file selcommands.cpp and did the same operation, it works here as expected. So I am wondering why it is failing in existing file vs new one. Looks like there are some header included in existing file is messing it up but I have no clue yet. This only happens with json object.

For your reference, I am attaching both below files

Storagecommands.cpp – where this fails

Selcommands.cpp – where it works.

I know it is a debug issue and I have been struggling last 2 days so thought of asking for help to team. Any help here would be appreciated.

Regards

-Vijay

#3 lore raw

Thanks a lot guys, It really helped.

Regards
-Vijay

On 8/1/19, 11:27 PM, "vishwa" <[email protected]> wrote:

    Here is another instance: from one of my old commits.
    
    https://github.com/openbmc/phosphor-host-ipmid/blob/master/systemintfcmds.cpp#L131
    
    !! Vishwa !!
    
    On 8/2/19 7:39 AM, Lei YU wrote:
    > I did not check the code details, but from the description, it is likely due to
    > the global variable's initialization order in a shared library.
    >
    > Basically, you need to specify `__attribute__((init_priority(101)))` for your
    > global variables.
    >
    > Please check below commit for details and how to manually specify the init
    > order for such variables.
    > https://github.com/openbmc/phosphor-host-ipmid/commit/4b0ddb68b4e76420358589213bc400155fa12e43
    >
    >
    > On Fri, Aug 2, 2019 at 4:12 AM Vijay Khemka <[email protected]> wrote:
    >> Team,
    >>
    >> I am having small issue of global json object corruption and I am unable to find reason for it. I am defining a global json object (nlohmann::json appSelData) at the start of file. I am initializing this object in register function and accessing this in different functions defined in same shared library. While accessing in different function, it doesn’t hold initialized data. This is failing in my storagecommands.cpp for IPMI.
    >>
    >>
    >>
    >> Then I created a separate new file selcommands.cpp and did the same operation, it works here as expected. So I am wondering why it is failing in existing file vs new one. Looks like there are some header included in existing file is messing it up but I have no clue yet. This only happens with json object.
    >>
    >>
    >>
    >> For your reference, I am attaching both below files
    >>
    >> Storagecommands.cpp – where this fails
    >>
    >> Selcommands.cpp – where it works.
    >>
    >>
    >>
    >> I know it is a debug issue and I have been struggling last 2 days so thought of asking for help to team. Any help here would be appreciated.
    >>
    >>
    >>
    >> Regards
    >>
    >> -Vijay
#4 lore raw
Quoted 11 lines · Vijay Khemka

Thanks a lot guys, It really helped.

Regards
-Vijay

On 8/1/19, 11:27 PM, "vishwa" <[email protected]> wrote:

Here is another instance: from one of my old commits.

https://github.com/openbmc/phosphor-host-ipmid/blob/master/systemintfcmds.cpp#L131

Quoted 5 lines
    !! Vishwa !!

    On 8/2/19 7:39 AM, Lei YU wrote:
    > I did not check the code details, but from the description, it is

likely due to

Quoted 3 lines
Quoted 3 lines

the global variable's initialization order in a shared library.

Basically, you need to specify

__attribute__((init_priority(101))) for your

Quoted 3 lines
Quoted 3 lines

global variables.

Please check below commit for details and how to manually specify

the init

Quoted 2 lines
Quoted 2 lines

order for such variables.

https://github.com/openbmc/phosphor-host-ipmid/commit/4b0ddb68b4e76420358589213bc400155fa12e43

Quoted 3 lines
Quoted 3 lines

On Fri, Aug 2, 2019 at 4:12 AM Vijay Khemka <[email protected]>

Quoted 3 lines · wrote:
Quoted 3 lines
Quoted 3 lines

Team,

I am having small issue of global json object corruption and I am

unable to find reason for it. I am defining a global json object (nlohmann::json appSelData) at the start of file. I am initializing this object in register function and accessing this in different functions defined in same shared library. While accessing in different function, it doesn’t hold initialized data. This is failing in my storagecommands.cpp for IPMI.

I would advise against using static global variables exactly for this issue.
See also static initialization fiasco
<https://isocpp.org/wiki/faq/ctors#static-init-order>, another subtle way to crash your app nondeterministically.
They were called out and forbidden
<https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables>
in Google C++ style guide (except for constants).
Using a factory and use smart pointers to manage ownership would be a safer bet.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Then I created a separate new file selcommands.cpp and did the

same operation, it works here as expected. So I am wondering why it is failing in existing file vs new one. Looks like there are some header included in existing file is messing it up but I have no clue yet. This only happens with json object.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

For your reference, I am attaching both below files

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Storagecommands.cpp – where this fails

Selcommands.cpp – where it works.

I know it is a debug issue and I have been struggling last 2 days

so thought of asking for help to team. Any help here would be appreciated.

Quoted 9 lines
Quoted 6 lines
Quoted 6 lines

Regards

-Vijay

Signature · 2 lines
Regards,
Kun