Skip to content

Commit ccecba0

Browse files
Ari SaifAri Saif
authored andcommitted
Clean up and fixed broken tests
1 parent f34e73e commit ccecba0

10 files changed

Lines changed: 151 additions & 220 deletions

File tree

.bazelrc

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@
33

44
# Address sanitizer config
55
# Use with bazel run --config=asan or lsan
6-
build:asan --strip=never
7-
build:asan --copt -fsanitize=address
8-
build:asan --copt -DADDRESS_SANITIZER
9-
build:asan --copt -O0
10-
build:asan --copt -g
11-
build:asan --copt -fno-omit-frame-pointer
12-
build:asan --linkopt -fsanitize=address
13-
build:asan --sandbox_debug
14-
build:asan --spawn_strategy=standalone
6+
build:asan --strip=never # Keep symbols
7+
build:asan --copt=-fsanitize=address # Enable address sanitizer
8+
build:asan --copt=-DADDRESS_SANITIZER # Optional: only needed if your code uses this macro
9+
build:asan --copt=-O0
10+
build:asan --copt=-g # Debug info
11+
build:asan --copt=-fno-omit-frame-pointer # Required for stack traces
12+
build:asan --linkopt=-fsanitize=address # Link with ASAN
13+
build:asan --sandbox_debug # Optional: for easier debugging
14+
build:asan --spawn_strategy=standalone # Good for sanitizers (avoid sandbox hiding issues)
15+
16+
# Leak sanitizer config
17+
build:lsan --copt=-fsanitize=leak
18+
build:lsan --linkopt=-fsanitize=leak
19+
20+
1521

1622
# undefined behavior sanitizer config
1723
build:ubsan --strip=never

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ You can use this template for most of your C++ projects with minimal changes.
2828
## Prerequisite: Installing Bazel
2929

3030
This repo uses `Bazel` for building C++ files.
31-
You can install Bazel using this [link](https://docs.bazel.build/versions/master/install.html).
31+
You can install Bazel using this [link](https://bazel.build/start).
3232

3333
## Cloning this repo
3434

@@ -82,7 +82,7 @@ bazel run src/main:main_flags_absl
8282
You can run this using `bazel`:
8383

8484
```bash
85-
bazel run --config=asan //src/main:main_address_sanitize -- --choice=0
85+
bazel run --config=asan //src/main:main_address_sanitize -- 0
8686
```
8787

8888
Note that you should run bazel with `--config=asan`.
@@ -103,7 +103,7 @@ Output:
103103
You can run this using `bazel`:
104104

105105
```bash
106-
bazel run --config=ubsan //src/main:main_undefined_behavior_sanitizer -- --choice=0
106+
bazel run --config=ubsan //src/main:main_undefined_behavior_sanitizer -- 0
107107
```
108108

109109
Note that you should run bazel with `--config=ubsan`.
@@ -145,7 +145,7 @@ A sample test file is [tests/cpplib_test.cc](tests/cpplib_test.cc) which uses [t
145145
You can run the test using [`bazel`](installing-bazel):
146146

147147
```bash
148-
bazel test tests:tests
148+
bazel test //tests/gtest_demo:fib_test
149149
```
150150

151151
# More info on GLOG

precheckin.csh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# This script is used to run pre-checkin tasks for a project.
2+
bazel clean --expunge
3+
# Remove any existing build artifacts
4+
rm -rf bazel-bin bazel-out bazel-testlogs
5+
# Run the build command for all targets in the src directory
6+
# This will ensure that all source files are compiled and linked correctly
7+
bazel build $(bazel query //src/...)
8+
# Run the test command for all targets in the src directory
9+
# Note: not all tests are expected to pass since this is a demo of gtests.
10+
bazel test $(bazel query //tests/gtest_demo/...)
11+
12+
bazel test $(bazel query //tests/gmock_demo/...)
13+
14+
bazel test $(bazel query 'kind(".*_test", //tests:all)')
15+
16+
# Run the commands from the README.md file that are in the bash code blocks
17+
awk '
18+
/^\`\`\`bash/ {in_bash=1; next}
19+
/^\`\`\`/ {in_bash=0}
20+
in_bash && /^bazel / {print}
21+
' README.md | while read -r cmd; do
22+
eval "$cmd"
23+
done

src/main/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ cc_binary(
5757
cc_binary(
5858
name = "main_address_sanitize_simple",
5959
srcs = ["main_address_sanitize_simple.cc"],
60+
copts = ["-w"], # This disables *all* warnings
6061
# Set this variable to the right path for llvm-symbolizer on your machine.
6162
env =
6263
{

src/main/main_address_sanitize.cc

Lines changed: 102 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -50,126 +50,126 @@ int main(int argc, char **argv) {
5050
std::cout << "Usage: main_address_sanitize <choice>" << std::endl;
5151
return -1;
5252
}
53-
53+
std::cout << argv[1] << std::endl;
5454
int choice = std::stoi(argv[1]);
5555
std::cout << "choice: " << choice << std::endl;
5656

5757
switch (choice) {
58-
case 0: {
59-
// SEGV on unknown address.
60-
char *ptr = nullptr;
61-
*ptr = 5;
62-
break;
63-
}
58+
case 0: {
59+
// SEGV on unknown address.
60+
char *ptr = nullptr;
61+
*ptr = 5;
62+
break;
63+
}
6464

65-
case 1: {
66-
// SEGV on unknown address.
67-
char *ptr = (char *)1;
68-
*ptr = 5;
69-
break;
70-
}
65+
case 1: {
66+
// SEGV on unknown address.
67+
char *ptr = (char *)1;
68+
*ptr = 5;
69+
break;
70+
}
7171

72-
case 2: {
73-
// SEGV on unknown address.
74-
std::vector<char> v;
75-
std::cout << v[100] << std::endl;
76-
break;
77-
}
72+
case 2: {
73+
// SEGV on unknown address.
74+
std::vector<char> v;
75+
std::cout << v[100] << std::endl;
76+
break;
77+
}
7878

79-
case 3: {
80-
// Heap buffer overflow (Example 1).
81-
std::vector<char> v(1);
82-
std::cout << v[100] << std::endl;
83-
break;
84-
}
79+
case 3: {
80+
// Heap buffer overflow (Example 1).
81+
std::vector<char> v(1);
82+
std::cout << v[100] << std::endl;
83+
break;
84+
}
8585

86-
case 4: {
87-
// Heap buffer overflow (Example 2).
88-
char *array = new char[16];
89-
array[16] = 1; // BOOM!
90-
break;
91-
}
86+
case 4: {
87+
// Heap buffer overflow (Example 2).
88+
char *array = new char[16];
89+
array[16] = 1; // BOOM!
90+
break;
91+
}
9292

93-
case 5: {
94-
// Heap buffer overflow (Example 3).
95-
char *ptr = new char;
96-
*ptr = 'a';
97-
std::cout << *(ptr + 1) << std::endl;
98-
break;
99-
}
93+
case 5: {
94+
// Heap buffer overflow (Example 3).
95+
char *ptr = new char;
96+
*ptr = 'a';
97+
std::cout << *(ptr + 1) << std::endl;
98+
break;
99+
}
100100

101-
case 6: {
102-
// Heap use after free.
103-
char *p = new char;
104-
delete p;
105-
std::cout << *p << std::endl;
106-
break;
107-
}
101+
case 6: {
102+
// Heap use after free.
103+
char *p = new char;
104+
delete p;
105+
std::cout << *p << std::endl;
106+
break;
107+
}
108108

109-
case 7: {
110-
// Double-free.
111-
char *p = new char;
112-
delete p;
113-
delete p;
114-
break;
115-
}
109+
case 7: {
110+
// Double-free.
111+
char *p = new char;
112+
delete p;
113+
delete p;
114+
break;
115+
}
116116

117-
case 8: {
118-
// Stack buffer overflow.
119-
char array[16];
120-
array[20] = 1; // BOOM!
121-
break;
122-
}
117+
case 8: {
118+
// Stack buffer overflow.
119+
char array[16];
120+
array[20] = 1; // BOOM!
121+
break;
122+
}
123123

124-
case 9: {
125-
// stack-use-after-scope
126-
volatile char *ptr = nullptr;
127-
{
128-
char x = 0;
129-
ptr = &x;
130-
}
131-
*ptr = 5;
132-
break;
133-
}
134-
case 10: {
135-
// stack-use-after-return
136-
// Set this env variable before running:
137-
// export ASAN_OPTIONS=detect_stack_use_after_return=1
138-
FunctionThatEscapesLocalObject();
139-
return global_ptr[0];
124+
case 9: {
125+
// stack-use-after-scope
126+
volatile char *ptr = nullptr;
127+
{
128+
char x = 0;
129+
ptr = &x;
140130
}
131+
*ptr = 5;
132+
break;
133+
}
134+
case 10: {
135+
// stack-use-after-return
136+
// Set this env variable before running:
137+
// export ASAN_OPTIONS=detect_stack_use_after_return=1
138+
FunctionThatEscapesLocalObject();
139+
return global_ptr[0];
140+
}
141141

142-
case 11: {
143-
// global-buffer-overflow
144-
// char global_array[10]; // global variable.
145-
global_array[11] = 1; // Boom!
146-
break;
147-
}
142+
case 11: {
143+
// global-buffer-overflow
144+
// char global_array[10]; // global variable.
145+
global_array[11] = 1; // Boom!
146+
break;
147+
}
148148

149-
case 12: {
150-
// global-buffer-overflow
151-
static char array[10];
152-
array[11] = 1; // Boom!
153-
break;
154-
}
149+
case 12: {
150+
// global-buffer-overflow
151+
static char array[10];
152+
array[11] = 1; // Boom!
153+
break;
154+
}
155155

156-
case 13: {
157-
// Leak detection (Doesn't work on Mac?)
158-
// Set this env variable before running:
159-
// export ASAN_OPTIONS=detect_leaks=1
160-
// Or with bazel:
161-
/*
162-
bazel run --config=asan //src/main:main_address_sanitize \
163-
--run_under='export ASAN_OPTIONS=detect_leaks=1 &&' -- 13
164-
*/
165-
char *p = new char;
166-
*p = 10;
167-
std::cout << "*p: " << *p << std::endl;
168-
break;
169-
}
156+
case 13: {
157+
// Leak detection (Doesn't work on Mac?)
158+
// Set this env variable before running:
159+
// export ASAN_OPTIONS=detect_leaks=1
160+
// Or with bazel:
161+
/*
162+
bazel run --config=asan //src/main:main_address_sanitize \
163+
--run_under='export ASAN_OPTIONS=detect_leaks=1 &&' -- 13
164+
*/
165+
char *p = new char;
166+
*p = 10;
167+
std::cout << "*p: " << *p << std::endl;
168+
break;
169+
}
170170

171-
default:
172-
std::cout << "Error: Invalid choice value: " << choice << std::endl;
171+
default:
172+
std::cout << "Error: Invalid choice value: " << choice << std::endl;
173173
}
174174

175175
return 0;

src/main/main_address_sanitize_simple.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ __attribute__((noinline)) void FunctionThatEscapesLocalObject() {
4545
char global_array[10];
4646

4747
//-----------------------------------------------------------------------------
48-
int main() {
48+
int main(int argc, char *argv[]) {
49+
// This line is just to make sure that the code is not optimized away.
4950
char *ptr = nullptr;
5051
*ptr = 5;
5152
if (argc != 2) {

0 commit comments

Comments
 (0)