Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make longjmp/exceptions helpers thread-safe #12056

Merged
merged 29 commits into from
Sep 26, 2020
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
5786570
Make asm.js threading globals thread-safe
kripken Aug 25, 2020
0faaddd
make compiler-rt an MT library?
kripken Aug 26, 2020
0ae338f
fix
kripken Aug 26, 2020
e4f69ed
Merge remote-tracking branch 'origin/master' into exc
kripken Aug 26, 2020
3cba11a
Merge remote-tracking branch 'origin/master' into exc
kripken Aug 26, 2020
fccf48c
feedback: refactor into a side file
kripken Aug 26, 2020
a9741ea
add a test
kripken Aug 26, 2020
c4d680f
Merge remote-tracking branch 'origin/master' into exc
kripken Aug 26, 2020
daff2df
add a test that can't pass yet without a wasm backend fix [ci skip]
kripken Aug 26, 2020
5a4ea53
Merge remote-tracking branch 'origin/master' into exc
kripken Sep 24, 2020
faa03bc
use threads.h [ci skip]
kripken Sep 24, 2020
06ab316
add a compiler-rt mt variant [ci skip]
kripken Sep 24, 2020
b3db6a9
review feedback [ci skip]
kripken Sep 24, 2020
7955ce2
Also build libc_rt_wasm with -mt when needed
kripken Sep 25, 2020
96cbf74
fix test
kripken Sep 25, 2020
af889b0
Merge remote-tracking branch 'origin/master' into exc
kripken Sep 25, 2020
0629608
Revert "Disable tests to allow LLVM to roll in (#12337)"
kripken Sep 25, 2020
9e6193e
[ci skip]
kripken Sep 25, 2020
586767e
Merge remote-tracking branch 'origin/master' into exc
kripken Sep 25, 2020
e55647a
revert the extra disabled test too [ci skip]
kripken Sep 25, 2020
c76b122
comment
kripken Sep 25, 2020
d42e148
embind and asan_js libraries now also need to be built with -mt
kripken Sep 25, 2020
36c0ee0
-mt
kripken Sep 25, 2020
05257b8
Revert "-mt"
kripken Sep 25, 2020
5657df5
Revert "embind and asan_js libraries now also need to be built with -mt"
kripken Sep 25, 2020
b312b57
Revert "Also build libc_rt_wasm with -mt when needed"
kripken Sep 25, 2020
44b8dda
fix comment
kripken Sep 25, 2020
704a676
cleaner [ci skip]
kripken Sep 25, 2020
84ab45b
remove unneeded change after the llvm change
kripken Sep 25, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions system/lib/compiler-rt/emscripten_exception_builtins.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2018 The Emscripten Authors. All rights reserved.
* Emscripten is available under two separate licenses, the MIT license and the
* University of Illinois/NCSA Open Source License. Both these licenses can be
* found in the LICENSE file.
*/

/*
References to these longjmp- and exceptions-supporting things are generated
in the llvm backend.

Note that these might make more sense in compiler-rt, but they need to be
built with multithreading support when relevant (to avoid races between
threads that throw at the same time etc.), and compiler-rt is not built
that way atm.
kripken marked this conversation as resolved.
Show resolved Hide resolved
*/

#include <threads.h>

thread_local int __THREW__ = 0;
thread_local int __threwValue = 0;

void setThrew(int threw, int value) {
if (__THREW__ == 0) {
__THREW__ = threw;
__threwValue = value;
}
}
22 changes: 0 additions & 22 deletions system/lib/compiler-rt/extras.c

This file was deleted.

1 change: 0 additions & 1 deletion system/lib/libc/extras.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,3 @@ long* _get_timezone() {

void __lock(void* ptr) {}
void __unlock(void* ptr) {}

78 changes: 78 additions & 0 deletions tests/core/pthread/exceptions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2019 The Emscripten Authors. All rights reserved.
// Emscripten is available under two separate licenses, the MIT license and the
// University of Illinois/NCSA Open Source License. Both these licenses can be
// found in the LICENSE file.

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <assert.h>
#include <emscripten.h>

#include <atomic>

#define NUM_THREADS 2
#define TOTAL 1000
#define THREAD_ADDS 750
#define MAIN_ADDS 5

static std::atomic<int> sum;
static std::atomic<int> total;

void *ThreadMain(void *arg) {
for (int i = 0; i < TOTAL; i++) {
try {
// Throw two different types, to make sure we check throwing and landing
// pad behavior.
if (i & 3) {
throw 3.14159f;
}
throw i;
} catch (int x) {
total += x;
} catch (float f) {
sum++;
// wait for a change, so we see interleaved processing.
int last = sum.load();
while (sum.load() == last) {}
}
}
pthread_exit((void*)TOTAL);
}

pthread_t thread[NUM_THREADS];

void CreateThread(int i)
{
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
static int counter = 1;
kripken marked this conversation as resolved.
Show resolved Hide resolved
int rc = pthread_create(&thread[i], &attr, ThreadMain, (void*)i);
kripken marked this conversation as resolved.
Show resolved Hide resolved
assert(rc == 0);
pthread_attr_destroy(&attr);
}

void loop() {
static int main_adds = 0;
int worker_adds = sum.load() - main_adds;
sum++;
main_adds++;
printf("main iter %d : %d\n", main_adds, worker_adds);
if (worker_adds == NUM_THREADS * THREAD_ADDS &&
main_adds >= MAIN_ADDS) {
printf("done: %d.\n", total.load());
emscripten_cancel_main_loop();
exit(0);
}
}

int main() {
// Create initial threads.
for(int i = 0; i < NUM_THREADS; ++i) {
printf("maek\n");
CreateThread(i);
}

emscripten_set_main_loop(loop, 0, 0);
}
1 change: 1 addition & 0 deletions tests/core/pthread/exceptions.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
done: 249000.
6 changes: 6 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8176,6 +8176,12 @@ def test():
self.emcc_args += ['-DPOOL']
test()

@node_pthreads
def test_pthread_exceptions(self):
self.set_setting('PTHREAD_POOL_SIZE', '2')
self.emcc_args += ['-fexceptions']
self.do_run_in_out_file_test('tests', 'core', 'pthread', 'exceptions.cpp')

def test_emscripten_atomics_stub(self):
self.do_run_in_out_file_test('tests', 'core', 'pthread', 'emscripten_atomics.c')

Expand Down
4 changes: 2 additions & 2 deletions tools/system_libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ def get_default_variation(cls, **kwargs):
return super(AsanInstrumentedLibrary, cls).get_default_variation(is_asan=shared.Settings.USE_ASAN, **kwargs)


class libcompiler_rt(Library):
class libcompiler_rt(MuslInternalLibrary, MTLibrary):
name = 'libcompiler_rt'
# compiler_rt files can't currently be part of LTO although we are hoping to remove this
# restriction soon: https://reviews.llvm.org/D71738
Expand All @@ -644,9 +644,9 @@ class libcompiler_rt(Library):
cflags = ['-O2', '-fno-builtin']
src_dir = ['system', 'lib', 'compiler-rt', 'lib', 'builtins']
src_files = glob_in_path(src_dir, '*.c')
src_files.append(shared.path_from_root('system', 'lib', 'compiler-rt', 'extras.c'))
src_files.append(shared.path_from_root('system', 'lib', 'compiler-rt', 'stack_ops.s'))
src_files.append(shared.path_from_root('system', 'lib', 'compiler-rt', 'emscripten_setjmp.c'))
src_files.append(shared.path_from_root('system', 'lib', 'compiler-rt', 'emscripten_exception_builtins.c'))


class libc(AsanInstrumentedLibrary, MuslInternalLibrary, MTLibrary):
Expand Down