[libc] Fix printing on the GPU when given a `cpp::string_ref`
authorJoseph Huber <jhuber6@vols.utk.edu>
Fri, 28 Apr 2023 22:44:17 +0000 (17:44 -0500)
committerJoseph Huber <jhuber6@vols.utk.edu>
Sat, 29 Apr 2023 02:32:01 +0000 (21:32 -0500)
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

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

index 13ab96dacc284c3ac31d6b28e0d59849fe6148a7..9f597574d01c8eacbc16fac4077d58b912bee901 100644 (file)
@@ -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();
 }
 
index af432fcfe1fb732f7ed3923419d56f2de216ec4a..cd043359b1eaef78109a3d4bdd282b720f7d4cc5 100644 (file)
@@ -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: {