Thanks to visit codestin.com
Credit goes to github.com

Skip to content

withHostTargetMachine not thread safe #414

@guibou

Description

@guibou

https://hackage.haskell.org/package/llvm-hs-9.0.1/docs/LLVM-Target.html#v:withHostTargetMachine is unsafe in a threading context. It uses initializeAllTargets which is, according to llvm documentation, supposed to be in the "main" thread.

I'm unsure about the implementation, but it looks like there is an internal structure (later read by lookupTarget and related functions) which is unsafe to read and initialize at the same time.

I'm unsure what can be done in llvm-hs, either adding a lock, or documenting that it is unsafe.

Here, the concurrent call of initializeAllTargets as well as lookupTarget leads to an infinite loop in lookupTarget eating 100% CPU according to perf top:

  74.07%  libLLVM-9.so                   [.] llvm::TargetRegistry::lookupTarget                                                                                                                                                                
  15.06%  libLLVM-9.so                   [.] llvm::RegisterTarget<(llvm::Triple::ArchType)48, false>::getArchMatch  

Note that I found this problem while using the alternate initializeAllTargets implementation from #263

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions