pub struct Instruction<'a> { /* private fields */ }Expand description
A decoded value borrowing its exact bytes. No operand-sized allocation occurs.
Implementations§
Source§impl<'a> Instruction<'a>
impl<'a> Instruction<'a>
pub const fn code(&self) -> Code
pub const fn mnemonic(&self) -> Mnemonic
Sourcepub const fn name(&self) -> &'static str
pub const fn name(&self) -> &'static str
The exact encoding name, e.g. ldc.i4.s, rather than the normalized mnemonic.
pub const fn op_code(&self) -> &'a OpCodeInfo
Sourcepub const fn encoding(&self) -> u16
pub const fn encoding(&self) -> u16
Actual encoded identity, including registered private opcodes.
pub const fn flow_control(&self) -> FlowControl
Sourcepub const fn ip(&self) -> u64
pub const fn ip(&self) -> u64
Examples found in repository?
examples/support/stress.rs (line 136)
118pub fn exercise(
119 data: &[u8],
120 ip: u64,
121 options: DecoderOptions,
122 dialect: MonoDialect,
123 table: ExtensionTable<'_>,
124) -> usize {
125 let mut decoder = Decoder::with_extensions(data, ip, options, table).with_mono_dialect(dialect);
126 let mut count = 0;
127 while decoder.can_decode() {
128 let before = decoder.position();
129 let mut cloned = decoder.clone();
130 let instruction = decoder.decode();
131 let mut into = Instruction::default();
132 cloned.decode_into(&mut into);
133 assert_eq!(instruction, into);
134 assert!(!instruction.is_empty());
135 assert_eq!(instruction.offset(), before);
136 assert_eq!(instruction.ip(), ip.wrapping_add(before as u64));
137 assert_eq!(instruction.bytes(), &data[before..decoder.position()]);
138 assert_eq!(instruction.next_ip(), decoder.ip());
139 assert_eq!(instruction.error(), decoder.last_error());
140 match instruction.operand() {
141 Operand::Branch {
142 displacement,
143 target,
144 } => assert_eq!(
145 target,
146 instruction
147 .next_ip()
148 .wrapping_add(displacement as i64 as u64)
149 ),
150 Operand::Switch(targets) => {
151 assert_eq!(targets.iter().len(), targets.len());
152 for (slot, target) in targets.into_iter().enumerate() {
153 assert_eq!(targets.get(slot), Some(target));
154 }
155 assert_eq!(targets.get(targets.len()), None);
156 }
157 Operand::Bytes(payload) => assert!(instruction.bytes().ends_with(payload)),
158 _ => {}
159 }
160 let mut storage = [0u8; 64];
161 let mut sink = BufSink::new(&mut storage);
162 let _ = FmtFormatter::new().format(&instruction, &mut sink);
163 count += 1;
164 }
165 assert_eq!(decoder.position(), data.len());
166 assert_eq!(decoder.next(), None);
167 assert!(decoder.decode().is_empty());
168 let mut consumed = 0;
169 for group in Decoder::with_extensions(data, ip, options, table)
170 .with_mono_dialect(dialect)
171 .into_prefixed()
172 {
173 assert!(!group.is_empty());
174 assert_eq!(group.offset(), consumed);
175 assert_eq!(group.bytes(), &data[consumed..consumed + group.len()]);
176 assert_eq!(
177 group.prefix_bytes().len() + group.instruction.len(),
178 group.len()
179 );
180 let prefixes: usize = group
181 .prefixes()
182 .map(|prefix| {
183 assert!(prefix.is_prefix());
184 prefix.len()
185 })
186 .sum();
187 assert_eq!(prefixes, group.prefix_bytes().len());
188 let _ = group.validate_prefixes(PrefixValidationOptions::Ecma335);
189 let _ = group.validate_prefixes(PrefixValidationOptions::Runtime);
190 consumed += group.len();
191 }
192 assert_eq!(consumed, data.len());
193 count
194}Sourcepub const fn next_ip(&self) -> u64
pub const fn next_ip(&self) -> u64
Examples found in repository?
examples/support/stress.rs (line 138)
118pub fn exercise(
119 data: &[u8],
120 ip: u64,
121 options: DecoderOptions,
122 dialect: MonoDialect,
123 table: ExtensionTable<'_>,
124) -> usize {
125 let mut decoder = Decoder::with_extensions(data, ip, options, table).with_mono_dialect(dialect);
126 let mut count = 0;
127 while decoder.can_decode() {
128 let before = decoder.position();
129 let mut cloned = decoder.clone();
130 let instruction = decoder.decode();
131 let mut into = Instruction::default();
132 cloned.decode_into(&mut into);
133 assert_eq!(instruction, into);
134 assert!(!instruction.is_empty());
135 assert_eq!(instruction.offset(), before);
136 assert_eq!(instruction.ip(), ip.wrapping_add(before as u64));
137 assert_eq!(instruction.bytes(), &data[before..decoder.position()]);
138 assert_eq!(instruction.next_ip(), decoder.ip());
139 assert_eq!(instruction.error(), decoder.last_error());
140 match instruction.operand() {
141 Operand::Branch {
142 displacement,
143 target,
144 } => assert_eq!(
145 target,
146 instruction
147 .next_ip()
148 .wrapping_add(displacement as i64 as u64)
149 ),
150 Operand::Switch(targets) => {
151 assert_eq!(targets.iter().len(), targets.len());
152 for (slot, target) in targets.into_iter().enumerate() {
153 assert_eq!(targets.get(slot), Some(target));
154 }
155 assert_eq!(targets.get(targets.len()), None);
156 }
157 Operand::Bytes(payload) => assert!(instruction.bytes().ends_with(payload)),
158 _ => {}
159 }
160 let mut storage = [0u8; 64];
161 let mut sink = BufSink::new(&mut storage);
162 let _ = FmtFormatter::new().format(&instruction, &mut sink);
163 count += 1;
164 }
165 assert_eq!(decoder.position(), data.len());
166 assert_eq!(decoder.next(), None);
167 assert!(decoder.decode().is_empty());
168 let mut consumed = 0;
169 for group in Decoder::with_extensions(data, ip, options, table)
170 .with_mono_dialect(dialect)
171 .into_prefixed()
172 {
173 assert!(!group.is_empty());
174 assert_eq!(group.offset(), consumed);
175 assert_eq!(group.bytes(), &data[consumed..consumed + group.len()]);
176 assert_eq!(
177 group.prefix_bytes().len() + group.instruction.len(),
178 group.len()
179 );
180 let prefixes: usize = group
181 .prefixes()
182 .map(|prefix| {
183 assert!(prefix.is_prefix());
184 prefix.len()
185 })
186 .sum();
187 assert_eq!(prefixes, group.prefix_bytes().len());
188 let _ = group.validate_prefixes(PrefixValidationOptions::Ecma335);
189 let _ = group.validate_prefixes(PrefixValidationOptions::Runtime);
190 consumed += group.len();
191 }
192 assert_eq!(consumed, data.len());
193 count
194}Sourcepub const fn offset(&self) -> usize
pub const fn offset(&self) -> usize
Position relative to the original byte slice, independent of the IP.
Examples found in repository?
examples/support/stress.rs (line 135)
118pub fn exercise(
119 data: &[u8],
120 ip: u64,
121 options: DecoderOptions,
122 dialect: MonoDialect,
123 table: ExtensionTable<'_>,
124) -> usize {
125 let mut decoder = Decoder::with_extensions(data, ip, options, table).with_mono_dialect(dialect);
126 let mut count = 0;
127 while decoder.can_decode() {
128 let before = decoder.position();
129 let mut cloned = decoder.clone();
130 let instruction = decoder.decode();
131 let mut into = Instruction::default();
132 cloned.decode_into(&mut into);
133 assert_eq!(instruction, into);
134 assert!(!instruction.is_empty());
135 assert_eq!(instruction.offset(), before);
136 assert_eq!(instruction.ip(), ip.wrapping_add(before as u64));
137 assert_eq!(instruction.bytes(), &data[before..decoder.position()]);
138 assert_eq!(instruction.next_ip(), decoder.ip());
139 assert_eq!(instruction.error(), decoder.last_error());
140 match instruction.operand() {
141 Operand::Branch {
142 displacement,
143 target,
144 } => assert_eq!(
145 target,
146 instruction
147 .next_ip()
148 .wrapping_add(displacement as i64 as u64)
149 ),
150 Operand::Switch(targets) => {
151 assert_eq!(targets.iter().len(), targets.len());
152 for (slot, target) in targets.into_iter().enumerate() {
153 assert_eq!(targets.get(slot), Some(target));
154 }
155 assert_eq!(targets.get(targets.len()), None);
156 }
157 Operand::Bytes(payload) => assert!(instruction.bytes().ends_with(payload)),
158 _ => {}
159 }
160 let mut storage = [0u8; 64];
161 let mut sink = BufSink::new(&mut storage);
162 let _ = FmtFormatter::new().format(&instruction, &mut sink);
163 count += 1;
164 }
165 assert_eq!(decoder.position(), data.len());
166 assert_eq!(decoder.next(), None);
167 assert!(decoder.decode().is_empty());
168 let mut consumed = 0;
169 for group in Decoder::with_extensions(data, ip, options, table)
170 .with_mono_dialect(dialect)
171 .into_prefixed()
172 {
173 assert!(!group.is_empty());
174 assert_eq!(group.offset(), consumed);
175 assert_eq!(group.bytes(), &data[consumed..consumed + group.len()]);
176 assert_eq!(
177 group.prefix_bytes().len() + group.instruction.len(),
178 group.len()
179 );
180 let prefixes: usize = group
181 .prefixes()
182 .map(|prefix| {
183 assert!(prefix.is_prefix());
184 prefix.len()
185 })
186 .sum();
187 assert_eq!(prefixes, group.prefix_bytes().len());
188 let _ = group.validate_prefixes(PrefixValidationOptions::Ecma335);
189 let _ = group.validate_prefixes(PrefixValidationOptions::Runtime);
190 consumed += group.len();
191 }
192 assert_eq!(consumed, data.len());
193 count
194}Sourcepub const fn len(&self) -> usize
pub const fn len(&self) -> usize
CIL instructions can exceed 255 bytes; the length is never narrowed.
Examples found in repository?
examples/support/stress.rs (line 177)
118pub fn exercise(
119 data: &[u8],
120 ip: u64,
121 options: DecoderOptions,
122 dialect: MonoDialect,
123 table: ExtensionTable<'_>,
124) -> usize {
125 let mut decoder = Decoder::with_extensions(data, ip, options, table).with_mono_dialect(dialect);
126 let mut count = 0;
127 while decoder.can_decode() {
128 let before = decoder.position();
129 let mut cloned = decoder.clone();
130 let instruction = decoder.decode();
131 let mut into = Instruction::default();
132 cloned.decode_into(&mut into);
133 assert_eq!(instruction, into);
134 assert!(!instruction.is_empty());
135 assert_eq!(instruction.offset(), before);
136 assert_eq!(instruction.ip(), ip.wrapping_add(before as u64));
137 assert_eq!(instruction.bytes(), &data[before..decoder.position()]);
138 assert_eq!(instruction.next_ip(), decoder.ip());
139 assert_eq!(instruction.error(), decoder.last_error());
140 match instruction.operand() {
141 Operand::Branch {
142 displacement,
143 target,
144 } => assert_eq!(
145 target,
146 instruction
147 .next_ip()
148 .wrapping_add(displacement as i64 as u64)
149 ),
150 Operand::Switch(targets) => {
151 assert_eq!(targets.iter().len(), targets.len());
152 for (slot, target) in targets.into_iter().enumerate() {
153 assert_eq!(targets.get(slot), Some(target));
154 }
155 assert_eq!(targets.get(targets.len()), None);
156 }
157 Operand::Bytes(payload) => assert!(instruction.bytes().ends_with(payload)),
158 _ => {}
159 }
160 let mut storage = [0u8; 64];
161 let mut sink = BufSink::new(&mut storage);
162 let _ = FmtFormatter::new().format(&instruction, &mut sink);
163 count += 1;
164 }
165 assert_eq!(decoder.position(), data.len());
166 assert_eq!(decoder.next(), None);
167 assert!(decoder.decode().is_empty());
168 let mut consumed = 0;
169 for group in Decoder::with_extensions(data, ip, options, table)
170 .with_mono_dialect(dialect)
171 .into_prefixed()
172 {
173 assert!(!group.is_empty());
174 assert_eq!(group.offset(), consumed);
175 assert_eq!(group.bytes(), &data[consumed..consumed + group.len()]);
176 assert_eq!(
177 group.prefix_bytes().len() + group.instruction.len(),
178 group.len()
179 );
180 let prefixes: usize = group
181 .prefixes()
182 .map(|prefix| {
183 assert!(prefix.is_prefix());
184 prefix.len()
185 })
186 .sum();
187 assert_eq!(prefixes, group.prefix_bytes().len());
188 let _ = group.validate_prefixes(PrefixValidationOptions::Ecma335);
189 let _ = group.validate_prefixes(PrefixValidationOptions::Runtime);
190 consumed += group.len();
191 }
192 assert_eq!(consumed, data.len());
193 count
194}Sourcepub const fn is_empty(&self) -> bool
pub const fn is_empty(&self) -> bool
Examples found in repository?
examples/support/stress.rs (line 134)
118pub fn exercise(
119 data: &[u8],
120 ip: u64,
121 options: DecoderOptions,
122 dialect: MonoDialect,
123 table: ExtensionTable<'_>,
124) -> usize {
125 let mut decoder = Decoder::with_extensions(data, ip, options, table).with_mono_dialect(dialect);
126 let mut count = 0;
127 while decoder.can_decode() {
128 let before = decoder.position();
129 let mut cloned = decoder.clone();
130 let instruction = decoder.decode();
131 let mut into = Instruction::default();
132 cloned.decode_into(&mut into);
133 assert_eq!(instruction, into);
134 assert!(!instruction.is_empty());
135 assert_eq!(instruction.offset(), before);
136 assert_eq!(instruction.ip(), ip.wrapping_add(before as u64));
137 assert_eq!(instruction.bytes(), &data[before..decoder.position()]);
138 assert_eq!(instruction.next_ip(), decoder.ip());
139 assert_eq!(instruction.error(), decoder.last_error());
140 match instruction.operand() {
141 Operand::Branch {
142 displacement,
143 target,
144 } => assert_eq!(
145 target,
146 instruction
147 .next_ip()
148 .wrapping_add(displacement as i64 as u64)
149 ),
150 Operand::Switch(targets) => {
151 assert_eq!(targets.iter().len(), targets.len());
152 for (slot, target) in targets.into_iter().enumerate() {
153 assert_eq!(targets.get(slot), Some(target));
154 }
155 assert_eq!(targets.get(targets.len()), None);
156 }
157 Operand::Bytes(payload) => assert!(instruction.bytes().ends_with(payload)),
158 _ => {}
159 }
160 let mut storage = [0u8; 64];
161 let mut sink = BufSink::new(&mut storage);
162 let _ = FmtFormatter::new().format(&instruction, &mut sink);
163 count += 1;
164 }
165 assert_eq!(decoder.position(), data.len());
166 assert_eq!(decoder.next(), None);
167 assert!(decoder.decode().is_empty());
168 let mut consumed = 0;
169 for group in Decoder::with_extensions(data, ip, options, table)
170 .with_mono_dialect(dialect)
171 .into_prefixed()
172 {
173 assert!(!group.is_empty());
174 assert_eq!(group.offset(), consumed);
175 assert_eq!(group.bytes(), &data[consumed..consumed + group.len()]);
176 assert_eq!(
177 group.prefix_bytes().len() + group.instruction.len(),
178 group.len()
179 );
180 let prefixes: usize = group
181 .prefixes()
182 .map(|prefix| {
183 assert!(prefix.is_prefix());
184 prefix.len()
185 })
186 .sum();
187 assert_eq!(prefixes, group.prefix_bytes().len());
188 let _ = group.validate_prefixes(PrefixValidationOptions::Ecma335);
189 let _ = group.validate_prefixes(PrefixValidationOptions::Runtime);
190 consumed += group.len();
191 }
192 assert_eq!(consumed, data.len());
193 count
194}Sourcepub const fn bytes(&self) -> &'a [u8] ⓘ
pub const fn bytes(&self) -> &'a [u8] ⓘ
Examples found in repository?
examples/support/stress.rs (line 137)
118pub fn exercise(
119 data: &[u8],
120 ip: u64,
121 options: DecoderOptions,
122 dialect: MonoDialect,
123 table: ExtensionTable<'_>,
124) -> usize {
125 let mut decoder = Decoder::with_extensions(data, ip, options, table).with_mono_dialect(dialect);
126 let mut count = 0;
127 while decoder.can_decode() {
128 let before = decoder.position();
129 let mut cloned = decoder.clone();
130 let instruction = decoder.decode();
131 let mut into = Instruction::default();
132 cloned.decode_into(&mut into);
133 assert_eq!(instruction, into);
134 assert!(!instruction.is_empty());
135 assert_eq!(instruction.offset(), before);
136 assert_eq!(instruction.ip(), ip.wrapping_add(before as u64));
137 assert_eq!(instruction.bytes(), &data[before..decoder.position()]);
138 assert_eq!(instruction.next_ip(), decoder.ip());
139 assert_eq!(instruction.error(), decoder.last_error());
140 match instruction.operand() {
141 Operand::Branch {
142 displacement,
143 target,
144 } => assert_eq!(
145 target,
146 instruction
147 .next_ip()
148 .wrapping_add(displacement as i64 as u64)
149 ),
150 Operand::Switch(targets) => {
151 assert_eq!(targets.iter().len(), targets.len());
152 for (slot, target) in targets.into_iter().enumerate() {
153 assert_eq!(targets.get(slot), Some(target));
154 }
155 assert_eq!(targets.get(targets.len()), None);
156 }
157 Operand::Bytes(payload) => assert!(instruction.bytes().ends_with(payload)),
158 _ => {}
159 }
160 let mut storage = [0u8; 64];
161 let mut sink = BufSink::new(&mut storage);
162 let _ = FmtFormatter::new().format(&instruction, &mut sink);
163 count += 1;
164 }
165 assert_eq!(decoder.position(), data.len());
166 assert_eq!(decoder.next(), None);
167 assert!(decoder.decode().is_empty());
168 let mut consumed = 0;
169 for group in Decoder::with_extensions(data, ip, options, table)
170 .with_mono_dialect(dialect)
171 .into_prefixed()
172 {
173 assert!(!group.is_empty());
174 assert_eq!(group.offset(), consumed);
175 assert_eq!(group.bytes(), &data[consumed..consumed + group.len()]);
176 assert_eq!(
177 group.prefix_bytes().len() + group.instruction.len(),
178 group.len()
179 );
180 let prefixes: usize = group
181 .prefixes()
182 .map(|prefix| {
183 assert!(prefix.is_prefix());
184 prefix.len()
185 })
186 .sum();
187 assert_eq!(prefixes, group.prefix_bytes().len());
188 let _ = group.validate_prefixes(PrefixValidationOptions::Ecma335);
189 let _ = group.validate_prefixes(PrefixValidationOptions::Runtime);
190 consumed += group.len();
191 }
192 assert_eq!(consumed, data.len());
193 count
194}Sourcepub const fn is_invalid(&self) -> bool
pub const fn is_invalid(&self) -> bool
Examples found in repository?
examples/disasm.rs (line 32)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 let mut args = std::env::args().skip(1);
5 let Some(path) = args.next() else {
6 eprintln!(
7 "Usage: cargo run --features std --example disasm -- <raw-cil.bin> [hex-base-ip] [modern|legacy|july2002|early2002|may2004]"
8 );
9 std::process::exit(2);
10 };
11 let ip = args
12 .next()
13 .map(|value| u64::from_str_radix(value.trim_start_matches("0x"), 16))
14 .transpose()?
15 .unwrap_or(0);
16 let dialect = match args.next().as_deref().unwrap_or("modern") {
17 "modern" => MonoDialect::Modern,
18 "legacy" => MonoDialect::Legacy,
19 "july2002" => MonoDialect::July2002,
20 "early2002" => MonoDialect::Early2002,
21 "may2004" => MonoDialect::May2004,
22 _ => return Err("unknown Mono dialect".into()),
23 };
24 let bytes = std::fs::read(path)?;
25 let formatter = FmtFormatter::with_options(FormatterOptions {
26 show_address: true,
27 ..FormatterOptions::default()
28 });
29 for instruction in Decoder::new(&bytes, ip, DecoderOptions::NONE).with_mono_dialect(dialect) {
30 let mut text = String::new();
31 formatter.format(&instruction, &mut text)?;
32 if instruction.is_invalid() {
33 println!("{text} ; {}", instruction.error());
34 } else {
35 println!("{text}");
36 }
37 }
38 Ok(())
39}Sourcepub const fn is_prefix(&self) -> bool
pub const fn is_prefix(&self) -> bool
Examples found in repository?
examples/support/stress.rs (line 183)
118pub fn exercise(
119 data: &[u8],
120 ip: u64,
121 options: DecoderOptions,
122 dialect: MonoDialect,
123 table: ExtensionTable<'_>,
124) -> usize {
125 let mut decoder = Decoder::with_extensions(data, ip, options, table).with_mono_dialect(dialect);
126 let mut count = 0;
127 while decoder.can_decode() {
128 let before = decoder.position();
129 let mut cloned = decoder.clone();
130 let instruction = decoder.decode();
131 let mut into = Instruction::default();
132 cloned.decode_into(&mut into);
133 assert_eq!(instruction, into);
134 assert!(!instruction.is_empty());
135 assert_eq!(instruction.offset(), before);
136 assert_eq!(instruction.ip(), ip.wrapping_add(before as u64));
137 assert_eq!(instruction.bytes(), &data[before..decoder.position()]);
138 assert_eq!(instruction.next_ip(), decoder.ip());
139 assert_eq!(instruction.error(), decoder.last_error());
140 match instruction.operand() {
141 Operand::Branch {
142 displacement,
143 target,
144 } => assert_eq!(
145 target,
146 instruction
147 .next_ip()
148 .wrapping_add(displacement as i64 as u64)
149 ),
150 Operand::Switch(targets) => {
151 assert_eq!(targets.iter().len(), targets.len());
152 for (slot, target) in targets.into_iter().enumerate() {
153 assert_eq!(targets.get(slot), Some(target));
154 }
155 assert_eq!(targets.get(targets.len()), None);
156 }
157 Operand::Bytes(payload) => assert!(instruction.bytes().ends_with(payload)),
158 _ => {}
159 }
160 let mut storage = [0u8; 64];
161 let mut sink = BufSink::new(&mut storage);
162 let _ = FmtFormatter::new().format(&instruction, &mut sink);
163 count += 1;
164 }
165 assert_eq!(decoder.position(), data.len());
166 assert_eq!(decoder.next(), None);
167 assert!(decoder.decode().is_empty());
168 let mut consumed = 0;
169 for group in Decoder::with_extensions(data, ip, options, table)
170 .with_mono_dialect(dialect)
171 .into_prefixed()
172 {
173 assert!(!group.is_empty());
174 assert_eq!(group.offset(), consumed);
175 assert_eq!(group.bytes(), &data[consumed..consumed + group.len()]);
176 assert_eq!(
177 group.prefix_bytes().len() + group.instruction.len(),
178 group.len()
179 );
180 let prefixes: usize = group
181 .prefixes()
182 .map(|prefix| {
183 assert!(prefix.is_prefix());
184 prefix.len()
185 })
186 .sum();
187 assert_eq!(prefixes, group.prefix_bytes().len());
188 let _ = group.validate_prefixes(PrefixValidationOptions::Ecma335);
189 let _ = group.validate_prefixes(PrefixValidationOptions::Runtime);
190 consumed += group.len();
191 }
192 assert_eq!(consumed, data.len());
193 count
194}Sourcepub const fn error(&self) -> DecodeError
pub const fn error(&self) -> DecodeError
Examples found in repository?
examples/disasm.rs (line 33)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 let mut args = std::env::args().skip(1);
5 let Some(path) = args.next() else {
6 eprintln!(
7 "Usage: cargo run --features std --example disasm -- <raw-cil.bin> [hex-base-ip] [modern|legacy|july2002|early2002|may2004]"
8 );
9 std::process::exit(2);
10 };
11 let ip = args
12 .next()
13 .map(|value| u64::from_str_radix(value.trim_start_matches("0x"), 16))
14 .transpose()?
15 .unwrap_or(0);
16 let dialect = match args.next().as_deref().unwrap_or("modern") {
17 "modern" => MonoDialect::Modern,
18 "legacy" => MonoDialect::Legacy,
19 "july2002" => MonoDialect::July2002,
20 "early2002" => MonoDialect::Early2002,
21 "may2004" => MonoDialect::May2004,
22 _ => return Err("unknown Mono dialect".into()),
23 };
24 let bytes = std::fs::read(path)?;
25 let formatter = FmtFormatter::with_options(FormatterOptions {
26 show_address: true,
27 ..FormatterOptions::default()
28 });
29 for instruction in Decoder::new(&bytes, ip, DecoderOptions::NONE).with_mono_dialect(dialect) {
30 let mut text = String::new();
31 formatter.format(&instruction, &mut text)?;
32 if instruction.is_invalid() {
33 println!("{text} ; {}", instruction.error());
34 } else {
35 println!("{text}");
36 }
37 }
38 Ok(())
39}More examples
examples/support/stress.rs (line 139)
118pub fn exercise(
119 data: &[u8],
120 ip: u64,
121 options: DecoderOptions,
122 dialect: MonoDialect,
123 table: ExtensionTable<'_>,
124) -> usize {
125 let mut decoder = Decoder::with_extensions(data, ip, options, table).with_mono_dialect(dialect);
126 let mut count = 0;
127 while decoder.can_decode() {
128 let before = decoder.position();
129 let mut cloned = decoder.clone();
130 let instruction = decoder.decode();
131 let mut into = Instruction::default();
132 cloned.decode_into(&mut into);
133 assert_eq!(instruction, into);
134 assert!(!instruction.is_empty());
135 assert_eq!(instruction.offset(), before);
136 assert_eq!(instruction.ip(), ip.wrapping_add(before as u64));
137 assert_eq!(instruction.bytes(), &data[before..decoder.position()]);
138 assert_eq!(instruction.next_ip(), decoder.ip());
139 assert_eq!(instruction.error(), decoder.last_error());
140 match instruction.operand() {
141 Operand::Branch {
142 displacement,
143 target,
144 } => assert_eq!(
145 target,
146 instruction
147 .next_ip()
148 .wrapping_add(displacement as i64 as u64)
149 ),
150 Operand::Switch(targets) => {
151 assert_eq!(targets.iter().len(), targets.len());
152 for (slot, target) in targets.into_iter().enumerate() {
153 assert_eq!(targets.get(slot), Some(target));
154 }
155 assert_eq!(targets.get(targets.len()), None);
156 }
157 Operand::Bytes(payload) => assert!(instruction.bytes().ends_with(payload)),
158 _ => {}
159 }
160 let mut storage = [0u8; 64];
161 let mut sink = BufSink::new(&mut storage);
162 let _ = FmtFormatter::new().format(&instruction, &mut sink);
163 count += 1;
164 }
165 assert_eq!(decoder.position(), data.len());
166 assert_eq!(decoder.next(), None);
167 assert!(decoder.decode().is_empty());
168 let mut consumed = 0;
169 for group in Decoder::with_extensions(data, ip, options, table)
170 .with_mono_dialect(dialect)
171 .into_prefixed()
172 {
173 assert!(!group.is_empty());
174 assert_eq!(group.offset(), consumed);
175 assert_eq!(group.bytes(), &data[consumed..consumed + group.len()]);
176 assert_eq!(
177 group.prefix_bytes().len() + group.instruction.len(),
178 group.len()
179 );
180 let prefixes: usize = group
181 .prefixes()
182 .map(|prefix| {
183 assert!(prefix.is_prefix());
184 prefix.len()
185 })
186 .sum();
187 assert_eq!(prefixes, group.prefix_bytes().len());
188 let _ = group.validate_prefixes(PrefixValidationOptions::Ecma335);
189 let _ = group.validate_prefixes(PrefixValidationOptions::Runtime);
190 consumed += group.len();
191 }
192 assert_eq!(consumed, data.len());
193 count
194}Sourcepub const fn operand(&self) -> Operand<'a>
pub const fn operand(&self) -> Operand<'a>
Examples found in repository?
examples/support/stress.rs (line 140)
118pub fn exercise(
119 data: &[u8],
120 ip: u64,
121 options: DecoderOptions,
122 dialect: MonoDialect,
123 table: ExtensionTable<'_>,
124) -> usize {
125 let mut decoder = Decoder::with_extensions(data, ip, options, table).with_mono_dialect(dialect);
126 let mut count = 0;
127 while decoder.can_decode() {
128 let before = decoder.position();
129 let mut cloned = decoder.clone();
130 let instruction = decoder.decode();
131 let mut into = Instruction::default();
132 cloned.decode_into(&mut into);
133 assert_eq!(instruction, into);
134 assert!(!instruction.is_empty());
135 assert_eq!(instruction.offset(), before);
136 assert_eq!(instruction.ip(), ip.wrapping_add(before as u64));
137 assert_eq!(instruction.bytes(), &data[before..decoder.position()]);
138 assert_eq!(instruction.next_ip(), decoder.ip());
139 assert_eq!(instruction.error(), decoder.last_error());
140 match instruction.operand() {
141 Operand::Branch {
142 displacement,
143 target,
144 } => assert_eq!(
145 target,
146 instruction
147 .next_ip()
148 .wrapping_add(displacement as i64 as u64)
149 ),
150 Operand::Switch(targets) => {
151 assert_eq!(targets.iter().len(), targets.len());
152 for (slot, target) in targets.into_iter().enumerate() {
153 assert_eq!(targets.get(slot), Some(target));
154 }
155 assert_eq!(targets.get(targets.len()), None);
156 }
157 Operand::Bytes(payload) => assert!(instruction.bytes().ends_with(payload)),
158 _ => {}
159 }
160 let mut storage = [0u8; 64];
161 let mut sink = BufSink::new(&mut storage);
162 let _ = FmtFormatter::new().format(&instruction, &mut sink);
163 count += 1;
164 }
165 assert_eq!(decoder.position(), data.len());
166 assert_eq!(decoder.next(), None);
167 assert!(decoder.decode().is_empty());
168 let mut consumed = 0;
169 for group in Decoder::with_extensions(data, ip, options, table)
170 .with_mono_dialect(dialect)
171 .into_prefixed()
172 {
173 assert!(!group.is_empty());
174 assert_eq!(group.offset(), consumed);
175 assert_eq!(group.bytes(), &data[consumed..consumed + group.len()]);
176 assert_eq!(
177 group.prefix_bytes().len() + group.instruction.len(),
178 group.len()
179 );
180 let prefixes: usize = group
181 .prefixes()
182 .map(|prefix| {
183 assert!(prefix.is_prefix());
184 prefix.len()
185 })
186 .sum();
187 assert_eq!(prefixes, group.prefix_bytes().len());
188 let _ = group.validate_prefixes(PrefixValidationOptions::Ecma335);
189 let _ = group.validate_prefixes(PrefixValidationOptions::Runtime);
190 consumed += group.len();
191 }
192 assert_eq!(consumed, data.len());
193 count
194}Sourcepub const fn op_count(&self) -> u32
pub const fn op_count(&self) -> u32
Encoded operand count: zero for macro forms, one for a whole switch table.
pub const fn op_kind(&self, index: u32) -> Option<OperandKind>
pub const fn immediate_i8(&self) -> Option<i8>
pub const fn immediate_u8(&self) -> Option<u8>
Sourcepub const fn immediate_i32(&self) -> Option<i32>
pub const fn immediate_i32(&self) -> Option<i32>
Widened signed constant, also including ldc.i4.* macro forms.
pub const fn immediate_i64(&self) -> Option<i64>
pub const fn immediate_f32_bits(&self) -> Option<u32>
pub const fn immediate_f64_bits(&self) -> Option<u64>
pub fn immediate_f32(&self) -> Option<f32>
pub fn immediate_f64(&self) -> Option<f64>
Sourcepub const fn variable_index(&self) -> Option<u16>
pub const fn variable_index(&self) -> Option<u16>
Argument or local number, including implicit macro indices.
pub const fn metadata_token(&self) -> Option<MetadataToken>
pub const fn near_branch_target(&self) -> Option<u64>
pub const fn branch_displacement(&self) -> Option<i32>
pub const fn switch_targets(&self) -> Option<SwitchTargets<'a>>
Trait Implementations§
Source§impl<'a> Clone for Instruction<'a>
impl<'a> Clone for Instruction<'a>
impl<'a> Copy for Instruction<'a>
Source§impl<'a> Debug for Instruction<'a>
impl<'a> Debug for Instruction<'a>
Source§impl Default for Instruction<'_>
impl Default for Instruction<'_>
Source§impl Display for Instruction<'_>
impl Display for Instruction<'_>
impl<'a> Eq for Instruction<'a>
Source§impl<'a> Hash for Instruction<'a>
impl<'a> Hash for Instruction<'a>
Source§impl<'a> PartialEq for Instruction<'a>
impl<'a> PartialEq for Instruction<'a>
impl<'a> StructuralPartialEq for Instruction<'a>
Auto Trait Implementations§
impl<'a> Freeze for Instruction<'a>
impl<'a> RefUnwindSafe for Instruction<'a>
impl<'a> Send for Instruction<'a>
impl<'a> Sync for Instruction<'a>
impl<'a> Unpin for Instruction<'a>
impl<'a> UnsafeUnpin for Instruction<'a>
impl<'a> UnwindSafe for Instruction<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more