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