-
Notifications
You must be signed in to change notification settings - Fork 825
Add a string lowering mode disallowing non-UTF-8 strings #6861
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
Conversation
The best way to lower strings is via the "magic imports" API that uses the names of imported string globals as their values. This approach only works for valid UTF-8 strings, though. The existing string-lowering-magic-imports pass falls back to putting non-UTF-8 strings in a JSON custom section, but this requires the runtime to support that custom section for correctness. To help catch errors early when runtimes do not support the strings custom section, add a new pass that uses magic imports and raises an error if there are any invalid strings.
kripken
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm % suggestion to make the new mode depend on magic imports being set, which seems a bit simpler.
src/passes/StringLowering.cpp
Outdated
| StringLowering(bool useMagicImports = false) | ||
| : useMagicImports(useMagicImports) {} | ||
| // Whether to throw a fatal error on non-UTF8 strings that would not be able | ||
| // to use the "magic import" mechanism. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| // to use the "magic import" mechanism. | |
| // to use the "magic import" mechanism. This is only relevant when useMagicImports. |
src/passes/StringLowering.cpp
Outdated
| bool assertUTF8; | ||
|
|
||
| StringLowering(bool useMagicImports = false, bool assertUTF8 = false) | ||
| : useMagicImports(useMagicImports), assertUTF8(assertUTF8) {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| : useMagicImports(useMagicImports), assertUTF8(assertUTF8) {} | |
| : useMagicImports(useMagicImports), assertUTF8(assertUTF8) { | |
| // if we assert UTF8, we must have enabled magic imports. | |
| assert(!assertUTF8 || useMagicImports); | |
| } |
src/passes/StringLowering.cpp
Outdated
| global->module = "'"; | ||
| global->base = Name(utf8.str()); | ||
| } else { | ||
| if (useMagicImports && assertUTF8) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if (useMagicImports && assertUTF8) { | |
| if (assertUTF8) { |
The best way to lower strings is via the "magic imports" API that uses
the names of imported string globals as their values. This approach only
works for valid UTF-8 strings, though. The existing
string-lowering-magic-imports pass falls back to putting non-UTF-8
strings in a JSON custom section, but this requires the runtime to
support that custom section for correctness. To help catch errors early
when runtimes do not support the strings custom section, add a new pass
that uses magic imports and raises an error if there are any invalid
strings.