[llvm] 201c4b9 - [demangler] Rust demangler buffer return

Nathan Sidwell via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 13 08:50:50 PDT 2022


Author: Nathan Sidwell
Date: 2022-04-13T08:50:04-07:00
New Revision: 201c4b9cc4a649e4a54b418749ab5b8bd227e4f0

URL: https://github.com/llvm/llvm-project/commit/201c4b9cc4a649e4a54b418749ab5b8bd227e4f0
DIFF: https://github.com/llvm/llvm-project/commit/201c4b9cc4a649e4a54b418749ab5b8bd227e4f0.diff

LOG: [demangler] Rust demangler buffer return

The rust demangler has some odd buffer handling code, which will copy
the demangled string into the provided buffer, if it will fit.
Otherwise it uses the allocated buffer it made.  But the length of the
incoming buffer will have come from a previous call, which was the
length of the demangled string -- not the buffer size.  And of course,
we're unconditionally allocating a temporary buffer in the first
place.  So we don't actually get buffer reuse, and we get a memcpy in
somecases.

However, nothing in LLVM ever passes in a non-null pointer.  Neither
does anything pass in a status pointer that is then made use of.  The
only exercise these have is in the test suite.

So let's just make the rust demangler have the same API as the dlang
demangler.

Reviewed By: tmiasko

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

Added: 
    

Modified: 
    lldb/source/Core/Mangled.cpp
    llvm/include/llvm/Demangle/Demangle.h
    llvm/lib/Demangle/Demangle.cpp
    llvm/lib/Demangle/RustDemangle.cpp
    llvm/tools/llvm-rust-demangle-fuzzer/llvm-rust-demangle-fuzzer.cpp
    llvm/unittests/Demangle/RustDemangleTest.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/source/Core/Mangled.cpp b/lldb/source/Core/Mangled.cpp
index 31e1d68c29f31..0c4d9f78c4402 100644
--- a/lldb/source/Core/Mangled.cpp
+++ b/lldb/source/Core/Mangled.cpp
@@ -168,7 +168,7 @@ static char *GetItaniumDemangledStr(const char *M) {
 }
 
 static char *GetRustV0DemangledStr(const char *M) {
-  char *demangled_cstr = llvm::rustDemangle(M, nullptr, nullptr, nullptr);
+  char *demangled_cstr = llvm::rustDemangle(M);
 
   if (Log *log = GetLog(LLDBLog::Demangle)) {
     if (demangled_cstr && demangled_cstr[0])

diff  --git a/llvm/include/llvm/Demangle/Demangle.h b/llvm/include/llvm/Demangle/Demangle.h
index 3150e049320bf..6133d0b95bbfc 100644
--- a/llvm/include/llvm/Demangle/Demangle.h
+++ b/llvm/include/llvm/Demangle/Demangle.h
@@ -57,8 +57,8 @@ char *microsoftDemangle(const char *mangled_name, size_t *n_read, char *buf,
                         size_t *n_buf, int *status,
                         MSDemangleFlags Flags = MSDF_None);
 
-// Demangles a Rust v0 mangled symbol. The API follows that of __cxa_demangle.
-char *rustDemangle(const char *MangledName, char *Buf, size_t *N, int *Status);
+// Demangles a Rust v0 mangled symbol.
+char *rustDemangle(const char *MangledName);
 
 // Demangles a D mangled symbol.
 char *dlangDemangle(const char *MangledName);

diff  --git a/llvm/lib/Demangle/Demangle.cpp b/llvm/lib/Demangle/Demangle.cpp
index 13aa2864c183d..9d128424cabf4 100644
--- a/llvm/lib/Demangle/Demangle.cpp
+++ b/llvm/lib/Demangle/Demangle.cpp
@@ -51,7 +51,7 @@ bool llvm::nonMicrosoftDemangle(const char *MangledName, std::string &Result) {
   if (isItaniumEncoding(MangledName))
     Demangled = itaniumDemangle(MangledName, nullptr, nullptr, nullptr);
   else if (isRustEncoding(MangledName))
-    Demangled = rustDemangle(MangledName, nullptr, nullptr, nullptr);
+    Demangled = rustDemangle(MangledName);
   else if (isDLangEncoding(MangledName))
     Demangled = dlangDemangle(MangledName);
 

diff  --git a/llvm/lib/Demangle/RustDemangle.cpp b/llvm/lib/Demangle/RustDemangle.cpp
index d62891d69dc3f..32b10db2a968d 100644
--- a/llvm/lib/Demangle/RustDemangle.cpp
+++ b/llvm/lib/Demangle/RustDemangle.cpp
@@ -147,57 +147,27 @@ class Demangler {
 
 } // namespace
 
-char *llvm::rustDemangle(const char *MangledName, char *Buf, size_t *N,
-                         int *Status) {
-  if (MangledName == nullptr || (Buf != nullptr && N == nullptr)) {
-    if (Status != nullptr)
-      *Status = demangle_invalid_args;
+char *llvm::rustDemangle(const char *MangledName) {
+  if (MangledName == nullptr)
     return nullptr;
-  }
 
   // Return early if mangled name doesn't look like a Rust symbol.
   StringView Mangled(MangledName);
-  if (!Mangled.startsWith("_R")) {
-    if (Status != nullptr)
-      *Status = demangle_invalid_mangled_name;
+  if (!Mangled.startsWith("_R"))
     return nullptr;
-  }
 
   Demangler D;
-  if (!initializeOutputBuffer(nullptr, nullptr, D.Output, 1024)) {
-    if (Status != nullptr)
-      *Status = demangle_memory_alloc_failure;
+  if (!initializeOutputBuffer(nullptr, nullptr, D.Output, 1024))
     return nullptr;
-  }
 
   if (!D.demangle(Mangled)) {
-    if (Status != nullptr)
-      *Status = demangle_invalid_mangled_name;
     std::free(D.Output.getBuffer());
     return nullptr;
   }
 
   D.Output += '\0';
-  char *Demangled = D.Output.getBuffer();
-  size_t DemangledLen = D.Output.getCurrentPosition();
-
-  if (Buf != nullptr) {
-    if (DemangledLen <= *N) {
-      std::memcpy(Buf, Demangled, DemangledLen);
-      std::free(Demangled);
-      Demangled = Buf;
-    } else {
-      std::free(Buf);
-    }
-  }
-
-  if (N != nullptr)
-    *N = DemangledLen;
-
-  if (Status != nullptr)
-    *Status = demangle_success;
 
-  return Demangled;
+  return D.Output.getBuffer();
 }
 
 Demangler::Demangler(size_t MaxRecursionLevel)

diff  --git a/llvm/tools/llvm-rust-demangle-fuzzer/llvm-rust-demangle-fuzzer.cpp b/llvm/tools/llvm-rust-demangle-fuzzer/llvm-rust-demangle-fuzzer.cpp
index c2891e532e28b..3a4b7ca7af8ff 100644
--- a/llvm/tools/llvm-rust-demangle-fuzzer/llvm-rust-demangle-fuzzer.cpp
+++ b/llvm/tools/llvm-rust-demangle-fuzzer/llvm-rust-demangle-fuzzer.cpp
@@ -13,9 +13,7 @@
 
 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
   std::string NullTerminatedString((const char *)Data, Size);
-  int Status = 0;
-  char *Demangled = llvm::rustDemangle(NullTerminatedString.c_str(), nullptr,
-                                       nullptr, &Status);
+  char *Demangled = llvm::rustDemangle(NullTerminatedString.c_str());
   std::free(Demangled);
   return 0;
 }

diff  --git a/llvm/unittests/Demangle/RustDemangleTest.cpp b/llvm/unittests/Demangle/RustDemangleTest.cpp
index 670e48ed216fa..e48ddb7ee6f01 100644
--- a/llvm/unittests/Demangle/RustDemangleTest.cpp
+++ b/llvm/unittests/Demangle/RustDemangleTest.cpp
@@ -13,78 +13,19 @@
 #include <cstdlib>
 
 TEST(RustDemangle, Success) {
-  char *Demangled =
-      llvm::rustDemangle("_RNvC1a4main", nullptr, nullptr, nullptr);
-  EXPECT_STREQ(Demangled, "a::main");
-  std::free(Demangled);
-
-  // With status.
-  int Status = 0;
-  Demangled = llvm::rustDemangle("_RNvC1a4main", nullptr, nullptr, &Status);
-  EXPECT_EQ(Status, llvm::demangle_success);
-  EXPECT_STREQ(Demangled, "a::main");
-  std::free(Demangled);
-
-  // With status and length.
-  size_t N = 0;
-  Demangled = llvm::rustDemangle("_RNvC1a4main", nullptr, &N, &Status);
-  EXPECT_EQ(Status, llvm::demangle_success);
-  EXPECT_EQ(N, 8u);
+  char *Demangled = llvm::rustDemangle("_RNvC1a4main");
   EXPECT_STREQ(Demangled, "a::main");
   std::free(Demangled);
 }
 
 TEST(RustDemangle, Invalid) {
-  int Status = 0;
   char *Demangled = nullptr;
 
   // Invalid prefix.
-  Demangled = llvm::rustDemangle("_ABCDEF", nullptr, nullptr, &Status);
-  EXPECT_EQ(Status, llvm::demangle_invalid_mangled_name);
+  Demangled = llvm::rustDemangle("_ABCDEF");
   EXPECT_EQ(Demangled, nullptr);
 
   // Correct prefix but still invalid.
-  Demangled = llvm::rustDemangle("_RRR", nullptr, nullptr, &Status);
-  EXPECT_EQ(Status, llvm::demangle_invalid_mangled_name);
+  Demangled = llvm::rustDemangle("_RRR");
   EXPECT_EQ(Demangled, nullptr);
 }
-
-TEST(RustDemangle, OutputBufferWithoutLength) {
-  char *Buffer = static_cast<char *>(std::malloc(1024));
-  ASSERT_NE(Buffer, nullptr);
-
-  int Status = 0;
-  char *Demangled =
-      llvm::rustDemangle("_RNvC1a4main", Buffer, nullptr, &Status);
-
-  EXPECT_EQ(Status, llvm::demangle_invalid_args);
-  EXPECT_EQ(Demangled, nullptr);
-  std::free(Buffer);
-}
-
-TEST(RustDemangle, OutputBuffer) {
-  size_t N = 1024;
-  char *Buffer = static_cast<char *>(std::malloc(N));
-  ASSERT_NE(Buffer, nullptr);
-
-  int Status = 0;
-  char *Demangled = llvm::rustDemangle("_RNvC1a4main", Buffer, &N, &Status);
-
-  EXPECT_EQ(Status, llvm::demangle_success);
-  EXPECT_EQ(Demangled, Buffer);
-  EXPECT_STREQ(Demangled, "a::main");
-  std::free(Demangled);
-}
-
-TEST(RustDemangle, SmallOutputBuffer) {
-  size_t N = 1;
-  char *Buffer = static_cast<char *>(std::malloc(N));
-  ASSERT_NE(Buffer, nullptr);
-
-  int Status = 0;
-  char *Demangled = llvm::rustDemangle("_RNvC1a4main", Buffer, &N, &Status);
-
-  EXPECT_EQ(Status, llvm::demangle_success);
-  EXPECT_STREQ(Demangled, "a::main");
-  std::free(Demangled);
-}


        


More information about the llvm-commits mailing list