[libc-commits] [libc] [libc] Move the pointer to pin off the stack to the heap (PR #74118)

Joseph Huber via libc-commits libc-commits at lists.llvm.org
Fri Dec 1 10:04:06 PST 2023


https://github.com/jhuber6 updated https://github.com/llvm/llvm-project/pull/74118

>From 2ff9ccecca3b05f81ee23bf14aa35c8c1a0acd51 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Fri, 1 Dec 2023 11:46:24 -0600
Subject: [PATCH 1/3] [libc] Move the pointer to pin off the stack to the heap

Summary:
This may be problematic to pin a stack pointer. Allocate it via the OS
allocator instead as the documentation suggests.

For some reason, if you attempt to free this pointer after the memory
region has been unlocked, it will return an invalid pointer.
---
 libc/utils/gpu/loader/amdgpu/Loader.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/utils/gpu/loader/amdgpu/Loader.cpp b/libc/utils/gpu/loader/amdgpu/Loader.cpp
index a9a687656efcb39..d3d2c2b42080837 100644
--- a/libc/utils/gpu/loader/amdgpu/Loader.cpp
+++ b/libc/utils/gpu/loader/amdgpu/Loader.cpp
@@ -471,7 +471,7 @@ int load(int argc, char **argv, char **envp, void *image, size_t size,
     handle_error(err);
 
   // Pin some memory we can use to obtain the address of the rpc client.
-  void *rpc_client_storage = nullptr;
+  void **rpc_client_storage = new void *;
   void *rpc_client_host = nullptr;
   if (hsa_status_t err =
           hsa_amd_memory_lock(&rpc_client_storage, sizeof(void *),

>From 8aa7cc756162c7cb4db4d6e67f5a71f45c73429c Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Fri, 1 Dec 2023 11:52:31 -0600
Subject: [PATCH 2/3] Change to malloc and free right after.

---
 libc/utils/gpu/loader/amdgpu/Loader.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libc/utils/gpu/loader/amdgpu/Loader.cpp b/libc/utils/gpu/loader/amdgpu/Loader.cpp
index d3d2c2b42080837..941caa520575ca6 100644
--- a/libc/utils/gpu/loader/amdgpu/Loader.cpp
+++ b/libc/utils/gpu/loader/amdgpu/Loader.cpp
@@ -471,12 +471,13 @@ int load(int argc, char **argv, char **envp, void *image, size_t size,
     handle_error(err);
 
   // Pin some memory we can use to obtain the address of the rpc client.
-  void **rpc_client_storage = new void *;
+  void *rpc_client_storage = malloc(sizeof(void *));
   void *rpc_client_host = nullptr;
   if (hsa_status_t err =
           hsa_amd_memory_lock(&rpc_client_storage, sizeof(void *),
                               /*agents=*/nullptr, 0, &rpc_client_host))
     handle_error(err);
+  free(rpc_client_storage);
 
   // Copy the address of the client buffer from the device to the host.
   if (hsa_status_t err = hsa_memcpy(rpc_client_host, host_agent, rpc_client_dev,

>From 33ccd33ece2b7779f360f0b4c95fa50a078fd774 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Fri, 1 Dec 2023 12:03:46 -0600
Subject: [PATCH 3/3] update update

---
 libc/utils/gpu/loader/amdgpu/Loader.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/libc/utils/gpu/loader/amdgpu/Loader.cpp b/libc/utils/gpu/loader/amdgpu/Loader.cpp
index 941caa520575ca6..d344f9342321399 100644
--- a/libc/utils/gpu/loader/amdgpu/Loader.cpp
+++ b/libc/utils/gpu/loader/amdgpu/Loader.cpp
@@ -471,13 +471,12 @@ int load(int argc, char **argv, char **envp, void *image, size_t size,
     handle_error(err);
 
   // Pin some memory we can use to obtain the address of the rpc client.
-  void *rpc_client_storage = malloc(sizeof(void *));
+  void *rpc_client_storage = /* Intentionally leaks */ malloc(sizeof(void *));
   void *rpc_client_host = nullptr;
   if (hsa_status_t err =
           hsa_amd_memory_lock(&rpc_client_storage, sizeof(void *),
                               /*agents=*/nullptr, 0, &rpc_client_host))
     handle_error(err);
-  free(rpc_client_storage);
 
   // Copy the address of the client buffer from the device to the host.
   if (hsa_status_t err = hsa_memcpy(rpc_client_host, host_agent, rpc_client_dev,



More information about the libc-commits mailing list