C/C++ Fiddle
gcc.godbolt.org
gcc.godbolt.org
int launched = 0;
// ...
start_thread(...);
while (!launched) {}
could result in an endless loop since the compiler just rewrote it to 'if(!launched) { while(true){} }'. The person had the thread write to 'launched' and expected the first thread to exit the loop once launched had been set to 1.Great tool. I hope it doesn't get a HN hug of death.
volatile
to the rescue! int launched = 0;
start_thread(...);
if (!launched) {
while (true) {}
}
That's a perfectly legal optimization. The integer here isn't atomic and if another thread modifies the variable, that would be a data race and those have undefined behavior. Therefore, the compiler can assume that there is no race and move the comparison out of the loop.Changing it to be "std::atomic<int> launched;" fixes that problem.
In this case, the data race is fairly minimal, since we're talking about a 4 byte integer being used as a boolean flag. Operations like x = 1 for 4 byte ints are pretty much guaranteed to be atomic with x86, and worst case, you go through 1 more iteration of your loop than you would have otherwise(which your code should account for at any rate with locking for critical sections). Any change to the int should cause the while loop to exit.
It should be a volatile(C pre C11) or atomic(C++11/C11) integer if you're passing it between threads, though.
It's really slow (unusable) at the moment due to, I reckon, being on HN. Do revisit it later!
I have had some issues with it in the past when importing just iostream and trying to see the output for a small example. The combined source is too large, and GCC Explorer refuses to compile and show the assembly sometimes for even small examples.
Do play with the settings. Colourise is especially nice.
The code is on GitHub [0], so I suppose you can self host it, although I have not tried to do so yet.
Unfortunate. Running this on the client side would avoid that, I wonder if they considered that option.
http://kripken.github.io/clangor/demo.html
http://kripken.github.io/llvm.js/demo.html
A bit old by now, but the first compiles C++ into LLVM IR, the second LLVM IR into JS and runs it. All of that happens clientside.
https://gist.github.com/panzi/7010082 https://gist.github.com/panzi/7010082
C example (-i for include, -p for printf, standard flags: -I, -L, -l, -W, -O, -D, -S, -E):
$ cexpr -imath.h -iunistd.h -p '%f %d' 'pow(2,3)' 'getuid()'
8.000000 1000
C++ example (-i for include, standard flags: -I, -L, -l, -W, -O, -D, -S, -E): $ cppexpr -ilimits 'std::numeric_limits<long>::max()' '"hello world"'
9223372036854775807 hello worldAlso, I assume the author has thought of this, but just throwing this out there....
`-ftemplate-depth1024` (1024 should guarantee a stack overflow)
#include <iostream>
template<int N>
struct ZVTemplate{
static void out(std::ostream& os)
{
ZVTemplate<N>::out(os);
os << "zv was here" << std::endl;
}
};
template<>
struct ZVTemplate<1>{
static void out(std::ostream& os)
{
os << "deftly evaded :)" << std::endl;
}
};
int main(){
ZVTemplate<2>::out(std::cout);
}Doing the more usual template<N-1> trick lets you hit the -ftemplate-depth limit if you pass, for example ZVTemplate<2048>. It still works ok though :)
Please do let me know if you can find a "poison" input...although at the moment the site is pretty busy just through sheer weight of users, so it's hard to tell I know!
Thanks, Matt
-x86_64 -x86 -arm -mips -or32 -avr
and so on. Yeah, I guess that requires a couple recompiled GCC binaries :D
I've had to put a variety of "anti-hacker" features into the public instance, even though it runs on a (somewhat) throwaway EC2 instance.
Check out the source, and the LDPRELOAD stuff I do to try and make the compilers vaguely secure...!
template<class T> struct Loop { Loop<T*> operator->(); };
Loop<int> i, j = i->hooray;
And on older gcc versions this eats a gig RAM and a takes couple of hours: struct a{typedef int foo;};struct a1:a{};struct a2:a{};
#define X(b,a) struct a##1:b##1,b##2{};struct a##2:b##1,b##2{};
X(a,b)X(b,c)X(c,d)X(d,e)X(e,f)X(f,g)X(g,h)X(h,i)X(i,j)X(j,k)X(k,l)
X(l,m)X(m,n) n1::foo main(){}
From slide 11 and 12 of this presentation: http://www.fefe.de/c++/c%2B%2B-talk.pdfThanks for sharing those utterly evil examples though! :D