Skip to content

[LLDB] Don't cache module sp when Activate() fails. #95586

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

Merged
merged 5 commits into from
Jun 18, 2024

Conversation

thetruestblue
Copy link
Contributor

Currently, the instrumentation runtime is caching a library the first time it sees it in the module list. However, in some rare cases on Darwin, the cached pre-run unloaded modules are different from the runtime module that is loaded at runtime. This patch removes the cached module if the plugin fails to activate, ensuring that on subsequent calls we don't try to activate using the unloaded cached module.

There are a few related bugs to fix in a follow up: CheckIfRuntimeValid should have a stronger check to ensure the module is loaded and can be activated. Further investigation in UpdateSpecialBinariesFromNewImageInfos calling ModulesDidLoad when the module list may have unloaded modules.

I have not included a test for the following reasons:

  1. This is an incredibly rare occurance and is only observed in a specific circumstance on Darwin. It is tied to behavior in the DynamicLoader thai is not commonly encountered.

  2. It is difficult to reproduce -- this bug requires precise conditions on darwin and it is unclear how we'd reproduce that in a controlled testing environment.

rdar://128971453

thetruestblue and others added 2 commits June 14, 2024 15:05
Currently, the instrumentation runtime is caching a library the first time it sees it in the module list. However, in some rare cases on Darwin, the cached pre-run unloaded modules are different from the runtime module that is loaded at runtime. This patch removes the cached module if the plugin fails to activate, ensuring that on subsequent calls we don't try to activate using the unloaded cached module.

There are a few related bugs to fix in a follow up: CheckIfRuntimeValid should have a stronger check to ensure the module is loaded and can be activated. Further investigation in UpdateSpecialBinariesFromNewImageInfos calling ModulesDidLoad when the module list may have unloaded modules.

I have not included a test for the following reasons:
1. This is an incredibly rare occurance and is only observed in a specific circumstance on Darwin. It is tied to behavior in the DynamicLoader thai is not commonly encountered.

2. It is difficult to reproduce -- this bug requires precise conditions on darwin and it is unclear how we'd reproduce that in a controlled testing environment.

rdar://128971453
@thetruestblue thetruestblue reopened this Jun 14, 2024
@thetruestblue thetruestblue marked this pull request as ready for review June 14, 2024 19:14
@llvmbot llvmbot added the lldb label Jun 14, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 14, 2024

@llvm/pr-subscribers-lldb

Author: None (thetruestblue)

Changes

Currently, the instrumentation runtime is caching a library the first time it sees it in the module list. However, in some rare cases on Darwin, the cached pre-run unloaded modules are different from the runtime module that is loaded at runtime. This patch removes the cached module if the plugin fails to activate, ensuring that on subsequent calls we don't try to activate using the unloaded cached module.

There are a few related bugs to fix in a follow up: CheckIfRuntimeValid should have a stronger check to ensure the module is loaded and can be activated. Further investigation in UpdateSpecialBinariesFromNewImageInfos calling ModulesDidLoad when the module list may have unloaded modules.

I have not included a test for the following reasons:

  1. This is an incredibly rare occurance and is only observed in a specific circumstance on Darwin. It is tied to behavior in the DynamicLoader thai is not commonly encountered.

  2. It is difficult to reproduce -- this bug requires precise conditions on darwin and it is unclear how we'd reproduce that in a controlled testing environment.

rdar://128971453


Full diff: https://github.com/llvm/llvm-project/pull/95586.diff

1 Files Affected:

  • (modified) lldb/source/Target/InstrumentationRuntime.cpp (+2)
diff --git a/lldb/source/Target/InstrumentationRuntime.cpp b/lldb/source/Target/InstrumentationRuntime.cpp
index 9f22a1be20ccb..94f4ca353d7ef 100644
--- a/lldb/source/Target/InstrumentationRuntime.cpp
+++ b/lldb/source/Target/InstrumentationRuntime.cpp
@@ -60,6 +60,8 @@ void InstrumentationRuntime::ModulesDidLoad(
       if (CheckIfRuntimeIsValid(module_sp)) {
         SetRuntimeModuleSP(module_sp);
         Activate();
+        if (IsActive())
+          SetRuntimeModuleSP({}); // Don't cache module if activation failed.
         return false; // Stop iterating, we're done.
       }
     }

Copy link
Collaborator

@jimingham jimingham left a comment

Choose a reason for hiding this comment

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

LGTM
I also looked at this a bit, and couldn't come up with a reliable way to write a test.

Comment on lines 63 to 64
if (IsActive())
SetRuntimeModuleSP({}); // Don't cache module if activation failed.
Copy link
Member

Choose a reason for hiding this comment

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

Should this be if (!IsActive())?

Copy link
Contributor Author

@thetruestblue thetruestblue Jun 14, 2024

Choose a reason for hiding this comment

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

Thanks Jonas. 100%.
Fixed.

Copy link
Member

@JDevlieghere JDevlieghere left a comment

Choose a reason for hiding this comment

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

LGTM

@thetruestblue thetruestblue merged commit 41f6aee into llvm:main Jun 18, 2024
3 of 5 checks passed
@thetruestblue thetruestblue deleted the instrumention-runtime-activate branch June 18, 2024 14:23
thetruestblue added a commit to swiftlang/llvm-project that referenced this pull request Jun 18, 2024
Currently, the instrumentation runtime is caching a library the first
time it sees it in the module list. However, in some rare cases on
Darwin, the cached pre-run unloaded modules are different from the
runtime module that is loaded at runtime. This patch removes the cached
module if the plugin fails to activate, ensuring that on subsequent
calls we don't try to activate using the unloaded cached module.

There are a few related bugs to fix in a follow up: CheckIfRuntimeValid
should have a stronger check to ensure the module is loaded and can be
activated. Further investigation in
UpdateSpecialBinariesFromNewImageInfos calling ModulesDidLoad when the
module list may have unloaded modules.

I have not included a test for the following reasons:
1. This is an incredibly rare occurance and is only observed in a
specific circumstance on Darwin. It is tied to behavior in the
DynamicLoader thai is not commonly encountered.

2. It is difficult to reproduce -- this bug requires precise conditions
on darwin and it is unclear how we'd reproduce that in a controlled
testing environment.

rdar://128971453
JDevlieghere added a commit to swiftlang/llvm-project that referenced this pull request Jun 18, 2024
[LLDB] Don't cache module sp when Activate() fails. (llvm#95586)
AlexisPerry pushed a commit to llvm-project-tlp/llvm-project that referenced this pull request Jul 9, 2024
Currently, the instrumentation runtime is caching a library the first
time it sees it in the module list. However, in some rare cases on
Darwin, the cached pre-run unloaded modules are different from the
runtime module that is loaded at runtime. This patch removes the cached
module if the plugin fails to activate, ensuring that on subsequent
calls we don't try to activate using the unloaded cached module.

There are a few related bugs to fix in a follow up: CheckIfRuntimeValid
should have a stronger check to ensure the module is loaded and can be
activated. Further investigation in
UpdateSpecialBinariesFromNewImageInfos calling ModulesDidLoad when the
module list may have unloaded modules.

I have not included a test for the following reasons:
1. This is an incredibly rare occurance and is only observed in a
specific circumstance on Darwin. It is tied to behavior in the
DynamicLoader thai is not commonly encountered.

2. It is difficult to reproduce -- this bug requires precise conditions
on darwin and it is unclear how we'd reproduce that in a controlled
testing environment.

rdar://128971453
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants