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

asyncdispatch+stackTraceOverride: fix premature garbage collection #18039

Merged
merged 1 commit into from
May 19, 2021
Merged

asyncdispatch+stackTraceOverride: fix premature garbage collection #18039

merged 1 commit into from
May 19, 2021

Conversation

stefantalpalaru
Copy link
Contributor

Copying StackTraceEntry instances, when nimStackTraceOverride is defined, breaks the link between a cstring field that's supposed to point at another string field in the same object.

Sometimes, the original object is garbage collected, that memory region reused for storing other strings, so when the StackTraceEntry copy tries to use its cstring pointer to construct a traceback message, it accesses unrelated strings.

This only happens for async tracebacks and this patch prevents that by making sure we only use the string fields when nimStackTraceOverride is defined.

Async tracebacks also beautified slightly by getting rid of an extra line that was supposed to be commented out, along with the corresponding debugging output.

There's also a micro-optimisation to avoid concatenating two strings just to get their combined length and some trivial cleanup.

It would make sense to backport this to 1.4 and 1.2.

@@ -299,20 +312,23 @@ proc `$`*(stackTraceEntries: seq[StackTraceEntry]): string =
else:
let entries = stackTraceEntries

result = ""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't delete this assignment, Nim will get definite assignment analysis for all types.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty string assignments brought back.

Copying StackTraceEntry instances when nimStackTraceOverride is defined
breaks the link between a cstring field that's supposed to point at
another string field in the same object.

Sometimes, the original object is garbage collected, that memory region
reused for storing other strings, so when the StackTraceEntry copy tries
to use its cstring pointer to construct a traceback message, it accesses
unrelated strings.

This only happens for async tracebacks and this patch prevents that by
making sure we only use the string fields when nimStackTraceOverride is
defined.

Async tracebacks also beautified slightly by getting rid of an extra line
that was supposed to be commented out, along with the corresponding debugging output.

There's also a micro-optimisation to avoid concatenating two strings just
to get their combined length.
Copy link
Contributor

@dom96 dom96 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix.

Should we also deprecate the filename/procname accessors and then in a few releases remove them/make them private?

@stefantalpalaru
Copy link
Contributor Author

Should we also deprecate the filename/procname accessors and then in a few releases remove them/make them private?

I don't like breaking backwards compatibility in a public API.

We committed the sin, we need to pay the price by maintaining that API for as long as we can ;-)

@Araq Araq merged commit a1c82c3 into nim-lang:devel May 19, 2021
@stefantalpalaru stefantalpalaru deleted the stacktraceoverride branch May 19, 2021 17:52
narimiran pushed a commit that referenced this pull request May 19, 2021
…backport:1.2]

Copying StackTraceEntry instances when nimStackTraceOverride is defined
breaks the link between a cstring field that's supposed to point at
another string field in the same object.

Sometimes, the original object is garbage collected, that memory region
reused for storing other strings, so when the StackTraceEntry copy tries
to use its cstring pointer to construct a traceback message, it accesses
unrelated strings.

This only happens for async tracebacks and this patch prevents that by
making sure we only use the string fields when nimStackTraceOverride is
defined.

Async tracebacks also beautified slightly by getting rid of an extra line
that was supposed to be commented out, along with the corresponding debugging output.

There's also a micro-optimisation to avoid concatenating two strings just
to get their combined length.

(cherry picked from commit a1c82c3)
@dom96
Copy link
Contributor

dom96 commented May 19, 2021

well, this wouldn't be breaking backwards compat. It would be first a deprecation, we can figure out what to do next. The idea is to make it clear that these fields are dangerous.

narimiran pushed a commit that referenced this pull request May 20, 2021
…backport:1.2]

Copying StackTraceEntry instances when nimStackTraceOverride is defined
breaks the link between a cstring field that's supposed to point at
another string field in the same object.

Sometimes, the original object is garbage collected, that memory region
reused for storing other strings, so when the StackTraceEntry copy tries
to use its cstring pointer to construct a traceback message, it accesses
unrelated strings.

This only happens for async tracebacks and this patch prevents that by
making sure we only use the string fields when nimStackTraceOverride is
defined.

Async tracebacks also beautified slightly by getting rid of an extra line
that was supposed to be commented out, along with the corresponding debugging output.

There's also a micro-optimisation to avoid concatenating two strings just
to get their combined length.

(cherry picked from commit a1c82c3)
PMunch pushed a commit to PMunch/Nim that referenced this pull request Mar 28, 2022
…18039) [backport:1.2]

Copying StackTraceEntry instances when nimStackTraceOverride is defined
breaks the link between a cstring field that's supposed to point at
another string field in the same object.

Sometimes, the original object is garbage collected, that memory region
reused for storing other strings, so when the StackTraceEntry copy tries
to use its cstring pointer to construct a traceback message, it accesses
unrelated strings.

This only happens for async tracebacks and this patch prevents that by
making sure we only use the string fields when nimStackTraceOverride is
defined.

Async tracebacks also beautified slightly by getting rid of an extra line
that was supposed to be commented out, along with the corresponding debugging output.

There's also a micro-optimisation to avoid concatenating two strings just
to get their combined length.
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.

3 participants