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(vmgen): string literal changing at run-time #1296

Merged
merged 1 commit into from
May 2, 2024

Conversation

zerbina
Copy link
Collaborator

@zerbina zerbina commented May 2, 2024

Summary

Fix a string literal's underlying storage being modified when passing
a string literal to a sink string parameter, where the parameter is
modified within the callee. Only the VM backend was affected.

Details

No copy of the string literal (which was lifted into a constant
earlier) is created for string literals passed to sink parameters.
This is not a problem for the C (copy-on-write strings are used) and
JS backend (a new run-time string instance is created on each string
literal usage), but it is for the VM backend.

Without a copy, the callee can directly modify the constant string's
underlying character storage, affecting all string literal usage
throughout the program.

mirgen should introduce an intermediate temporary for the argument in
this case, but - at the moment - doing so would lead to worse code
generation for all code generators, so the problem is worked around in
vmgen, by creating a copy of the string constant when passed to a
sink string parameter.

@zerbina zerbina added bug Something isn't working compiler/backend Related to backend system of the compiler labels May 2, 2024
Summary
=======

Fix a string literal's underlying storage being modified when passing
a string literal to a `sink string` parameter, where the parameter is
modified within the callee. Only the VM backend was affected.

Details
=======

No copy of the string literal (which was lifted into a constant
earlier) is created for string literals passed to `sink` parameters.
This is not a problem for the C (copy-on-write strings are used) and
JS backend (a new run-time string instance is created on each string
literal usage), but it is for the VM backend.

Without a copy, the callee can directly modify the constant string's
underlying character storage, affecting all string literal usage
throughout the program.

`mirgen` should introduce an intermediate temporary for the argument in
this case, but - at the moment - doing so would lead to worse code
generation for all code generators, so the problem is worked around in
`vmgen`, by creating a copy of the string constant when passed to a
`sink string` parameter.
@saem
Copy link
Collaborator

saem commented May 2, 2024

/merge

Copy link

github-actions bot commented May 2, 2024

Merge requested by: @saem

Contents after the first section break of the PR description has been removed and preserved below:


Notes for Reviewers

@chore-runner chore-runner bot added this pull request to the merge queue May 2, 2024
Merged via the queue into nim-works:devel with commit f244f3d May 2, 2024
31 checks passed
@zerbina zerbina deleted the fix-vm-sink-bug branch May 2, 2024 21:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler/backend Related to backend system of the compiler
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants