Skip to content

librasan: Simplify assembly patches #3192

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

wfdewith
Copy link
Contributor

@wfdewith wfdewith commented May 3, 2025

Description

@domenukk as per your comment in #3176, I've simplified the assembly patches a bit to remove the Vec usage. The rustfmt::skip attributes are there to keep lines matching with the assembly code in the comments.

Checklist

  • I have run ./scripts/precommit.sh and addressed all comments

@wfdewith wfdewith force-pushed the assembly branch 2 times, most recently from bbcb655 to 472b5b3 Compare May 3, 2025 05:52
@WorksButNotTested
Copy link
Collaborator

Nice. Let's land this.

[0xff, 0xe0].to_vec(),
let addr = destination.to_ne_bytes();
#[rustfmt::skip]
let insns: &[&[u8]] = &[
Copy link
Member

Choose a reason for hiding this comment

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

Random question @WorksButNotTested can't we also do dynasm here like we do in libafl_frida?

fn generate_instrumentation_blobs(&mut self) {

Would be more maintable than pasting random bytes, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's pretty nice actually, but dynasm doesn't support ARMv7 and PowerPC. We can migrate the supported architectures if you'd like.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah. Nobody loves powerpc! Just went with it as it was the lowest common denominator. Happy if you want to port the others though.

@wfdewith
Copy link
Contributor Author

wfdewith commented May 4, 2025

I've added dynasm for the architectures that it supports.

@wfdewith
Copy link
Contributor Author

wfdewith commented May 4, 2025

Actually, scratch that, dynasm requires std, so we cannot use it, unfortunately.

@WorksButNotTested
Copy link
Collaborator

Oh weird. You'd think it would so all its work at compile time! Some crates only support nostd with the default features disabled, does that help at all?

@wfdewith
Copy link
Contributor Author

wfdewith commented May 4, 2025

Dynasm needs some synchronization primitives for the Assembler that also manages mmap-ing memory with the correct permissions etc, when you use it for building JIT compilers. In theory, it could have a no_std subset with the VecAssembler (which just assembles into a Vec) that this PR uses. However, even just VecAssembler would also need some alternative HashMap implementation for no_std environments. So no, at the moment there is no no_std feature, and it's not entirely trivial to add.

@WorksButNotTested
Copy link
Collaborator

BTreeMap is part of core and is a good substitute for HashMap for most usages. That could be an option?

@wfdewith
Copy link
Contributor Author

wfdewith commented May 4, 2025

There is an open PR already: CensoredUsername/dynasm-rs#105.

@domenukk
Copy link
Member

domenukk commented May 5, 2025

It's been idle for quite a while though, sadly. Let's see if they react to the latest comments :)

@WorksButNotTested
Copy link
Collaborator

There's no alternative crates we can use are there?

@wfdewith
Copy link
Contributor Author

wfdewith commented May 5, 2025

There's no alternative crates we can use are there?

In my opinion, if that nostd PR in dynasm doesn't land soon, we should just revert to using the raw bytes, because I expect very little churn in these assembly trampolines. I don't expect dynasm to start supporting new architectures anytime soon, considering the fair amount of complexity in a full fledged assembler, so we need a fallback anyway.

@domenukk
Copy link
Member

domenukk commented May 5, 2025

Yes let's ignore the move to dynasm for now and maybe keep an eye on their no_std support.
It'll be more maintainable going forward, but there is no reason it should block our PR right now

@domenukk
Copy link
Member

domenukk commented May 5, 2025

We could hack together something that emits the right bytes at compile time (build.rs or our own proc macro) and uses a different assembler (or even dynasm) if we wanted

@wfdewith
Copy link
Contributor Author

wfdewith commented May 5, 2025

We could hack together something that emits the right bytes at compile time (build.rs or our own proc macro) and uses a different assembler (or even dynasm) if we wanted

We could, 'easiest' way would probably to use the correct as and objcopy for each architecture. Unfortunately, the cc crate only exposes the C/C++ compilers and ar and ranlib, but not as and objcopy. Detecting the correct version to use is not easy to solve.

I still feel that this would be enormous overkill for quite literally 8 to 16 bytes of assembly per target architecture.

@WorksButNotTested
Copy link
Collaborator

I think the C compiler should detect a .S file as assembly and compile accordingly? As you say though, could be overkill.

@wfdewith
Copy link
Contributor Author

wfdewith commented May 5, 2025

I think the C compiler should detect a .S file as assembly and compile accordingly? As you say though, could be overkill.

True, but that still leaves objcopy.

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.

3 participants