Can't run simple Emscripten thread example in Firefox - pthreads

I'd like to compile the following C++ snippet using threads with Emscripten
#include <cstdio>
#include <thread>
void foo() { puts("foo\n"); }
void bar(int x) { printf("bar %d\n", x); }
int main(int argc, char* argv[]) {
std::thread first(foo);
std::thread second(bar, 123);
puts("main, foo and bar now execute concurrently...\n");
first.join();
second.join();
puts("foo and bar completed.\n");
}
To get Emscripten to use threads I pass the "-s USE_PTHREADS=1" option during compilation and linking
target_compile_options(
Emscripten PRIVATE -v "SHELL:-s USE_PTHREADS=1 -s PTHREAD_POOL_SIZE=8")
target_link_options(
Emscripten PRIVATE -v --emrun
"SHELL:-s MINIFY_HTML=0 -s USE_PTHREADS=1 -s PTHREAD_POOL_SIZE=8")
I also change the suffix of the compilation output to .html which leads to Emscripten directly producing a .html page
set(CMAKE_EXECUTABLE_SUFFIX ".html")
So far so good. The snippet compiles as expected and I can see the toolchain passing some pthread arguments on the command line.
In order to test the output I fire up a webserver with python and open the .html page with Firefox. The Firefox option javascript.options.shared_memory was enabled by default in my version (78.0.1) so I thought that my code should work out of the box. Sadly this isn't the case and I'm getting Uncaught ReferenceError: SharedArrayBuffer is not defined exceptions thrown at me through the console.
Testing the code with node works though:
$ node --experimental-wasm-threads --experimental-wasm-bulk-memory Emscripten.js
main, foo and bar now execute concurrently...
foo
bar 123
foo and bar completed.
/edit
Ok, seems like SharedArrayBuffer still undergoes some standardization process...
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer/Planned_changes

Related

How to get a wasm stacktrace

I'm looking for something gdb --core equivalent on webassembly.
Take this example:
//crash.cpp
#include <iostream>
int main() {
std::cout << "crashing soon..." << std::endl;
int *a = 0;
*a = 1;
}
I compile this with:
$ em++ -g4 crash.cpp -o crash.html --source-map-base http://localhost:8080/
And start the server:
$ emrun --no_browser --port 8080 crash.html
So how can I get a stack trace of this core dump/crash? The console on both chrome/firefox when visiting page just shows a js stacktrace and that won't help me. Looking at Sources => Call stack on chrome console just shows "Not paused", after the crash.
This is on debian 11, emscripten 2.0.12~dfsg-2, clang-11.
The reason is that what you're doing is not an error in WebAssembly. Like on many embedded platforms, writing to or reading from zero pointer is a perfectly valid operation in WebAssembly memory model.
However, Emscripten tries to help you catch this as for C/C++ it's a common mistake, so what it does instead is checks the value at the address zero after the program has finished execution and throws a helpful assertion if that value happened to be overwritten. For this reason you're getting a stacktrace with only JavaScript bits in it - because the check is done by JavaScript when Wasm stack has already been exited.
If you tried a different operation that does cause immediate abort, for example, assert(false), then you would see WebAssembly and/or C/C++ on the stack as expected.

How do I construct a syntax-only match-finder tool?

I am building an ASTMatcher-based tool that I would like to run over my sources:
int main(int argc, const char** argv) {
CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
ClangTool Tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());
MatchFinder Finder;
// Repeated calls to Finder.addMatcher(...);
Tool.run(newFrontendActionFactory(&Finder).get());
// Handle the results of the matching.
}
Running this over a source file that depends on other headers yields the following error:
~$ /path/to/my/tool /path/to/my/file.cpp --
/path/to/my/file.cpp:8:10: fatal error: 'string' file not found
#include <string>
^~~~~~~~
1 error generated.
Error while processing /path/to/my/file.cpp.
I do not want to include any other headers in this processing, lest my matchers find content in those headers that I do not want to handle.
I tried passing -fsyntax_only to the tool, but I get the same result as above:
~$ /path/to/my/tool /path/to/my/file.cpp -- -fsyntax-only
I noticed in the ASTMatcher tutorial that there is a clang::SyntaxOnlyAction. However, I have been unable to figure out how MatchFinder and SyntaxOnlyAction can be used in conjunction with one another. Likewise, I have been able to do an AST dump from the command line of the same file, no problem, so I know it's possible.
Is it possible to configure a MatchFinder-based tool to honor the syntax-only behavior?

Getting bison parser to divulge debug information

I am having trouble writing a bison parser, and unexpectedly ran into difficulties getting the parser to print debug information. I found two solutions on the web, but neither seems to work.
This advocates to put this code in the main routine:
extern int yydebug;
yydebug = 1;
Unfortunately the C++ compiler detects an undefined reference to `yydebug'.
This suggests putting
#if YYDEBUG == 1
extern yydebug;
yydebug = 1;
#endif
into the grammar file. It compiles but does not produce output.
What does work is to edit the parser file itself, replacing
int yydebug;
by
int yydebug = 1;
The big disadvantage is that I have to redo this every time I change the grammar file, which during debugging would happen constantly. Is there any other way I can provoke the parser into coughing up its secret machinations?
I am using bison v2.4.1 to generate the parser, with the following command-line options:
bison -ldv -p osil -o $(srcdir)/OSParseosil.tab.cpp OSParseosil.y
Although the output is a C++ file, I am using the standard C skeleton.
With bison and the standard C skeleton, to enable debug support you need to do one of the following:
Use the -t (Posix) or --debug (Bison extension) command-line option when you create your grammar. (bison -t ...)
Use the -DYYDEBUG=1 command-line option (gcc or clang, at least) when you compile the generated grammar (gcc -DYYDEBUG=1 parser.tab.c ...`).
Add the %debug directive to your bison source
Put #define YYDEBUG 1 in the prologue in your bison source (the part of the file between %{ and %}.
I'd use -t in the bison command line. It's simple, and since it is Posix standard it probably will also work on other derived parser generators. However, adding %debug to the bison source is also simple; while it is not as portable, it works in bison 2.4.
Once you've done that, simply setting yydebug to a non-zero value is sufficient to produce debug output.
If you want to set yydebug in some translation unit other than the generated parser itself, you need to be aware of the parser prefix you declared in the bison command line. (In the parser itself, yydebug is #defined to the prefixed name.) And you need to declare the debug variable (with the correct prefix) as extern. So in your main, you probably want to use:
extern int osildebug;
// ...
int main(int argc, char** argv) {
osildebug = 1;
// ...
}
If you're using bison, your best place to find information is the bison manual; most of the above answer will be found in that page.

win32-g++ error on for_each and lambda

I know this theme often turns up on stackoverflow, but the case here is different:
The following code compiles with MsVC++ 2013 but not with win32-g++. Is there any way of making it work with both compilers?
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
vector<int>my_vector;
for(int i=0; i < 6; i++)
my_vector.push_back(i);
for_each( my_vector.begin(), my_vector.end(), [ ](int n){cout<<n;});
return 0;
}
The errors reported are
||=== stdtest, Debug ===|
D:\dev\CplusPlus\stdtest\main.cpp||In function 'int main()':|
D:\dev\CplusPlus\stdtest\main.cpp|13|warning: lambda expressions only available with -std=c++11 or -std=gnu++11 [enabled by default]|
D:\dev\CplusPlus\stdtest\main.cpp|13|error: no matching function for call to 'for_each(std::vector<int>::iterator, std::vector<int>::iterator, main()::<lambda(int)>)'|
D:\dev\CplusPlus\stdtest\main.cpp|13|note: candidate is:|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_algo.h|4436|note: template<class _IIter, class _Funct> _Funct std::for_each(_IIter, _IIter, _Funct)|
D:\dev\CplusPlus\stdtest\main.cpp|13|error: template argument for 'template<class _IIter, class _Funct> _Funct std::for_each(_IIter, _IIter, _Funct)' uses local type 'main()::<lambda(int)>'|
D:\dev\CplusPlus\stdtest\main.cpp|13|error: trying to instantiate 'template<class _IIter, class _Funct> _Funct std::for_each(_IIter, _IIter, _Funct)'|
||=== Build finished: 3 errors, 1 warnings (0 minutes, 0 seconds) ===|
I should add that I am assuming that the C++11 standard is turned on because "enabled by default". In case I am wrong, I am having a hard time finding out how to change the compiler switches from Code::Blocks.
it turns out that I was misled by the statement that -sdd=C++11 was "enabled by default".
I ended up finding a way of turning it on with the Project > Build Options> compiler settings
and then I clicked on the checkbox "have g++ follow the c++11 ISO C++ language settings" checkbox.
For those who are working with QtCreator add the following line to the project file:
QMAKE_CXXFLAGS += -std=c++11

Cannot link a minimal Lua program

I have the following trivial Lua program which I copied from the book Programming In Lua
#include <stdio.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
int main (void)
{
char buff[256];
int error;
lua_State *L = luaL_newstate(); /* opens Lua */
luaL_openlibs(L); /* opens the standard libraries */
while (fgets(buff, sizeof(buff), stdin) != NULL)
{
error = luaL_loadbuffer(L, buff, strlen(buff), "line") ||
lua_pcall(L, 0, 0, 0);
if (error)
{
fprintf(stderr, "%s", lua_tostring(L, -1));
lua_pop(L, 1); /* pop error message from the stack */
}
}
lua_close(L);
return 0;
}
my environment is cywin
my make file looks like this:
CC=gcc
INCLUDE='-I/home/xyz/c_drive/Program Files/Lua/5.1/include'
LINKFLAGS='-L/home/xyz/c_drive/Program Files/Lua/5.1/lib' -llua51
li.o:li.c
$(CC) $(INCLUDE) -c li.c
main:li.o
$(CC) -o main $(LINKFLAGS) li.o
clean:
rm *.o
rm main
My /home/xyz/c_drive/Program Files/Lua/5.1/lib directory contains lua5.1.dll lua5.1.lib lua51.dll and lua51.lib
Trying to build my main target I am getting the following errors:
li.o:li.c:(.text+0x35): undefined reference to `_luaL_newstate'
li.o:li.c:(.text+0x49): undefined reference to `_luaL_openlibs'
li.o:li.c:(.text+0xaf): undefined reference to `_luaL_loadbuffer'
li.o:li.c:(.text+0xd9): undefined reference to `_lua_pcall'
li.o:li.c:(.text+0x120): undefined reference to `_lua_tolstring'
li.o:li.c:(.text+0x154): undefined reference to `_lua_settop'
li.o:li.c:(.text+0x167): undefined reference to `_lua_close'
Any ideas about what I might be doing wrong here?
The problem is that you have named the libraries on the link command line before the object files that require them. The linker loads modules from left to right on the command line. At the point on the line where you name -llua51, no undefined symbols that could be satisfied by that library are known. Then you name li.o, which does have unknown symbols.
Some Unix-like environments don't treat this as an error because part of the link process is deferred to the program load when reference to .so files are satisfied. But Cygwin, MinGW, and Windows in general must treat this as an error because DLLs work quite differently from .so files.
The solution is to put -llua51 after all the .o files on your link line.
Edit: Incidentally, it appears you are linking against the Lua for Windows distribution, but building with GCC under Cygwin. You will want to use Dependency Walker to make sure that your program does not depend on the Cygwin runtime, and that it does depend on the same C runtime as the lua51.dll from Lua for Windows. IIRC, that will be the runtime for the previous version of Visual Studio. It is possible to make GCC link against that, but you will need to be using the MinGW port (which you can use from Cygwin), and link against a couple of specific libraries to get that version. I'm away from my usual PC, or I'd quote an exact link line. (I believe you need -lmoldname -lmsvcr80 or something like that, as the last items on the link line.)
It will cause mysterious and very hard to diagnose problems if more than one C runtime library is in use. The easy answer is to use the same one as your preferred Lua DLL. Another alternative is that the Lua Binaries project has pre-compiled Lua DLLs for a wide array of C toolchains on Windows. If you need a Lua application that understands the Cygwin environment, you will want one that is built by GCC for Cygwin and not the Lua for Windows flavor. Lua Binaries will be your friend, or you can build Lua your self from source.
The names in the Lua API do not have those leading underscores. Try compiling with -fno-leading-underscore.

Resources