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

String literals can technically be dropped prematurely #777

Open
yorickpeterse opened this issue Nov 6, 2024 · 0 comments
Open

String literals can technically be dropped prematurely #777

yorickpeterse opened this issue Nov 6, 2024 · 0 comments
Labels
bug Defects, unintended behaviour, etc compiler Changes related to the compiler runtime Changes related to the Rust-based runtime library

Comments

@yorickpeterse
Copy link
Collaborator

Please describe the bug

When referring to a string literal, the compiler inserts an atomic increment automatically. This ensures that when we're done with the string literal, the ref count is reduced to what it was before, and thus the memory isn't dropped. This means that the minimum ref count when we start using the string is always 2.

However, it's technically possible to increment the same string $2^{32} - 1$ times such that the counter overflows to 1. The next decrement then observes the new value to be zero, and proceeds to drop the string. The next decrement (or any use of the string for that matter) then operates on invalid memory.

Although it's unlikely to run into such many increments in reality, it's technically possible and thus technically unsound. Since at compile time we can't tell the difference between a compile-time string and a runtime string (as they're meant to be interchangeable), this would require additional runtime support (e.g. an extra flag in the object header). This however could incur an additional cost, while in reality we might never need it. As such, I'm not sure yet what to do here (if anything).

Operating system

Fedora

Inko version

main

@yorickpeterse yorickpeterse added bug Defects, unintended behaviour, etc compiler Changes related to the compiler runtime Changes related to the Rust-based runtime library labels Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Defects, unintended behaviour, etc compiler Changes related to the compiler runtime Changes related to the Rust-based runtime library
Projects
None yet
Development

No branches or pull requests

1 participant