Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime: Add null pointer exception for read #2780

Merged
merged 6 commits into from
Sep 9, 2021

Conversation

evaporei
Copy link
Contributor

@evaporei evaporei commented Sep 8, 2021

This can be reviewed commit by commit 🙂

Before this PR users would get the Subtract overflow error in apiVersion 0.0.5, by using operator overloading from types like BigInt:

image

This PR aims at making the error message more clear. Also there are tests for that case as well, the AS code for it should be self explanatory.

Also, this message will be avoided for operator overloading in graph-ts just like the SafeBigInt did on the tests. PR for this: graphprotocol/graph-ts#204

@evaporei evaporei self-assigned this Sep 8, 2021
Copy link
Collaborator

@lutter lutter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

"Subtract overflow on pointer: {}", // Usually when pointer is zero because of null in AssemblyScript
self.0
))
DeterministicHostError(anyhow::anyhow!("Subtract overflow on pointer: {}", self.0))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we say something more helpful here (like what that means in terms of pointers)? Is this always an internal error? And it seems that size_of_rt_size could be const.

Copy link
Contributor Author

@evaporei evaporei Sep 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're correct, the size of rt_size can be changed to a const, I'll change that.

This Subtract overflow shouldn't happen, it's related to the way every type is layed out in memory.

All class instances in AssemblyScript have a header of 20 bytes, of which the last 4 hold the size of the following data.

AS header memory layout docs: https://www.assemblyscript.org/memory.html#common-header-layout.

Our AscPtr structure holds a number which is the address in WebAssembly memory of where that class instance starts their data (right after the header).

So this part of the code tries to do a pointer arithmetic to get the size of the structure, so it can read it fully in a correct fashion.

Maybe what I can write instead of this message is that it failed to find the size of the following structure.

The part about being an internal/external error, in this case it actually can appear in the explorer logs, like Adam found them. It will either happen when we messed up (me in this case, cause I did this code haha) the AS memory layout, or AS is writing data to memory that doesn't conform to the especification.

Copy link
Contributor Author

@evaporei evaporei Sep 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the constant but I'll let these (there are more) "Subtract overflow" to fix later. I want to discuss more with @leoyvens on this. Thanks for your input!

/// this function does this error handling.
///
/// Summary: ALWAYS call this before reading an AscPtr.
pub fn assert_is_not_null(&self) -> Result<(), DeterministicHostError> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor, but it would be better to call this check_is_not_null, when I see assert I expect that the code could panic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense! I'll change this rn :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

@evaporei evaporei merged commit baefb00 into master Sep 9, 2021
@evaporei evaporei deleted the otavio/add-null-ptr-message branch September 9, 2021 11:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants