- 
                Notifications
    You must be signed in to change notification settings 
- Fork 305
          Use Into<Message> in signing api
          #755
        
          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
          
     Merged
      
      
    
  
     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
    
  
  
    
              
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  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.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.
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 don't get why we have a local var here but on line 262 we just chain the calls? Did you mean to change both?
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.
We need the local variable to ensure that the object lives through the entire time that we are using the pointer.
If we're still chaining the calls on line 262 then that also needs to be changed. Thanks for beating me to the review!
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.
Thanks, I thought as much.
Out of interest how can the value not exist the entire time we are using it when its pass by value, so is part of the stack frame, calling
Intois just borrow checker stuff andas_c_ptris type checker stuff, neither of which effects the actual data (I think). Then the value is used in a single function call (the ffi function call).Uh oh!
There was an error while loading. Please reload this page.
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'm not sure what you mean by
Intobeing "just borrow checker stuff".Into::intotakes an abstract message by value and returns aMessageby value. Thenas_c_ptrborrows this object and returns a raw pointer whose lifetime must not exceed the lifetime of theMessage. The borrow checker is barely involved with any of this, and even if it was, it can only do sanity checks; it never affects the semantics of code.But if we don't give the
Messagea variable binding, its lifetime will only consist of one line of code. If we give it a binding it'll live until the end of the function.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.
@tcharding note that this is the same underlying issue as this lint: https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html#temporary-cstring-as-ptr
(one day we can write an attribute to add these lints for user types)
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 just musings and for my education, so please only respond if it amuses you to do so.
When
sign_ecdsa_with_noncedata_pointeris compiled is the parametrmsg: impl Into<Message>,just 32 bytes in the stack frame?I misspoke, FTR I don't know the exact correct terminology for all the parses of the compiler.
I read the link above but that is different in that a
strhas to have memory backing it but in our case I thought the memory backing it would be the 32 bytes in the stack frame that was passed in asmsg(in functionsign_ecdsa_with_noncedata_pointer) - so I can't understand why creating a local variable is fixing the problem.Said another way, I get that at the end of
msg.into().as_c_ptr()that there are no guarantees that the pointer is valid, but I don't get why we cannot tell it is valid because we know where the value is on the stack already because it was passed in with the function.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 think I can map C functions to opcodes but I do not know exactly how to map Rust functions to opcodes.)
Uh oh!
There was an error while loading. Please reload this page.
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.
@tcharding it's because here it's used in a loop.
impl Into<Message>doesn't implementCopy, so if we.intoit inside the loop we lose ownership and cannot proceed with more iterations.By calling
.intooutside of the loop, we get theMessagetype which implementsCopyso it can be used inside the loop as many times as needed.@apoelstra interesting. So once Rust passes
.into().as_c_ptr()to the ffi, it marks the memory as unused and so that memory could be recycled before the C code finishes doing what it needs with that memory slice? I wonder if that is what was causing the UB in the release build.I've updated all instances of chaining to have the
.into()in a higher scope so the memory is held onto.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.
How could it be? The 32 bytes in the stack frame belong to an object of a completely different type (an opaque
Into<Message>vs aMessage). And we are telling the compiler that we don't need theInto<Message>anymore and it can reclaim the memory.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 need to read a book on how the Rust compiler works. Thanks for your patience.