Skip to content

Jitting expression templates yeets a UD2 instruction #17

@JPenuchot

Description

@JPenuchot

Hi,

I don't really have any way to explain that bug honestly, what happens is that I simply JIT an expression template program to dynamically generate & evaluate expression templates, and the result is an illegal instruction that is triggered by either the JIT compiler or the code it runs.

I don't have enough RAM to even compile clang in debug mode so I can't really do any further investigation, but by debugging the executable it seems like the illegal instruction was purposely called to crash the program as it's a UD2 instruction.

Here's the program if you want to replicate that bug, it only requires Blaze which is a header only library:

#include <iostream>
#include <type_traits>

#include <blaze/Blaze.h>

template<unsigned int I, typename T1, typename T2>
inline auto add( T1 const& e1, T2 const& e2 )
  -> std::enable_if_t < !(I > 0), decltype(e1)> {
  return e1;
}

template<unsigned int I, typename T1, typename T2>
inline auto add( T1 const& e1, T2 const& e2 )
  -> std::enable_if_t < (I > 0), decltype(add<I-1>( e1 + e2, e2 ))> {
  return add<I-1>( e1 + e2, e2 );
}

template<unsigned int I, typename T1, typename T2>
[[clang::jit]]
blaze::DynamicVector<float> eval ( T1 const& e1, T2 const& e2 ) {
  blaze::DynamicVector<float> res = add<I>( e1,e2 );
  return res;
}

int main ()
{
  blaze::DynamicVector<float> a( 10,1.f ), b( 10,1.f );
  for( auto const i : { 1, 2, 3, 4 } )
    std::cout << eval<i>( a, b ) << '\n';
}

The code runs perfectly when I do the same without jitting eval, so the error is clearly related to the jit compiler or generated code execution.

Regards,
Jules

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions