Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
vtjnash committed May 11, 2021
1 parent abeb8dc commit cb5a359
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 42 deletions.
2 changes: 1 addition & 1 deletion cli/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ endif
$(build_shlibdir)/libjulia-debug.$(JL_MAJOR_MINOR_SHLIB_EXT): $(LIB_DOBJS) $(SRCDIR)/list_strip_symbols.h | $(build_shlibdir) $(build_libdir)
@$(call PRINT_LINK, $(CC) $(call IMPLIB_FLAGS,$@.tmp) $(LOADER_CFLAGS) -DLIBRARY_EXPORTS -shared $(DEBUGFLAGS) $(LIB_DOBJS) -o $@ \
$(JLIBLDFLAGS) $(LOADER_LDFLAGS) $(RPATH_LIB) $(call SONAME_FLAGS,libjulia-debug.$(JL_MAJOR_SHLIB_EXT)))
@$(INSTALL_NAME_CMD)libjulia-debug.$(SHLIB_EXT) $@.tmp
@$(INSTALL_NAME_CMD)libjulia-debug.$(SHLIB_EXT) $@
ifeq ($(OS), WINNT)
@$(call PRINT_ANALYZE, $(OBJCOPY) $(build_libdir)/$(notdir $@).tmp.a $(STRIP_EXPORTED_FUNCS) $(build_libdir)/$(notdir $@).a && rm $(build_libdir)/$(notdir $@).tmp.a)
endif
Expand Down
6 changes: 6 additions & 0 deletions src/julia_fasttls.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ extern "C" {
#include "support/dirpath.h"

typedef struct _jl_gcframe_t jl_gcframe_t;

#if defined(_OS_DARWIN_)
#include <pthread.h>
typedef void *(jl_get_pgcstack_func)(pthread_key_t); // aka typeof(pthread_getspecific)
#else
typedef jl_gcframe_t **(jl_get_pgcstack_func)(void);
#endif

#if !defined(_OS_DARWIN_) && !defined(_OS_WINDOWS_)
#define JULIA_DEFINE_FAST_TLS \
Expand Down
3 changes: 2 additions & 1 deletion src/julia_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,6 @@ int jl_safepoint_consume_sigint(void);
void jl_wake_libuv(void);

void jl_set_pgcstack(jl_gcframe_t **) JL_NOTSAFEPOINT;
#if !defined(__clang_analyzer__)
#if defined(_OS_DARWIN_)
typedef pthread_key_t jl_pgcstack_key_t;
#elif defined(_OS_WINDOWS_)
Expand All @@ -737,6 +736,8 @@ typedef DWORD jl_pgcstack_key_t;
typedef jl_gcframe_t ***(*jl_pgcstack_key_t)(void) JL_NOTSAFEPOINT;
#endif
void jl_pgcstack_getkey(jl_get_pgcstack_func **f, jl_pgcstack_key_t *k);

#if !defined(__clang_analyzer__)
static inline void jl_set_gc_and_wait(void)
{
jl_task_t *ct = jl_current_task;
Expand Down
17 changes: 9 additions & 8 deletions src/llvm-ptls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct LowerPTLS: public ModulePass {
Function *pgcstack_getter;
LLVMContext *ctx;
MDNode *tbaa_const;
FunctionType *FT_pgcstack_getter;
PointerType *T_pgcstack_getter;
PointerType *T_ppjlvalue;
PointerType *T_pppjlvalue;
Expand Down Expand Up @@ -228,7 +229,7 @@ void LowerPTLS::fix_pgcstack_use(CallInst *pgcstack)
auto key = new LoadInst(T_size, pgcstack_key_slot, "", false, pgcstack);
key->setMetadata(llvm::LLVMContext::MD_tbaa, tbaa_const);
key->setMetadata(llvm::LLVMContext::MD_invariant_load, MDNode::get(*ctx, None));
auto new_pgcstack = CallInst::Create(T_pgcstack_getter, getter, {key}, "", pgcstack);
auto new_pgcstack = CallInst::Create(FT_pgcstack_getter, getter, {key}, "", pgcstack);
new_pgcstack->takeName(pgcstack);
pgcstack->replaceAllUsesWith(new_pgcstack);
pgcstack->eraseFromParent();
Expand All @@ -252,7 +253,7 @@ void LowerPTLS::fix_pgcstack_use(CallInst *pgcstack)
#if defined(_OS_DARWIN_)
assert(sizeof(k) == sizeof(uintptr_t));
Constant *key = ConstantInt::get(T_size, (uintptr_t)k);
auto new_pgcstack = CallInst::Create(T_pgcstack_getter, val, {key}, "", pgcstack);
auto new_pgcstack = CallInst::Create(FT_pgcstack_getter, val, {key}, "", pgcstack);
new_pgcstack->takeName(pgcstack);
pgcstack->replaceAllUsesWith(new_pgcstack);
pgcstack->eraseFromParent();
Expand All @@ -274,17 +275,17 @@ bool LowerPTLS::runOnModule(Module &_M)
ctx = &M->getContext();
tbaa_const = tbaa_make_child("jtbaa_const", nullptr, true).first;

auto FT_pgcstack_getter = pgcstack_getter->getFunctionType();
T_int8 = Type::getInt8Ty(*ctx);
T_size = sizeof(size_t) == 8 ? Type::getInt64Ty(*ctx) : Type::getInt32Ty(*ctx);
T_pint8 = T_int8->getPointerTo();
FT_pgcstack_getter = pgcstack_getter->getFunctionType();
#if defined(_OS_DARWIN_)
assert(sizeof(key) == sizeof(unsigned long));
FT_pgcstack_getter = FunctionType::get(FT_pgcstack_getter->getReturnType(), {key->getType()});
assert(sizeof(jl_pgcstack_key_t) == sizeof(uintptr_t));
FT_pgcstack_getter = FunctionType::get(FT_pgcstack_getter->getReturnType(), {T_size}, false);
#endif
T_pgcstack_getter = FT_pgcstack_getter->getPointerTo();
T_pppjlvalue = cast<PointerType>(FT_pgcstack_getter->getReturnType());
T_ppjlvalue = cast<PointerType>(T_pppjlvalue->getElementType());
T_int8 = Type::getInt8Ty(*ctx);
T_size = sizeof(size_t) == 8 ? Type::getInt64Ty(*ctx) : Type::getInt32Ty(*ctx);
T_pint8 = T_int8->getPointerTo();
if (imaging_mode) {
pgcstack_func_slot = create_aliased_global(T_pgcstack_getter, "jl_pgcstack_func_slot");
pgcstack_key_slot = create_aliased_global(T_size, "jl_pgcstack_key_slot"); // >= sizeof(jl_pgcstack_key_t)
Expand Down
73 changes: 49 additions & 24 deletions src/threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ extern "C" {

#include "threading.h"

JL_DLLEXPORT void *jl_get_ptls_states(void)
{
// mostly deprecated: use current_task instead
return jl_current_task->ptls;
}

#if !defined(_OS_WINDOWS_)
static pthread_key_t jl_safe_restore_key;

__attribute__((constructor)) void _jl_init_safe_restore(void)
Expand All @@ -52,13 +59,7 @@ JL_DLLEXPORT void jl_set_safe_restore(jl_jmp_buf *sr)
{
pthread_setspecific(jl_safe_restore_key, (void*)sr);
}

JL_DLLEXPORT void *jl_get_ptls_states(void)
{
// mostly deprecated: use current_task instead
return jl_current_task->ptls;
}

#endif


// The tls_states buffer:
Expand Down Expand Up @@ -92,7 +93,7 @@ JL_CONST_FUNC jl_gcframe_t **jl_get_pgcstack(void) JL_NOTSAFEPOINT

void jl_set_pgcstack(jl_gcframe_t **pgcstack) JL_NOTSAFEPOINT
{
pthread_setspecific(jl_pgcstack_key, pgcstack);
pthread_setspecific(jl_pgcstack_key, (void*)pgcstack);
}

void jl_pgcstack_getkey(jl_get_pgcstack_func **f, pthread_key_t *k)
Expand All @@ -113,6 +114,7 @@ JL_DLLEXPORT void jl_pgcstack_setkey(jl_get_pgcstack_func *f, pthread_key_t k)
// reliably used from a shared library) either..... Use `TLSAlloc` instead.

static DWORD jl_pgcstack_key;
static DWORD jl_safe_restore_key;

// Put this here for now. We can move this out later if we find more use for it.
BOOLEAN WINAPI DllMain(IN HINSTANCE hDllHandle, IN DWORD nReason,
Expand All @@ -122,44 +124,67 @@ BOOLEAN WINAPI DllMain(IN HINSTANCE hDllHandle, IN DWORD nReason,
case DLL_PROCESS_ATTACH:
jl_pgcstack_key = TlsAlloc();
assert(jl_pgcstack_key != TLS_OUT_OF_INDEXES);
jl_safe_restore_key = TlsAlloc();
assert(jl_safe_restore_key != TLS_OUT_OF_INDEXES);
// Fall through
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
TlsFree(jl_tls_key);
TlsFree(jl_pgcstack_key);
TlsFree(jl_safe_restore_key);
break;
}
return 1; // success
}

JL_CONST_FUNC jl_gcframe_t **jl_get_pgcstack(void) JL_NOTSAFEPOINT
{
#if defined(_CPU_X86_64_)
DWORD *plast_error = (DWORD*)(__readgsqword(0x30) + 0x68);
DWORD last_error = *plast_error;
#define SAVE_ERRNO \
DWORD *plast_error = (DWORD*)(__readgsqword(0x30) + 0x68); \
DWORD last_error = *plast_error
#define LOAD_ERRNO \
*plast_error = last_error
#elif defined(_CPU_X86_)
DWORD *plast_error = (DWORD*)(__readfsdword(0x18) + 0x34);
DWORD last_error = *plast_error;
#define SAVE_ERRNO \
DWORD *plast_error = (DWORD*)(__readfsdword(0x18) + 0x34); \
DWORD last_error = *plast_error
#define LOAD_ERRNO \
*plast_error = last_error
#else
DWORD last_error = GetLastError();
#define SAVE_ERRNO \
DWORD last_error = GetLastError()
#define LOAD_ERRNO \
SetLastError(last_error)
#endif

JL_DLLEXPORT jl_jmp_buf *jl_get_safe_restore(void)
{
SAVE_ERRNO;
jl_jmp_buf *sr = (jl_jmp_buf*)TlsGetValue(jl_safe_restore_key);
LOAD_ERRNO;
return sr;
}

JL_DLLEXPORT void jl_set_safe_restore(jl_jmp_buf *sr)
{
SAVE_ERRNO;
TlsSetValue(jl_safe_restore_key, (void*)sr);
LOAD_ERRNO;
}

JL_CONST_FUNC jl_gcframe_t **jl_get_pgcstack(void) JL_NOTSAFEPOINT
{
SAVE_ERRNO;
jl_gcframe_t **pgcstack = (jl_ptls_t)TlsGetValue(jl_pgcstack_key);
#if defined(_CPU_X86_64_)
*plast_error = last_error;
#elif defined(_CPU_X86_)
*plast_error = last_error;
#else
SetLastError(last_error);
#endif
LOAD_ERRNO;
return pgcstack;
}

void jl_set_pgcstack(jl_gcframe_t **pgcstack) JL_NOTSAFEPOINT
{
// n.b.: this smashes GetLastError
TlsSetValue(jl_pgcstack_key, pgcstack);
TlsSetValue(jl_pgcstack_key, (void*)pgcstack);
}

void jl_pgcstack_getkey(jl_get_pgcstack_func **f, DWORD *k)
Expand Down
8 changes: 0 additions & 8 deletions test/clangsa/MissingRoots.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,14 +409,6 @@ void stack_rooted(jl_value_t *lb JL_MAYBE_UNROOTED, jl_value_t *ub JL_MAYBE_UNRO
JL_GC_POP();
}

void JL_NORETURN throw_internal(jl_value_t *e JL_MAYBE_UNROOTED)
{
jl_ptls_t ptls = jl_get_ptls_states();
ptls->sig_exception = e;
jl_gc_unsafe_enter(ptls);
look_at_value(e);
}

JL_DLLEXPORT jl_value_t *jl_totally_used_function(int i)
{
jl_value_t *v = jl_box_int32(i); // expected-note{{Started tracking value here}}
Expand Down

0 comments on commit cb5a359

Please sign in to comment.