Skip to content
This repository was archived by the owner on Mar 24, 2022. It is now read-only.

Commit c89381d

Browse files
authored
Issue #417: Retain whether or not a module was compiled with --count-instructions. (#421)
1 parent d4b7ffd commit c89381d

7 files changed

Lines changed: 69 additions & 11 deletions

File tree

lucet-module/src/module_data.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub struct ModuleFeatures {
5858
pub bmi2: bool,
5959
pub lzcnt: bool,
6060
pub popcnt: bool,
61+
pub instruction_count: bool,
6162
_hidden: (),
6263
}
6364

@@ -73,6 +74,7 @@ impl ModuleFeatures {
7374
bmi2: false,
7475
lzcnt: false,
7576
popcnt: false,
77+
instruction_count: false,
7678
_hidden: (),
7779
}
7880
}

lucet-runtime/lucet-runtime-internals/src/instance.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -730,8 +730,11 @@ impl Instance {
730730
}
731731

732732
#[inline]
733-
pub fn get_instruction_count(&self) -> u64 {
734-
self.get_instance_implicits().instruction_count
733+
pub fn get_instruction_count(&self) -> Option<u64> {
734+
if self.module.is_instruction_count_instrumented() {
735+
return Some(self.get_instance_implicits().instruction_count);
736+
}
737+
None
735738
}
736739

737740
#[inline]

lucet-runtime/lucet-runtime-internals/src/module.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ pub trait Module: ModuleInternal {
3838
}
3939

4040
pub trait ModuleInternal: Send + Sync {
41+
/// Determine whether this module has been instrumented with additional
42+
/// instructions that monitor the number of wasm operations executed
43+
/// during runtime.
44+
fn is_instruction_count_instrumented(&self) -> bool;
45+
4146
fn heap_spec(&self) -> Option<&HeapSpec>;
4247

4348
/// Get the WebAssembly globals of the module.

lucet-runtime/lucet-runtime-internals/src/module/dl.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ impl DlModule {
203203
impl Module for DlModule {}
204204

205205
impl ModuleInternal for DlModule {
206+
fn is_instruction_count_instrumented(&self) -> bool {
207+
self.module.module_data.features().instruction_count
208+
}
209+
206210
fn heap_spec(&self) -> Option<&HeapSpec> {
207211
self.module.module_data.heap_spec()
208212
}

lucet-runtime/lucet-runtime-internals/src/module/mock.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ unsafe impl Sync for MockModule {}
260260
impl Module for MockModule {}
261261

262262
impl ModuleInternal for MockModule {
263+
fn is_instruction_count_instrumented(&self) -> bool {
264+
self.module_data.features().instruction_count
265+
}
266+
263267
fn heap_spec(&self) -> Option<&HeapSpec> {
264268
self.module_data.heap_spec()
265269
}

lucet-runtime/tests/instruction_counting.rs

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ use std::path::Path;
77
use std::sync::Arc;
88
use tempfile::TempDir;
99

10-
pub fn wasm_test<P: AsRef<Path>>(wasm_file: P) -> Result<Arc<DlModule>, Error> {
10+
pub fn wasm_test<P: AsRef<Path>>(
11+
wasm_file: P,
12+
icount_option: bool,
13+
) -> Result<Arc<DlModule>, Error> {
1114
let workdir = TempDir::new().expect("create working directory");
1215

13-
let native_build = Lucetc::new(wasm_file).with_count_instructions(true);
16+
let native_build = Lucetc::new(wasm_file).with_count_instructions(icount_option);
1417

1518
let so_file = workdir.path().join("out.so");
1619

@@ -21,9 +24,8 @@ pub fn wasm_test<P: AsRef<Path>>(wasm_file: P) -> Result<Arc<DlModule>, Error> {
2124
Ok(dlmodule)
2225
}
2326

24-
#[test]
25-
pub fn check_instruction_counts() {
26-
let files: Vec<DirEntry> = std::fs::read_dir("./tests/instruction_counting")
27+
pub fn get_instruction_count_test_files() -> Vec<DirEntry> {
28+
std::fs::read_dir("./tests/instruction_counting")
2729
.expect("can iterate test files")
2830
.map(|ent| {
2931
let ent = ent.expect("can get test files");
@@ -33,7 +35,12 @@ pub fn check_instruction_counts() {
3335
);
3436
ent
3537
})
36-
.collect();
38+
.collect()
39+
}
40+
41+
#[test]
42+
pub fn check_instruction_count_off() {
43+
let files: Vec<DirEntry> = get_instruction_count_test_files();
3744

3845
assert!(
3946
files.len() > 0,
@@ -42,7 +49,8 @@ pub fn check_instruction_counts() {
4249

4350
files.par_iter().for_each(|ent| {
4451
let wasm_path = ent.path();
45-
let module = wasm_test(&wasm_path).expect("can load module");
52+
let do_not_instrument = false;
53+
let module = wasm_test(&wasm_path, do_not_instrument).expect("can load module");
4654

4755
let region = MmapRegion::create(1, &Limits::default()).expect("region can be created");
4856

@@ -53,6 +61,37 @@ pub fn check_instruction_counts() {
5361
inst.run("test_function", &[]).expect("instance runs");
5462

5563
let instruction_count = inst.get_instruction_count();
64+
if instruction_count.is_some() {
65+
panic!("instruction count instrumentation was not expected from instance");
66+
}
67+
});
68+
}
69+
70+
#[test]
71+
pub fn check_instruction_count() {
72+
let files: Vec<DirEntry> = get_instruction_count_test_files();
73+
74+
assert!(
75+
files.len() > 0,
76+
"there are no test cases in the `instruction_counting` directory"
77+
);
78+
79+
files.par_iter().for_each(|ent| {
80+
let wasm_path = ent.path();
81+
let do_instrument = true;
82+
let module = wasm_test(&wasm_path, do_instrument).expect("can load instrumented module");
83+
84+
let region = MmapRegion::create(1, &Limits::default()).expect("region can be created");
85+
86+
let mut inst = region
87+
.new_instance(module)
88+
.expect("instance can be created");
89+
90+
inst.run("test_function", &[]).expect("instance runs");
91+
92+
let instruction_count = inst
93+
.get_instruction_count()
94+
.expect("instruction count expected from instance");
5695

5796
assert_eq!(
5897
instruction_count,

lucetc/src/compiler.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,9 @@ impl<'a> Compiler<'a> {
131131
}
132132

133133
pub fn module_features(&self) -> ModuleFeatures {
134-
// This will grow in the future to encompass other options describing the compiled module.
135-
(&self.cpu_features).into()
134+
let mut mf: ModuleFeatures = (&self.cpu_features).into();
135+
mf.instruction_count = self.count_instructions;
136+
mf
136137
}
137138

138139
pub fn module_data(&self) -> Result<ModuleData<'_>, Error> {

0 commit comments

Comments
 (0)