avast / avast/retdec

Simple main C program decompiled wrong

Open
#343 0 comments 0 reactions 0 assignees View on GitHub
bug enhancement P-output
Dominant language
C++
Stars
8.6k
Forks
1k
PR merge metrics
No merged PRs in 30d

Description

Are you mostly interested in Delphi? Below is a simple C program, for which decompiler confuses arguments in a weird way.

commit 1da7146b9a4d4c777eff73a975337d84f7ab0e17

```c
$ cat test.cc
#include

volatile double val = 6.0;

int main(int argc, char* argv[])
{
int result = 0;
if (argc > 1)
result = (int)val + argc;
else
result = (int)val - argc;

printf("result = %d\n", result);

return result;
}
```

```
$ gcc -m32 -O0 test.cc -o test
```

```
$ ./retdec-decompiler.sh ./test
##### Checking if file is a Mach-O Universal static library...
...
##### Done!
```

```c
$ cat test.c
//
// This file was generated by the Retargetable Decompiler
// Website: https://retdec.com
// Copyright (c) 2018 Retargetable Decompiler
//

#include
#include

// ----------------- Float Types Definitions ------------------

typedef double float64_t;

// --------------------- Global Variables ---------------------

float64_t g1 = 6.0;

// ------------------------ Functions -------------------------

// Address range: 0x804840b - 0x8048494
int main(int argc, char ** argv) {
int32_t v1 = (int32_t)argv; // 0x8048449
int32_t v2 = argv > (char **)1 ? v1 : -v1;
int32_t result = (int32_t)g1 + v2;
printf("result = %d\n", result);
return result;
}

// --------------- Dynamically Linked Functions ---------------

// int printf(const char * restrict format, ...);

// --------------------- Meta-Information ---------------------

// Detected compiler/packer: gcc (5.5.0)
// Detected functions: 1
// Decompilation date: 2018-06-23 22:29:18
```

The resulting code is seriously broken: instead of checking `argc`, it *somehow* ends up checking the value of `argv`. Obviously, the result is completely wrong:

```
$ gcc -O0 -m32 test.c -o test.check
```

```
$ ./test 1
result = 8

$ ./test.check 1
result = -7295126
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Reproduce the report using commit 1da7146b9a4d4c777eff73a975337d84f7ab0e17, test.cc, gcc -m32 -O0, and retdec-decompiler.sh. Compare the generated test.c with the original and run both binaries using an argument such as ./test 1. Done means the decompiled program checks argc and produces the same result as the original.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, cpp
Domain
compilers, reverse-engineering
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.