generated from bazel-contrib/rules-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
164 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include "cc_asan_test_with_automatic_reset/lib.h" | ||
|
||
#include <iostream> | ||
|
||
int main() { | ||
trigger_asan(); | ||
std::cerr << "Did not get expected AddressSanitizer error, is the test instrumented correctly?" << std::endl; | ||
return 1; | ||
} |
File renamed without changes.
File renamed without changes.
This file contains 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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#include "cc_asan_test_with_automatic_reset/lib.h" | ||
|
||
int trigger_asan() { | ||
int* array = new int[100] {}; | ||
delete[] array; | ||
// Triggers a heap-use-after-free error. | ||
return array[0]; | ||
} |
File renamed without changes.
7 changes: 7 additions & 0 deletions
7
examples/cc_asan_test_with_automatic_reset/third_party/BUILD.bazel
This file contains 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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# This is a mock of a large third-party dependency that should not be built with | ||
# AddressSanitizer to reduce build time. | ||
cc_library( | ||
name = "large_dep", | ||
srcs = ["large_dep.cpp"], | ||
visibility = ["//cc_asan_test_with_automatic_reset:__pkg__"], | ||
) |
File renamed without changes.
This file contains 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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
load("@with_cfg.bzl", "original_settings") | ||
load(":cc_asan_test.bzl", "cc_asan_test", "cc_asan_test_reset") | ||
|
||
cc_asan_test( | ||
name = "asan_test", | ||
srcs = ["asan_test.cpp"], | ||
env = { | ||
# Effectively invert the exit code so that the test passes if and only | ||
# if the expected ASAN error is detected. | ||
"ASAN_OPTIONS": "exitcode=0:abort_on_error=0", | ||
}, | ||
deps = [ | ||
":lib", | ||
], | ||
) | ||
|
||
cc_library( | ||
name = "lib", | ||
srcs = ["lib.cpp"], | ||
hdrs = ["lib.h"], | ||
deps = [":large_dep_reset"], | ||
) | ||
|
||
# This ensures that the "large" third-party dependency is built with the | ||
# original top-level settings, that is, without AddressSanitizer. | ||
cc_asan_test_reset( | ||
name = "large_dep_reset", | ||
exports = "//cc_asan_test_with_manual_reset/third_party:large_dep", | ||
) | ||
|
||
original_settings( | ||
name = "cc_asan_test_original_settings", | ||
) |
2 changes: 1 addition & 1 deletion
2
...les/cc_asan_test_with_reset/asan_test.cpp → ...asan_test_with_manual_reset/asan_test.cpp
This file contains 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#include "cc_asan_test_with_reset/lib.h" | ||
#include "cc_asan_test_with_manual_reset/lib.h" | ||
|
||
#include <iostream> | ||
|
||
|
This file contains 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
load("@with_cfg.bzl", "with_cfg") | ||
|
||
_builder = with_cfg(native.cc_test) | ||
_builder.extend("copt", ["-fsanitize=address"]) | ||
_builder.extend("linkopt", select({ | ||
# link.exe doesn't require or recognize -fsanitize=address and would emit a warning. | ||
Label("@rules_cc//cc/compiler:msvc-cl"): [], | ||
"//conditions:default": ["-fsanitize=address"], | ||
})) | ||
_builder.resettable(Label(":cc_asan_test_original_settings")) | ||
cc_asan_test, cc_asan_test_reset = _builder.build() |
2 changes: 1 addition & 1 deletion
2
examples/cc_asan_test_with_reset/lib.cpp → ...es/cc_asan_test_with_manual_reset/lib.cpp
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#pragma once | ||
|
||
int trigger_asan(); |
This file contains 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
10 changes: 10 additions & 0 deletions
10
examples/cc_asan_test_with_manual_reset/third_party/large_dep.cpp
This file contains 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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// This file is set up to fail when compiled with AddressSanitizer. We use it to | ||
// simulate a large third-party library that shouldn't be instrumented with | ||
// sanitizers. | ||
|
||
#ifndef __has_feature | ||
#define __has_feature(x) 0 // Compatibility with non-clang compilers. | ||
#endif | ||
#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) | ||
#error "large_dep.cpp should not be compiled with ASAN" | ||
#endif |
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
load(":setting.bzl", "get_attr_type", "validate_and_get_attr_name") | ||
|
||
visibility("private") | ||
|
||
def make_transitioned_rule(*, rule_info, transition, values): | ||
settings_attrs = { | ||
validate_and_get_attr_name(setting): getattr(attr, get_attr_type(value))() | ||
for setting, value in values.items() | ||
} | ||
return rule( | ||
implementation = _transitioned_rule_impl, | ||
parent = rule_info.kind, | ||
cfg = transition, | ||
initializer = lambda **kwargs: _initializer_base(kwargs = kwargs, values = values), | ||
attrs = settings_attrs, | ||
) | ||
|
||
def _transitioned_rule_impl(ctx): | ||
return ctx.super() | ||
|
||
def _initializer_base(*, kwargs, values): | ||
return kwargs | { | ||
validate_and_get_attr_name(setting): value | ||
for setting, value in values.items() | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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