Skip to content

Commit e971b09

Browse files
authored
[debug] Disable printing on nodes (#10561)
## Description Despite trying to disable printing using feature flags in #8795, it came back because of the additive nature of `feature`s. This time, disable debug printing on validators and full nodes for good, by controlling their inclusion using a flag passed in during native function creation, that controls whether to link against a silent version of the debug print functions or not. Unit tests, and other CLI usages of the VM typically link against the unsilenced native functions, and the version used by validators is silenced. ## Test Plan Run all existing tests: ``` $ cargo simtest $ env SUI_SKIP_SIMTESTS=1 cargo nextest run ``` Create a test move package with the following content: ``` module test::test { struct ImALittleTeapot has copy, drop, store {} public entry fun print() { std::debug::print(&ImALittleTeapot {}); } #[test] fun test_printing() { print(); } } ``` And confirm that the following does print debug output ``` sui$ cargo build --bin sui --release sui$ cargo build --bin sui-node --release sui$ ./target/release/sui move test -p $PKG ``` While publishing to a local network and repeatedly calling `print` does not pollute the validator or fullnode logs.
1 parent 70f6f16 commit e971b09

File tree

5 files changed

+71
-15
lines changed

5 files changed

+71
-15
lines changed

documentation/examples/diem-framework/crates/cli/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ fn main() -> Result<()> {
3636
)
3737
.into_iter()
3838
.chain(move_stdlib::natives::nursery_natives(
39+
/* silent */ false,
3940
CORE_CODE_ADDRESS,
4041
// We may want to switch to a different gas schedule in the future, but for now,
4142
// the all-zero one should be enough.

move-stdlib/src/natives/debug.rs

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ pub struct PrintGasParameters {
2626
pub base_cost: InternalGas,
2727
}
2828

29+
#[inline]
30+
fn native_print_nop(
31+
gas_params: &PrintGasParameters,
32+
ty_args: Vec<Type>,
33+
args: VecDeque<Value>,
34+
) -> PartialVMResult<NativeResult> {
35+
debug_assert!(ty_args.len() == 1);
36+
debug_assert!(args.len() == 1);
37+
Ok(NativeResult::ok(gas_params.base_cost, smallvec![]))
38+
}
39+
2940
#[inline]
3041
fn native_print(
3142
gas_params: &PrintGasParameters,
@@ -68,14 +79,23 @@ fn native_print(
6879
}
6980

7081
pub fn make_native_print(
82+
silent: bool,
7183
gas_params: PrintGasParameters,
7284
move_std_addr: AccountAddress,
7385
) -> NativeFunction {
74-
Arc::new(
75-
move |context, ty_args, args| -> PartialVMResult<NativeResult> {
76-
native_print(&gas_params, context, ty_args, args, move_std_addr)
77-
},
78-
)
86+
if silent {
87+
Arc::new(
88+
move |_context, ty_args, args| -> PartialVMResult<NativeResult> {
89+
native_print_nop(&gas_params, ty_args, args)
90+
},
91+
)
92+
} else {
93+
Arc::new(
94+
move |context, ty_args, args| -> PartialVMResult<NativeResult> {
95+
native_print(&gas_params, context, ty_args, args, move_std_addr)
96+
},
97+
)
98+
}
7999
}
80100

81101
/***************************************************************************************************
@@ -88,6 +108,17 @@ pub struct PrintStackTraceGasParameters {
88108
pub base_cost: InternalGas,
89109
}
90110

111+
#[inline]
112+
fn native_print_stack_trace_nop(
113+
gas_params: &PrintStackTraceGasParameters,
114+
ty_args: Vec<Type>,
115+
args: VecDeque<Value>,
116+
) -> PartialVMResult<NativeResult> {
117+
debug_assert!(ty_args.is_empty());
118+
debug_assert!(args.is_empty());
119+
Ok(NativeResult::ok(gas_params.base_cost, smallvec![]))
120+
}
121+
91122
#[allow(unused_variables)]
92123
#[inline]
93124
fn native_print_stack_trace(
@@ -109,12 +140,23 @@ fn native_print_stack_trace(
109140
Ok(NativeResult::ok(gas_params.base_cost, smallvec![]))
110141
}
111142

112-
pub fn make_native_print_stack_trace(gas_params: PrintStackTraceGasParameters) -> NativeFunction {
113-
Arc::new(
114-
move |context, ty_args, args| -> PartialVMResult<NativeResult> {
115-
native_print_stack_trace(&gas_params, context, ty_args, args)
116-
},
117-
)
143+
pub fn make_native_print_stack_trace(
144+
silent: bool,
145+
gas_params: PrintStackTraceGasParameters,
146+
) -> NativeFunction {
147+
if silent {
148+
Arc::new(
149+
move |_context, ty_args, args| -> PartialVMResult<NativeResult> {
150+
native_print_stack_trace_nop(&gas_params, ty_args, args)
151+
},
152+
)
153+
} else {
154+
Arc::new(
155+
move |context, ty_args, args| -> PartialVMResult<NativeResult> {
156+
native_print_stack_trace(&gas_params, context, ty_args, args)
157+
},
158+
)
159+
}
118160
}
119161

120162
/***************************************************************************************************
@@ -127,14 +169,18 @@ pub struct GasParameters {
127169
}
128170

129171
pub fn make_all(
172+
silent: bool,
130173
gas_params: GasParameters,
131174
move_std_addr: AccountAddress,
132175
) -> impl Iterator<Item = (String, NativeFunction)> {
133176
let natives = [
134-
("print", make_native_print(gas_params.print, move_std_addr)),
177+
(
178+
"print",
179+
make_native_print(silent, gas_params.print, move_std_addr),
180+
),
135181
(
136182
"print_stack_trace",
137-
make_native_print_stack_trace(gas_params.print_stack_trace),
183+
make_native_print_stack_trace(silent, gas_params.print_stack_trace),
138184
),
139185
];
140186

move-stdlib/src/natives/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ impl NurseryGasParameters {
157157
}
158158

159159
pub fn nursery_natives(
160+
silent: bool,
160161
move_std_addr: AccountAddress,
161162
gas_params: NurseryGasParameters,
162163
) -> NativeFunctionTable {
@@ -171,7 +172,10 @@ pub fn nursery_natives(
171172
}
172173

173174
add_natives!("event", event::make_all(gas_params.event));
174-
add_natives!("debug", debug::make_all(gas_params.debug, move_std_addr));
175+
add_natives!(
176+
"debug",
177+
debug::make_all(silent, gas_params.debug, move_std_addr)
178+
);
175179

176180
make_table_from_iter(move_std_addr, natives)
177181
}

move-stdlib/tests/move_unit_test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ fn run_tests_for_pkg(path_to_pkg: impl Into<String>, include_nursery_natives: bo
2020
);
2121
if include_nursery_natives {
2222
natives.extend(nursery_natives(
23+
/* silent */ false,
2324
AccountAddress::from_hex_literal("0x1").unwrap(),
2425
NurseryGasParameters::zeros(),
2526
))

tools/move-cli/src/main.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ fn main() -> Result<()> {
1212
let addr = AccountAddress::from_hex_literal("0x1").unwrap();
1313
let natives = all_natives(addr, GasParameters::zeros())
1414
.into_iter()
15-
.chain(nursery_natives(addr, NurseryGasParameters::zeros()))
15+
.chain(nursery_natives(
16+
/* silent */ false,
17+
addr,
18+
NurseryGasParameters::zeros(),
19+
))
1620
.collect();
1721

1822
move_cli::move_cli(natives, cost_table, &error_descriptions)

0 commit comments

Comments
 (0)