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

Exception switching #612

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
52 changes: 52 additions & 0 deletions thread/test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ limitations under the License.
#include <stdlib.h>
#include <queue>
#include <algorithm>
#include <exception>
#include <sys/time.h>
#include <gflags/gflags.h>
#include "../../test/gtest.h"
Expand Down Expand Up @@ -404,6 +405,57 @@ TEST(Perf, ThreadSwitchWithStandaloneTSUpdater)
return;
}

inline int std_uncaught_exceptions() {
#if __cplusplus > 201700L
return std::uncaught_exceptions();
#else
return std::uncaught_exception();
#endif
}

TEST(exception, switch)
{
class Foo {
public:
~Foo() {
LOG_INFO("before thread_yield():", VALUE(std_uncaught_exceptions()));
photon::thread_yield();
LOG_INFO("after thread_yield():", VALUE(std_uncaught_exceptions()));
}
};

class Bar {
public:
~Bar() {
try {
Foo foo;
throw "asdf";
} catch(...) { }
}
};

bool quit = false;
auto th = photon::thread_create11([&](){
while(!quit) {
LOG_INFO(VALUE(std_uncaught_exceptions()));
EXPECT_EQ(std_uncaught_exceptions(), 0);
try {
throw 3.1415926f;
} catch(...) { }
photon::thread_yield();
}
});
thread_enable_join(th);
try {
Foo foo;
Bar bar;
throw 123;
} catch(...) { }

quit = true;
thread_join((join_handle*)th);
}

thread_local int shot_count;
thread_local photon::condition_variable shot_cond;
uint64_t on_timer_asdf(void* arg)
Expand Down
29 changes: 29 additions & 0 deletions thread/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ limitations under the License.
#include <thread>
#include <mutex>
#include <condition_variable>
#include <dlfcn.h>

#ifdef _WIN64
#include <processthreadsapi.h>
Expand Down Expand Up @@ -197,6 +198,7 @@ namespace photon
size_t stack_size;
// offset 96B
condition_variable cond; /* used for join */
uint64_t __gh_globals[2] = {0};

enum shift {
joinable = 0,
Expand Down Expand Up @@ -649,6 +651,33 @@ namespace photon
}

static void _photon_thread_die(thread* th) asm("_photon_thread_die");
struct __cxa_eh_globals {
void* caughtExceptions;
unsigned int uncaughtExceptions;
};
inline void install_hook(void* target, void* hook) {
auto page = (uint64_t)target & ~4095UL;
int ret = mprotect((void*)page, 4096, PROT_READ | PROT_WRITE | PROT_EXEC);
assert(ret == 0); (void)ret;
*(uint16_t*)((char*)target + 0) = 0xb848; // movabs imm64 to rax
*(uint64_t*)((char*)target + 2) = (uint64_t)hook;
*(uint16_t*)((char*)target + 10) = 0xe0ff; // jmp *rax
}
inline void* install_hook(const char* sym, void* hook) {
auto addr = dlsym(RTLD_DEFAULT, sym);
assert(addr);
install_hook(addr, hook);
return addr;
}
static __cxa_eh_globals* cxa_get_globals (void) {
thread_local __cxa_eh_globals eh = {0};
return !CURRENT ? &eh : (__cxa_eh_globals*) CURRENT->__gh_globals;
}
__attribute__((constructor))
static void install_eh_globals_hook() {
install_hook("__cxa_get_globals", (void*)&cxa_get_globals);
}


#if defined(__x86_64__)
#if !defined(_WIN64)
Expand Down
Loading