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

Fix Steam init errors in Unity IL2CPP builds #803

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ndepoel
Copy link

@ndepoel ndepoel commented Jan 23, 2025

This resolves #802

After running into the above issue (which was also mentioned in pull request #773) and reporting it, I kept digging and eventually found out that the issue lies with the custom string marshaller used to convert the interface versions string into a native C string pointer. As the problem only happens in Unity IL2CPP standalone builds I am unable to analyze it any deeper but I do have a theory what might be happening.

I suspect that the IL2CPP runtime (possibly its GC) is calling cleanup on the custom marshaller too early, causing the native string memory to be released before the SteamAPI_Init function is done reading its contents. This is a classic use-after-free scenario. This freed memory may then be overwritten by other data, explaining the garbage that Steam returns in its error message, as it will be reading that new data from memory expecting it to be part of the interface versions string.

I managed to circumvent the problem by performing the string conversion in-place, so I can control the lifetime of the native string buffer explicitly, ensuring it won't get freed until after SteamAPI_Init returns a result. With this fix in place, the intermittent Steam initialization errors in our Unity IL2CPP builds have gone away entirely.

…age in Unity IL2CPP builds, caused by a suspected use-after-free scenario from the custom string marshaller being cleaned up too early.
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.

Steam client initialization fails with VersionMismatch error (Unity, Windows, IL2CPP)
1 participant