avast / avast/retdec

[llvmir2hlltool] Incorrect translation LLVM IR to C

Open
#677 3 comments 0 reactions 0 assignees View on GitHub
C-llvmir2hll Q-discussion
Dominant language
C++
Stars
8.6k
Forks
1k
PR merge metrics
No merged PRs in 30d

Description

Hi
I tested retdec on the test:
Original C file:
```C++
$ cat test.c
struct A{
int arr[2];
};
void test_fn(struct A * p){
p->arr[0] = 0;
```
I translate to LLVM IR code
```llvm
clang -S -emit-llvm test.c -o test.ll && cat test.ll

; ModuleID = 'test.c'
source_filename = "test.c"
...
%struct.A = type { [2 x i32] }

; Function Attrs: noinline nounwind optnone uwtable
define void @test_fn(%struct.A*) #0 {
%2 = alloca %struct.A*, align 8
store %struct.A* %0, %struct.A** %2, align 8
%3 = load %struct.A*, %struct.A** %2, align 8
%4 = getelementptr inbounds %struct.A, %struct.A* %3, i32 0, i32 0
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
store i32 0, i32* %5, align 4
ret void
}
```
Then i applied llvmir2hlltool:
```C++
$ retdec-llvmir2hll -o test.retdec.c -target-hll=c test.ll ; cat test.retdec.c
//
// This file was generated by the Retargetable Decompiler
// Website: https://retdec.com
// Copyright (c) 2019 Retargetable Decompiler
//
#include
// ------------------------ Structures ------------------------
struct struct_A {
int32_t e0[2];
};
// ------------------- Function Prototypes --------------------
void test_fn(struct struct_A * a1);
// ------------------------ Functions -------------------------
void test_fn(struct struct_A * a1) {
(&a1->e0)[0] = 0; // <--- error
}
```
line `(&a1->e0)[0] = 0;` has error
operator **&** is extra ~~or operator **->** is wrong which need to replace on operator **.**~~ (sorry, i was wrong)

Contributor guide

No contributing guide indexed for this repository

Research direction

Reproduce the issue with the provided test.c, test.ll, and retdec-llvmir2hll command, then trace the LLVM IR-to-C translation entry point for the getelementptr sequence. Done means the generated C compiles and represents the array assignment without the erroneous address operator; add or run a regression test for this input if the project provides one.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.