2013-04-21 36 views
27

Tôi muốn sử dụng vệ sinh địa chỉ clang trên OSX Mountain Lion, vì Valgrind gặp vấn đề với kiểm tra bộ nhớ trên nền tảng này. Nhưng khi tôi đã có -fsanitize=address trong thời gian biên dịch (như tôi nhìn thấy trên trang này: http://clang.llvm.org/docs/AddressSanitizer.html), tôi đã nhận lỗi này: clang: error: argument unused during compilation: '-fsanitize=address'Clang Địa chỉ Sanitizer trên OS X

Vì vậy, câu hỏi của tôi là làm thế nào để sử dụng Clang Địa chỉ Sanitizer trên OS X? Nếu tôi không thể sử dụng nó, tôi có thể sử dụng công cụ nào?

Tôi đã tải xuống clang bằng Xcode và đã được cập nhật. (Có thể phiên bản này vẫn chưa giải quyết khử trùng xây dựng với nó)

Trả lời

6

Theo nguồn tin (đặc biệt là nếu bạn lấy clang-425.0.24 bó từ Apple's Open Source Site, file test src/tools/clang/test/Driver/darwin-asan-nofortify.c:

// rdar://11496765, rdar://12417750 
// -faddress-sanitizer is not currently supported. 
// XFAIL: * 

Và, tất nhiên, có là một lỗi sử dụng -faddress-sanitizer, vì vậy nó trông giống như dưới OS X, bạn sẽ cần phải xây dựng cho mình Clang từ nguồn gần đây để có được khử trùng địa chỉ.

tùy chọn dòng lệnh

Hãy thử sử dụng -faddress-sanitizer thay vì -fsanitize=address. Phiên bản Apple dường như dựa trên phiên bản cũ hơn của CLANG và nếu bạn chạy clang --help, các chất khử trùng có tất cả dạng này trong phiên bản này.

+4

Tôi đã nhận ra lỗi quá: 'kêu vang: lỗi: tùy chọn không được hỗ trợ '-faddress-sanitizer''. Tuy nhiên, trong 'clang --help' tôi có thể thấy tùy chọn này – Guillaume

+0

@gaige: vấn đề là Apple đang triển khai một phiên bản ** cũ hơn của Clang, sử dụng' -faddress-sanitizer' cũ. Mới hơn '-fsanitize = address' đã tham gia sau, khi các chất khử trùng khác được thêm vào. – 0xC0000022L

+0

@ 0xC0000022L Xin lỗi, tôi nghĩ rằng tôi đã làm cho điểm đó ở trên khi tôi nói "bạn sẽ cần phải xây dựng clang mình từ một nguồn gần đây hơn". Phải thừa nhận rằng, nó không nói rõ ràng rằng phiên bản của Apple là cũ hơn, nhưng nó đề nghị xây dựng từ một phiên bản mới hơn. Tôi sẽ cập nhật ngôn ngữ để làm cho nó rõ ràng hơn. – gaige

12

Địa chỉ Sanitizer đã được thêm vào như là một tính năng mới trong Xcode 7.

Sử dụng cờ Runtime Sanitization>Enable Address Sanitizer trong chương trình của bạn để kích hoạt tùy chọn.

git sau đó sẽ thể hiện sự thay đổi này để .xcscheme tập tin của bạn:

enableAddressSanitizer = "YES" 

Từ New Features in Xcode 7 tài liệu:

Address Sanitizer. Xcode 7 can build your app with instrumentation designed to catch and debug memory corruption using the address sanitizer.

Objective-C and C code is susceptible to memory corruption issues such as stack and heap buffer overruns and use-after-free issues. When these memory violations occur, your app can crash unpredictably or display odd behavior. Memory corruption issues are difficult to track down because the crashes and odd behavior are often hard to reproduce and the cause can be far from the origin of the problem.

Enable Address Sanitizer

You enable the address sanitizer in the build scheme. Once enabled, added instrumentation is built into the app to catch memory violations immediately, enabling you to inspect the problem right at the place where it occurs. Other diagnostic information is provided as well, such as the relationship between the faulty address and a valid object on the heap and allocation/deallocation information, which helps you pinpoint and fix the problem quickly.

Address sanitizer is efficient—fast enough to be used regularly, as well as with interactive applications. It is supported on OS X, in the Simulator, and on iOS devices.