C Stack Corruption?

D

Dev

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="myApplication.c"/>
<arg line="-FeMyApplication" />
<arg line="C:Generated/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\library\file.dll" />
<arg line="-Wl,--add-stdcall-alias" />
<arg line="-o MyApplication"/>
<arg line="MyApplication.c" />
</exec>
</target>
 
W

Walter Roberson

: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.
 
C

CBFalconer

Dev said:
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.
.... snip ...

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. =)

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.
 
D

Dev

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++.

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?
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.

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.
 
D

Dev

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.
 
C

CBFalconer

Dev said:
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?

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.
 
A

Alan Balmer

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?
I'm willing to bet that some folks who read alt.sex.bestiality are
Windows programmers and they may be able to help you. I suggest you
post your problem there.
 
M

Mark McIntyre

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.

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

about sausages?
I thank you, Chuck, for your condescension.

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.
My personal rates put me at slightly over 1100 US$ a day, plus expenses.

Thats nice.
 
G

Gordon Burditt

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.

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.

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.

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 ______".
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.

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.
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.

If possible, try compiling with the same compiler as the proprietary
dll.
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.

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
 
D

Dev

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".

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

Dev

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
 
D

Dev

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.
 
M

Mark McIntyre

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

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?
 
M

Mark McIntyre

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.

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.
That'll fry a stack real fast.

not as fast as the flames would fry ...
 
D

Dev

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.

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?
 
M

Mark McIntyre

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. =)

good, cos if you post offtopic stuff here, you'll rapidly get flamed to a
crisp.
Speaking of offtopic, would it be inappropriate for me to test my
script that posts hourly news updates on comp.lang.c?

What exactly is it with you? Why such a daft attitude - do you /like/
people insulting you? Do you /like/ annoying other people?
 
D

Dev

What exactly is it with you? Why such a daft attitude - do you /like/
people insulting you? Do you /like/ annoying other people?

Let me start with the first question.

I don't particularly like people insulting me, and my guess is that few
people do. Since it's almost universally understood that when you
insult someone they will not like it, why is this newsgroup so
extremely unfriendly? The average regular here comes in two flavors:
those who want to help people for the sake of helping people and
sharing knowledge, and those who want to help people to show off their
knowledge. While the former seem to be genuinely nice people, the
latter seem to represent the darker half of our society and seem only
interested in flaming others and participating in dick swinging
contests. They are, on average, entirely too egotistical for their
actual abilities, and, while they may be the smartest person at their
shithole of a place of employment, they "forget" to give strangers the
courtesy of assuming at least an average level of intelligence. So,
yes, I've read the FAQ, and no, I'm not new to newsgroups, certainly
not comp.lang.*. Regardless of how many times and places the attitudes
of the regulars here and elsewhere are rationalized or explained, they
still remain rude and uncalled for.

As for the second question, yes, occasionally I like annoying other
people, but only when I feel like they really deserve it, and many here
have deserved it for a long, long time. Besides, I don't actually have
the power to annoy you; you, however, have the power to choose to be
annoyed by my actions. Or not.

So, to all the self-titled gurus that fall into my latter category of
regulars, I have a bit of advice: rethink your dogmatic viewpoints,
quit being such absolute dickheads, don't let yourself get annoyed over
very little things, and try smiling once in a while.
 
D

Default User

Dev said:
So, to all the self-titled gurus that fall into my latter category of
regulars, I have a bit of advice: rethink your dogmatic viewpoints,
quit being such absolute dickheads, don't let yourself get annoyed over
very little things, and try smiling once in a while.

I have a better idea.

*plonk*




Brian
 
M

Mark McIntyre

I don't particularly like people insulting me, and my guess is that few
people do. Since it's almost universally understood that when you
insult someone they will not like it, why is this newsgroup so
extremely unfriendly?

Its not. Typically, when someone posts offtopic, they get politely pointed
elsewhere. This happened to you.

However when they then persist in asking offtopic stuff, or reply saying
"screw you, I can post what I like" or otherwise insult the group, THE they
get an unfriendly response. This happened to you. I refer you to your
sarcastic and rude response to Chuck's redirection.

I've read the FAQ, and no, I'm not new to newsgroups, certainly
not comp.lang.*.

then WHY are you still posting offtopic stuff? For goodness' sake..
So, to all the self-titled gurus that fall into my latter category of
regulars, I have a bit of advice: rethink your dogmatic viewpoints,
quit being such absolute dickheads, don't let yourself get annoyed over
very little things, and try smiling once in a while.

Perhaps you ought to consider which of us is being an arrogant dickhead?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top