- 
                Notifications
    
You must be signed in to change notification settings  - Fork 15.1k
 
[msan] Sharpen instrumentation of Intrinsic::{ctlz,cttz} #145609
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 4 commits
      Commits
    
    
            Show all changes
          
          
            6 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      13736a6
              
                [msan] Sharpen instrumentation of Intrinsic::{ctlz,cttz}
              
              
                thurstond 3cee7d1
              
                Add comment about possible further improvement
              
              
                thurstond 358dc26
              
                Typo
              
              
                thurstond 3656c9e
              
                Remove stray change
              
              
                thurstond 23e3b65
              
                Clarify little-endian (and ctlz)
              
              
                thurstond fb7c2a7
              
                Clarify zero-poison comment
              
              
                thurstond 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 | 
|---|---|---|
| 
          
            
          
           | 
    @@ -3278,22 +3278,49 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> { | |
| setOrigin(&I, getOrigin(Op)); | ||
| } | ||
| 
     | 
||
| void handleCountZeroes(IntrinsicInst &I) { | ||
| // Uninitialized bits are ok if they appear after the leading/trailing 0's | ||
| // and a 1. If the input is all zero, it is fully initialized iff | ||
| // !is_zero_poison. | ||
| // | ||
| // e.g., if 0/1 are initialized bits with concrete value 0/1, and ? is an | ||
| // uninitialized bit: | ||
                
       | 
||
| // - 0001 0??? is fully initialized | ||
| // - 000? ???? is fully uninitialized (*) | ||
| // - ???? ???? is fully uninitialized | ||
| // - 0000 0000 is fully initialized iff !is_zero_poison | ||
| // (*) TODO: arguably, since the number of zeros is in the range [3, 8], we | ||
| // only need to poison 4 bits. | ||
| // | ||
| // OutputShadow = | ||
| // ((ConcreteZerosCount >= ShadowZerosCount) && !AllZeroShadow) | ||
| // || (is_zero_poison && AllZeroSrc) | ||
| void handleCountLeadingTrailingZeros(IntrinsicInst &I) { | ||
| IRBuilder<> IRB(&I); | ||
| Value *Src = I.getArgOperand(0); | ||
| Value *SrcShadow = getShadow(Src); | ||
| 
     | 
||
| Value *False = IRB.getInt1(false); | ||
| Value *ConcreteZerosCount = IRB.CreateIntrinsic( | ||
| I.getType(), I.getIntrinsicID(), {Src, /*is_zero_poison=*/False}); | ||
| Value *ShadowZerosCount = IRB.CreateIntrinsic( | ||
| I.getType(), I.getIntrinsicID(), {SrcShadow, /*is_zero_poison=*/False}); | ||
| 
     | 
||
| // Set the Output shadow based on input Shadow | ||
| Value *BoolShadow = IRB.CreateIsNotNull(getShadow(Src), "_mscz_bs"); | ||
| Value *CompareConcreteZeros = IRB.CreateICmpUGE( | ||
| ConcreteZerosCount, ShadowZerosCount, "_mscz_cmp_zeros"); | ||
| 
     | 
||
| Value *NotAllZeroShadow = | ||
| IRB.CreateIsNotNull(SrcShadow, "_mscz_shadow_not_null"); | ||
| Value *OutputShadow = | ||
| IRB.CreateAnd(CompareConcreteZeros, NotAllZeroShadow, "_mscz_main"); | ||
| 
     | 
||
| // If zero poison is requested, mix in with the shadow | ||
| Constant *IsZeroPoison = cast<Constant>(I.getOperand(1)); | ||
| if (!IsZeroPoison->isZeroValue()) { | ||
| Value *BoolZeroPoison = IRB.CreateIsNull(Src, "_mscz_bzp"); | ||
| BoolShadow = IRB.CreateOr(BoolShadow, BoolZeroPoison, "_mscz_bs"); | ||
| OutputShadow = IRB.CreateOr(OutputShadow, BoolZeroPoison, "_mscz_bs"); | ||
| } | ||
| 
     | 
||
| Value *OutputShadow = | ||
| IRB.CreateSExt(BoolShadow, getShadowTy(Src), "_mscz_os"); | ||
| OutputShadow = IRB.CreateSExt(OutputShadow, getShadowTy(Src), "_mscz_os"); | ||
| 
     | 
||
| setShadow(&I, OutputShadow); | ||
| setOriginForNaryOp(I); | ||
| 
          
            
          
           | 
    @@ -4710,7 +4737,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> { | |
| break; | ||
| case Intrinsic::ctlz: | ||
| case Intrinsic::cttz: | ||
| handleCountZeroes(I); | ||
| handleCountLeadingTrailingZeros(I); | ||
| break; | ||
| case Intrinsic::masked_compressstore: | ||
| handleMaskedCompressStore(I); | ||
| 
          
            
          
           | 
    ||
  
    
      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.
        
    
  
  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.