[compiler-rt] [compiler-rt][ASan] Remove alignment checks in ASan error reporting (PR #94103)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 23 12:27:47 PDT 2024


AdvenamTacet wrote:

Example showing that `__sanitizer_annotate_contiguous_container` works with unaligned buffers: [godbolt](https://godbolt.org/#g:!((g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,selection:(endColumn:2,endLineNumber:15,positionColumn:1,positionLineNumber:1,selectionStartColumn:2,selectionStartLineNumber:15,startColumn:1,startLineNumber:1),source:'%23include+%3Csanitizer/asan_interface.h%3E%0A%23include+%3Ciostream%3E%0A%0Aconstexpr+size_t+N+%3D+64%3B%0Aconstexpr+size_t+off+%3D+1%3B%0A%0Aint+main()+%7B%0A%0A++++char+buffer%5BN+%2B+off%5D%3B%0A++++char+*beg+%3D+buffer+%2B+off%3B%0A++++char+*end+%3D+beg+%2B+N%3B%0A%0A++++__sanitizer_annotate_contiguous_container(beg,+end,+end,+beg)%3B%0A++++std::cout+%3C%3C+beg%5B0%5D+%3C%3C+std::endl%3B%0A%7D'),l:'5',n:'1',o:'C%2B%2B+source+%231',t:'0')),k:62.5,l:'4',m:50,n:'0',o:'',s:0,t:'0'),(g:!((h:output,i:(compilerName:'x86-64+gcc+13.2',editorid:1,fontScale:14,fontUsePx:'0',j:1,wrap:'1'),l:'5',n:'0',o:'Output+of+x86-64+clang+18.1.0+(Compiler+%231)',t:'0')),header:(),l:'4',m:50,n:'0',o:'',s:0,t:'0')),k:62.5,l:'3',n:'0',o:'',t:'0'),(g:!((h:compiler,i:(compiler:clang1810,filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',debugCalls:'1',demangle:'0',directives:'0',execute:'0',intel:'0',libraryCode:'0',trim:'1',verboseDemangling:'0'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,libs:!(),options:'-fsanitize%3Daddress+-std%3Dlibc%2B%2B+-std%3Dc%2B%2B20',overrides:!(),selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:'5',n:'0',o:'+x86-64+clang+18.1.0+(Editor+%231)',t:'0')),header:(),k:37.5,l:'4',m:100,n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4)

```cpp
#include <sanitizer/asan_interface.h>
#include <iostream>

constexpr size_t N = 64;
constexpr size_t off = 1;

int main() {

    char buffer[N + off];
    char *beg = buffer + off;
    char *end = beg + N;

    __sanitizer_annotate_contiguous_container(beg, end, end, beg);
    std::cout << beg[0] << std::endl;
}
```

This example correctly detects container overflow (whole buffer is poisoned).
```python
=>0x7a1ccb000000: f1 f1 f1 f1[01]fc fc fc fc fc fc fc fc f3 f3 f
```

To get this message, it's enough to provide incorrect arguments to the function, for example providing `old_mid` outside of the container, like `__sanitizer_annotate_contiguous_container(beg, end, end+1, beg);`.
Then [we get](https://godbolt.org/z/4Yrsvd8es):
```cpp
==1==ERROR: AddressSanitizer: bad parameters to __sanitizer_annotate_contiguous_container:
      beg     : 0x74ab7b600021
      end     : 0x74ab7b600061
      old_mid : 0x74ab7b600062
      new_mid : 0x74ab7b600021
==1==ERROR: beg is not aligned by 8
```

Clearly `beg` not being aligned by 8 is not a problem here.

---

Those are only two lines with that error message.
```bash
$ grep -R -I -H "is not aligned by"
compiler-rt/lib/asan/asan_errors.cpp:    Report("ERROR: beg is not aligned by %zu\n", granularity);
compiler-rt/lib/asan/asan_errors.cpp:    Report("ERROR: storage_beg is not aligned by %zu\n", granularity);
llvm/test/tools/llvm-objcopy/ELF/binary-paddr.test:## PAddr is not aligned by sh_addralign(.data)
$
```

---

@vitalybuka do you think adding anything to that PR makes sense? If yes, what test do you have in mind?

https://github.com/llvm/llvm-project/pull/94103


More information about the llvm-commits mailing list