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

[CIR][ABI][Lowering] Fixes calling convention #1308

Merged
merged 4 commits into from
Feb 5, 2025

Conversation

gitoleg
Copy link
Collaborator

@gitoleg gitoleg commented Feb 4, 2025

This PR fixes two run time bugs in the calling convention pass. These bugs were found with csmith.

Case #1. Return value from a function.
Before this PR the returned value were stored in a bit casted memory location.
But for the next example it's not safe: the size of a memory slot is less than the size of return value. And the store operation cause a segfault!

#pragma pack(push)
#pragma pack(1)
 typedef struct {
    int f0 : 18;
    int f1 : 31;
    int f2 : 5;
    int f3 : 29;
    int f4 : 24;
 } PackedS;
#pragma pack(pop)

CIR type for this struct is !ty_PackedS1_ = !cir.struct<struct "PackedS1" {!cir.array<!u8i x 14>}>, i.e. it occupies 14 bytes.
Before this PR the next code

PackedS foo(void) {
     PackedS s;
     return s;
}

 void check(void) {     
     PackedS y = foo();    
}

produced the next CIR:

  %0 = cir.alloca !ty_PackedS1_, !cir.ptr<!ty_PackedS1_>, ["y", init] {alignment = 1 : i64}
  %1 = cir.call @foo() : () -> !cir.array<!u64i x 2> 
  %2 = cir.cast(bitcast, %0 : !cir.ptr<!ty_PackedS1_>), !cir.ptr<!cir.array<!u64i x 2>>
  cir.store %1, %2 : !cir.array<!u64i x 2>, !cir.ptr<!cir.array<!u64i x 2>>

As one cat see, %1 is an array of two 64-bit integers and the memory was allocated for 14 bytes only (size of struct). Hence the segfault! This PR fixes such cases and now we have a coercion through memory, which is even with the OG.

Case #2. Passing an argument from a pointer deref.
Previously for the struct types passed by value we tried to find alloca instruction in order to use it as a source for memcpy operation. But if we have pointer dereference, (in other words if we have a <!cir.ptr < !cir.ptr ... > > as alloca result) we don't need to search for the address of the location where this pointer stored - instead we're interested in the pointer itself. And it's a general approach - instead of trying to find an alloca instruction we need to find a first pointer on the way - that will be an address we meed to use for the memcpy source.

I combined these two cases into a single PR since there are only few changes actually. But I can split in two if you'd prefer

Copy link

github-actions bot commented Feb 4, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@gitoleg gitoleg changed the title Fix call conv [CIR][ABI][Lowering] Fixes calling convention Feb 4, 2025
Copy link
Member

@bcardosolopes bcardosolopes left a comment

Choose a reason for hiding this comment

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

Great!

@bcardosolopes bcardosolopes merged commit 5373f42 into llvm:main Feb 5, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants