如何在内存访问时禁用地址检查功能。

huangapple go评论62阅读模式
英文:

How to disable address Sanitizer Checking Function when memory access

问题

I wonder that how to disable address sanitizer insert checking function when memory access.
As I know that address sanitizer pass insert the checking function to detect out of access or buffer overflow..etc.[(https://github.com/llvm/llvmproject/blob/main/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp) ]

I want to disable that address sanitizer insert the checking function.
Is there any flag to disable to insert checking function?
Or, How to disable address sanitizer code to check?

Thank you!
Have a nice day 如何在内存访问时禁用地址检查功能。

英文:

I wonder that how to disable address sanitizer insert checking function when memory access.
As I know that address sanitizer pass insert the checking function to detect out of access or buffer overflow..etc.[(https://github.com/llvm/llvmproject/blob/main/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp) ]

I want to disable that address sanitizer insert the checking function.
Is there any flag to disable to insert checking function?
Or, How to disable address sanitizer code to check?

Thank you!
Have a nice day 如何在内存访问时禁用地址检查功能。

I expect to check the code line in AddressSanitizer.cpp

答案1

得分: 2

你不能单独禁用仪器的不同部分 - 仪器是综合进行的,既用于检查内存访问,又用于维护相关的元数据。如果你想要跟踪类似Address Sanitizer的元数据但不进行其检查,你需要编写一个新的sanitizer pass来实现这个功能。

然而,你可以以更精细的方式禁用Address Sanitizer。例如,你可以将一些文件编译时使用-fsanitize=address,而另一些文件则不使用。你还可以使用函数属性来禁用它:

https://clang.llvm.org/docs/AddressSanitizer.html#disabling-instrumentation-with-attribute-no-sanitize-address

你可以在这里看到它的示例:
https://cpp.compiler-explorer.com/z/rj95nKMY8

#include <iostream>

__attribute__((no_sanitize("address")))
int uninstrumented_access(int* array, int index) {
  return array[index];
}

int access(int* array, int index) {
  return array[index];
}

int main() {
  int *array = new int[10]();

  (void)uninstrumented_access(array, 42);
  std::cerr << "No ASan error from un-instrumented access due to attribute!\n";

  (void)access(array, 42);
  std::cerr << "No ASan error even from instrumented access!\n";
}

在这里,uninstrumented_access 禁用了所有ASan的检查。如果你查看编译器资源管理器的输出,你会看到该函数没有ASan错误,但正常版本的函数会产生错误。

英文:

You can't disable different parts of the instrumentation independently - the instrumentation is done holistically both to check memory accesses and maintain the relevant metadata. If you want to track metadata similar to Address Sanitizer but w/o its checking, you'd have to write a new sanitizer pass to implement that.

However, you can disable Address Sanitizer on a more granular basis. For example, you con link together files where some are compiled with -fsanitize=address and some are not. You can also disable it using a function attribute:

https://clang.llvm.org/docs/AddressSanitizer.html#disabling-instrumentation-with-attribute-no-sanitize-address

You can see this in action here:
https://cpp.compiler-explorer.com/z/rj95nKMY8

#include <iostream>

__attribute__((no_sanitize("address")))
int uninstrumented_access(int* array, int index) {
  return array[index];
}

int access(int* array, int index) {
  return array[index];
}

int main() {
  int *array = new int[10]();

  (void)uninstrumented_access(array, 42);
  std::cerr << "No ASan error from un-instrumented access due to attribute!\n";

  (void)access(array, 42);
  std::cerr << "No ASan error even from instrumented access!\n";
}

Here, uninstrumented_access disables all of the ASan checks. If you look at the Compiler Explorer output, you'll see that there is no ASan error from that function, but the normal version of it does produce an error.

huangapple
  • 本文由 发表于 2023年1月9日 15:46:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054358.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定