94 points · by RyanMcGreal · 16y ago · 73 comments
MS speaker: and we've got POSIX, and we've got POSIX ACLs, and we've got a Unix Subsystem and we've got Korn Shell, and it works just the same way as under Unix.
Heckler: NO IT DOESN'T
MS speaker: er, ok. It does work. Anyway...
Heckler: NO IT DOESN'T WORK
MS speaker: What doesn't work?
Heckler: (rattles off differences in behavior and brokenness of ksh on NT)
MS speaker: we'll we've had our top guys working on this ...
Heckler: I'M DAVID KORN.
Room erupts in laughter.
Update: source: http://slashdot.org/articles/01/02/06/2030205.shtml. See question 5.
http://www.cygwin.com/ml/cygwin/2005-08/msg00504.html
He questions whether it "should be fixed" in Cygwin, a perfectly reasonable request, and he fully admits that he isn't sure whether it's a problem in Cygwin or not.
His reference to Linus doing the same thing in kernel (not) sounds very much like a smack response.
Also he claims he knows what he's doing since he has done the same thing in DOS so when it's clear that he has no clue of what's going on just irrates people.
We are just humans. Totally unrelated input might result in a change on the output.
From the comments: I get this problem too. It only seems to happen if your IQ is less than 70, though.
The cygwin maintainers are not known for their patience and affability.
> Do you actually realise that not all computer languages can
> be implemented with a model of one stack?
>
Do you actually realize that there are far better ways to implement stacks
for interpreters than dinking with esp? Ways that actually have a chance of
working? I suggest you investigate those alternatives instead of trying to
do something that cannot work, and then getting all pissy when people try to
dissuade you from wasting your time.
And don't make me break out my Turing Machine on you about that statement of
yours.The original commenter --- despite his very fragile code --- is right: for what he wants to do, he needs to create temporary stacks. What he doesn't have is a mechanism to allocate and swap in and out of those stacks (he's hoping that switching stacks is a 2-liner that just relies on knowing the inline asm incantation for changing ESP).
Regardless, it is a uniquely and flagrantly bad way to educate (both the questioner and the world who's reading the thread) to start from the premise that "you're not ready to build the thing you want to build" --- at least when "the thing" we're talking about is systems code. 90% of C programmers are not "ready" to build production C code that will get deployed in hostile environments, and as a result, in every web project we ever do, we have to embark on a tedious inventory of all the 3rd party stuff our clients use to find the horrible C ZIP library they accidentally included. No C coder is blameless.
Unless I missed a whole leg of this thread (the part where they explained temp stacks and stack switching to this guy, who, in challenging the notion that C has a real runtime or the notion that the OS should [heh] be controlling ESP, is demonstrating more on-the-ground systems programming knowhow than several of the commenters), I think this is a remarkably douchey example of a programming help thread.
But if you know how to use The Google, you can actually find code of mine that does this. I doubt very highly you can say the same thing.
Then the bleeding corpse should be hung, drawn and quartered.
Then burnt.
Then the smouldering rubble should be jumped up and down on.
By a hippo.
By a hippo....He points out the errors,the incorrect assumptions and offers pointers of what to do. The general standard of discourse on most mailing lists tend to be quite a bit lower than this.
For the low level mess, he sets the stack frame pointer to a uninitialized heap-allocated buffer which contains God-know-what garbage. When main() returns, it pops its return address from the stack frame pointer, which contains garbage and crashes. The printf() might sometime work depending on how the compiler emits code to set up the stack frame.
It just shows how dangerous some ignorant programmers can be.
First, he's writing a nonportable language runtime. For Cywgin. It's not a fair critique to say "he's making the mistake of writing x86-specific code".
Second, you don't have to initialize a stack before you use it. The stack isn't magically cleared before it gets used. In fact, that's rather the point of having the stack implemented with a single pointer, and is also the reason that in C you have to explicitly initialize (say) integers before using them.
Third, there's nothing "dangerous" about what he's doing, in that what he's doing can't ever work. He created a temp stack, write code that attempts to switch into it, and never wrote the code to switch out of it. That's not going to slip into production. But that's also not a fundamental critique of the approach.
What he's trying to do works fine, including under Cygwin, when completely implemented. Despite much peanut gallery whining that it can't possibly work, or that this guy should give up on doing inline/threaded/coroutine stacks and instead implement some CS101 interpreter instead.
He said his code works on Linux but not Cygwin. Linux is not a hardware architecture. The code generated for Linux Arm is very different from Linux i386. I merely pointed out he's confused about different machine abstractions and his claim of "working on Linux" is not a good basis to expect it would work on Cygwin. Does that make sense for you?
Second, re-read what I wrote. I talked about returning from main(). I wasn't talking about sub-function calls within main(). Returning from main() will take the garbage content from the uninitialized buffer as return address and crash. And if you initialize the buffer with the correct stack frame for the function you want to jump to, you can "return" to it. That's how co-routine work, or buffer overflow attack. Does that make sense for you?
Second (b) a stack is not necessarily implemented as a single pointer. On i386, it's a double-indirect pointer EBP:ESP. He only set up ESP and forgot about EBP. Depending on how the generated code sets up the runtime environment, EBP is not necessary (most likely not) the same as the heap.
Third, whatever, it's your pure speculation. He's jumping wildly to whatever location. It's lucky it crashes rather than silently executes whatever code and format his drive.
The fact remains that what he's doing doesn't work, despite what you insist. No one says he should give up doing thread/coroutine stacks. Just his approach is wrong and non-portable. There are better ways to do coroutine.
But the real issue is that what he's doing is completely unnecessary, because you could manage multiple stacks yourself using ordinary C data structures and ordinary C code. Evidently he thinks he's gaining some kind of speed advantage by doing things this way, but the risk is very high versus the gain. This kind of optimization may have worked OK on a DOS machine, but it's neither safe nor obviously faster to do things like this on a modern CPU and OS.
Also depending on how the compiler sets up the runtime environment, setting ESP is not sufficient. Intel also has the segment registers. EBP is the stack segment pointer. I'm pretty sure the heap is not in the EBP segment.
I personally haven't done anything with coroutines but looks like from a low level perspective, ultimately what he is trying to do is not insane… aka do some form of task switching. Arguably, he shouldn't be doing this by himself and he should've resorted to something that is already available. Just a quick look at (http://en.wikipedia.org/wiki/Coroutine#Coroutine_alternative...) should've been sufficient. Yet.., maybe he had some reason to try and do it himself. Looks like at the end he managed to figure out what he wanted to do.
In my book, a worthy attempt to grow your stack on the heap, but you've really got to be ready to hit the asm debugger when things go awry.
Edit: as mentioned in other comments, the mistake was (at least) in not restoring the stack pointer before main returned.
In this case he's using in-line assembly to modify the stack pointer.
That's bound to end in bad results.....
st1 = (void *)malloc(5000) + 5000;
completely invalid too? You can't (I think it's undefined) increment a void pointer.
My C is a bit rusty, but how else are you meant to allocate data of different types on the heap?
First, as someone else already pointed out, it can lead to an LP64 bug if you miss the declaration for malloc(), since int and caddr_t are not necessarily the same thing.
But the bigger problem is that casting to void* basically tells the compiler "stop all further type checking". That's not a big deal if the token "malloc" on that line never changes, but, in real codebases, allocators get changed all the time. If you change from foo_alloc() to bar_alloc(), the compiler will rightly freak out when you try to assign a foo* to a bar. But that (void) construction explicitly prevents that check from occurring, thus turning what should be a compile-time error into a runtime error.
C++, however, does not allow this. So, if for some reason you're calling malloc rather than new in C++, you need to cast.
edit: Though free()ing it would not work so well until you move the pointer back to the beginning of the 5000 bytes.