Repository: angr/angr

Issue:
Exception when importing angr in binary ninja
I tried importing angr in the Binary Ninja Python Shell and got this traceback
```
>>> import angr
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/user/.virtualenvs/angr/lib/python2.7/site-packages/angr/__init__.py", line 26, in <module>
    from .project import *
  File "/home/user/.virtualenvs/angr/lib/python2.7/site-packages/angr/project.py", line 603, in <module>
    from .analyses.analysis import Analyses
  File "/home/user/.virtualenvs/angr/lib/python2.7/site-packages/angr/analyses/__init__.py", line 8, in <module>
    from .cfg import CFGFast, CFGAccurate, CFG, CFGArchOptions
  File "/home/user/.virtualenvs/angr/lib/python2.7/site-packages/angr/analyses/cfg/__init__.py", line 3, in <module>
    from .cfg_fast import CFGFast
  File "/home/user/.virtualenvs/angr/lib/python2.7/site-packages/angr/analyses/cfg/cfg_fast.py", line 19, in <module>
    from .indirect_jump_resolvers.default_resolvers import default_indirect_jump_resolvers
  File "/home/user/.virtualenvs/angr/lib/python2.7/site-packages/angr/analyses/cfg/indirect_jump_resolvers/__init__.py", line 2, in <module>
    from .mips_elf_fast import MipsElfFastResolver
  File "/home/user/.virtualenvs/angr/lib/python2.7/site-packages/angr/analyses/cfg/indirect_jump_resolvers/mips_elf_fast.py", line 11, in <module>
    from ....surveyors import Slicecutor
  File "/home/user/.virtualenvs/angr/lib/python2.7/site-packages/angr/surveyors/__init__.py", line 3, in <module>
    from .surveyor import Surveyor, Surveyors
  File "/home/user/.virtualenvs/angr/lib/python2.7/site-packages/angr/surveyors/surveyor.py", line 46, in <module>
    signal.signal(signal.SIGUSR1, handler)
ValueError: signal only works in main thread
```

Fix would probably just be catching this exception too (currently only AttributeError is expected) to decide that SIGUSR1 isn't supported


Discussion:
We really need to get rid of surveyors...

A PR that wraps that signal call in a try/catch would be really appreciated!

- Yan

On Wed, May 16, 2018 at 2:29 PM Florian Magin <notifications@github.com>
wrote:

> I tried importing angr in the Binary Ninja Python Shell and got this
> traceback
>
> >>> import angr
> Traceback (most recent call last):
>   File "<console>", line 1, in <module>
>   File "/home/user/.virtualenvs/angr/lib/python2.7/site-packages/angr/__init__.py", line 26, in <module>
>     from .project import *
>   File "/home/user/.virtualenvs/angr/lib/python2.7/site-packages/angr/project.py", line 603, in <module>
>     from .analyses.analysis import Analyses
>   File "/home/user/.virtualenvs/angr/lib/python2.7/site-packages/angr/analyses/__init__.py", line 8, in <module>
>     from .cfg import CFGFast, CFGAccurate, CFG, CFGArchOptions
>   File "/home/user/.virtualenvs/angr/lib/python2.7/site-packages/angr/analyses/cfg/__init__.py", line 3, in <module>
>     from .cfg_fast import CFGFast
>   File "/home/user/.virtualenvs/angr/lib/python2.7/site-packages/angr/analyses/cfg/cfg_fast.py", line 19, in <module>
>     from .indirect_jump_resolvers.default_resolvers import default_indirect_jump_resolvers
>   File "/home/user/.virtualenvs/angr/lib/python2.7/site-packages/angr/analyses/cfg/indirect_jump_resolvers/__init__.py", line 2, in <module>
>     from .mips_elf_fast import MipsElfFastResolver
>   File "/home/user/.virtualenvs/angr/lib/python2.7/site-packages/angr/analyses/cfg/indirect_jump_resolvers/mips_elf_fast.py", line 11, in <module>
>     from ....surveyors import Slicecutor
>   File "/home/user/.virtualenvs/angr/lib/python2.7/site-packages/angr/surveyors/__init__.py", line 3, in <module>
>     from .surveyor import Surveyor, Surveyors
>   File "/home/user/.virtualenvs/angr/lib/python2.7/site-packages/angr/surveyors/surveyor.py", line 46, in <module>
>     signal.signal(signal.SIGUSR1, handler)
> ValueError: signal only works in main thread
>
> Fix would probably just be catching this exception too (currently only
> AttributeError is expected) to decide that SIGUSR1 isn't supported
>
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly, view it on GitHub
> <https://github.com/angr/angr/issues/994>, or mute the thread
> <https://github.com/notifications/unsubscribe-auth/ADSzlzntHQexdb9lDOKX53hp8i2Y0YL_ks5tzJofgaJpZM4UCEZy>
> .
>

It is already wrapped in a try catch call, but it doesn't expect _this_ exception.
I talked to @ltfish, the point of this issue was mainly so the functionality would be fixed when surveyors are dropped and this is moved elsewhere.
At least as far as I understood
> A PR that wraps that signal call in a try/catch would be really appreciated!

Surveyors will be ditched as soon as I have some time. But the same functionality will be implemented in simgr/exploration techniques, so I asked him to create this issue to track the problem.

Resolving patch:
diff --git a/angr/surveyors/surveyor.py b/angr/surveyors/surveyor.py
--- a/angr/surveyors/surveyor.py
+++ b/angr/surveyors/surveyor.py
@@ -45,6 +45,8 @@ def handler(signum, frame):  # pylint: disable=W0613,
     signal.signal(signal.SIGUSR2, handler)
 except AttributeError:
     l.info("Platform doesn't support SIGUSR")
+except ValueError:
+    l.info("angr imported from non-main thread")
 
 # function that produces unpredictable results that should appease pylint's
 # static analysis and stop giving us those awful errors!!!!


Test patch: