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

Adding a check to verify that functions names do not conflict when translated to assembly labels #1067

Merged
merged 1 commit into from
Mar 5, 2025

Conversation

MrDaiki
Copy link
Collaborator

@MrDaiki MrDaiki commented Feb 25, 2025

Issue

Assembly generation use PrintCommon.escape function to build labels which can creates names conflicts because of namespaces. see (#993).

Solution

We introduce a map in pretyping.ml environment that check if a label is already used by another function.

@MrDaiki MrDaiki added the bug label Feb 25, 2025
@MrDaiki MrDaiki force-pushed the label-names-conflicts branch from 9dd611a to b86a6e7 Compare February 25, 2025 14:06
@MrDaiki MrDaiki marked this pull request as ready for review February 25, 2025 14:07
@vbgl
Copy link
Member

vbgl commented Feb 25, 2025

The issue only arises for export function. Is it necessary to also fail when one of the functions is internal or inline?

@MrDaiki
Copy link
Collaborator Author

MrDaiki commented Feb 25, 2025

The issue only arises for export function. Is it necessary to also fail when one of the functions is internal or inline?

With this variant of original issue example :

namespace ble {
    fn e() -> reg u64 {
        reg u64 x = 0;
        return x;
    }
}

fn ble__e() -> reg u64 {
    reg u64 x = 1;
    return x;
}

export fn sad() -> reg u64 {
    reg u64 x = ble::e();
    reg u64 y = ble__e();
    return y;
}

generated assembly code is :

        .att_syntax
        .text
        .p2align        5
        .globl  _sad
        .globl  sad
_sad:
sad:
        movq    %rsp, %rsi
        andq    $-8, %rsp
        call    Lble__e$1
Lsad$2:
        call    Lble__e$1
Lsad$1:
        movq    %rsi, %rsp
        ret
Lble__e$1:
        movq    $1, %rax
        ret
Lble__e$1:
        ret

We can see that ble::e assembly code isn't generated, and that the two function calls are done on ble__e. So this issue exist even for internal functions.

I didn't found any bug using inline functions, and I don't expect one since inline dissapears before printasm. I will update the pull request to raise an error only if the function is not inline.

@MrDaiki MrDaiki force-pushed the label-names-conflicts branch 2 times, most recently from b220b93 to e70a1fa Compare February 25, 2025 15:15
Copy link
Contributor

@bgregoir bgregoir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it will be nicer to not modify the type Env.env.
Write a checker that given a list gfunc ensure that this clash of name do not occurs.
We can call it when we want, maybe at the end of pretyping.

@MrDaiki
Copy link
Collaborator Author

MrDaiki commented Feb 28, 2025

I think it will be nicer to not modify the type Env.env. Write a checker that given a list gfunc ensure that this clash of name do not occurs. We can call it when we want, maybe at the end of pretyping.

I agree with your proposition, but this will require deeper work. For the moment, I will qualify this PR as draft so we can find a suitable (and reusable) solution.

@MrDaiki MrDaiki marked this pull request as draft February 28, 2025 08:30
@MrDaiki MrDaiki force-pushed the label-names-conflicts branch from e70a1fa to ff94257 Compare February 28, 2025 10:09
@MrDaiki MrDaiki changed the title Adding a check in pre-typing to verify that functions names do not conflict when translated to assembly labels Adding a check to verify that functions names do not conflict when translated to assembly labels Feb 28, 2025
@MrDaiki
Copy link
Collaborator Author

MrDaiki commented Feb 28, 2025

I released a new version applying your suggestion @bgregoir. I think it is ready for review now.

@MrDaiki MrDaiki marked this pull request as ready for review February 28, 2025 10:11
@MrDaiki MrDaiki force-pushed the label-names-conflicts branch from ff94257 to bd5a829 Compare February 28, 2025 10:13
Check if functions have conflicting names when translated to their assembly label names.
Raise `DuplicateLabelError` if a conflict is found between two non inline functions (because inline function are substituted before assembly traduction).
*)
let check_labels (prog: ('len,'info,'asm) gprog) =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should return the list of conflicts instead of failing early.

@@ -134,6 +134,13 @@ let main () =
exit 0
end;

ignore (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ignore is fishy.

ignore (
try Label_check.check_labels pprog with
| Label_check.DuplicateLabelError (loc, code) ->
hierror ~loc:(Lone loc) ~kind:"label error"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, a warning might be more appropriate.

@MrDaiki MrDaiki force-pushed the label-names-conflicts branch 2 times, most recently from c3833f8 to 72d23ea Compare March 4, 2025 08:59
@MrDaiki
Copy link
Collaborator Author

MrDaiki commented Mar 4, 2025

I did changes as suggested by @vbgl . Since It only print warning now, I didn't saw the interested or returning error list (at least in the current version of error handling). If you think it is better, I can add it.

@MrDaiki MrDaiki force-pushed the label-names-conflicts branch from 72d23ea to 5c389fa Compare March 4, 2025 10:01
Copy link
Member

@vbgl vbgl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Please add a label_check.mli
  • What is the purpose of the test case?

@MrDaiki MrDaiki force-pushed the label-names-conflicts branch from 5c389fa to 9812715 Compare March 4, 2025 13:41
vbgl
vbgl previously approved these changes Mar 4, 2025
@MrDaiki
Copy link
Collaborator Author

MrDaiki commented Mar 4, 2025

I rewrote error type to be simpler to understand and change the error message to be more clear. I think it is ready to merge now.

Comment on lines +7 to +10
type function_label = {
loc : L.t;
fname : string;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about type function_label = string L.located

Copy link
Collaborator Author

@MrDaiki MrDaiki Mar 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like it but it is also a good type. 'a L.located should only represent located object in my opinion. But I can make the change if you prefer

…n translated to assembly labels. Inline functions are not affected by this change
@vbgl vbgl force-pushed the label-names-conflicts branch from 3a7fbca to 4fb6b55 Compare March 5, 2025 14:26
@vbgl vbgl dismissed bgregoir’s stale review March 5, 2025 15:47

Benjamin comment has been implemented.

@vbgl vbgl merged commit bb50445 into jasmin-lang:main Mar 5, 2025
1 check passed
@vbgl vbgl deleted the label-names-conflicts branch March 5, 2025 15:47
@vbgl vbgl added this to the 2025.02.1 milestone Mar 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants