Skip to content
  • Richard W.M. Jones's avatar
    lib: Add dynamic validation of struct nbd_handle · 39473b23
    Richard W.M. Jones authored
    Add a magic field into struct nbd_handle, and assert that this is set
    in most libnbd calls.  For a small overhead this adds additional
    checks against some kinds of memory corruption and clients passing
    invalid pointers.
    
    Notes:
    
    - Cause an assert fail rather than an error return as these
      would be unexpected internal errors.
    
    - Check happens outside the handle lock, which should be safe and
      slightly reduces the overhead in parallel cases (and is necessary
      because we can only take the handle lock after checking that the
      handle is valid).
    
    - Use a small constant so that the check can be turned into a single
      instruction (with immediate) on x86-64:
    
          11:       48 81 3f bd bd 00 00    cmpq   $0xbdbd,(%rdi)
    
      Larger constants need 'movabs' and consume an extra register while
      being no safer, for example:
    
           4:       48 b8 cb cb cb cb cb    movabs $0xbdbdbcbcbcbcbcb,%rax
          1b:       48 39 07                cmp    %rax,(%rdi)
    39473b23