C Stack Corruption?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dev

    #1

    C Stack Corruption?

    I'm working with some IBM sponsored C APIs to interface with a
    corporate legacy system, and it seems that I'm getting some stack
    corruption after any API call.

    I believe the APIs were designed to be used with IBM VisualAge, but I'm
    compiling my program with both gcc/cygwin and Microsoft's Visual C++
    compiler. While running I actually get different behavior with each
    compiler. To link to the library with cl.exe I generated a .lib file
    from the .dll file using a small open source application (script) which
    I can't seem to find online anymore (google reindex?).

    I can get the program to continue to run for a while by adding a char
    x[10000]; buffer as the last local variable in my function, but
    eventually I get a core dump. Stepping through the application in gdb
    I see my char buffer x go from '\0' (repeats 9999 times) to garbage
    values after any IBM API call. Commenting out x yields an immediate
    crash at best, or overwrites other necessary local variables which
    wreaks bloody havoc on subsequent calls.

    I've checked and double checked the parameters going into and coming
    out of the API calls, and they appear to be correct. I've also
    initialized all local variables.

    I highly doubt that the IBM API is at fault, and I'm running out of
    ideas to test my code. Is it possible that it's in how I compile my
    code and link to the DLL? This is running as JNI code for a larger
    Java application, so I'm using ANT to build (the behavior still
    exhibits itself when all JNI code is removed and the program is run as
    a c console application). Below are my build targets.

    Does anyone have any further suggestions for debugging a gcc compiled
    application linked to a proprietary dll in Windows? Is there any way
    to get an easy printout of the stack at any given point in time?

    Unfortunately, I do not own my code, so code samples can't be posted.
    Just imagine horribly named API methods that take pointers to pointers
    to deeply nested structs of pointers. =)

    All help is greatly appreciated. My coworkers and I are starting to
    bang our heads against the wall.

    Thanks,
    Dev



    ANT targets:

    <target name="compile-cl">
    <exec dir="src" executable="cl. exe">
    <arg line="-WX" />
    <arg line="-Iinclude" />
    <arg line="myApplica tion.c"/>
    <arg line="-FeMyApplication " />
    <arg line="C:Generat ed/API/Library/File.lib" />
    </exec>
    </target>

    <target name="compile-gcc">
    <exec dir="src" executable="gcc .exe">
    <arg line="-Wall -g" />
    <arg line="-mno-cygwin" />
    <arg line="-Iinclude" />
    <arg line="-Wl,\provided\li brary\file.dll" />
    <arg line="-Wl,--add-stdcall-alias" />
    <arg line="-o MyApplication"/>
    <arg line="MyApplica tion.c" />
    </exec>
    </target>

  • Walter Roberson

    #2
    Re: C Stack Corruption?

    In article <1109714539.606 063.102500@f14g 2000cwb.googleg roups.com>,
    Dev <dev.elope@gmai l.com> wrote:
    :I'm working with some IBM sponsored C APIs to interface with a
    :corporate legacy system, and it seems that I'm getting some stack
    :corruption after any API call.

    :I highly doubt that the IBM API is at fault, and I'm running out of
    :ideas to test my code. Is it possible that it's in how I compile my
    :code and link to the DLL?

    It sounds to me as if you are using an API with a different calling
    convention than the new code. I do not have any experience building
    on Windows, so I don't know what is available, but generally speaking,
    I have heard that there are at least 3 major compilers each of which
    produces libraries that are incompatible with each other.

    As an example of what could in theory go wrong: many years ago,
    I worked with systems where the calling convention was that the
    calling function had to allocate the temporary storage on the stack
    to save the registers; in other systems, it is the responsibility of
    the called function to do this allocation. If a calling function expected
    the called function to allocate the storage but the called function
    expected the called function to do it, then data above the return
    address could get clobbered.
    --
    Rump-Titty-Titty-Tum-TAH-Tee -- Fritz Lieber

    Comment

    • CBFalconer

      #3
      Re: C Stack Corruption?

      Dev wrote:[color=blue]
      >
      > I'm working with some IBM sponsored C APIs to interface with a
      > corporate legacy system, and it seems that I'm getting some stack
      > corruption after any API call.
      >[/color]
      .... snip ...[color=blue]
      >
      > Unfortunately, I do not own my code, so code samples can't be
      > posted. Just imagine horribly named API methods that take pointers
      > to pointers to deeply nested structs of pointers. =)[/color]

      System specific problems are off-topic here. c.l.c deals with the
      portable C language. Dlls smells of windoze, and methods smells of
      C++.

      If you won't post the code you can't be helped here, and probably
      not anywhere. You need to look around for suitable consultants,
      who will require payment, and can be trusted with your code. Make
      sure any such consultant is familiar with assembly language on your
      machine. Expect to pay USD 500 to 1000 per day, plus expenses.

      --
      Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
      Available for consulting/temporary embedded and systems.
      <http://cbfalconer.home .att.net> USE worldnet address!


      Comment

      • Dev

        #4
        Re: C Stack Corruption?

        > System specific problems are off-topic here. c.l.c deals with the[color=blue]
        > portable C language. Dlls smells of windoze, and methods smells of
        > C++.[/color]

        How helpful you are to inform me that no one reading comp.lang.c is
        familiar with any specific system, or with cross-system and
        cross-compiler complexities. Oh, whatever was I thinking?
        [color=blue]
        > If you won't post the code you can't be helped here, and probably
        > not anywhere. You need to look around for suitable consultants,
        > who will require payment, and can be trusted with your code. Make
        > sure any such consultant is familiar with assembly language on your
        > machine. Expect to pay USD 500 to 1000 per day, plus expenses.[/color]

        I thank you, Chuck, for your condescension. My personal rates put me
        at slightly over 1100 US$ a day, plus expenses. But, it's always nice
        to get a reminder that other people make less.

        Comment

        • Dev

          #5
          Re: C Stack Corruption?

          Thanks, Fritz.

          We've been looking at that as being one of the problems, but are
          running into snags with the precompiled library. I appreciate the help.

          Comment

          • CBFalconer

            #6
            Re: C Stack Corruption?

            Dev wrote:[color=blue]
            >[color=green]
            >> System specific problems are off-topic here. c.l.c deals with the
            >> portable C language. Dlls smells of windoze, and methods smells of
            >> C++.[/color]
            >
            > How helpful you are to inform me that no one reading comp.lang.c is
            > familiar with any specific system, or with cross-system and
            > cross-compiler complexities. Oh, whatever was I thinking?[/color]

            No, I informed you that it was off-topic on c.l.c and that this
            newsgroup does not deal with systems specific issues. If you look
            closely at what you quoted you will see the verbiage involved.
            Richard Heathfield used to offer a course on "Reading for
            Comprehension".

            BTW, it still is off-topic.

            --
            "If you want to post a followup via groups.google.c om, don't use
            the broken "Reply" link at the bottom of the article. Click on
            "show options" at the top of the article, then click on the
            "Reply" at the bottom of the article headers." - Keith Thompson

            Comment

            • Alan Balmer

              #7
              Re: C Stack Corruption?

              On 2 Mar 2005 06:41:42 -0800, "Dev" <dev.elope@gmai l.com> wrote:
              [color=blue][color=green]
              >> System specific problems are off-topic here. c.l.c deals with the
              >> portable C language. Dlls smells of windoze, and methods smells of
              >> C++.[/color]
              >
              >How helpful you are to inform me that no one reading comp.lang.c is
              >familiar with any specific system, or with cross-system and
              >cross-compiler complexities. Oh, whatever was I thinking?
              >[/color]
              I'm willing to bet that some folks who read alt.sex.bestial ity are
              Windows programmers and they may be able to help you. I suggest you
              post your problem there.

              --
              Al Balmer
              Balmer Consulting
              removebalmercon sultingthis@att .net

              Comment

              • Mark McIntyre

                #8
                Re: C Stack Corruption?

                On 2 Mar 2005 06:41:42 -0800, in comp.lang.c , "Dev" <dev.elope@gmai l.com>
                wrote:
                [color=blue]
                >How helpful you are to inform me that no one reading comp.lang.c is
                >familiar with any specific system, or with cross-system and
                >cross-compiler complexities.[/color]

                That wasn't what he said. He pointed out that such details are offtopic
                here, and you are asking in the wrong place.
                ..[color=blue]
                >Oh, whatever was I thinking?[/color]

                about sausages?
                [color=blue][color=green]
                >> If you won't post the code you can't be helped here, and probably
                >> not anywhere. You need to look around for suitable consultants,
                >> who will require payment, and can be trusted with your code. Make
                >> sure any such consultant is familiar with assembly language on your
                >> machine. Expect to pay USD 500 to 1000 per day, plus expenses.[/color]
                >
                >I thank you, Chuck, for your condescension.[/color]

                So its condescending to note that since you can't show the problem code for
                commercial reasons, helping you for free is probably impossible? And its
                condescending to suggest employing a consultant to do this, which
                presumably is better commercially since you can bind them into an NDA? Hmm.
                [color=blue]
                >My personal rates put me at slightly over 1100 US$ a day, plus expenses.[/color]

                Thats nice.

                --
                Mark McIntyre
                CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
                CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

                ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
                http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
                ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

                Comment

                • Gordon Burditt

                  #9
                  Re: C Stack Corruption?

                  >I'm working with some IBM sponsored C APIs to interface with a[color=blue]
                  >corporate legacy system, and it seems that I'm getting some stack
                  >corruption after any API call.[/color]

                  There is no guarantee that you can mix the outputs of two different
                  compilers together, particularly if they use different linkage
                  conventions for passing and retrieving arguments. In the worst
                  case, one compiler won't even recognize the other's object code as
                  object code. You don't seem to have that problem, but stuff like
                  how stuff gets pushed on the stack (or put in registers), where to
                  look for return values of various types, which registers are saved
                  or trashed across function calls, etc. may be an issue.

                  One possibility to consider is a "glue" routine, written in assembly
                  language. It accepts a call from one compiler module, re-arranges
                  the arguments the way the other module wants it, then accepts back
                  a return value and passes it back to the first compiler modules.
                  You might need a lot of "glue" routines here: possibly one per
                  function on either side.

                  [color=blue]
                  >I believe the APIs were designed to be used with IBM VisualAge, but I'm
                  >compiling my program with both gcc/cygwin and Microsoft's Visual C++
                  >compiler. While running I actually get different behavior with each
                  >compiler. To link to the library with cl.exe I generated a .lib file
                  >from the .dll file using a small open source application (script) which
                  >I can't seem to find online anymore (google reindex?).
                  >
                  >I can get the program to continue to run for a while by adding a char
                  >x[10000]; buffer as the last local variable in my function, but
                  >eventually I get a core dump. Stepping through the application in gdb
                  >I see my char buffer x go from '\0' (repeats 9999 times) to garbage
                  >values after any IBM API call. Commenting out x yields an immediate
                  >crash at best, or overwrites other necessary local variables which
                  >wreaks bloody havoc on subsequent calls.[/color]

                  One screwup I've seen with stack imbalance on gcc happens when the
                  function thinks it's returning a double but the caller thinks it's
                  returning int. Blam! floating point unit stack overflow. The real
                  problem here is the code is broken (and it could be fixed by putting
                  a declaration in a header file and using it where needed). I think I've
                  seen a case where the C stack got trashed, but I don't remember the
                  situation. Again, bad code, most likely caller and callee not agreeing
                  on the types of stuff.

                  Warnings like those produced by gcc -Wall are your friend. Try
                  fixing these first, especially those that say "implicit declaration
                  of function ______".
                  [color=blue]
                  >I've checked and double checked the parameters going into and coming
                  >out of the API calls, and they appear to be correct. I've also
                  >initialized all local variables.
                  >
                  >I highly doubt that the IBM API is at fault, and I'm running out of
                  >ideas to test my code.[/color]

                  If you're using two different APIs on the calling and called side,
                  that's probably a mistake, whether or not either API is "wrong",
                  whatever that means.
                  [color=blue]
                  >Is it possible that it's in how I compile my
                  >code and link to the DLL? This is running as JNI code for a larger
                  >Java application, so I'm using ANT to build (the behavior still
                  >exhibits itself when all JNI code is removed and the program is run as
                  >a c console application). Below are my build targets.[/color]

                  If possible, try compiling with the same compiler as the proprietary
                  dll.
                  [color=blue]
                  >Does anyone have any further suggestions for debugging a gcc compiled
                  >application linked to a proprietary dll in Windows? Is there any way
                  >to get an easy printout of the stack at any given point in time?
                  >
                  >Unfortunatel y, I do not own my code, so code samples can't be posted.
                  >Just imagine horribly named API methods that take pointers to pointers
                  >to deeply nested structs of pointers. =)
                  >
                  >All help is greatly appreciated. My coworkers and I are starting to
                  >bang our heads against the wall.[/color]

                  You're probably not going to get much useful help if you can't show
                  us the code. I can only speculate in generalities, which roughly
                  comes down to "undefined behavior -> shit happens".

                  Gordon L. Burditt

                  Comment

                  • Dev

                    #10
                    Re: C Stack Corruption?

                    > No, I informed you that it was off-topic on c.l.c and that this[color=blue]
                    > newsgroup does not deal with systems specific issues. If you look
                    > closely at what you quoted you will see the verbiage involved.
                    > Richard Heathfield used to offer a course on "Reading for
                    > Comprehension".[/color]

                    Are you familiar with the advanced course, "Reading for Comprehension
                    of Subtext?"

                    Comment

                    • Dev

                      #11
                      Re: C Stack Corruption?

                      Thanks, Gordon, you and Walter gave me enough to go on to actually find
                      the problem.

                      Turns out it was in calling conventions and an incorrect header file.
                      The header file was defining everything as using Pascal calling
                      conventions whereas the library was actually compiled to use standard
                      calling conventions.

                      That'll fry a stack real fast.

                      Dev

                      Comment

                      • Dev

                        #12
                        Re: C Stack Corruption?

                        I'm thankful that I have better things to do than bicker with people
                        who obviously don't.

                        Cheers, mates. Thanks to those who tried to help. Looks like this
                        wasn't such a horrible place to look for help, after all.

                        Comment

                        • Mark McIntyre

                          #13
                          Re: C Stack Corruption?

                          On 3 Mar 2005 10:14:52 -0800, in comp.lang.c , "Dev" <dev.elope@gmai l.com>
                          wrote:
                          [color=blue][color=green]
                          >> No, I informed you that it was off-topic on c.l.c and that this
                          >> newsgroup does not deal with systems specific issues. If you look
                          >> closely at what you quoted you will see the verbiage involved.
                          >> Richard Heathfield used to offer a course on "Reading for
                          >> Comprehension".[/color]
                          >
                          >Are you familiar with the advanced course, "Reading for Comprehension
                          >of Subtext?"[/color]

                          Perhaps, if you want help on a topical subject, you'd find it more
                          appropriate to post your request in the text, and leave the subtext alone?


                          --
                          Mark McIntyre
                          CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
                          CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

                          ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
                          http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
                          ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

                          Comment

                          • Mark McIntyre

                            #14
                            Re: C Stack Corruption?

                            On 3 Mar 2005 10:37:52 -0800, in comp.lang.c , "Dev" <dev.elope@gmai l.com>
                            wrote:
                            [color=blue]
                            >Turns out it was in calling conventions and an incorrect header file.
                            >The header file was defining everything as using Pascal calling
                            >conventions whereas the library was actually compiled to use standard
                            >calling conventions.[/color]

                            And all of this is offtopic here. Please don't consider the fact that you
                            got help this time as an indication that you should come back and ask more
                            offtopic stuff.
                            [color=blue]
                            >That'll fry a stack real fast.[/color]

                            not as fast as the flames would fry ...
                            --
                            Mark McIntyre
                            CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
                            CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

                            ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
                            http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
                            ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

                            Comment

                            • Dev

                              #15
                              Re: C Stack Corruption?

                              > And all of this is offtopic here. Please don't consider the fact that
                              you[color=blue]
                              > got help this time as an indication that you should come back and ask[/color]
                              more[color=blue]
                              > offtopic stuff.[/color]

                              Good thing it's an unmoderated newsgroup and I can post all the
                              offtopic stuff I want, limited solely by my frustration at replies from
                              people like you. But, thankfully, I have a very high tolerance. =)

                              Speaking of offtopic, would it be inappropriate for me to test my
                              script that posts hourly news updates on comp.lang.c?

                              Comment

                              Working...