Bug 470713

Summary: Failure on the Yosys project: valgrind: m_libcfile.c:1802 (Bool vgPlain_realpath(const HChar *, HChar *)): Assertion 'resolved' failed.
Product: [Developer tools] valgrind Reporter: Yuri <yuri>
Component: generalAssignee: Paul Floyd <pjfloyd>
Status: RESOLVED FIXED    
Severity: normal CC: pjfloyd
Priority: NOR    
Version First Reported In: 3.21.0   
Target Milestone: ---   
Platform: Other   
OS: FreeBSD   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:

Description Yuri 2023-06-06 18:43:27 UTC
SUMMARY

This command line fails on the Yosys project https://github.com/YosysHQ/yosys:
$ valgrind -v --tool=memcheck --show-mismatched-frees=no /usr/local/bin/yosys

Failure log:
--18248-- REDIR: 0x4da9fd0 (libc.so.7:free) redirected to 0x484e780 (free)
--18248-- REDIR: 0x4df93a0 (libc.so.7:strcpy) redirected to 0x4851f40 (strcpy)

valgrind: m_libcfile.c:1802 (Bool vgPlain_realpath(const HChar *, HChar *)): Assertion 'resolved' failed.

host stacktrace:
==18248==    at 0x3810EDA1: ??? (in /usr/local/libexec/valgrind/memcheck-amd64-freebsd)
==18248==    by 0x10035AAFDF: ???
==18248==    by 0x38107366: ??? (in /usr/local/libexec/valgrind/memcheck-amd64-freebsd)
==18248==    by 0x3810EDA0: ??? (in /usr/local/libexec/valgrind/memcheck-amd64-freebsd)
==18248==    by 0x10035AA7BF: ???
==18248==    by 0x10020082AF: ???

sched status:
  running_tid=1

Thread 1: status = VgTs_Runnable syscall 202 (lwpid 108800)
==18248==    at 0x4D6772A: __sysctl (in /lib/libc.so.7)
==18248==    by 0x4CE52C1: sysctl (in /lib/libc.so.7)
==18248==    by 0x63CB22: Yosys::proc_self_dirname() (in /usr/local/bin/yosys)
==18248==    by 0x63B0E9: Yosys::init_share_dirname() (in /usr/local/bin/yosys)
==18248==    by 0x639CB1: Yosys::yosys_setup() (in /usr/local/bin/yosys)
==18248==    by 0x5A0AB7: main (in /usr/local/bin/yosys)
client stack range: [0x1FFBFFD000 0x1FFC000FFF] client SP: 0x1FFC000158
valgrind stack range: [0x10034AB000 0x10035AAFFF] top usage: 6416 of 1048576




STEPS TO REPRODUCE
1. 
2. 
3. 

OBSERVED RESULT
see above

EXPECTED RESULT
success

SOFTWARE/OS VERSIONS
FreeBSD 13.2


ADDITIONAL INFORMATION
Comment 1 Paul Floyd 2023-06-07 08:15:55 UTC
I'm not handling a NULL for returned_path

https://man.freebsd.org/cgi/man.cgi?query=realpath&sektion=3&format=html
Comment 2 Paul Floyd 2023-06-07 08:27:04 UTC
Though I thought libc was supost to allocate memory if it's NULL.
Comment 3 Paul Floyd 2023-06-07 09:27:42 UTC
Ah but it's not using realpath.

https://github.com/YosysHQ/yosys/blob/5813809ad9afbe1c38f65c6aae7c3441d7614d0b/kernel/yosys.cc#L890

#elif defined(__FreeBSD__)
std::string proc_self_dirname()
{
	int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
	size_t buflen;
	char *buffer;
	std::string path;
	if (sysctl(mib, 4, NULL, &buflen, NULL, 0) != 0)
		log_error("sysctl failed: %s\n", strerror(errno));
	buffer = (char*)malloc(buflen);
	if (buffer == NULL)
		log_error("malloc failed: %s\n", strerror(errno));
	if (sysctl(mib, 4, buffer, &buflen, NULL, 0) != 0)
		log_error("sysctl failed: %s\n", strerror(errno));
	while (buflen > 0 && buffer[buflen-1] != '/')
		buflen--;
	path.assign(buffer, buflen);
	free(buffer);
	return path;
}

There are two calls to sysctl kern.proc.pathname, one to get the buffer size and one to get the pathname.
Comment 4 Paul Floyd 2023-06-07 20:47:04 UTC
commit 840ccb9915c675fd7db527107e6b38343fafdf86 (HEAD -> master, origin/master, origin/HEAD)
Author: Paul Floyd <pjfloyd@wanadoo.fr>
Date:   Wed Jun 7 22:27:08 2023 +0200

    Bug 470713 -  Failure on the Yosys project: valgrind: m_libcfile.c:1802 (Bool vgPlain_realpath(const HChar *, HChar *)): Assertion 'resolved' failed
    
        When using sysctl kern proc pathname with the pid of the guest or -1
        we need to intercept the call otherwise the syscall will return the path
        of the memcheck tool and not the path of the guest.
    
        This uses VG_(realpath), which asserts if it doesn't get valid
        input pointers.
    
        sysctl kern proc pathname can use a NULL pointer in order to
        determine the length of the path (so users can allocate the minumum
        necessary). The NULL pointer was being passed on to VG_(realpath)
        without being checked, resulting in an assert.
Comment 5 Paul Floyd 2023-06-07 20:48:00 UTC
Should be fixed with the above change. Can you build Valgrind from source and test it?

Otherwise I'll do a test with Yosys.
Comment 6 Paul Floyd 2023-06-07 20:56:38 UTC
Rather try this as I made one mistake

commit 8bc3a55d50570ef3e3077a7bdfb6354895a56878 (HEAD -> master, origin/master, origin/HEAD)
Author: Paul Floyd <pjfloyd@wanadoo.fr>
Date:   Wed Jun 7 22:54:22 2023 +0200

    Merge error, missing continuation in Makefile.am
Comment 7 Paul Floyd 2023-06-08 06:38:24 UTC
I tried with the yosys port, and it now gets to the command prompt.