4db4a6
commit 4831385c6706b377851284adc4c4545fff4c6564
4db4a6
Author: Nicholas Nethercote <nnethercote@apple.com>
4db4a6
Date:   Tue Nov 9 12:30:07 2021 +1100
4db4a6
4db4a6
    Fix Rust v0 demangling.
4db4a6
    
4db4a6
    It's currently broken due to a silly test that prevents the v0
4db4a6
    demangling code from even running.
4db4a6
    
4db4a6
    The commit also adds a test, to avoid such problems in the future.
4db4a6
4db4a6
diff --git a/coregrind/m_demangle/demangle.c b/coregrind/m_demangle/demangle.c
4db4a6
index 16161da2a..3fd7cb75f 100644
4db4a6
--- a/coregrind/m_demangle/demangle.c
4db4a6
+++ b/coregrind/m_demangle/demangle.c
4db4a6
@@ -118,8 +118,13 @@ void VG_(demangle) ( Bool do_cxx_demangling, Bool do_z_demangling,
4db4a6
    }
4db4a6
 
4db4a6
    /* Possibly undo (1) */
4db4a6
+   // - C++ mangled symbols start with "_Z" (possibly with exceptions?)
4db4a6
+   // - Rust "legacy" mangled symbols start with "_Z".
4db4a6
+   // - Rust "v0" mangled symbols start with "_R".
4db4a6
+   // XXX: the Java/Rust/Ada demangling here probably doesn't work. See
4db4a6
+   // https://bugs.kde.org/show_bug.cgi?id=445235 for details.
4db4a6
    if (do_cxx_demangling && VG_(clo_demangle)
4db4a6
-       && orig != NULL && orig[0] == '_' && orig[1] == 'Z') {
4db4a6
+       && orig != NULL && orig[0] == '_' && (orig[1] == 'Z' || orig[1] == 'R')) {
4db4a6
       /* !!! vvv STATIC vvv !!! */
4db4a6
       static HChar* demangled = NULL;
4db4a6
       /* !!! ^^^ STATIC ^^^ !!! */
4db4a6
diff --git a/memcheck/tests/demangle-rust.c b/memcheck/tests/demangle-rust.c
4db4a6
new file mode 100644
4db4a6
index 000000000..f2a458b2a
4db4a6
--- /dev/null
4db4a6
+++ b/memcheck/tests/demangle-rust.c
4db4a6
@@ -0,0 +1,31 @@
4db4a6
+// Valgrind supports demangling Rust symbols (both the "v0" and "legacy"
4db4a6
+// mangling schemes), but we don't want to add a dependency on the Rust
4db4a6
+// compiler for a single test. So this is a C program with function names that
4db4a6
+// are mangled Rust symbols. In the output, they become demangled Rust names.
4db4a6
+// It's a hack, but a useful one.
4db4a6
+
4db4a6
+#include <stdlib.h>
4db4a6
+
4db4a6
+// A v0 symbol that demangles to: <rustc_middle::ty::PredicateKind as rustc_middle::ty::fold::TypeFoldable>::fold_with::<rustc_infer::infer::resolve::OpportunisticVarResolver>
4db4a6
+int _RINvYNtNtCs4uGc65yWeeX_12rustc_middle2ty13PredicateKindNtNtB5_4fold12TypeFoldable9fold_withNtNtNtCsgI90OQiJWEs_11rustc_infer5infer7resolve24OpportunisticVarResolverECsdozMG8X9FIu_21rustc_trait_selection(int *p)
4db4a6
+{
4db4a6
+   return *p ? 1 : 2;
4db4a6
+}
4db4a6
+
4db4a6
+// A v0 symbol that demangles to: rustc_expand::mbe::macro_parser::parse_tt
4db4a6
+int _RNvNtNtCsaqSe1lZGvEL_12rustc_expand3mbe12macro_parser8parse_tt(int* p)
4db4a6
+{
4db4a6
+   return _RINvYNtNtCs4uGc65yWeeX_12rustc_middle2ty13PredicateKindNtNtB5_4fold12TypeFoldable9fold_withNtNtNtCsgI90OQiJWEs_11rustc_infer5infer7resolve24OpportunisticVarResolverECsdozMG8X9FIu_21rustc_trait_selection(p);
4db4a6
+}
4db4a6
+
4db4a6
+// A legacy symbol that demangles to: core::str::lossy::Utf8Lossy::from_bytes
4db4a6
+int _ZN4core3str5lossy9Utf8Lossy10from_bytes17heb1677c8cb728b0bE(int* p)
4db4a6
+{
4db4a6
+   return _RNvNtNtCsaqSe1lZGvEL_12rustc_expand3mbe12macro_parser8parse_tt(p);
4db4a6
+}
4db4a6
+
4db4a6
+int main(void)
4db4a6
+{
4db4a6
+   return _ZN4core3str5lossy9Utf8Lossy10from_bytes17heb1677c8cb728b0bE(malloc(sizeof(int)));
4db4a6
+}
4db4a6
+
4db4a6
diff --git a/memcheck/tests/demangle-rust.stderr.exp b/memcheck/tests/demangle-rust.stderr.exp
4db4a6
new file mode 100644
4db4a6
index 000000000..f04bb625b
4db4a6
--- /dev/null
4db4a6
+++ b/memcheck/tests/demangle-rust.stderr.exp
4db4a6
@@ -0,0 +1,6 @@
4db4a6
+Conditional jump or move depends on uninitialised value(s)
4db4a6
+   at 0x........: <rustc_middle::ty::PredicateKind as rustc_middle::ty::fold::TypeFoldable>::fold_with::<rustc_infer::infer::resolve::OpportunisticVarResolver> (demangle-rust.c:12)
4db4a6
+   by 0x........: rustc_expand::mbe::macro_parser::parse_tt (demangle-rust.c:18)
4db4a6
+   by 0x........: core::str::lossy::Utf8Lossy::from_bytes (demangle-rust.c:24)
4db4a6
+   by 0x........: main (demangle-rust.c:29)
4db4a6
+
4db4a6
diff --git a/memcheck/tests/demangle-rust.vgtest b/memcheck/tests/demangle-rust.vgtest
4db4a6
new file mode 100644
4db4a6
index 000000000..d726c6b2e
4db4a6
--- /dev/null
4db4a6
+++ b/memcheck/tests/demangle-rust.vgtest
4db4a6
@@ -0,0 +1,2 @@
4db4a6
+prog: demangle-rust
4db4a6
+vgopts: -q
4db4a6
4db4a6
commit c1bfa115f985633722f25922d2996c231e8c9d8d
4db4a6
Author: Mark Wielaard <mark@klomp.org>
4db4a6
Date:   Wed Nov 10 09:02:36 2021 +0100
4db4a6
4db4a6
    Add demangle-rust.vgtest demangle-rust.stderr.exp to EXTRA_DIST
4db4a6
4db4a6
diff --git a/memcheck/tests/Makefile.am b/memcheck/tests/Makefile.am
4db4a6
index 4d0476e2d..7837d87c7 100644
4db4a6
--- a/memcheck/tests/Makefile.am
4db4a6
+++ b/memcheck/tests/Makefile.am
4db4a6
@@ -281,6 +281,7 @@ EXTRA_DIST = \
4db4a6
 	realloc3.stderr.exp realloc3.vgtest \
4db4a6
 	recursive-merge.stderr.exp recursive-merge.vgtest \
4db4a6
 	resvn_stack.stderr.exp resvn_stack.vgtest \
4db4a6
+	demangle-rust.vgtest demangle-rust.stderr.exp \
4db4a6
 	sbfragment.stdout.exp sbfragment.stderr.exp sbfragment.vgtest \
4db4a6
 	sem.stderr.exp sem.vgtest \
4db4a6
 	sendmsg.stderr.exp sendmsg.stderr.exp-solaris sendmsg.vgtest \
4db4a6
4db4a6
commit d151907e5d8ff393f4fef126c8ae445ea8813661
4db4a6
Author: Mark Wielaard <mark@klomp.org>
4db4a6
Date:   Thu Nov 11 18:02:09 2021 +0100
4db4a6
4db4a6
    Add demangle-rust to check_PROGRAMS
4db4a6
    
4db4a6
    The demangle-rust.vgtest would fail because the demangle-rust binary
4db4a6
    wasn't build by default. Add it to check_PROGRAMS and define
4db4a6
    demangle_rust_SOURCES to make sure it is always build.
4db4a6
4db4a6
diff --git a/memcheck/tests/Makefile.am b/memcheck/tests/Makefile.am
4db4a6
index 7837d87c7..449710020 100644
4db4a6
--- a/memcheck/tests/Makefile.am
4db4a6
+++ b/memcheck/tests/Makefile.am
4db4a6
@@ -392,6 +392,7 @@ check_PROGRAMS = \
4db4a6
 	custom_alloc \
4db4a6
 	custom-overlap \
4db4a6
 	demangle \
4db4a6
+	demangle-rust \
4db4a6
 	big_debuginfo_symbol \
4db4a6
 	deep-backtrace \
4db4a6
 	describe-block \
4db4a6
@@ -505,6 +506,7 @@ endif
4db4a6
 leak_cpp_interior_SOURCES	= leak_cpp_interior.cpp
4db4a6
 
4db4a6
 demangle_SOURCES = demangle.cpp
4db4a6
+demangle_rust_SOURCES = demangle-rust.c
4db4a6
 
4db4a6
 # Suppress various gcc warnings which are correct, but for things
4db4a6
 # we are actually testing for at runtime.