Skip to content

Commit

Permalink
fix ASAN issues
Browse files Browse the repository at this point in the history
  • Loading branch information
vtjnash committed Oct 18, 2024
1 parent d4956ef commit 557f16f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 25 deletions.
14 changes: 7 additions & 7 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1610,7 +1610,7 @@ static const auto jltuple_func = new JuliaFunction<>{XSTR(jl_f_tuple), get_func_
static const auto jlintrinsic_func = new JuliaFunction<>{XSTR(jl_f_intrinsic_call), get_func3_sig, get_func_attrs};

static const auto &builtin_func_map() {
static std::map<jl_fptr_args_t, JuliaFunction<>*> builtins = {
static auto builtins = new DenseMap<jl_fptr_args_t, JuliaFunction<>*> {
{ jl_f_is_addr, new JuliaFunction<>{XSTR(jl_f_is), get_func_sig, get_func_attrs} },
{ jl_f_typeof_addr, new JuliaFunction<>{XSTR(jl_f_typeof), get_func_sig, get_func_attrs} },
{ jl_f_sizeof_addr, new JuliaFunction<>{XSTR(jl_f_sizeof), get_func_sig, get_func_attrs} },
Expand Down Expand Up @@ -1653,18 +1653,18 @@ static const auto &builtin_func_map() {
{ jl_f__svec_ref_addr, new JuliaFunction<>{XSTR(jl_f__svec_ref), get_func_sig, get_func_attrs} },
{ jl_f_current_scope_addr, new JuliaFunction<>{XSTR(jl_f_current_scope), get_func_sig, get_func_attrs} },
};
return builtins;
return *builtins;
}

static const auto &may_dispatch_builtins() {
static std::unordered_set<jl_fptr_args_t> builtins(
static auto builtins = new DenseSet<jl_fptr_args_t>(
{jl_f__apply_iterate_addr,
jl_f__apply_pure_addr,
jl_f__call_in_world_addr,
jl_f__call_in_world_total_addr,
jl_f__call_latest_addr,
});
return builtins;
return *builtins;
}

static const auto jl_new_opaque_closure_jlcall_func = new JuliaFunction<>{XSTR(jl_new_opaque_closure_jlcall), get_func_sig, get_func_attrs};
Expand Down Expand Up @@ -10138,14 +10138,14 @@ jl_llvm_functions_t jl_emit_codeinst(
}

// --- initialization ---
SmallVector<std::pair<jl_value_t**, JuliaVariable*>, 0> gv_for_global;
static auto gv_for_global = new SmallVector<std::pair<jl_value_t**, JuliaVariable*>, 0>();
static void global_jlvalue_to_llvm(JuliaVariable *var, jl_value_t **addr)
{
gv_for_global.push_back(std::make_pair(addr, var));
gv_for_global->push_back(std::make_pair(addr, var));
}
static JuliaVariable *julia_const_gv(jl_value_t *val)
{
for (auto &kv : gv_for_global) {
for (auto &kv : *gv_for_global) {
if (*kv.first == val)
return kv.second;
}
Expand Down
34 changes: 21 additions & 13 deletions src/jitlayers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1586,7 +1586,8 @@ namespace {
size_t PoolIdx;
if (auto opt_level = M.getModuleFlag("julia.optlevel")) {
PoolIdx = cast<ConstantInt>(cast<ConstantAsMetadata>(opt_level)->getValue())->getZExtValue();
} else {
}
else {
PoolIdx = jl_options.opt_level;
}
assert(PoolIdx < N && "Invalid optimization level for compiler!");
Expand Down Expand Up @@ -1893,10 +1894,6 @@ llvm::DataLayout jl_create_datalayout(TargetMachine &TM) {
return jl_data_layout;
}

#ifdef _COMPILER_ASAN_ENABLED_
int64_t ___asan_globals_registered;
#endif

JuliaOJIT::JuliaOJIT()
: TM(createTargetMachine()),
DL(jl_create_datalayout(*TM)),
Expand Down Expand Up @@ -2070,15 +2067,21 @@ JuliaOJIT::JuliaOJIT()
#endif
cantFail(GlobalJD.define(orc::absoluteSymbols(msan_crt)));
#endif
#if JL_LLVM_VERSION < 190000
#ifdef _COMPILER_ASAN_ENABLED_
// this is a hack to work around a bad assertion:
// /workspace/srcdir/llvm-project/llvm/lib/ExecutionEngine/Orc/Core.cpp:3028: llvm::Error llvm::orc::ExecutionSession::OL_notifyResolved(llvm::orc::MaterializationResponsibility&, const SymbolMap&): Assertion `(KV.second.getFlags() & ~JITSymbolFlags::Common) == (I->second & ~JITSymbolFlags::Common) && "Resolving symbol with incorrect flags"' failed.
// hopefully fixed upstream by e7698a13e319a9919af04d3d693a6f6ea7168a44
static int64_t jl___asan_globals_registered;
orc::SymbolMap asan_crt;
#if JL_LLVM_VERSION >= 170000
asan_crt[mangle("___asan_globals_registered")] = {ExecutorAddr::fromPtr(&___asan_globals_registered), JITSymbolFlags::Exported};
asan_crt[mangle("___asan_globals_registered")] = {ExecutorAddr::fromPtr(&jl___asan_globals_registered), JITSymbolFlags::Common | JITSymbolFlags::Exported};
#else
asan_crt[mangle("___asan_globals_registered")] = JITEvaluatedSymbol::fromPointer(&___asan_globals_registered, JITSymbolFlags::Exported);
asan_crt[mangle("___asan_globals_registered")] = JITEvaluatedSymbol::fromPointer(&jl___asan_globals_registered, JITSymbolFlags::Common | JITSymbolFlags::Exported);
#endif
cantFail(JD.define(orc::absoluteSymbols(asan_crt)));
#endif
#endif
}

JuliaOJIT::~JuliaOJIT() = default;
Expand Down Expand Up @@ -2136,8 +2139,7 @@ void JuliaOJIT::addModule(orc::ThreadSafeModule TSM)

Error JuliaOJIT::addExternalModule(orc::JITDylib &JD, orc::ThreadSafeModule TSM, bool ShouldOptimize)
{
if (auto Err = TSM.withModuleDo([&](Module &M) JL_NOTSAFEPOINT -> Error
{
if (auto Err = TSM.withModuleDo([&](Module &M) JL_NOTSAFEPOINT -> Error {
if (M.getDataLayout().isDefault())
M.setDataLayout(DL);
if (M.getDataLayout() != DL)
Expand All @@ -2146,9 +2148,10 @@ Error JuliaOJIT::addExternalModule(orc::JITDylib &JD, orc::ThreadSafeModule TSM,
M.getDataLayout().getStringRepresentation() + " (module) vs " +
DL.getStringRepresentation() + " (jit)",
inconvertibleErrorCode());

// OrcJIT requires that all modules / files have unique names:
M.setModuleIdentifier((M.getModuleIdentifier() + Twine("-") + Twine(jl_atomic_fetch_add_relaxed(&jitcounter, 1))).str());
return Error::success();
}))
}))
return Err;
//if (ShouldOptimize)
// return OptimizeLayer.add(JD, std::move(TSM));
Expand All @@ -2157,6 +2160,11 @@ Error JuliaOJIT::addExternalModule(orc::JITDylib &JD, orc::ThreadSafeModule TSM,

Error JuliaOJIT::addObjectFile(orc::JITDylib &JD, std::unique_ptr<MemoryBuffer> Obj) {
assert(Obj && "Can not add null object");
// OrcJIT requires that all modules / files have unique names:
// https://llvm.org/doxygen/namespacellvm_1_1orc.html#a1f5a1bc60c220cdccbab0f26b2a425e1
// so we have to force a copy here
std::string Name = ("jitted-" + Twine(jl_atomic_fetch_add_relaxed(&jitcounter, 1))).str();
Obj = Obj->getMemBufferCopy(Obj->getBuffer(), Name);
return ObjectLayer.add(JD.getDefaultResourceTracker(), std::move(Obj));
}

Expand Down Expand Up @@ -2402,7 +2410,7 @@ std::string JuliaOJIT::getMangledName(const GlobalValue *GV)

size_t JuliaOJIT::getTotalBytes() const
{
auto bytes = jit_bytes_size.load(std::memory_order_relaxed);
auto bytes = jl_atomic_load_relaxed(&jit_bytes_size);
#ifndef JL_USE_JITLINK
size_t getRTDyldMemoryManagerTotalBytes(RTDyldMemoryManager *mm) JL_NOTSAFEPOINT;
bytes += getRTDyldMemoryManagerTotalBytes(MemMgr.get());
Expand All @@ -2412,7 +2420,7 @@ size_t JuliaOJIT::getTotalBytes() const

void JuliaOJIT::addBytes(size_t bytes)
{
jit_bytes_size.fetch_add(bytes, std::memory_order_relaxed);
jl_atomic_fetch_add_relaxed(&jit_bytes_size, bytes);
}

void JuliaOJIT::printTimers()
Expand Down
3 changes: 2 additions & 1 deletion src/jitlayers.h
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,8 @@ class JuliaOJIT {
std::mutex llvm_printing_mutex{};
SmallVector<std::function<void()>, 0> PrintLLVMTimers;

std::atomic<size_t> jit_bytes_size{0};
_Atomic(size_t) jit_bytes_size{0};
_Atomic(size_t) jitcounter{0};
#ifdef JL_USE_JITLINK
const std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr;
ObjLayerT ObjectLayer;
Expand Down
4 changes: 2 additions & 2 deletions src/julia_atomics.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ enum jl_memory_order {
// this wrong thus we include the correct definitions here (with implicit
// conversion), instead of using the macro version
template<class T>
T jl_atomic_load(std::atomic<T> *ptr)
T jl_atomic_load(const std::atomic<T> *ptr)
{
return std::atomic_load<T>(ptr);
}
template<class T>
T jl_atomic_load_explicit(std::atomic<T> *ptr, std::memory_order order)
T jl_atomic_load_explicit(const std::atomic<T> *ptr, std::memory_order order)
{
return std::atomic_load_explicit<T>(ptr, order);
}
Expand Down
4 changes: 2 additions & 2 deletions src/stackwalk.c
Original file line number Diff line number Diff line change
Expand Up @@ -642,13 +642,13 @@ void jl_print_native_codeloc(uintptr_t ip) JL_NOTSAFEPOINT
for (i = 0; i < n; i++) {
jl_frame_t frame = frames[i];
if (!frame.func_name) {
jl_safe_printf("unknown function (ip: %p)\n", (void*)ip);
jl_safe_printf("unknown function (ip: %p) at %s\n", (void*)ip, frame.file_name ? frame.file_name : "(unknown file)");
}
else {
jl_safe_print_codeloc(frame.func_name, frame.file_name, frame.line, frame.inlined);
free(frame.func_name);
free(frame.file_name);
}
free(frame.file_name);
}
free(frames);
}
Expand Down

0 comments on commit 557f16f

Please sign in to comment.