-
Notifications
You must be signed in to change notification settings - Fork 1.5k
asm: introduce a new x64 assembler #10110
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
de34824
asm: add initial infrastructure for an external assembler
abrown 9e74026
asm: bless Cranelift file tests
abrown 872c420
ci: skip formatting when `rustfmt` not present
abrown 0268ef9
vet: audit `arbtest` for use as a dev-dependency
abrown f6e4f1d
ci: make assembler crates publishable
abrown 594a3ca
review: use Cargo workspace values
abrown c3e28c4
review: document `Inst`, move `Inst::name`
abrown ff778af
review: clarify 'earlier' doc comment
abrown 043f501
review: document multi-byte opcodes
abrown 1fd0e1d
review: document `Rex` builder methods
abrown 04aeb44
review: document encoding rules
abrown 6084920
review: clarify 'bits' -> 'width'
abrown fab310a
review: clarify confusing legacy prefixes
abrown 7811d40
review: tweak IA-32e language
abrown d782bb8
review: expand documentation for format
abrown 2072817
review: move feature list closer to enum
abrown b460c77
review: add a TODO to remove AT&T operand ordering
abrown f03fccf
review: move prefix emission to separate lines
abrown 10db196
review: add testing note
abrown 1a5cffe
review: fix incomplete sentence
abrown d228460
review: rename `MinusRsp` to `NonRspGpr`
abrown 1427b69
review: add TODO for commented out instructions
abrown c6147c6
review: add conservative down-conversion to `is_imm*`
abrown 57a9991
Fuzzing updates for cranelift-assembler-x64 (#10)
alexcrichton 6935335
vet: skip audit for `cranelift-assembler-x64-fuzz`
abrown 2c8a276
review: use 32-bit form for 8-bit and 16-bit reg-reg
abrown 39a3aa7
fix: skip `rustfmt` on generated code in more cases
abrown 6707cb1
fix: feed Cargo the meta crate version
abrown File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[package] | ||
name = "cranelift-assembler-x64" | ||
description = "A Cranelift-specific x64 assembler" | ||
version = "0.117.0" | ||
license = "Apache-2.0 WITH LLVM-exception" | ||
edition.workspace = true | ||
|
||
[dependencies] | ||
arbitrary = { version = "1.3.2", features = ["derive"] } | ||
capstone = "0.12.0" | ||
|
||
[dev-dependencies] | ||
arbtest = "0.3.1" | ||
|
||
[build-dependencies] | ||
cranelift-assembler-x64-meta = { path = "meta" } | ||
|
||
[lints.clippy] | ||
all = "deny" | ||
pedantic = "warn" | ||
module_name_repetitions = { level = "allow", priority = 1 } | ||
similar_names = { level = "allow", priority = 1 } | ||
wildcard_imports = { level = "allow", priority = 1 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# `cranelift-assembler-x64` | ||
|
||
A Cranelift-specific x64 assembler. Unlike the existing `cranelift-codegen` | ||
assembler, this assembler uses instructions, not instruction classes, as the | ||
core abstraction. | ||
|
||
### Use | ||
|
||
Like `cranelift-codegen`, using this assembler starts with `enum Inst`. For | ||
convenience, a `main.rs` script prints the path to this generated code: | ||
|
||
```console | ||
$ cat $(cargo run) | ||
#[derive(arbitrary::Arbitrary, Debug)] | ||
pub enum Inst { | ||
andb_i(andb_i), | ||
andw_i(andw_i), | ||
andl_i(andl_i), | ||
... | ||
``` | ||
|
||
### Test | ||
|
||
In order to check that this assembler emits correct machine code, we fuzz it | ||
against a known-good disassembler. We can run a quick, one-second check: | ||
|
||
```console | ||
$ cargo test -- --nocapture | ||
``` | ||
|
||
Or we can run the fuzzer indefinitely: | ||
|
||
```console | ||
$ cargo +nightly fuzz run -s none roundtrip -j16 | ||
``` | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use cranelift_assembler_x64_meta as meta; | ||
use std::env; | ||
use std::path::Path; | ||
|
||
fn main() { | ||
println!("cargo:rerun-if-changed=build.rs"); | ||
|
||
let out_dir = env::var("OUT_DIR").expect("The OUT_DIR environment variable must be set"); | ||
let out_dir = Path::new(&out_dir); | ||
let built_files = [ | ||
meta::generate_rust_assembler(out_dir.join("assembler.rs")), | ||
meta::generate_isle_macro(out_dir.join("assembler-isle-macro.rs")), | ||
meta::generate_isle_definitions(out_dir.join("assembler-definitions.isle")), | ||
]; | ||
|
||
println!( | ||
"cargo:rustc-env=ASSEMBLER_BUILT_FILES={}", | ||
built_files | ||
.iter() | ||
.map(|p| p.to_string_lossy().to_string()) | ||
.collect::<Vec<_>>() | ||
.join(":") | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
target | ||
corpus | ||
artifacts | ||
coverage |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
name = "cranelift-assembler-x64-fuzz" | ||
version = "0.0.0" | ||
publish = false | ||
edition = "2021" | ||
|
||
[package.metadata] | ||
cargo-fuzz = true | ||
|
||
[dependencies] | ||
libfuzzer-sys = "0.4" | ||
cranelift-assembler-x64 = { path = ".." } | ||
|
||
[[bin]] | ||
name = "roundtrip" | ||
path = "fuzz_targets/roundtrip.rs" | ||
test = false | ||
doc = false | ||
bench = false | ||
|
||
[workspace] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#![no_main] | ||
|
||
use cranelift_assembler_x64::{fuzz, Inst}; | ||
use libfuzzer_sys::fuzz_target; | ||
|
||
fuzz_target!(|inst: Inst<fuzz::FuzzRegs>| { | ||
fuzz::roundtrip(&inst); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# This extra configuration allows defining extra-long lines in | ||
# `src/instructions`. | ||
fn_call_width = 100 | ||
max_width = 110 | ||
struct_lit_width = 50 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "cranelift-assembler-x64-meta" | ||
description = "Generate a Cranelift-specific assembler for x64 instructions" | ||
version = "0.117.0" | ||
license = "Apache-2.0 WITH LLVM-exception" | ||
edition.workspace = true | ||
|
||
[dependencies] | ||
|
||
[lints.clippy] | ||
all = "deny" | ||
pedantic = "warn" | ||
enum_glob_use = { level = "allow", priority = 1 } | ||
just_underscores_and_digits = { level = "allow", priority = 1 } | ||
wildcard_imports = { level = "allow", priority = 1 } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.