Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Code explanation

Name: Anonymous 2012-01-11 12:49

Can somebody explain why this code outputs what it does?

// tested with Core 2 Duo, Core 2 Quad and Xeon
// tested with gcc4.1.2 gcc4.4.3 and gcc4.6.1
// compile with: gcc -O0 -m32
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <setjmp.h>

jmp_buf p;
void (*q)();

const char *data =
    "\x8b\x44\x24\x04\x8b\x5c\x24\x08"
    "\x8b\x00\x8b\x1b\x31\xc3\x31\xd8"
    "\x31\xc3\x8b\x4c\x24\x04\x89\x01"
    "\x8b\x4c\x24\x08\x89\x19\xc3\x90"
    "\x55\x89\xe5\x8b\x45\x04\xc9\xc3"
    "\x55\x90\x90\x89\xe5\x90\x90\x90"
    "\x8b\x45\x08\x89\x45\x04\xc9\xc3"
    "\x60\x09\x0e\x13\x14\x01\x0c\x0c"
    "\xc0\x07\x05\x0e\x14\x0f\x0f\x60"
    "\x00\x67\x6f\x74\x6f\x20\x63\x6f"
    "\x6e\x73\x69\x64\x65\x72\x65\x64"
    "\x20\x68\x61\x72\x6d\x66\x75\x6c"
    "\x6c\x00\x90\x90\x1c\x1b\x0a\x20";

int f(int x)
{
    static int b = 0; static int s = 0;
    int a = 0, t;
    if (!s) {
        a = b; b = x;
    } else {
        a = x; t = b;
        do {
            a ^= b;
            b = (a^b) & b;
            b <<= 1;
        } while (b);
        b = t;
    }
    s = (s+1) % 2;
    return a;
}

int g(int i, int *j)
{
    *j = i;
    i = (int) putchar;
    if (*j == (48 << 1)) 
        __asm volatile (
                "movl 8(%ebp),%eax;"
                "leave;"
                "ret"
                );
    return (int) puts;
}

void h(int i)
{
    int b;
    q = (void(*)()) g(i++[data],&b);
    for (f(b);*(data+i)!=b;++i,f(b))
        q(f(i[data])%0xff);
}

void sh(int s)
{
    if (s == 010)
        ((void(*)())g(s,&s))("F");
    longjmp(p,s);
}

int main(void)
{
    int base, addr = 0xffffffff, offs = 16;
    int a = 11, b = 32, i = 25;
    int s = 8, t = 1, u = 4;
    ((void(*)()) data)(&a,&b);
    ((void(*)()) data)(&b,&t);
    ((void(*)()) data)(&t,&s);
    addr ^= a;
       a ^= addr;
    addr ^= a;
    base = ((int(*)())data+addr)();
    if (a == -1)
        goto over;
    puts("A");

    base = (1<<3) | ((f(addr) + f(offs)) & ~0xff);
    h(base+addr+offs);
    exit(0);

over:
    signal(t,sh);signal(s,sh);signal(u,sh);

    if (!(s = setjmp(p))) {
        q = (void(*)()) g(0x30, &a);
        q(data + a + i);
        s = a / (b-1);
        puts("B");
    } else if (s == 0xb) {
        puts("C");
        ((int(*)(int)) data+addr+(offs/2))(base);
    } else {
        puts("D");
        *((int*) base+s) = 0xffffffff;
    }
   
    puts("E");
    return 1;
}

Name: Anonymous 2012-01-11 17:02

>>154
This program will block in it's called to read()
Nice English.

Name: Anonymous 2012-01-11 17:06

>>160
Not out of the box

Name: Anonymous 2012-01-11 17:10

Wow, all the autism in this thread. OP is clearly a /g/ faggot, but even though the code is ultimately undefined it was in fact a fun puzzle.

Name: Anonymous 2012-01-11 17:12

>>153
That will add an additional new line to the end.

Name: Anonymous 2012-01-11 17:14

Remind me why we rage because of architecture dependent code again?

Name: Anonymous 2012-01-11 17:23

>>161
Don't be j.

Name: Anonymous 2012-01-11 17:24

| Can anyone explain why this program outputs what it does
| HURR DURR OP IT IS UNDEFINED YOUR A FAAGGOTT GB2 GEEEE

Name: Anonymous 2012-01-11 17:32

>>117
>warning: cast from pointer to integer of different size
>gcc -m32

I don't think so Tim.

Name: Anonymous 2012-01-11 17:55

>>162
TERRIBLE!

Name: Anonymous 2012-01-11 20:06

>>165
Because Intel is a Jewish company. If there is any architecture-dependent code, it has to be for a noble architecture like the PDP-anything, VAX, MIPS, or Lisp Machine.

Name: Anonymous 2012-01-11 21:52

>>170
lol dead architectures

Okay, not MIPS, but the rest are. And you didn't mention ARM you fucking gay.

Name: Anonymous 2012-01-11 22:02

>>170
What's noble in the PDP-11 who gave life to C?

Name: Anonymous 2012-01-12 7:15

>>170
| Because Intel is a Jewish company.
Protip: It's not.

Development hq is in jewland though.

Name: Anonymous 2012-01-12 7:36

This thread is why none of you have a real job and spend all day lurking. 10 / 10 OP for provoking /prog/ into a semi-autistic craze.

Name: Anonymous 2012-01-12 7:40

>>174
This thread is why none of you fags from /g/ know how to program properly

Name: Anonymous 2012-01-12 7:44

>>175
You don't get that the code was intentionally backwards? Are you this stupid. Also, not from /g/ but from /sci/ btw.

Name: Anonymous 2012-01-12 7:50

>>176
You don't get that /g/ clearly shows their inability to even know simple terms relating to C nor their ability to know what a compiler does.

You don't get that posting obscure undefined code on /prog/ isn't going to boost your e-peen hax0r creed with us like it would on your hipster imageboards

Name: Anonymous 2012-01-12 7:54

>>177
| your
Try again.

Name: Anonymous 2012-01-12 7:55

>>177
Also, your rage is showing.

Name: Anonymous 2012-01-12 7:59

>>178'
>your hipster imageboards
>your e-peen
>Implying either of those are suppose to be `you are'

You see on the textboards trolls like you fail hard.

>>179
strawmaning.

If you can't stay on topic then go back to your shit imageboards

Name: Anonymous 2012-01-12 8:01

>>180
It wasn't my post, you dumbwit. I'm not OP nor have I commented in this thread before >>176

Also complaining about strawman arguments when you throw strawman after strawman is just ridiculous and shows a complete lack of self-insight.

Name: Anonymous 2012-01-12 8:05

>>181
Pointing out undefined behavior is not a strawman.


If you're not OP and you have no place in this threads discussion other than to try to troll people then please leave. Go back to your shitty imageboards and never come back.

Name: Anonymous 2012-01-12 8:08

>>182
| Pointing out undefined behavior is not a strawman.
I wasn't referring to that. You have no reading comprehension what so ever. I refuse to believe that you are that stupid.

| Go back to your shitty imageboards and never come back.
hurr durr gb2>>>/g/ n00b
herp derp how dare you post strawman fallacies and/or trolls

Jesus, you are unbelievable. Also,
pointing out that you didn't get OP's post != trolling

Name: Anonymous 2012-01-12 8:09

>>182
| If you're not OP and you have no place in this threads discussion other than to try to troll people then please leave.

Now who is throwing strawman arguments into the thread again?

Name: Anonymous 2012-01-12 8:10

>>183
Everyone in this thread understand what OPs post was meant to be: obscure shit code to show off his e-peen.

No one gives a shit about how smart you think you are with your witty comments.

Go back to /sci/ and shit post on there you dumb fucker.

Name: Anonymous 2012-01-12 8:11

>>184
You are, you threw it off topic now get the fuck out if you have nothing good to say that's on topic.

The only thing you fucking shit posters from the imageboards are good at is going off topic and shitty troll attempts.

Name: Anonymous 2012-01-12 8:13

>>185
| obscure shit code to show off his e-peen.
I didn't read it that way, but then again I'm not OP. I was seriously considering to try to trace my way through the code but meh.

| No one gives a shit about how smart you think you are with your witty comments.
Wow, I clearly pushed your buttons. Also, apropos strawman anyone? Just give up, the guy who's always in for the last word, like you're doing now, usually comes off as a dick.

Name: Anonymous 2012-01-12 8:14

>>187
>...to try to trace my way...

Tracing through undefined code? Good one that'll lead you straight to the right answer you dumb fuck.

Name: Anonymous 2012-01-12 8:15

>>182,185-186
All I read from these posts is:
1) Shit posting how dumb OP and other imageboarders is
2) Accusations of off-topic when those accusations being answered
3) More shit posting and off-topic bullshit

Name: Anonymous 2012-01-12 8:18

>>188
Why do you care how I want to spend my time or not, or if I even want to post in this thread and answer OP or not. Just ignore the thread, moron.

Name: Anonymous 2012-01-12 8:18

>>189
*are

Name: Anonymous 2012-01-12 8:20

>>189
All I read from your posts are:
1) You being a dumb fuck
2) You shit posting and acting like we didn't realize it was obscure code to begin with
3) You throwing it off topic

Name: Anonymous 2012-01-12 8:24

>>192
| You shit posting and acting like we didn't realize it was obscure code to begin with
| we
No, all of "us" probably got it. You, on the other hand were more interested in pointing out how stupid OP and everyone is rather than simply ignoring the thread if you thought it was to boost OP's e-peen. That tells me that you probably are a manchild having a hard time accepting that others might do and say stuff you disagree with.

Also, this thread was off-topic as soon as the first guy bashing on OP (most likely you, since you seem to have your feelings hurt) posted.

Name: Anonymous 2012-01-12 8:25

testing
testing

Name: Anonymous 2012-01-12 8:26

The output is undefined.

Name: Anonymous 2012-01-12 8:28

>>193
first guy bashing OP
For posting undefined code and expecting us to tell him the output when the OP clearly knew what the output of his code was? That sure sounds like a good reason for people in this thread to call him a moron and an idiot.

If you couldn't comprehend the fact that the code was obscure shit with undefined uses then you need to go back to learning how to program and stop wasting your time here.

Name: Anonymous 2012-01-12 8:30

>>196
| For posting undefined code and expecting us to tell him the output
Are you this blind? OP clearly says
| Can somebody explain why this code outputs what it does?
>WHY

| If you couldn't comprehend the fact that the code was obscure shit with undefined uses
I do, but on the specified architectures it gives the same output. You, on the otherhand, aren't at all interested and dismiss everything that you don't bother to do as a waste of time. Of course the program relies on undefined behavior, that's what makes it "tricky" to figure out.

Name: Anonymous 2012-01-12 8:32

>>197
The question of what the code outputs is not a well defined question since the output of program is undefined.

The program might as well crash as output something, and when it does output there is no telling what it outputs as it still undefined.

Name: Anonymous 2012-01-12 8:33

>>196
Also, since you are willing to go there:
architecture dependent != undefined behavior
undefined behavior == non-deterministic behavior

Name: Anonymous 2012-01-12 8:35

>>198
See >>142. It's not producing different output.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List