-
Notifications
You must be signed in to change notification settings - Fork 116
Linter PR 6: eliminate usage of unsafe #653
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
base: main
Are you sure you want to change the base?
Conversation
- Replaced unsafe calls to `go_error.into_result` with `go_error.into_result_safe` in `api.rs` and `storage.rs` to enhance safety and eliminate unsafe blocks. - Removed `SafeUnmanagedVector` from imports and code where it was unnecessary. - Added a new method `into_result_safe` in `GoError` to ensure safe error handling without risking reuse of error messages.
- Updated `GoIter` and `GoQuerier` implementations to replace unsafe calls to `go_result.into_result` with `go_result.into_result_safe`, enhancing safety in error handling. - This change improves the overall robustness of the Go API by eliminating unsafe blocks in the iterator and querier modules.
- Modified error messages in `iterator_test.go` and `lib_test.go` to provide more specific feedback regarding gas limit and checksum format issues. - Enhanced clarity of error reporting to improve debugging and user experience.
…king - Introduced a new example in `debug_vectors.go` to demonstrate vector debugging capabilities. - Enhanced `SafeUnmanagedVector` to track consumption state and provide detailed debug information, including stack traces for consumption attempts. - Added functions to enable vector debugging and retrieve vector creation/consumption statistics. - Updated error handling in `StoreCode` and `StoreCodeUnchecked` to ensure proper validation and error messaging.
… for safer memory management - Updated comments to clarify the purpose of contract functions. - Replaced instances of copyAndDestroyUnmanagedVector with the safer CopyAndDestroyToSafeVector pattern across multiple contract functions, enhancing memory safety. - Introduced a new receiveVectorSafe function to handle UnmanagedVector safely, preventing potential double-free issues.
…or improved memory safety - Replaced all instances of copyAndDestroyUnmanagedVector with CopyAndDestroyToSafeVector in contract functions to enhance memory management. - Updated comments to reflect the safer pattern being implemented across the codebase.
- Updated test cases to replace instances of copyAndDestroyUnmanagedVector with CopyAndDestroyToSafeVector, ensuring consistent use of safer memory management practices. - Enhanced comments to clarify the safer approach being implemented in the tests.
- Updated the `Instantiate`, `Execute`, and `Query` methods to use a default `deserCost` value of 1/10000 gas per byte as defined in the VMConfig. - Replaced direct JSON unmarshalling with `DeserializeResponse` to account for gas costs in the contract function implementations. - Improved comments to clarify the changes made for gas cost management.
…d unmanaged vector functions - Introduced `is_available` method in `SafeByteSlice` to check if the byte slice is not consumed and not nil, enhancing defensive programming. - Added early return in `destroy_unmanaged_vector` to avoid unnecessary consumption of a nil vector. - Implemented early checks in `safe_unmanaged_vector_to_bytes` to prevent consuming already consumed vectors. - Updated test assertions for clarity and conciseness in error handling for consumed vectors.
- Introduced #[allow(non_camel_case_types)] attribute to the cache_t struct to suppress warnings related to naming conventions, improving code clarity and compliance with Go's conventions.
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.
I will not review a 7000+ additions PR that's a total mess of all kinds of changes. I simply do not have time for that. Please keep your PRs separate unless it's absolutely necessary for them to depend on each other.
Looking at some of the functions you wrote here, I am not convinced this is as big of an improvement as you make it out to be, so I will not review further until there is a minimal PR with only the necessary changes for your SafeUnmanagedVector
idea.
let _ = v.consume(); | ||
// Wrap in SafeUnmanagedVector for safer handling | ||
let mut safe_vector = SafeUnmanagedVector { | ||
inner: v, | ||
consumed: false, | ||
}; | ||
|
||
// If the vector is None, we don't need to consume it | ||
if safe_vector.inner.is_none() { | ||
return; | ||
} | ||
|
||
// This will prevent double consumption by setting consumed flag | ||
// and returning an error if already consumed | ||
if let Err(e) = safe_vector.consume() { | ||
// Log error but don't crash - better than double free | ||
eprintln!("Error during vector destruction: {}", e); | ||
} | ||
} |
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 is complete nonsense and doesn't make this safer at all. You start out by creating a new SafeUnmanagedVector
with consumed
set to false
, so your whole codepath just boils down to calling safe_vector.inner.consume()
anyways, just with a lot of unnecessary fluff around it.
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 is what I needed!
thank you!
safeVec := CopyAndDestroyToSafeVector(errmsg) | ||
errMsg := string(safeVec.ToBytesAndDestroy()) |
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.
None of the uses of CopyAndDestroyToSafeVector
make sense here. You always just call ToBytesAndDestroy
immediately anyways. In that case, you might as well just use the existing copyAndDestroyUnmanagedVector
directly, which returns perfectly safe []byte
to you.
Using CopyAndDestroyToSafeVector
gives no additional safety at all. Nothing stops you from calling it twice and therefore double-freeing.
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.
I was worried about this.
hey @pinosu -- to be clear, this is exactly the kind of guidance I needed! I will look into the rest, no worries, but I also want to provide you with some guidance -- the critical-path PR, that I must ask that you review is #589 , and reviewing that will greatly reduce the scope of others. But in the interests of getting things done, and because I know the dangers of giant crazy PRs I'm going to break that down into other PRs, which work exactly linter per linter. But pelase, treat 589 as the critical one because we need to ensure that we don't do stupid things, that's really all the linter does. I found a lot of them but that's no problem everyone does stupid things from time to time just please don't take it as an affront because it isn't meant that way. |
the worst part is that on second thought you really may want to review this PR. Tangled up in my linting I found a lot of stuff that's definitely a bug. |
Sorry, input-validation 2 PLEASE READ IT IT CONTAINS THINGS THAT ARE BAD THANK YOU LAST FEW COMMITS |
This pull request makes the use of unsafe code much less common in wasmvm, and makes a number of other tightenings and enhancements.
Chat log from chat with Skip/ICL