Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Frida Refactor: Split FridaHelper into each Runtime #368

Merged
merged 16 commits into from
Nov 16, 2021
Merged

Conversation

tokatoka
Copy link
Member

@tokatoka tokatoka commented Nov 8, 2021

This pr includes several frida refactoring
#307

  • Split coverage runtime
  • Test builing for aarch64
  • Clean up

@tokatoka
Copy link
Member Author

tokatoka commented Nov 8, 2021

I think I should split helper, and make a CoverageRuntime
just like AsanRuntime, CmpLogRuntime

@tokatoka tokatoka changed the title Dynasm maybe_log Frida Refactor: Split CoverageRuntime from FridaHelper Nov 8, 2021
fn generate_maybe_log_blob(&mut self) -> Box<[u8]> {
macro_rules! maybe_log{
($ops:ident) => {dynasm!($ops
; .arch x64
Copy link
Collaborator

@s1341 s1341 Nov 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don’t really need the macro, do we?

Copy link
Member Author

@tokatoka tokatoka Nov 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No 😉 we don't need macro
I plan to move this to CoverageRuntime.

@s1341
Copy link
Collaborator

s1341 commented Nov 8, 2021

Yes splitting out CoverageRuntime is a good idea. It might also be worth moving the is_interesting functions out of helper into their various Runtimes.

@s1341
Copy link
Collaborator

s1341 commented Nov 8, 2021

Also it doesn’t look like you used your new function anywhere, did you? It should be called only once and the result cached.

@tokatoka
Copy link
Member Author

tokatoka commented Nov 8, 2021

no, I can't use it yet.
that's why it's still a draft.
At first I tried to call this from FridaHelper->new(), but this does not look good because we can't call self.generate_maybe_log_blob to slot the blob into self in new().
(what I can do is like calling FridaInstrumentationHelper::<'a>::generate_maybe_log_blob(&mut helper) but that doesn't look nice)
To achieve this, I find it's nicer to create a CoverageRuntime and we can call that generate-blob function in something like CoverageRuntime->init()

@s1341
Copy link
Collaborator

s1341 commented Nov 8, 2021

Cool. Makes sense. I’ll let you work. ;)

@tokatoka
Copy link
Member Author

tokatoka commented Nov 9, 2021

This change already works on amd64
i'm gonna rewrite the aarch64 part and test building for aarch64 tomorrow
(I hope we have an android ci tbh...)

@tokatoka tokatoka marked this pull request as ready for review November 9, 2021 13:58
@tokatoka
Copy link
Member Author

tokatoka commented Nov 9, 2021

how about moving hook_funcs to another file like asan_func_hooks.rs? @s1341
currently it is just... toooooo long

@tokatoka tokatoka changed the title Frida Refactor: Split CoverageRuntime from FridaHelper Frida Refactor: Split FridaHelper into each Runtime Nov 9, 2021
@s1341
Copy link
Collaborator

s1341 commented Nov 9, 2021

Good idea. Do you want to do it in this PR or in another?

@tokatoka
Copy link
Member Author

tokatoka commented Nov 9, 2021

The fix would be simple. I would do it in this pr.

@s1341
Copy link
Collaborator

s1341 commented Nov 9, 2021

Ok. Go ahead.

@s1341
Copy link
Collaborator

s1341 commented Nov 9, 2021

Let me know when you want me to test on aarch64.

@tokatoka
Copy link
Member Author

I come to feel that I should do these in seperate prs...

@tokatoka
Copy link
Member Author

so that I can track down my mistakes if I broke something.

@tokatoka
Copy link
Member Author

tokatoka commented Nov 10, 2021

I checked building for aarch64.

@tokatoka tokatoka requested a review from s1341 November 10, 2021 16:39
@tokatoka
Copy link
Member Author

I would like to get this merged and work on remaining issues in other prs

Copy link
Collaborator

@s1341 s1341 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few nitpicks... but otherwise lgtm!


pub struct CoverageRuntime {
map: [u8; MAP_SIZE],
previous_pc: [u64; 1],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this an array?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was an array before ;)
but i'll fix

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Didn't realize... Yeah if you can fix...

self.generate_maybe_log_blob();
}

pub fn map_ptr(&mut self) -> *mut u8 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be map_ptr_mut to stick to the normal rust conventions?

; ldr x2, #0x38
; ldr x4, [x2]
; eor x4, x4, x0
; and x4, x4, 0xffff
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be MAP_SIZE - 1 instead of 0xffff

; stp x1, x2, [sp, -0x10]!
; stp x3, x4, [sp, -0x10]!
; ldr x1, >map_addr
; ldr x2, #0x38
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be >previous_loc

@tokatoka
Copy link
Member Author

I fixed all, but didn't test how aarch64 build goes with the change.

@s1341
Copy link
Collaborator

s1341 commented Nov 11, 2021

I fixed a minor issue which was breaking aarch64 compilation, but running on aarch64 blows up for some reason. I'm debugging now.

@domenukk
Copy link
Member

Can you maybe try to add the build to CI whole you do?

@s1341
Copy link
Collaborator

s1341 commented Nov 11, 2021

It requires adding/installing an ndk... which I don't know how to do under CI in a way that won't be very time-expensive.

@domenukk
Copy link
Member

We already build parts of it, see

run: cargo ndk -t arm64-v8a build --release

@s1341
Copy link
Collaborator

s1341 commented Nov 11, 2021

Hrm. I'll try to look into it.

@tokatoka
Copy link
Member Author

tokatoka commented Nov 11, 2021

I fixed a minor issue which was breaking aarch64 compilation, but running on aarch64 blows up for some reason. I'm debugging now.

I observe that arm64 run is broken on main branch too. (with segmentation fault)

@tokatoka
Copy link
Member Author

mine fails at this

Thread 3.1 "frida_libpng" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xfffff7c03430 (LWP 41363)]
0x0000aaaaaad30880 in nix::sys::mman::mmap (addr=0x400000000000, length=<optimized out>, prot=..., flags=..., fd=-1, offset=0)
    at /home/ubuntu/.cargo/registry/src/github.com-1ecc6299db9ec823/nix-0.23.0/src/sys/mman.rs:339
339         let ret = libc::mmap(addr, length, prot.bits(), flags.bits(), fd, offset);

but I'm not turning asan on.... 🤔

@s1341
Copy link
Collaborator

s1341 commented Nov 11, 2021

I think the coverage is broken. Haven't had a moment to debug today... Hopefully will get to it a little later.

@tokatoka
Copy link
Member Author

I confirmed that this error already occurs back at f0daeb3 this commit, (just before amd64 asan)

@s1341
Copy link
Collaborator

s1341 commented Nov 11, 2021

Is it possible that the machine you are running on doesn't have memory over-commit?

@tokatoka
Copy link
Member Author

Oh, ... it is possible.
It is just a potato aws instance...
ok, so my comments would be unrelated, sorry.

@domenukk
Copy link
Member

I don't think it's possible, they should be normal Linux OSses with normal configuration.

BTW @tokatoka in case you are spawning instances mainly for libafl dev, let us know, I'm sure we have some leftover gsoc funds for that or something :)

@tokatoka
Copy link
Member Author

it's ok.
yesterday I used that for like 2 hours but just costed me 10 jpy (like less than 0.1 euro)

@andreafioraldi
Copy link
Member

Should I merge?

@tokatoka
Copy link
Member Author

no aarch64 run is broken, let's wait s1341

@domenukk domenukk requested a review from s1341 November 14, 2021 21:42
@domenukk domenukk merged commit c7512fc into main Nov 16, 2021
@tokatoka tokatoka deleted the dynasm_coverage branch November 16, 2021 19:50
khang06 pushed a commit to khang06/LibAFL that referenced this pull request Oct 11, 2022
* dynasm maybe_log

* create coverage_rt, trim helper

* add

* amd64 working

* aarch64 instrumentation, untested

* asan dir

* Revert "asan dir"

This reverts commit c7afc78.

* non x86_64 fix

* clippy

* change

* change

* fix

* Fix aarch64-linux-android build

* Fix aarch64 execution

* Fix fmt

Co-authored-by: s1341 <github@shmarya.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants