clang-randstruct / llvm-project

Randomize the order of fields in a structure layout as a compile-time hardening feature
3 stars 1 forks source link

Implement new attributes in the Clang compiler #2

Closed connorkuehl closed 5 years ago

connorkuehl commented 5 years ago

How do we add support for things like __randomize_layout and __no_randomize_layout?

connorkuehl commented 5 years ago

I found this in the Compiler Internals Manual. I haven't found an appropriate analog from the context of the Clang Plugin API.

nforbus commented 5 years ago

So we already have randstruct registered randstruct with the FrontendPluginRegistry, so we can call our plugin from command line via -plugin randstruct, is that correct?

So at this point what we need is a way for the plugin to be called from structs declared with a calling component, and attributes won't work because we're not going to modify the attr.td file (which is a route we would go if we were not doing a plugin).

Currently looking at other plugins to see how they manage this, nothing so far though.

tim-pugh commented 5 years ago

@jeffreytakahashi any progress on this?

Nixoncole commented 5 years ago

This is a lead from March 2017 that makes me believe we cannot add custom attributes without directly editing Clang. http://clang-developers.42468.n3.nabble.com/User-defined-attributes-in-clang-plugins-td4055940.html

This is a blog post that (kind of?) outlines using the clang attribute "annotate", to create a custom annotation that we can pull off at the "bitcode level": https://blog.quarkslab.com/implementing-a-custom-directive-handler-in-clang.html - I want to speak with ya'll about this tomorrow, because I don't have enough contextual knowledge to understand if we can use this.

Official docs for the llvm "intrinsic" leveraged by the blog post. I dont feel like it provided me with much information, but maybe someone can connect the dots? - http://llvm.org/docs/LangRef.html#llvm-var-annotation-intrinsic

jeffreytakahashi commented 5 years ago

@tim-pugh See above post from Cole, we're really struggling with this.

tim-pugh commented 5 years ago

https://stackoverflow.com/questions/4736738/how-can-you-add-a-new-keyword-to-clang-a-keyword-that-would-be-treated-as-main

My idea of an alternative wouldn't fix anything (it would make it more complicated actually). It would be adding a keyword to clang which they state can be more error prone.

tim-pugh commented 5 years ago

https://clang.llvm.org/docs/ClangPlugins.html

in here you can read a section talking about "pragmas". I'm really not sure if using this would be the correct approach, but it talks about registering them with the clang compiler via a plugin.

jeffreytakahashi commented 5 years ago

So after a lot of reading and a bit of poking we can add annotations to both variables and functions such that they show up in llvm metadata, however they do not when assigned to structs. In either case, if we are reading this correctly (and we would really like someone to verify that we are). I will post screenshots soon, and we will get some links up soon, but it appears that even using attribute((packed)) does not show up in the bytecode so we're not sure if it's just there in a way we cannot see.

jeffreytakahashi commented 5 years ago
screen shot 2019-01-27 at 9 28 22 pm screen shot 2019-01-27 at 9 26 17 pm
jeffreytakahashi commented 5 years ago

http://bholt.org/posts/llvm-quick-tricks.html

Nixoncole commented 5 years ago

Found a half-baked explanation for parsing the metadata: https://stackoverflow.com/questions/15114284/llvm-get-annotations

jeffreytakahashi commented 5 years ago

Now discussing possibly attempting to actually get the attributes we needed upstreamed.

jeffreytakahashi commented 5 years ago

Here is an example of an attribute in clang that applies to struct/class/union as a starting point:

https://github.com/llvm-mirror/clang/search?q=empty_bases&unscoped_q=empty_bases https://clang.llvm.org/docs/AttributeReference.html#empty-bases

Per some other reading, boilerplate might be relevant to future research in order to implement this properly: https://clang.llvm.org/docs/InternalsManual.html#boilerplate

connorkuehl commented 5 years ago

Once we have our attributes based on the research and discussion here, there are separate issues for integrating them into the plugin with their corresponding behaviors. #98 #97

connorkuehl commented 5 years ago

Looks like they've updated their Getting Started page with instructions for working with their official git repo due to their migration! Yay! :confetti_ball:

Once our fork of Clang has the required attributes we need, let's prepare the patch and disseminate it to the team so that we can implement manual structure selection as part of our MVP ASAP.

https://llvm.org/docs/GettingStarted.html#sending-patches

We'll also then be able to prepare the e-mail for the mailing list, get Bart's sign off, then send it off to the clang commits mailing list.

tim-pugh commented 5 years ago

I've fired off a email to the sponsor letting him know the game plan. You can find the email in the google team drive.

tim-pugh commented 5 years ago

This guide may be beneficial to read as well:

https://llvm.org/docs/DeveloperPolicy.html

I have the contact at Intel I can reach out to once more which I suggest we do. He may be able to shed some light on the process and help guide us.

tim-pugh commented 5 years ago

We've got the instructions to build LLVM (Linux based systems) here:

https://docs.google.com/document/d/1uJjruksHppqdo3GA2fv3OsyIJpXW_hALRvKC-2oWXSo/edit

We've attempted to add the attribute, but working on this yesterday the build failed.

Connor mentioned sending a patch out. In the meantime I'll help people get things setup on their machines.

I suspect there is a repo waiting for us to commit to, but will need to discuss this with other team members. This will likely be the method we go to receive the patch, after some git fu changing the origin.

connorkuehl commented 5 years ago

Closed by the following commits:

3b9829ee86f311ab232f8cbb49cf5b5e7fcd70ef f8da259829202a755af6c788d24fd049c3e65950