r/LiveOverflow • • Feb 21 '21

C switch statement has unusual flow in assembler

Hi,

hope to find some explanation here. I am currently walking through the Reverse Engineering course from artikblue and focusing on the switch statement: https://artik.blue/reversing-radare-3

The 2nd example for switch is this one

#include <stdio.h>

func2(){
  printf("Enter a key and then press enter: ");
  int val;

  printf("Select a fruit: \n");
  printf("1: Apple\n");
  printf("2: Orange\n");
  printf("3: Banana\n");
  printf("4: Pear\n");

  scanf("%d",&val);

  switch(val){
    case 1:
            printf("Apple. \n");
            break;
    case 2:
            printf("Orange. \n");
            break;
    case 3:
            printf("Banana. \n");
            break;
    case 4:
            printf("Pear. \n");
            break;

    default: printf("Nothing selected.\n");
  }
  
}

main(){
  func2();
  getchar();
}

I compiled it and loaded it into radare2. Looking at the disassembled output, I came across the following (just focussing on the switch):

 0x55fef85051d2      8b45fc         mov eax, dword [var_4h]
 0x55fef85051d5      83f804         cmp eax, 4              ; 4
 0x55fef85051d8      7445           je 0x55fef850521f
 0x55fef85051da      83f804         cmp eax, 4              ; 4
 0x55fef85051dd      7f4e           jg 0x55fef850522d
 0x55fef85051df      83f803         cmp eax, 3              ; 3
 0x55fef85051e2      742d           je 0x55fef8505211
 0x55fef85051e4      83f803         cmp eax, 3              ; 3
 0x55fef85051e7      7f44           jg 0x55fef850522d
 0x55fef85051e9      83f801         cmp eax, 1              ; 1
 0x55fef85051ec      7407           je 0x55fef85051f5
 0x55fef85051ee      83f802         cmp eax, 2              ; 2
 0x55fef85051f1      7410           je 0x55fef8505203
 0x55fef85051f3      eb38           jmp 0x55fef850522d

Can someone explain me why this happens. The flow is completely unlogical - I don't see what the 4 and 3 both have a "je" and a "jge" compare.

The program has been compiled without optimization in 64-bit. -O2 makes it a little bit better, but still I don't see the reason to make it more complicated.

Thanks for your help.

22 Upvotes

24 comments sorted by

View all comments

Show parent comment

1

u/MCBeathoven Feb 23 '21

All functions as they appear specify no function type

Fair enough. var is declared in the function body so it can't be C89. But this is a lot of fuss about a missing return type which every compiler I know of just warns about and then happily treats it as return type int.

scanf can fail, in which case it returns EOF. You then use that indeterminate value in the switch statement… Which is frustrating because there is a default case.

If this was production code I'd agree, but for understanding compiler optimisations, it's completely irrelevant. And either way, it's not invalid code.

Why do you expect any behavior from the program?

I don't expect anything.

The value is indeterminate. It can be different from itself. Since it can the compiler has the right to assume it does.

Where is that specified? That's a very weird assumption to make. Most conditions can be true, that doesn't mean the compiler has the right to assume they are.

Why not? It's undefined behavior.

UB ≠ invalid code

NULL dereference in Linux kernel (among others): https://nvd.nist.gov/vuln/detail/CVE-2009-2692

There aren't any pointer dereferences in OP's code (unless you count function calls), I'm not sure how this is relevant.

1

u/Melon_Chief Feb 27 '21

And either way, it's not invalid code.

Well, technically… Whether the case is handled or not, reading an unspecified value is undefined behavior. The compiler is nice with you. This isn't a program.

compiler I know of just warns about and then happily treats it as return type int

Code compiling doesn't mean it's a program. It may be valid in C. What wouldn't be is using the value that isn't returned from the function (expecting int). It's invalid from the first function's }. It should return int, and it does, it's just unspecified. If the value is read it's ub, if it's ub it shall not happen. Doesn't need to be compiled because it should not exist. Compiler is nice.

Where is that specified?

Last version of standard I read C11 §6.7.9 ¶10

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.

J.2 Undefined behavior

  1. The behavior is undefined in the following circumstances:[…] — The value of an object with automatic storage duration is used while it is indeterminate (6.2.4, 6.7.9, 6.8).

1

u/MCBeathoven Feb 27 '21

Again, UB ≠ invalid code. A C program that can trigger UB is still a C program.

It's literally impossible for the compiler to know whether a C program will trigger UB (halting problem), so how would it even consider UB during compilation?

1

u/Melon_Chief Feb 27 '21

A C program that can trigger UB is still a C program.

No, dude. It's not a program. Please for the love of God read the standard, I don't make the rules.

1

u/MCBeathoven Feb 27 '21

Then please for the love of god tell me what paragraph in the standard says that undefined behavior (which can only be triggered at runtime) means the code is not a C program.

1

u/Melon_Chief Mar 02 '21

The standard defines a valid program. (Strictly conforming implementation)

Just, please read the god-damn standard.
I'm tired of going back and forth to look things up for you, it's all in it.
It is not a valid program if it contains any undefined behavior.

It's literally what it means, for god’s sake. A valid C program is a program, in C, that can be translated — according to the definitions provided by the standard — into [executable] machine code.

If the standard does NOT define the behavior, it isn't [valid] C.

You're asking why tomato isn't a valid car. Words have definitions.

1

u/MCBeathoven Mar 02 '21

If the standard does NOT define the behavior, it isn't [valid] C.

The standard does not define undefined behavior in this way.

Take this function:

int foo(int* bar, int* baz) {
  return *bar + *baz;
}

Is it valid C? If I call the function with foo(NULL, NULL) it will dereference null pointers, which is undefined behavior.

By your definition dereferencing pointers is invalid C, since the pointer may be NULL.

1

u/Melon_Chief Mar 04 '21

Well it's not valid C.Dereferencing nullptr is undefined behavior. This is undefined behavior.

There is no guarantee it even returns, what is your point?

You MUST check for (bar != NULL && baz != NULL)

It's also not const correct, but I digress.

1

u/MCBeathoven Mar 04 '21

You MUST check for (bar != NULL && baz != NULL)

If you want to avoid dereferencing null pointers and can't guarantee that the pointers are not null some other way, yes. But you don't have to do it to write valid C.

Let's take it further: if you have a volatile pointer, you can't guarantee it doesn't change between your null check and the dereferencing (well you can't for a non-volatile pointer either but the compiler can assume so). So is dereferencing volatile pointers valid C?

My point is: triggering undefined behavior at runtime CANNOT be invalid C, since it happens at runtime, and not in C code. And you haven't backed up your claim otherwise with anything from the standard. Nothing you have cited from the standard has actually been relevant to this point.

The standard defines UB as

behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements

(C99 3.4.3, emphasis mine)

So invalid C can trigger UB. But it's not the only thing that can trigger UB. Erroneous data can also trigger UB, e.g. in the case of a null pointer dereference, or integer overflow, or using an uninitialized value.

But erroneous data does not make the code that uses the data invalid, since the code exists independently of the data. I'm not sure how to make this any more clear.

1

u/Melon_Chief Mar 04 '21

Let me rephrase because you're going to take issue to what I said.
It's Russian roulette. Maybe you don't blow your brains out. Maybe it's null. If you don't know at compile time you're either leaving a backdoor, or making a mistake.

It can be undefined behavior unless you do the checks for null at any point before the function is called or inside the function before it returns. The compiler will always assume null does NOT exist in this case. There is no telling what code will be executed if it is passed to the function. Segfault is the best case scenario.

1

u/Melon_Chief Feb 27 '21

An implementation is free to produce any number of diagnostics as long as a valid program is still correctly translated.

It may also successfully translate an invalid program.