This repository was archived by the owner on Nov 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
WASM: Add some DOS protection #8084
Merged
Changes from all commits
Commits
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
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 |
|---|---|---|
|
|
@@ -173,12 +173,14 @@ impl<'a> Runtime<'a> { | |
| /// Intuition about the return value sense is to aswer the question 'are we allowed to continue?' | ||
| fn charge_gas(&mut self, amount: u64) -> bool { | ||
| let prev = self.gas_counter; | ||
| if prev + amount > self.gas_limit { | ||
| // exceeds gas | ||
| false | ||
| } else { | ||
| self.gas_counter = prev + amount; | ||
| true | ||
| match prev.checked_add(amount) { | ||
| // gas charge overflow protection | ||
| None => false, | ||
| Some(val) if val > self.gas_limit => false, | ||
| Some(_) => { | ||
| self.gas_counter = prev + amount; | ||
| true | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -548,13 +550,13 @@ impl<'a> Runtime<'a> { | |
|
|
||
| fn debug(&mut self, args: RuntimeArgs) -> Result<()> | ||
| { | ||
| let msg_ptr: u32 = args.nth_checked(0)?; | ||
| let msg_len: u32 = args.nth_checked(1)?; | ||
|
|
||
| let msg = String::from_utf8(self.memory.get(msg_ptr, msg_len as usize)?) | ||
| .map_err(|_| Error::BadUtf8)?; | ||
| trace!(target: "wasm", "Contract debug message: {}", { | ||
| let msg_ptr: u32 = args.nth_checked(0)?; | ||
| let msg_len: u32 = args.nth_checked(1)?; | ||
|
|
||
| trace!(target: "wasm", "Contract debug message: {}", msg); | ||
| String::from_utf8(self.memory.get(msg_ptr, msg_len as usize)?) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will evaluate argument only when traces are |
||
| .map_err(|_| Error::BadUtf8)? | ||
| }); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will protect if someone charges gas manually and causes overflow