summaryrefslogtreecommitdiff
path: root/sci-libs/rocFFT/files/rocFFT-10.1.0-accountant-teardown.patch
blob: e8128dd8b40ce3956fcf31b20f978007b41d0c45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
Fix write-after-free of the device memory accountant at exit

Backports: https://github.com/ROCm/rocm-libraries/commit/d2ef3e645eb93bd28f01b022209e3de9d80af46f
--- a/clients/tests/unit_test.cpp
+++ b/clients/tests/unit_test.cpp
@@ -1196,3 +1196,42 @@ TEST(rocfft_UnitTest, DISABLED_plan_capacity_1m)
     PROB_SKIP_UNITTEST();
     run_plan_capacity_test(1'000'000);
 }
+
+// Test calling rocfft_destroy after any library singletons are torn down
+TEST(rocfft_DeathTest, plan_destroyed_at_exit)
+{
+    struct exit_time_plan
+    {
+        rocfft_plan plan = nullptr;
+        ~exit_time_plan()
+        {
+            if(plan)
+                (void)rocfft_plan_destroy(plan);
+        }
+    };
+
+    auto create_plan_destroyed_at_exit = []() -> void {
+        // Create object with static lifetime - we're aiming to have this
+        // be destroyed after any library singleton that might do device
+        // memory accounting.
+        static exit_time_plan holder;
+
+        size_t length = 64;
+        if(rocfft_plan_create(&holder.plan,
+                              rocfft_placement_inplace,
+                              rocfft_transform_type_complex_forward,
+                              rocfft_precision_single,
+                              1,
+                              &length,
+                              1,
+                              nullptr)
+           != rocfft_status_success)
+            std::exit(2);
+        std::exit(0);
+    };
+
+    // Death tests run in a fresh process, so library singletons are
+    // created after the static plan holder.
+    ::testing::GTEST_FLAG(death_test_style) = "threadsafe";
+    EXPECT_EXIT(create_plan_destroyed_at_exit(), ::testing::ExitedWithCode(0), "");
+}
--- a/library/src/CMakeLists.txt
+++ b/library/src/CMakeLists.txt
@@ -34,11 +34,21 @@ function( prepend_path prefix source_list_of_files return_list_of_files )
   set( ${return_list_of_files} ${new_list} PARENT_SCOPE )
 endfunction( )
 
+# Add a definition for internal library targets so code can be
+# conditional on being in-library or not
+function( rocfft_add_internal_build_flag target )
+  get_target_property( target_is_imported ${target} IMPORTED )
+  if( NOT target_is_imported )
+    target_compile_definitions( ${target} PRIVATE ROCFFT_BUILD_INTERNAL )
+  endif()
+endfunction()
+
 # Link to an internal object library.  Setting LINK_LIBRARIES
 # directly prevents the object libraries from being exported by
 # rocm_export_targets in a static build, since they're not supposed
 # to be visible by users of rocFFT.
 function( rocfft_link_internal_lib target lib )
+  rocfft_add_internal_build_flag( ${lib} )
   set_property( TARGET ${target} APPEND PROPERTY
     LINK_LIBRARIES ${lib}
   )
@@ -413,6 +423,7 @@ add_library( rocfft
   ${relative_rocfft_headers_public}
   )
 rocfft_add_coverage_flags( rocfft )
+rocfft_add_internal_build_flag( rocfft )
 
 if( WIN32 )
   configure_file(${CMAKE_CURRENT_SOURCE_DIR}/rocfft.rc.in ${CMAKE_CURRENT_BINARY_DIR}/rocfft.rc)
--- a/shared/gpubuf.h
+++ b/shared/gpubuf.h
@@ -31,6 +31,8 @@
 #include <string>
 #include <vector>
 
+#ifndef ROCFFT_BUILD_INTERNAL
+
 struct DEVICEBUF_MEM_USAGE : public std::runtime_error
 {
     using std::runtime_error::runtime_error;
@@ -190,146 +192,167 @@ private:
     }
 };
 
-// Simple RAII class for GPU buffers.  T is the type of pointer that
-// data() returns
-template <class T = void>
-class gpubuf_t
-{
-public:
-    gpubuf_t() {}
-    // buffers are movable but not copyable
-    gpubuf_t(gpubuf_t&& other)
-    {
-        std::swap(buf, other.buf);
-        std::swap(owned, other.owned);
-        std::swap(bsize, other.bsize);
-        std::swap(device, other.device);
-        std::swap(is_managed_memory, other.is_managed_memory);
-    }
-    gpubuf_t& operator=(gpubuf_t&& other)
-    {
-        std::swap(buf, other.buf);
-        std::swap(owned, other.owned);
-        std::swap(bsize, other.bsize);
-        std::swap(device, other.device);
-        std::swap(is_managed_memory, other.is_managed_memory);
-        return *this;
-    }
-    gpubuf_t(const gpubuf_t&) = delete;
-    gpubuf_t& operator=(const gpubuf_t&) = delete;
-
-    static gpubuf_t make_nonowned(T* p, size_t size_bytes = 0)
-    {
-        gpubuf_t ret;
-        ret.owned             = false;
-        ret.buf               = p;
-        ret.bsize             = size_bytes;
-        ret.is_managed_memory = false; // irrelevant if not owned
-        return ret;
-    }
+#endif
 
-    ~gpubuf_t()
-    {
-        free();
-    }
+#ifdef ROCFFT_BUILD_INTERNAL
+// Put the internal version of gpubuf_t in a different namespace -
+// the intention is for only one to be used in any module, but defend
+// against ODR violations if both are used simultaneously.
+inline namespace rocfft_internal
+{
+#endif
 
-    static bool use_alloc_managed()
+    // Simple RAII class for GPU buffers.  T is the type of pointer that
+    // data() returns
+    template <class T = void>
+    class gpubuf_t
     {
-        return std::getenv("ROCFFT_MALLOC_MANAGED");
-    }
+    public:
+        gpubuf_t() {}
+        // buffers are movable but not copyable
+        gpubuf_t(gpubuf_t&& other)
+        {
+            std::swap(buf, other.buf);
+            std::swap(owned, other.owned);
+            std::swap(bsize, other.bsize);
+            std::swap(device, other.device);
+            std::swap(is_managed_memory, other.is_managed_memory);
+        }
+        gpubuf_t& operator=(gpubuf_t&& other)
+        {
+            std::swap(buf, other.buf);
+            std::swap(owned, other.owned);
+            std::swap(bsize, other.bsize);
+            std::swap(device, other.device);
+            std::swap(is_managed_memory, other.is_managed_memory);
+            return *this;
+        }
+        gpubuf_t(const gpubuf_t&) = delete;
+        gpubuf_t& operator=(const gpubuf_t&) = delete;
 
-    hipError_t alloc(const size_t size, bool make_it_shared = false)
-    {
-        free();
-        // remember the device that was current as of alloc, so we can
-        // free on the correct device
-        auto ret = hipGetDevice(&device);
-        if(ret != hipSuccess)
+        static gpubuf_t make_nonowned(T* p, size_t size_bytes = 0)
+        {
+            gpubuf_t ret;
+            ret.owned             = false;
+            ret.buf               = p;
+            ret.bsize             = size_bytes;
+            ret.is_managed_memory = false; // irrelevant if not owned
             return ret;
+        }
 
-        if(size > device_memory_accountant::singleton().get_usable_bytes(device))
+        ~gpubuf_t()
         {
-            std::stringstream msg;
-            msg << "Unauthorized device allocation (device ID: " << device << ").\n"
-                << "\tRequested size is " << byte_size_to_str(size) << "\n"
-                << device_memory_accountant::singleton().get_details(device);
-            throw DEVICEBUF_MEM_USAGE{msg.str()};
+            free();
         }
 
-        bsize             = size;
-        is_managed_memory = use_alloc_managed() || make_it_shared;
-        ret = is_managed_memory ? hipMallocManaged(&buf, bsize) : hipMalloc(&buf, bsize);
-        if(ret != hipSuccess)
+        static bool use_alloc_managed()
         {
-            buf   = nullptr;
-            bsize = 0;
+            return std::getenv("ROCFFT_MALLOC_MANAGED");
         }
 
-        device_memory_accountant::singleton().record_used_bytes(bsize, device);
+        hipError_t alloc(const size_t size, bool make_it_shared = false)
+        {
+            free();
+            // remember the device that was current as of alloc, so we can
+            // free on the correct device
+            auto ret = hipGetDevice(&device);
+            if(ret != hipSuccess)
+                return ret;
+
+#ifndef ROCFFT_BUILD_INTERNAL
+            if(size > device_memory_accountant::singleton().get_usable_bytes(device))
+            {
+                std::stringstream msg;
+                msg << "Unauthorized device allocation (device ID: " << device << ").\n"
+                    << "\tRequested size is " << byte_size_to_str(size) << "\n"
+                    << device_memory_accountant::singleton().get_details(device);
+                throw DEVICEBUF_MEM_USAGE{msg.str()};
+            }
+#endif
 
-        return ret;
-    }
+            bsize             = size;
+            is_managed_memory = use_alloc_managed() || make_it_shared;
+            ret = is_managed_memory ? hipMallocManaged(&buf, bsize) : hipMalloc(&buf, bsize);
+            if(ret != hipSuccess)
+            {
+                buf   = nullptr;
+                bsize = 0;
+            }
 
-    size_t size() const
-    {
-        return bsize;
-    }
+#ifndef ROCFFT_BUILD_INTERNAL
+            device_memory_accountant::singleton().record_used_bytes(bsize, device);
+#endif
 
-    void free()
-    {
-        if(buf != nullptr)
+            return ret;
+        }
+
+        size_t size() const
         {
-            if(owned)
+            return bsize;
+        }
+
+        void free()
+        {
+            if(buf != nullptr)
             {
-                // free on the device we allocated on
-                rocfft_scoped_device dev(device);
-                (void)hipFree(buf);
-                device_memory_accountant::singleton().release_used_bytes(bsize, device);
+                if(owned)
+                {
+                    // free on the device we allocated on
+                    rocfft_scoped_device dev(device);
+                    (void)hipFree(buf);
+#ifndef ROCFFT_BUILD_INTERNAL
+                    device_memory_accountant::singleton().release_used_bytes(bsize, device);
+#endif
+                }
+                buf   = nullptr;
+                bsize = 0;
             }
-            buf   = nullptr;
-            bsize = 0;
+            owned = true;
         }
-        owned = true;
-    }
 
-    // return a pointer to the allocated memory, offset by the
-    // specified number of bytes
-    T* data_offset(size_t offset_bytes = 0) const
-    {
-        void* ptr = static_cast<char*>(buf) + offset_bytes;
-        return static_cast<T*>(ptr);
-    }
+        // return a pointer to the allocated memory, offset by the
+        // specified number of bytes
+        T* data_offset(size_t offset_bytes = 0) const
+        {
+            void* ptr = static_cast<char*>(buf) + offset_bytes;
+            return static_cast<T*>(ptr);
+        }
 
-    T* data() const
-    {
-        return static_cast<T*>(buf);
-    }
+        T* data() const
+        {
+            return static_cast<T*>(buf);
+        }
 
-    // equality/bool tests
-    bool operator==(std::nullptr_t n) const
-    {
-        return buf == n;
-    }
-    bool operator!=(std::nullptr_t n) const
-    {
-        return buf != n;
-    }
-    operator bool() const
-    {
-        return buf;
-    }
+        // equality/bool tests
+        bool operator==(std::nullptr_t n) const
+        {
+            return buf == n;
+        }
+        bool operator!=(std::nullptr_t n) const
+        {
+            return buf != n;
+        }
+        operator bool() const
+        {
+            return buf;
+        }
 
-private:
-    // The GPU buffer
-    void* buf = nullptr;
-    // whether this object owns the 'buf' pointer (and hence needs to
-    // free it)
-    bool   owned             = true;
-    bool   is_managed_memory = false;
-    size_t bsize             = 0;
-    int    device            = 0;
-};
+    private:
+        // The GPU buffer
+        void* buf = nullptr;
+        // whether this object owns the 'buf' pointer (and hence needs to
+        // free it)
+        bool   owned             = true;
+        bool   is_managed_memory = false;
+        size_t bsize             = 0;
+        int    device            = 0;
+    };
+
+    // default gpubuf that gives out void* pointers
+    typedef gpubuf_t<> gpubuf;
+
+#ifdef ROCFFT_BUILD_INTERNAL
+}
+#endif
 
-// default gpubuf that gives out void* pointers
-typedef gpubuf_t<> gpubuf;
 #endif
--- a/shared/rocfft_params.h
+++ b/shared/rocfft_params.h
@@ -405,7 +405,9 @@ public:
                     std::ostringstream oss;
                     oss << "work buffer allocation failed ("
                         << byte_size_to_str(workbuffersizes[device]) << " requested)";
+#ifndef ROCFFT_BUILD_INTERNAL
                     oss << "\n" << device_memory_accountant::singleton().get_details(device);
+#endif
                     throw work_buffer_alloc_failure(oss.str(), workbuffersizes[device], hip_status);
                 }