Skip to content

[MemProf] Use correct print_text value #125793

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 1 commit into from
Feb 5, 2025

Conversation

ellishg
Copy link
Contributor

@ellishg ellishg commented Feb 5, 2025

Use the correct value for print_text in FinishAndWrite().

This value was initialized in the constructor of a global variable

static Allocator instance(LINKER_INITIALIZED);

The flag values in flags() are populated in InitializeFlags(), which is also called in a constructor of a global variable

// Initialize as requested from some part of MemProf runtime library
// (interceptors, allocator, etc).
void MemprofInitFromRtl() { MemprofInitInternal(); }
#if MEMPROF_DYNAMIC
// Initialize runtime in case it's LD_PRELOAD-ed into uninstrumented executable
// (and thus normal initializers from .preinit_array or modules haven't run).
class MemprofInitializer {
public:
MemprofInitializer() { MemprofInitFromRtl(); }
};
static MemprofInitializer memprof_initializer;
#endif // MEMPROF_DYNAMIC

I think the order that these two functions run is undefined, and the value of the field print_text depends on this order when the user overrides default values.

@ellishg ellishg requested a review from snehasish February 5, 2025 01:25
@llvmbot llvmbot added compiler-rt PGO Profile Guided Optimizations labels Feb 5, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 5, 2025

@llvm/pr-subscribers-pgo

Author: Ellis Hoag (ellishg)

Changes

Use the correct value for print_text in FinishAndWrite().

This value was initialized in the constructor of a global variable

static Allocator instance(LINKER_INITIALIZED);

The flag values in flags() are populated in InitializeFlags(), which is also called in a constructor of a global variable

// Initialize as requested from some part of MemProf runtime library
// (interceptors, allocator, etc).
void MemprofInitFromRtl() { MemprofInitInternal(); }
#if MEMPROF_DYNAMIC
// Initialize runtime in case it's LD_PRELOAD-ed into uninstrumented executable
// (and thus normal initializers from .preinit_array or modules haven't run).
class MemprofInitializer {
public:
MemprofInitializer() { MemprofInitFromRtl(); }
};
static MemprofInitializer memprof_initializer;
#endif // MEMPROF_DYNAMIC

I think the order that these two functions run is undefined, and the value of the field print_text depends on this order when the user overrides default values.


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

1 Files Affected:

  • (modified) compiler-rt/lib/memprof/memprof_allocator.cpp (+3-4)
diff --git a/compiler-rt/lib/memprof/memprof_allocator.cpp b/compiler-rt/lib/memprof/memprof_allocator.cpp
index c3448c22532bb89..60f5c853f9d76ff 100644
--- a/compiler-rt/lib/memprof/memprof_allocator.cpp
+++ b/compiler-rt/lib/memprof/memprof_allocator.cpp
@@ -291,10 +291,9 @@ struct Allocator {
 
   atomic_uint8_t destructing;
   atomic_uint8_t constructed;
-  bool print_text;
 
   // ------------------- Initialization ------------------------
-  explicit Allocator(LinkerInitialized) : print_text(flags()->print_text) {
+  explicit Allocator(LinkerInitialized) {
     atomic_store_relaxed(&destructing, 0);
     atomic_store_relaxed(&constructed, 1);
   }
@@ -350,13 +349,13 @@ struct Allocator {
   }
 
   void FinishAndWrite() {
-    if (print_text && common_flags()->print_module_map)
+    if (flags()->print_text && common_flags()->print_module_map)
       DumpProcessMap();
 
     allocator.ForceLock();
 
     InsertLiveBlocks();
-    if (print_text) {
+    if (flags()->print_text) {
       if (!flags()->print_terse)
         Printf("Recorded MIBs (incl. live on exit):\n");
       MIBMap.ForEach(PrintCallback,

@ellishg ellishg merged commit f9dbf1a into llvm:main Feb 5, 2025
10 checks passed
@ellishg ellishg deleted the memprof-print-text-bug branch February 5, 2025 17:02
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler-rt PGO Profile Guided Optimizations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants