pyre-check icon indicating copy to clipboard operation
pyre-check copied to clipboard

Theory of abstract interpretation missing

Open njayinthehouse opened this issue 7 years ago • 9 comments

From the CONTRIBUTING.md : "The Abstract Interpretation section provides a theoretical background for the analysis."

I can't seem to find the referenced section. A quick grep of the repo doesn't find it, either. Could you please point me to the right place? :)

njayinthehouse avatar Jan 31 '19 09:01 njayinthehouse

Thanks for the report, I will see what that page should point to.

dark avatar Jan 31 '19 17:01 dark

Hi, any updates on this? I'm hoping to tweak a fork of this project for a pet project of mine, and it would easier to read the theory for analysis before trying to understand the code.

njayinthehouse avatar Feb 19 '19 08:02 njayinthehouse

@njayinthehouse: the documentation there is broken :( Will try to update. In the meantime I can point you to a few other sources:

Wikipedia: https://en.wikipedia.org/wiki/Abstract_interpretation Original paper (not the easiest read): https://www.di.ens.fr/~cousot/COUSOTpapers/publications.www/CousotCousot-POPL-77-ACM-p238--252-1977.pdf Slides: http://santos.cs.ksu.edu/schmidt/Escuela03/WSSA/talk1p.pdf

dkgi avatar Feb 19 '19 18:02 dkgi

@dkgi I'm interested in this as well; Thanks for the pointers on abstract interpretation in general. I'm aware of the general theory of AI. What I'm missing is the particular instantiation of abstract interpretation that's being used in Pyre --- what's the abstract domain, what are the transfer functions, what's the precise concrete domain and the concretization function? Any links on the particulars would be great :)

bollu avatar Feb 19 '19 20:02 bollu

@bollu: we simply call our abstract domains State. The module type for this can be found here: https://github.com/facebook/pyre-check/blob/d2176f243247537993f133a0d8ebcde47e28d57e/analysis/fixpoint.mli#L11-L35.

For type checking our main abstraction is a mapping from variable names to their types (hidden in the unfortunately named resolution object: https://github.com/facebook/pyre-check/blob/d2176f243247537993f133a0d8ebcde47e28d57e/analysis/typeCheck.ml#L22). The transfer functions for forward and backward analysis are called forward and backward respectively.

A good, self-contained example to start looking into might be our check for finding unawaited awaitables: https://github.com/facebook/pyre-check/blob/d2176f243247537993f133a0d8ebcde47e28d57e/analysis/awaitableCheck.ml. Note that the domain is not fully functional yet (we're not yet using this) but that has the advantage of making it easier to understand. Also have a look at the corresponding tests (https://github.com/facebook/pyre-check/blob/d2176f243247537993f133a0d8ebcde47e28d57e/analysis/test/awaitableCheckTest.ml) to see the expected behavior.

Note that we have a separate interface for the interprocedural analysis. I'm hoping we'll eventually unify that.

dkgi avatar Feb 19 '19 22:02 dkgi

There are also a set of implementations/interfaces of basic abstract domains that we use in the taint analysis here:

https://github.com/facebook/pyre-check/blob/5ffdae13f78ca731336ddc25f46a4ddfc2866e54/analysis/abstractDomain.ml#L19-L38 and the files abstractXXXDomain.ml

fahndrich avatar May 04 '19 03:05 fahndrich

Also, you can look at the code in taint/domains.ml where the general AI functors are instantiated. Basically, taint is represented as a set of source/sink kinds and the heap is represented as access path trees (assuming no aliasing). The top-level environment is mapping locals to access path trees.

It's tricky to write down proper abstraction/concretization functions, because you 1) needs an instrumented semantics that tracks taint, 2) our analysis is not sound, so we take shortcuts.

Happy to answer concrete questions though.

fahndrich avatar May 22 '19 04:05 fahndrich

I'm plotting out how I'd implement taint analysis in my own very basic tool. I've managed to copy a few algorithms from hh_check's OCaml into my PHP project previously, and I'm dumbly optimistic that I might be able to do the same here.

Has abstract interpretation already been done at this point, or is it only performed on functions as needed by the analysis (taint, awaitable etc.)?

muglug avatar Jul 31 '19 16:07 muglug

@muglug "abstract interpretation" describes the full set of operations: the domains that describe the abstraction we're interested in (e.g. types of variables, taint of variables, whether have type Awaitable and have been awaited); the transfer function which describes how our abstraction changes when we simulate executing a statement; and the fixpoint computation solving a set of equations we get from our domain and the transfer function.

We have two fixpoints. One that we run on every single function (the one you linked to above) and stitches together the aforementioned parts to compute a high-level description of the function (set of errors for type checking and awaitable checks, taint model for taint checking).

We compute another fixpoint for the taint analysis that computes a stable state over all functions. E.g. we might analyze a function that depends on another function for which we don't yet have a model (think recursive calls). In this case we'll have to compute a model for the function making some assumptions and then recompute that model later on. We do this until none of our models change anymore.

Hope that helps.

dkgi avatar Aug 03 '19 22:08 dkgi