Skip to content

Commit cb1bac3

Browse files
committed
Review feedback: remove dependence on (and test for) NOP being the literal byte 0.
1 parent 7cb42f9 commit cb1bac3

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

cranelift/codegen/src/isa/pulley_shared/inst/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -536,9 +536,13 @@ where
536536
}
537537

538538
fn gen_nop_unit() -> SmallVec<[u8; 8]> {
539-
// See unit test `nop_is_single_zero_byte` in Pulley crate
540-
// where this is validated.
541-
smallvec::smallvec![0]
539+
let mut bytes = smallvec::smallvec![];
540+
let nop = pulley_interpreter::op::Nop {};
541+
nop.encode(&mut bytes);
542+
// NOP needs to be a 1-byte opcode so it can be used to
543+
// overwrite a callsite of any length.
544+
assert_eq!(nop.len(), 1);
545+
bytes
542546
}
543547

544548
fn rc_for_type(ty: Type) -> CodegenResult<(&'static [RegClass], &'static [Type])> {

pulley/src/encode.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -335,13 +335,11 @@ for_each_extended_op!(impl_extended_encoders);
335335

336336
#[test]
337337
#[cfg(feature = "std")]
338-
fn nop_is_single_zero_byte() {
339-
// The fact that [0x00] is a nop is load-bearing; the MachInst
340-
// impl in the Cranelift backend uses this. To ensure this, we
341-
// list Nop first in the opcode list in lib.rs and give it zero
342-
// arguments.
338+
fn nop_is_single_byte() {
339+
// NOP needs to be a single byte so that it can be used to NOP out
340+
// an instruction sequence of any length.
343341
let inst = crate::op::Nop {};
344342
let mut bytes = vec![];
345343
inst.encode(&mut bytes);
346-
assert_eq!(bytes, vec![0]);
344+
assert_eq!(bytes.len(), 1);
347345
}

pulley/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ macro_rules! for_each_op {
8787
( $macro:ident ) => {
8888
$macro! {
8989
/// No-operation.
90-
///
91-
/// NOTE: must be first; we rely on the opcode being zero.
9290
nop = Nop;
9391

9492
/// Transfer control the address in the `lr` register.

0 commit comments

Comments
 (0)