Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Switch Restructuring] Insert CaseNodes #20

Closed
ebehner opened this issue Jan 12, 2022 · 4 comments · Fixed by #257
Closed

[Switch Restructuring] Insert CaseNodes #20

ebehner opened this issue Jan 12, 2022 · 4 comments · Fixed by #257
Assignees
Labels
feature-request New feature or request priority-low Low priority issue

Comments

@ebehner
Copy link
Collaborator

ebehner commented Jan 12, 2022

Proposal

The MissingCaseFinder searches for possible cases that were not found/there when constructing the initial switch. Now, we insert such a potential case node only if the corresponding case constant does not exist. However, there are cases where we still can insert the missing case.

Consider the following decompiled code (test_switch.zip test7_b):

int test7_b() {
    int var_0;
    int * var_1;
    printf("Enter week number(1-7): ");
    var_1 = &var_0;
    __isoc99_scanf(0x804c025, var_1);
    if (var_0 == 0x190) {
        printf("Thursday");  // <---------- missing case
    }
    switch(var_0) {
    case 0x0:
        printf("Monday");
        break;
    case 0x6:
        printf("Saturday");
        break;
    case 0x9:
        printf("Sunday");
        break;
    case 0xc:
        printf("Tuesday");
        break;
    case 0x22:
        printf("Wednesday");
        break;
    case 0x190:
    case 0x1f4:
        printf("Friday");
        break;
    default:
        printf("Invalid input! Please enter week number between 1-7.");
    }
    return 0;
}

The preferred output would be:

int test7_b() {
    int var_0;
    int * var_1;
    printf("Enter week number(1-7): ");
    var_1 = &var_0;
    __isoc99_scanf(0x804c025, var_1);
    switch(var_0) {
    case 0x0:
        printf("Monday");
        break;
    case 0x6:
        printf("Saturday");
        break;
    case 0x9:
        printf("Sunday");
        break;
    case 0xc:
        printf("Tuesday");
        break;
    case 0x22:
        printf("Wednesday");
        break;
    case 0x190:
        printf("Thursday");
    case 0x1f4:
        printf("Friday");
        break;
    default:
        printf("Invalid input! Please enter week number between 1-7.");
    }
    return 0;
}

A similar example is test18 in the same executable, we currently have:

int test18() {
    int var_0;
    int * var_1;
    printf("Enter week number(1-7): ");
    var_1 = &var_0;
    __isoc99_scanf(0x804c025, var_1);
    switch(var_0) {
    case 1:
        printf("Monday");
        var_0 += 0x1f4;
        break;
    case 0x1f4:
        printf("Friday");
        break;
    }
    if ((var_0 != 1) && (var_0 != 12)) {
        printf("Invalid input! Please enter week number between 1-7."); // <---------- do not find default do due missing case
    }
    else {
        printf("Tuesday"); // <---------- missing case
    }
    printf("the number is %d", var_0);
    return 0;
}

But we would like to have the following output:

int test18() {
    int var_0;
    int * var_1;
    printf("Enter week number(1-7): ");
    var_1 = &var_0;
    __isoc99_scanf(0x804c025, var_1);
    switch(var_0) {
    case 1:
        printf("Monday");
        var_0 += 0x1f4;
    case 12:
        printf("Tuesday");
        break;
    case 0x1f4:
        printf("Friday");
        break;
    default:
        printf("Invalid input! Please enter week number between 1-7.");
    }
    printf("the number is %d", var_0);
    return 0;
}

Remark: test18 also has another problem, see Bug #19

Approach

Find a good way to check whether it is possible to insert a case node whose case constant already exists.
It is important to consider the reachability of the nodes.

@ebehner ebehner added feature-request New feature or request priority-low Low priority issue labels Jan 12, 2022
@ebehner
Copy link
Collaborator Author

ebehner commented Jun 30, 2022

see also the sample of Issue #34

@ebehner
Copy link
Collaborator Author

ebehner commented Jul 5, 2022

Additionally:
In missing_case_finder.py line 216, two case candidates can have the same constant. At the moment, we add the first that fits. However, this may not be the best solution.

  • Check for the same constants and merge?
  • If this is not the case, find the most suitable (easiest in complexity?)
  • Check which can be added without conflict?

@ebehner ebehner self-assigned this Jun 20, 2023
@ebehner
Copy link
Collaborator Author

ebehner commented Jun 20, 2023

/cib

@github-actions
Copy link
Contributor

Branch issue-20-_Switch_Restructuring_Insert_CaseNodes created!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request New feature or request priority-low Low priority issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant