[libc-commits] [libc] 91528d2 - [libc] Fix printing on the GPU when given a `cpp::string_ref`

Joseph Huber via libc-commits libc-commits at lists.llvm.org
Fri Apr 28 19:32:09 PDT 2023


Author: Joseph Huber
Date: 2023-04-28T21:32:01-05:00
New Revision: 91528d20584066e393a926a3430e9380f394dabc

URL: https://github.com/llvm/llvm-project/commit/91528d20584066e393a926a3430e9380f394dabc
DIFF: https://github.com/llvm/llvm-project/commit/91528d20584066e393a926a3430e9380f394dabc.diff

LOG: [libc] Fix printing on the GPU when given a `cpp::string_ref`

The implementation of the test printing currently expects a null
terminated C-string. However, the `write_to_stderr` interface uses a
string view, which doesn't need to be null terminated. This patch
changes the printing interface to directly use `fwrite` instead rather
than relying on a null terminator.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D149493

Added: 
    

Modified: 
    libc/src/__support/OSUtil/gpu/io.cpp
    libc/utils/gpu/loader/Server.h

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/OSUtil/gpu/io.cpp b/libc/src/__support/OSUtil/gpu/io.cpp
index 13ab96dacc284..9f597574d01c8 100644
--- a/libc/src/__support/OSUtil/gpu/io.cpp
+++ b/libc/src/__support/OSUtil/gpu/io.cpp
@@ -16,7 +16,7 @@ namespace __llvm_libc {
 
 void write_to_stderr(cpp::string_view msg) {
   rpc::Port port = rpc::client.open(rpc::PRINT_TO_STDERR);
-  port.send_n(msg.data(), msg.size() + 1);
+  port.send_n(msg.data(), msg.size());
   port.close();
 }
 

diff  --git a/libc/utils/gpu/loader/Server.h b/libc/utils/gpu/loader/Server.h
index af432fcfe1fb7..cd043359b1eae 100644
--- a/libc/utils/gpu/loader/Server.h
+++ b/libc/utils/gpu/loader/Server.h
@@ -30,10 +30,15 @@ void handle_server() {
 
   switch (port->get_opcode()) {
   case __llvm_libc::rpc::Opcode::PRINT_TO_STDERR: {
-    void *str = nullptr;
-    port->recv_n([&](uint64_t size) { return str = malloc(size); });
-    fputs(reinterpret_cast<char *>(str), stderr);
-    free(str);
+    uint64_t str_size;
+    char *str = nullptr;
+    port->recv_n([&](uint64_t size) {
+      str_size = size;
+      str = new char[size];
+      return str;
+    });
+    fwrite(str, str_size, 1, stderr);
+    delete[] str;
     break;
   }
   case __llvm_libc::rpc::Opcode::EXIT: {


        


More information about the libc-commits mailing list