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

Incorrect PInvoke definition will implicitly modify context local variable #110110

Closed
Gascognya opened this issue Nov 23, 2024 · 2 comments
Closed

Comments

@Gascognya
Copy link

Description

About libsdl-org/SDL C# bindings by PInvoke.

public static void Main()
{
    SDL_CreateWindow("Demo", 800, 600, 0);

    var loop = true;
    
    while (loop)
    {
        Console.WriteLine(loop); // True
        SDL_PollEvent(out _);
        Console.WriteLine(loop); // False
    }
}
public static void Main()
{
    SDL_CreateWindow("Demo", 800, 600, 0);

    var a = true;
    var b = true;
    var c = true;
    
    while (a || b || c)
    {
        Console.WriteLine($"before: a:{a}, b:{b}, c:{c}");
        SDL_PollEvent(out _);
        Console.WriteLine($"after: a:{a}, b:{b}, c:{c}");
    }
}

it's infinite loop, but broken.

before: a:True, b:True, c:True
after: a:True, b:True, c:False
before: a:True, b:True, c:False
after: a:True, b:True, c:False
before: a:True, b:True, c:False
after: a:False, b:False, c:False

Bug only occurs when the out parameter type definitions are inconsistent.

struct SDL_Event;

[return: MarshalAs(UnmanagedType.I1)]
[LibraryImport("SDL3")]
private static partial bool SDL_PollEvent(out SDL_Event evt);

The true definitions. it's large c union.
https://wiki.libsdl.org/SDL3/SDL_Event

Reproduction Steps

PInvokeSDL.zip

Expected behavior

infinite loop.

Actual behavior

infinite loop was broken.

Regression?

.net 8 and .net 9

Known Workarounds

no

Configuration

.net 8 and .net 9
windows 10
x64

Other information

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Nov 23, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Nov 23, 2024
@Gascognya Gascognya changed the title Incorrect PInvoke definition will implicitly modify local variable Incorrect PInvoke definition will implicitly modify context local variable Nov 23, 2024
@huoyaoyuan
Copy link
Member

out parameters are allocated on the stack and passed by reference. If its size is incorrect, native code accessing it will overwrite other memory on the stack, including return address.

The behavior is expected. Incorrect P/Invoke definition is unsafe and will corrupt things.

@Gascognya
Copy link
Author

out parameters are allocated on the stack and passed by reference. If its size is incorrect, native code accessing it will overwrite other memory on the stack, including return address.

The behavior is expected. Incorrect P/Invoke definition is unsafe and will corrupt things.

that sounds crazy... and thank you bro

@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Nov 23, 2024
@teo-tsirpanis teo-tsirpanis added area-System.Runtime.InteropServices and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Nov 23, 2024
@teo-tsirpanis teo-tsirpanis closed this as not planned Won't fix, can't repro, duplicate, stale Nov 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants