valgrind error with __builtin_ctz - c++

I'm trying to profile my code but run into problems.
If I run the following code:
#include <iostream>
int main() {
size_t val = 8;
std::cout << sizeof(val) << std::endl;
std::cout << __builtin_ctz(val) << std::endl;
}
It returns as expected
8
3
If I run valgrind on it it returns:
==28602== Memcheck, a memory error detector
==28602== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==28602== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==28602== Command: ./test
==28602==
8
vex amd64->IR: unhandled instruction bytes: 0xF3 0xF 0xBC 0xC0 0x89 0xC6 0xBF 0x60
==28602== valgrind: Unrecognised instruction at address 0x400890.
==28602== at 0x400890: main (in /home/magu_/sod/test/test)
==28602== Your program just tried to execute an instruction that Valgrind
==28602== did not recognise. There are two possible reasons for this.
==28602== 1. Your program has a bug and erroneously jumped to a non-code
==28602== location. If you are running Memcheck and you just saw a
==28602== warning about a bad jump, it's probably your program's fault.
==28602== 2. The instruction is legitimate but Valgrind doesn't handle it,
==28602== i.e. it's Valgrind's fault. If you think this is the case or
==28602== you are not sure, please let us know and we'll try to fix it.
==28602== Either way, Valgrind will now raise a SIGILL signal which will
==28602== probably kill your program.
==28602==
==28602== Process terminating with default action of signal 4 (SIGILL)
==28602== Illegal opcode at address 0x400890
==28602== at 0x400890: main (in /home/magu_/sod/test/test)
==28602==
==28602== HEAP SUMMARY:
==28602== in use at exit: 0 bytes in 0 blocks
==28602== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==28602==
==28602== All heap blocks were freed -- no leaks are possible
==28602==
==28602== For counts of detected and suppressed errors, rerun with: -v
==28602== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
Illegal instruction (core dumped)
Is this an bug of valgrind or should I not use __builtin_ctz with my computer? __builtin_popcount does not raise any errors.
My system:
g++ (Ubuntu 4.8.1-2ubuntu1~12.04) 4.8.1
CPU : Intel Core Duo T7500

You need to upgrade valgrind to at least 4.8.1 or use an gcc older than v4.8.
The opcode you ran into -- F3 0F BC -- is the TZCNT opcode, introduced in BMI1, which your CPU doesn't implement. However, it is also REP;BSF (F3 is REP) and older CPUs, including yours, ignore the REP for this opcode, and the similar LZCNT == REP;BSR pair. There is very little difference between TZCNT and BSF (they differ in how they handle 0).
Older gcc versions used BSF for older CPUs and TZCNT for newer ones, but since the opcode is relatively rare, in newer gcc versions the logic was simplified and TZCNT is always used, since both older and newer CPUs understand it.
Unfortunately, valgrind did not correctly fallback from TZCNT to BSF until v4.8.1. See bug 295808.

On Debian/Sid/x86-64 (Intel i4750HQ processor) with gcc version 4.9.1 (Debian 4.9.1-4) and valgrind-3.9.0 your test is working ok (and valgrind runs successfully without reporting any errors).
So I suggest you to upgrade your GCC compiler and most importantly valgrind. Start first by compiling valgrind from its valgrind-3.9.0 source code tarball (and use aptitude build-dep valgrind before).
BTW, your distribution version is quite old. Did you consider upgrading to Ubuntu 14.0 LTS?
If you don't have root access, consider passing some explicit --prefix (e.g. $HOME/pub/ ) to valgrind-3.9.0/configure

Related

c++ - Valgrind on codeblocks (linux)

I already use the Valgrind in small programs to check memorys leaks and its work good.
Now i have a big program with many class and .cpp and .h files and i'm trying to use Valgrind to check the memory leak because i use a lot of pointers, memory, etc.
I'm using linux and codeblocks 16.01 with gcc and i trying to run the Valgrind directly in codeblocks but i'm getting the follow error:
--------------- Application output --------------
valgrind: /myPathToTheProject/ValgrindOut.xml: No such file or directory
If i test with a small project with only a .cpp file and main it works good and the Valgrind generate the ValgrindOut.xml. In this big project i always getting this error. Someone have some idea what is wrong? or other way or tool to test memory leak?
EDIT - LEAK SUMMARY after running Valgrind
Leak summary:
definitely lost: 673 bytes in 6 blocks.
indirectly lost: 89,128 bytes in 68 blocks.
possibly lost: 232 bytes in 2 blocks.
still reachable: 80,944 bytes in 6 blocks.
suppressed: 0 bytes in 0 blocks.
I am not sure how to run valgrind directly from codeblocks. I suggest you build your project using codeblocks. While executing, use valgrind as per below command.
Command
valgrind --tool=memcheck --leak-check=full --show-leak-kinds=all --log-file=leak.txt ./myexecutable <my command line arguments>
Example
valgrind --tool=memcheck --leak-check=full --show-leak-kinds=all --log-file=leak.txt ./myexecutable -i 192.168.1.10 -p 5000
This way you can generate valgrind output file, that is leak.txt that contains memory leaks etc.

Valgrind Unrecognised instruction

I have the following code:
#include <iostream>
#include <random>
int main() {
std::mt19937_64 rng(std::random_device{}());
std::cout << std::uniform_int_distribution<>(0, 100)(rng) << '\n';
}
I try to profile it using valgrind, but it says:
vex amd64->IR: unhandled instruction bytes: 0xF 0xC7 0xF0 0x89 0x6 0xF 0x42 0xC1
vex amd64->IR: REX=0 REX.W=0 REX.R=0 REX.X=0 REX.B=0
vex amd64->IR: VEX=0 VEX.L=0 VEX.nVVVV=0x0 ESC=0F
vex amd64->IR: PFX.66=0 PFX.F2=0 PFX.F3=0
==2092== valgrind: Unrecognised instruction at address 0x4cdc1b5.
==2092== at 0x4CDC1B5:std::(anonymous namespace)::__x86_rdrand() (random.cc:69)
==2092== by 0x4CDC321: std::random_device::_M_getval() (random.cc:130)
==2092== by 0x4009D4: main (random.h:1619)
Preceded by multiple instances of:
--2092-- WARNING: Serious error when reading debug info
--2092-- When reading debug info from /lib/x86_64-linux-gnu/ld-2.22.so:
--2092-- Ignoring non-Dwarf2/3/4 block in .debug_info
I am on Debian using standard packages on an x86-64 platform compiling with gcc 5.3.1 using valgrind-3.11.0. The illegal instruction seems to be inside libstdc++6.
How do I get valgrind to profile my code?
In fact, Valgrind emulate your program with an intermediate language (VEX) to know if it discovers memory violation.
This VEX language capture all the instructions of several assemblers such as i386, amd64, arm, ... But, from time to time, it miss a few instructions (especially specialized ones like rdrand which is linked to the AES specific instructions set).
Well, this is exactly what happened with your program. Valgrind did probably stumbled on an unknown instruction and could not translate it into the VEX intermediate language.
But, you are not the only one to be in line waiting for a fix:
Same issue on Launchpad.
Same issue on KDE bugtracker.
... and so on ...
Here is a patch that has been applied to Valgrind and that may solve the problem for you (depending on your CPU).
But, the only thing you can do is to install a newer version of Valgrind and hope that the instruction is supported in the newest version.

Valgrind report write error? why?

When running Valgrind's memcheck, occasionally valgrind report error like this:
==2745== Memcheck, a memory error detector
==2745== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==2745== Using Valgrind-3.6.0 and LibVEX; rerun with -h for copyright info
==2745== Command: ./HSFramework
==2745==
==2745== Invalid write of size 8
==2745== at 0x3B81C097C0: do_lookup_x (in /lib64/ld-2.12.so)
==2745== by 0x1C31032D: ???
==2745== by 0x3B81C09E19: _dl_lookup_symbol_x (in /lib64/ld-2.12.so)
==2745== Address 0x7feffee78 is on thread 1's stack
==2745==
platform: Linux 2.6.32-220.el6.x86_64 x86_64 x86_64 x86_64 GNU/Linux
There is not clue about my code from this error report.
I had no idea about this error report.
What reasons will lead to this error?
This error means you are getting a buffer overrun in do_lookup_x, if you got its source look at that or share with us.
http://valgrind.org/docs/manual/quick-start.html
This means that the do_lookup_x function has performed an invalid write access. That function is part of the runtime library (and not likely the origin of the issue). I would contact the author of HSFramework to see if they can fix this issue by running valgrind as you did

Is valgrind catching Qt 4.8 on Debian Wheezy leaking memory in minimalist app?

I've read several questions here where people run minimal Qt programs through valgrind, and post the results. The general verdict from looking over the output is "well, there are no actual leaks, it's just how Qt uses memory".
However, what I'm getting with a basically empty application looks...worse. I'm getting "definitely lost" leaks, for instance:
https://gist.github.com/3204769
==32147== LEAK SUMMARY:
==32147== definitely lost: 848 bytes in 11 blocks
==32147== indirectly lost: 1,756 bytes in 53 blocks
==32147== possibly lost: 1,720 bytes in 9 blocks
==32147== still reachable: 121,019 bytes in 2,257 blocks
==32147== suppressed: 0 bytes in 0 blocks
Running with:
valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes ./testing 2> valgrind.log
I'm a little bit bleeding edge with this setup, to try and get a relatively-recent C++11-compiling gcc:
Debian Wheezy 3.2.0-2-686-pae
gcc (Debian 4.7.1-2) 4.7.1
If I do sudo kwrite --version I get:
Qt: 4.8.1
KDE Development Platform: 4.8.4 (4.8.4)
KWrite: 4.8.3 (4.8.3)
Anyone in a similar situation, or know what is going on here? :-/
most of that stuff seems to be internal 'global' state of the libraries you are using. One can argue if it is good style to cleanup global resources by 'program termination', but it is probably ok if done right. I personally don't like it, as it makes detection of real leaks harder...

Is it possible that valgrind reports fatal errors of my program which runs correctly?

When I run my program or when I run it with gdb it seems to run correctly, there are no errors and I get an output as expected.
But when I run it with valgrind via valgrind ./program, it does not even get far. I get an VALGRIND INTERNAL ERROR: Valgrind received a signal 11 (SIGSEGV) - exiting
at the very beginning. The valgrind output does not help me at all. I invoke a childprocess, but valgrind does not follow it. I tried to use the --trace-children=yes option, but no change.
Another question: What is the difference between the calls valgrind program and valgrind ./program?
The output I get is:
$ valgrind --tool=memcheck --trace-children=yes ./program
==2616== Memcheck, a memory error detector
==2616== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==2616== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==2616== Command: ./program
==2616==
Parent pid is 2616
Child pid is 2619
main() could not create fifo
--2619-- VALGRIND INTERNAL ERROR: Valgrind received a signal 11 (SIGSEGV) - exiting
--2619-- si_code=2; Faulting address: 0x400DFD; sp: 0x403277b70
valgrind: the 'impossible' happened:
Killed by fatal signal
==2619== at 0x3802D33A: mash_colon_env (m_libcproc.c:195)
==2619== by 0x3802D6FD: vgPlain_env_remove_valgrind_env_stuff (m_libcproc.c:254)
==2619== by 0x3806EB7C: vgSysWrap_generic_sys_execve_before (syswrap-generic.c:2622)
==2619== by 0x38068750: vgPlain_client_syscall (syswrap-main.c:1443)
==2619== by 0x380651E9: handle_syscall (scheduler.c:895)
==2619== by 0x38066D6A: vgPlain_scheduler (scheduler.c:1091)
==2619== by 0x380763FC: run_a_thread_NORETURN (syswrap-linux.c:94)
sched status:
running_tid=1
Thread 1: status = VgTs_Runnable
==2619== at 0x567D957: execve (execve.c:60)
==2619== by 0x567E1E8: execvpe (execvpe.c:151)
==2619== by 0x400B2D: main (program2.cpp:34)
Thanks in advance.
That error indicates that you have most likely encountered a bug in valgrind. In fact it has already been reported (https://bugs.kde.org/show_bug.cgi?id=271582) so you should add yourself to that bug to be kept up to date with work on fixing it.
In general such "VALGRIND INTERNAL ERROR" messages should be reported at https://bugs.kde.org/enter_valgrind_bug.cgi, making sure you you include the full output from running valgrind -v on your program.
You should be using Valgrind "follow-fork-mode" child.
That is why Valgrind might not be following your child process.