Lemming Lemming - 1 year ago 105
C++ Question

Clang produces illegal instruction where GCC doesn't

I found a situation in which Clang produces an illegal instruction, where gcc doesn't,
while experimenting with this question.

My question is: Am I doing something very wrong, or is this an actual issue with Clang?

I boiled it down to the minimal snippet necessary to reproduce the problem.

Take the file


#include <iostream>

#define EIGEN_MATRIXBASE_PLUGIN "eigen_matrix_addons.hpp"
#include <Eigen/Dense>

int main() {
Eigen::Matrix2d A;

A << 0, 1, 2, 3;

std::cout << A << "\n";

And the file

friend std::ostream &operator<<(std::ostream &o, const Derived &m) {
o << static_cast<const MatrixBase<Derived> &>(m);

(See here for a detailed explanation on what this file does. In short, its contents are placed directly into the class definition of
template<class Derived> class MatrixBase;
. So, this introduces another ostream operator on
that calls the Eigen implementation of the ostream operator on
. The motivation for this becomes apparent if you read this question.)

Compile with GCC and run:

$ g++ -std=c++11 -Wall -Wextra -pedantic -isystem/usr/include/eigen3 -I. -o eigen_gcc eigen.cpp
$ ./eigen_gcc
0 1
2 3
$ g++ --version
g++ (SUSE Linux) 4.8.1 20130909 [gcc-4_8-branch revision 202388]
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO

And then compile with Clang and run:

$ clang++ -std=c++11 -Wall -Wextra -pedantic -isystem/usr/include/eigen3 -I. -o eigen_clang eigen.cpp
$ ./eigen_clang
0 1
Illegal instruction
$ clang++ --version
clang version 3.4 (branches/release_34 198681)
Target: x86_64-suse-linux
Thread model: posix

As you can see, the program is interrupted after executing an illegal instruction. A back-trace in gdb reveals that the issue happens in line two of

(gdb) bt
#0 0x00000000004013e1 in Eigen::operator<< (o=..., m=...)
at ./eigen_matrix_addons.hpp:2
#1 0x00000000004010f0 in main () at eigen.cpp:15

I.e. probably the

My Eigen version is 3.2.0-2.1.4 according to


The dis-assembly that @Mysticial asked for:

(gdb) disass
Dump of assembler code for function Eigen::operator<<(std::ostream&, Eigen::Matrix<double, 2, 2, 0, 2, 2> const&):
0x00000000004013c0 <+0>: push %rbp
0x00000000004013c1 <+1>: mov %rsp,%rbp
0x00000000004013c4 <+4>: sub $0x20,%rsp
0x00000000004013c8 <+8>: mov %rdi,-0x10(%rbp)
0x00000000004013cc <+12>: mov %rsi,-0x18(%rbp)
0x00000000004013d0 <+16>: mov -0x10(%rbp),%rdi
0x00000000004013d4 <+20>: mov -0x18(%rbp),%rsi
0x00000000004013d8 <+24>: callq 0x4013f0 <Eigen::operator<< <Eigen::Matrix<double, 2, 2, 0, 2, 2> >(std::ostream&, Eigen::DenseBase<Eigen::Matrix<double, 2, 2, 0, 2, 2> > const&)>
0x00000000004013dd <+29>: mov %rax,-0x20(%rbp)
=> 0x00000000004013e1 <+33>: ud2
End of assembler dump.

Answer Source

The "Illegal instruction" error is likely because your "operator <<" is missing a return statement. This leads to undefined behaviour.

Section 6.6.3 of the Standard says:

Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.

You should add:

return o;

at the end of the function.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download