Broken JavaScript generated for Boost Local Time with -O2
I have two files, main.cpp:
#include <string>
void _Test();
int main(int argc, char *argv[])
{
// No JavaScript exception if Test() is only called once,
// but the output is wrong regardless.
_Test();
_Test();
std::string s;
for (int i = 0; i < 4; i++)
s.push_back((unsigned char)i);
return 0;
}
and other.cpp:
#include <boost/date_time/posix_time/posix_time.hpp>
#include <sstream>
#include <string>
#include <emscripten.h>
void _Test()
{
std::stringstream dateTime;
dateTime << boost::date_time::second_clock<
boost::posix_time::ptime>::local_time();
emscripten_run_script(("Module.print('" + dateTime.str() + " ')").c_str());
}
I compile and link these two files like this:
python em++ -Id:\dev\base-00\lib\boost_1.47.0\include -O2 --closure 0 main.cpp other.cpp
When I run node on the resulting a.out.js, I get:
[Work] [none] 23:12 22673M y:\emscripten>node a.out.js
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
abort() at Error
at _abort (Y:\emscripten\a.out.js:45299:26)
at _free (Y:\emscripten\a.out.js:42194:7)
at __ZdlPv (Y:\emscripten\a.out.js:43059:5)
at Array.__ZNSt3__17collateIcED0Ev [as 664] (Y:\emscripten\a.out.js:23088:3)
at Array.__ZNSt3__16locale5facet16__on_zero_sharedEv [as 74] (Y:\emscripten\a.out.js:19606:22)
at __ZNSt3__114__shared_count16__release_sharedEv (Y:\emscripten\a.out.js:15836:22)
at __ZNSt3__16locale5__impD2Ev (Y:\emscripten\a.out.js:19189:11)
at Array.__ZNSt3__16locale5__impD0Ev [as 512] (Y:\emscripten\a.out.js:19159:3)
at Array.__ZNSt3__16locale5facet16__on_zero_sharedEv [as 74] (Y:\emscripten\a.out.js:19606:22)
at __ZNSt3__114__shared_count16__release_sharedEv (Y:\emscripten\a.out.js:15836:22)
If I compile with -O0 I get the expected output:
[Work] [none] 23:16 22665M y:\emscripten>node a.out.js
-- ::
-- ::
(This should really be two date and times on the form "2011-Oct-26 12:10:17", but that requires strftime_l() in library.js to be implemented.)
Here is the generated JavaScript file that crashes: http://dl.dropbox.com/u/1827923/a.out.js
In case you do not have boost installed, or you can not reproduce the bug, here is a preprocessed version of other.cpp: http://dl.dropbox.com/u/1827923/other.cpp
This file was generated by:
python em++ -Id:\dev\base-00\lib\boost_1.47.0\include -c -E -C -P other.cpp
(And if I compile and link with this file, I still get the same JavaScript crash.)
I should add that using a single file (i.e. gluing the code from other.cpp into main.cpp) makes the generated JS not crash, but the output is still not the expected one (so the code it still broken).
(And the same is the case for the push_back() loop in main.cpp, without it the generated code produces the wrong output, but it does not crash anymore.)
This looks like an LLVM bug. The unoptimized code for the crucial function is
define linkonce_odr i64 @_ZN5boost9date_time37time_resolution_traits_adapted64_impl9as_numberENS0_11int_adapterIxEE(%"class.boost::date_time::int_adapter"* byval align 4 %i) align 2 {
entry:
call void @emscripten_autodebug_i32(i32 -1, i32 61062)
%call = call i64 @_ZNK5boost9date_time11int_adapterIxE9as_numberEv(%"class.boost::date_time::int_adapter"* %i)
call void @emscripten_autodebug_i64(i32 61062, i64 %call)
call void @emscripten_autodebug_i32(i32 -2, i32 61065)
ret i64 %call
}
and the optimized version is
define internal fastcc i64 @_ZN5boost9date_time37time_resolution_traits_adapted64_impl9as_numberENS0_11int_adapterIxEE(i64 %i.0) nounwind align 2 {
entry:
%i = alloca %"class.boost::date_time::int_adapter", align 8
tail call fastcc void @emscripten_autodebug_i32(i32 -1, i32 61062)
%call = tail call fastcc i64 @_ZNK5boost9date_time11int_adapterIxE9as_numberEv(%"class.boost::date_time::int_adapter"* %i)
tail call fastcc void @emscripten_autodebug_i64(i32 61062, i64 %call)
tail call fastcc void @emscripten_autodebug_i32(i32 -2, i32 61065)
ret i64 %call
}
In the unoptimized one it uses byval so a copy is created, of the %i that is passes on. In the optimized one, it creates space on the stack for the copy - but doesn't actually copy. So it reads uninitialized stack data.
I'll file an LLVM bug.
Filed: http://llvm.org/bugs/show_bug.cgi?id=12537
Thank you for filing that bug! I verified that the same crash happens for the latest version of llvm (svn revision 154661).
Cool, can you mention that in the bug there? More likely it will get fixed if they know it is still an issue on trunk.
Problem is in LLVM, closing this.