blob: fa0797c80c639f0cb5e05a35f4e4dc3fcf08a6a8 [file] [log] [blame]
George Burgess IV9a274102019-06-04 15:39:52 -07001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef __clang__
18#error "Non-clang isn't supported"
19#endif
20
21// Clang compile-time and run-time tests for Bionic's FORTIFY.
22//
23// This file is compiled in two configurations ways to give us a sane set of tests for clang's
24// FORTIFY implementation.
25//
26// One configuration uses clang's diagnostic consumer
27// (https://clang.llvm.org/doxygen/classclang_1_1VerifyDiagnosticConsumer.html#details)
28// to check diagnostics (e.g. the expected-* comments everywhere).
29//
30// Please note that this test does things like leaking memory. That's WAI.
31
32// Silence all "from 'diagnose_if'" `note`s from anywhere, including headers; they're uninteresting
33// for this test case, and their line numbers may change over time.
34// expected-note@* 0+{{from 'diagnose_if'}}
35//
36// Similarly, there are a few overload tricks we have to emit errors. Ignore any notes from those.
37// expected-note@* 0+{{candidate function}}
38
39#ifndef _FORTIFY_SOURCE
40#error "_FORTIFY_SOURCE must be defined"
41#endif
42
43#include <sys/cdefs.h>
44
45// This is a test specifically of bionic's FORTIFY machinery. Other stdlibs need not apply.
46#ifndef __BIONIC__
47// expected-no-diagnostics
48#else
49
50// As alluded to above, we're going to be doing some obviously very broken things in this file.
51// FORTIFY helpfully flags a lot of it at compile-time, but we want it to *actually* crash, too. So
52// let's wipe out any build-time errors.
53#ifndef COMPILATION_TESTS
54#undef __clang_error_if
55#define __clang_error_if(...)
56#undef __clang_warning_if
57#define __clang_warning_if(...)
58#endif
59
60#include <err.h>
61#include <fcntl.h>
62#include <limits.h>
63#include <poll.h>
64#include <signal.h>
65#include <stdio.h>
66#include <stdlib.h>
67#include <string.h>
68#include <sys/socket.h>
69#include <sys/stat.h>
70#include <sys/wait.h>
71#include <syslog.h>
72#include <unistd.h>
73#include <wchar.h>
74
75#ifndef COMPILATION_TESTS
76#include <gtest/gtest.h>
77#include "BionicDeathTest.h"
78
79#define CONCAT2(x, y) x##y
80#define CONCAT(x, y) CONCAT2(x, y)
81#define FORTIFY_TEST_NAME CONCAT(clang_fortify_test_, _FORTIFY_SOURCE)
82
83namespace {
84struct FORTIFY_TEST_NAME : BionicDeathTest {
85 protected:
86 void SetUp() override {
87 stdin_saved = dup(STDIN_FILENO);
88 if (stdin_saved < 0) err(1, "failed to dup stdin");
89
90 int devnull = open("/dev/null", O_RDONLY);
91 if (devnull < 0) err(1, "failed to open /dev/null");
92
93 if (!dup2(devnull, STDIN_FILENO)) err(1, "failed to overwrite stdin");
94 static_cast<void>(close(devnull));
95
96 BionicDeathTest::SetUp();
97 }
98
99 void TearDown() override {
100 if (stdin_saved == -1) return;
101 if (!dup2(stdin_saved, STDIN_FILENO)) warn("failed to restore stdin");
102
103 static_cast<void>(close(stdin_saved));
104
105 BionicDeathTest::TearDown();
106 }
107
108 private:
109 int stdin_saved = -1;
110};
111} // namespace
112
113template <typename Fn>
114__attribute__((noreturn)) static void ExitAfter(Fn&& f) {
115 f();
116 // No need to tear things down; our parent process should handle that.
117 _exit(0);
118}
119
120// In any case (including failing tests), we always want to die after this.
121#define DIE_WITH(expr, cond, regex) EXPECT_EXIT(ExitAfter([&] { (expr); }), cond, regex)
122
123// EXPECT_NO_DEATH forks so that the test remains alive on a bug, and so that the environment
124// doesn't get modified on no bug. (Environment modification is especially tricky to deal with given
125// the *_STRUCT variants below.)
126#define EXPECT_NO_DEATH(expr) DIE_WITH(expr, testing::ExitedWithCode(0), "")
127#define EXPECT_FORTIFY_DEATH(expr) DIE_WITH(expr, testing::KilledBySignal(SIGABRT), "FORTIFY")
128// Expecting death, but only if we're doing a "strict" struct-checking mode.
129#if _FORTIFY_SOURCE > 1
130#define EXPECT_FORTIFY_DEATH_STRUCT EXPECT_FORTIFY_DEATH
131#else
132#define EXPECT_FORTIFY_DEATH_STRUCT EXPECT_NO_DEATH
133#endif
134
135#define FORTIFY_TEST(test_name) TEST(FORTIFY_TEST_NAME, test_name)
136
137#else // defined(COMPILATION_TESTS)
138
139#define EXPECT_NO_DEATH(expr) expr
140#define EXPECT_FORTIFY_DEATH(expr) expr
141#define EXPECT_FORTIFY_DEATH_STRUCT EXPECT_FORTIFY_DEATH
142#define FORTIFY_TEST(test_name) void test_name()
143#endif
144
145const static int kBogusFD = -1;
146
147FORTIFY_TEST(string) {
148 char small_buffer[8] = {};
149
150 {
151 char large_buffer[sizeof(small_buffer) + 1] = {};
152 // expected-error@+1{{size bigger than buffer}}
153 EXPECT_FORTIFY_DEATH(memcpy(small_buffer, large_buffer, sizeof(large_buffer)));
154 // expected-error@+1{{size bigger than buffer}}
155 EXPECT_FORTIFY_DEATH(memmove(small_buffer, large_buffer, sizeof(large_buffer)));
156 // FIXME: this should be EXPECT_FORTIFY_DEATH
157#if 0
158 // expected-error@+1{{called with bigger length than the destination}}
159#endif
160 EXPECT_NO_DEATH(mempcpy(small_buffer, large_buffer, sizeof(large_buffer)));
161 // expected-error@+1{{size bigger than buffer}}
162 EXPECT_FORTIFY_DEATH(memset(small_buffer, 0, sizeof(large_buffer)));
163 // expected-warning@+1{{arguments got flipped?}}
164 EXPECT_NO_DEATH(memset(small_buffer, sizeof(small_buffer), 0));
165 // FIXME: Should these be warnings?
166 // expected-warning@+1{{will always overflow}}
167 EXPECT_FORTIFY_DEATH(bcopy(large_buffer, small_buffer, sizeof(large_buffer)));
168 // expected-warning@+1{{will always overflow}}
169 EXPECT_FORTIFY_DEATH(bzero(small_buffer, sizeof(large_buffer)));
170 }
171
172 {
173 const char large_string[] = "Hello!!!";
174 static_assert(sizeof(large_string) > sizeof(small_buffer), "");
175
176 // expected-error@+1{{string bigger than buffer}}
177 EXPECT_FORTIFY_DEATH(strcpy(small_buffer, large_string));
178 // expected-error@+1{{string bigger than buffer}}
179 EXPECT_FORTIFY_DEATH(stpcpy(small_buffer, large_string));
George Burgess IV77f99aa2019-06-06 14:14:52 -0700180 // expected-error@+1{{size bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700181 EXPECT_FORTIFY_DEATH(strncpy(small_buffer, large_string, sizeof(large_string)));
George Burgess IV77f99aa2019-06-06 14:14:52 -0700182 // expected-error@+1{{size bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700183 EXPECT_FORTIFY_DEATH(stpncpy(small_buffer, large_string, sizeof(large_string)));
George Burgess IV77f99aa2019-06-06 14:14:52 -0700184 // expected-error@+1{{string bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700185 EXPECT_FORTIFY_DEATH(strcat(small_buffer, large_string));
George Burgess IV77f99aa2019-06-06 14:14:52 -0700186 // expected-error@+1{{size bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700187 EXPECT_FORTIFY_DEATH(strncat(small_buffer, large_string, sizeof(large_string)));
George Burgess IV77f99aa2019-06-06 14:14:52 -0700188 // expected-error@+1{{size bigger than buffer}}
189 EXPECT_FORTIFY_DEATH(strlcpy(small_buffer, large_string, sizeof(large_string)));
190 // expected-error@+1{{size bigger than buffer}}
191 EXPECT_FORTIFY_DEATH(strlcat(small_buffer, large_string, sizeof(large_string)));
George Burgess IV9a274102019-06-04 15:39:52 -0700192 }
193
194 {
195 struct {
196 char tiny_buffer[4];
197 char tiny_buffer2[4];
198 } split = {};
199
200 EXPECT_NO_DEATH(memcpy(split.tiny_buffer, &split, sizeof(split)));
201 EXPECT_NO_DEATH(memcpy(split.tiny_buffer, &split, sizeof(split)));
202 EXPECT_NO_DEATH(memmove(split.tiny_buffer, &split, sizeof(split)));
203 EXPECT_NO_DEATH(mempcpy(split.tiny_buffer, &split, sizeof(split)));
204 EXPECT_NO_DEATH(memset(split.tiny_buffer, 0, sizeof(split)));
205
206 EXPECT_NO_DEATH(bcopy(&split, split.tiny_buffer, sizeof(split)));
207 EXPECT_NO_DEATH(bzero(split.tiny_buffer, sizeof(split)));
208
209 const char small_string[] = "Hi!!";
210 static_assert(sizeof(small_string) > sizeof(split.tiny_buffer), "");
211
212#if _FORTIFY_SOURCE > 1
213 // expected-error@+2{{string bigger than buffer}}
214#endif
215 EXPECT_FORTIFY_DEATH_STRUCT(strcpy(split.tiny_buffer, small_string));
216
217#if _FORTIFY_SOURCE > 1
218 // expected-error@+2{{string bigger than buffer}}
219#endif
220 EXPECT_FORTIFY_DEATH_STRUCT(stpcpy(split.tiny_buffer, small_string));
221
222#if _FORTIFY_SOURCE > 1
George Burgess IV77f99aa2019-06-06 14:14:52 -0700223 // expected-error@+2{{size bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700224#endif
225 EXPECT_FORTIFY_DEATH_STRUCT(strncpy(split.tiny_buffer, small_string, sizeof(small_string)));
226
227#if _FORTIFY_SOURCE > 1
George Burgess IV77f99aa2019-06-06 14:14:52 -0700228 // expected-error@+2{{size bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700229#endif
230 EXPECT_FORTIFY_DEATH_STRUCT(stpncpy(split.tiny_buffer, small_string, sizeof(small_string)));
231
232#if _FORTIFY_SOURCE > 1
George Burgess IV77f99aa2019-06-06 14:14:52 -0700233 // expected-error@+2{{string bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700234#endif
235 EXPECT_FORTIFY_DEATH_STRUCT(strcat(split.tiny_buffer, small_string));
236
237#if _FORTIFY_SOURCE > 1
George Burgess IV77f99aa2019-06-06 14:14:52 -0700238 // expected-error@+2{{size bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700239#endif
240 EXPECT_FORTIFY_DEATH_STRUCT(strncat(split.tiny_buffer, small_string, sizeof(small_string)));
George Burgess IV77f99aa2019-06-06 14:14:52 -0700241
242#if _FORTIFY_SOURCE > 1
243 // expected-error@+2{{size bigger than buffer}}
244#endif
245 EXPECT_FORTIFY_DEATH_STRUCT(strlcat(split.tiny_buffer, small_string, sizeof(small_string)));
246
247#if _FORTIFY_SOURCE > 1
248 // expected-error@+2{{size bigger than buffer}}
249#endif
250 EXPECT_FORTIFY_DEATH_STRUCT(strlcpy(split.tiny_buffer, small_string, sizeof(small_string)));
George Burgess IV9a274102019-06-04 15:39:52 -0700251 }
252}
253
254// Since these emit hard errors, it's sort of hard to run them...
255#ifdef COMPILATION_TESTS
256namespace compilation_tests {
257template <typename T>
258static T declval() {
259 __builtin_unreachable();
260}
261
262static void testFcntl() {
263 // expected-error@+1{{too many arguments}}
264 open("/", 0, 0, 0);
265#if 0
266 // expected-error@+1{{either with 2 or 3 arguments, not more}}
267#endif
268 open64("/", 0, 0, 0);
269 // expected-error@+1{{too many arguments}}
270 openat(0, "/", 0, 0, 0);
271#if 0
272 // expected-error@+1{{either with 3 or 4 arguments, not more}}
273#endif
274 openat64(0, "/", 0, 0, 0);
275
276 // expected-error@+1{{missing mode}}
277 open("/", O_CREAT);
278 // expected-error@+1{{missing mode}}
279 open("/", O_TMPFILE);
280#if 0
281 // expected-error@+1{{needs 3 arguments}}
282#endif
283 open64("/", O_CREAT);
284#if 0
285 // expected-error@+1{{needs 3 arguments}}
286#endif
287 open64("/", O_TMPFILE);
288 // expected-error@+1{{missing mode}}
289 openat(0, "/", O_CREAT);
290 // expected-error@+1{{missing mode}}
291 openat(0, "/", O_TMPFILE);
292#if 0
293 // expected-error@+1{{needs 4 arguments}}
294#endif
295 openat64(0, "/", O_CREAT);
296#if 0
297 // expected-error@+1{{needs 4 arguments}}
298#endif
299 openat64(0, "/", O_TMPFILE);
300
301 // Superfluous modes are sometimes bugs, but not often enough to complain
302 // about, apparently.
303}
304
305static void testFormatStrings() {
306 const auto unsigned_value = declval<unsigned long long>();
307 const auto* unknown_string = declval<const char*>();
George Burgess IV06bb4ce2019-06-13 15:13:02 -0700308 const auto va = *declval<va_list*>();
George Burgess IV9a274102019-06-04 15:39:52 -0700309
310 {
311 auto some_fd = declval<int>();
312 // expected-warning@+1{{format specifies type 'int'}}
313 dprintf(some_fd, "%d", unsigned_value);
314 // expected-warning@+1{{format string is not a string literal}}
315 dprintf(some_fd, unknown_string, unsigned_value);
316 // expected-warning@+1{{format string is not a string literal}}
317 vdprintf(1, unknown_string, va);
318 }
319
320 {
321 auto* retval = declval<char*>();
322#if 0
323 // expected-error@+2{{ignoring return value}}
324#endif
325 // expected-warning@+1{{format specifies type 'int'}}
326 asprintf(&retval, "%d", unsigned_value);
327#if 0
328 // expected-error@+2{{ignoring return value}}
329#endif
330 // expected-warning@+1{{format string is not a string literal}}
331 asprintf(&retval, unknown_string, unsigned_value);
332#if 0
333 // expected-error@+2{{ignoring return value}}
334#endif
335 // expected-warning@+1{{format string is not a string literal}}
336 vasprintf(&retval, unknown_string, va);
337 }
338
339 // expected-warning@+1{{format specifies type 'int'}}
340 syslog(0, "%d", unsigned_value);
341 // expected-warning@+1{{format string is not a string literal}}
342 syslog(0, unknown_string, unsigned_value);
343 // expected-warning@+1{{format string is not a string literal}}
344 vsyslog(0, unknown_string, va);
345
346 {
347 auto* file = declval<FILE*>();
348 // expected-warning@+1{{format specifies type 'int'}}
349 fprintf(file, "%d", unsigned_value);
350 // expected-warning@+1{{format string is not a string literal}}
351 fprintf(file, unknown_string, unsigned_value);
352 // expected-warning@+1{{format string is not a string literal}}
353 vfprintf(file, unknown_string, va);
354 }
355
356 // expected-warning@+1{{format specifies type 'int'}}
357 printf("%d", unsigned_value);
358 // expected-warning@+1{{format string is not a string literal}}
359 printf(unknown_string, unsigned_value);
360 // expected-warning@+1{{format string is not a string literal}}
361 vprintf(unknown_string, va);
362
363 {
364 char buf[128];
365 // expected-warning@+1{{format specifies type 'int'}}
366 sprintf(buf, "%d", unsigned_value);
367 // expected-warning@+1{{format string is not a string literal}}
368 sprintf(buf, unknown_string, unsigned_value);
369 // expected-warning@+1{{format string is not a string literal}}
370 sprintf(buf, unknown_string, va);
371
372 // expected-warning@+1{{format specifies type 'int'}}
373 snprintf(buf, sizeof(buf), "%d", unsigned_value);
374 // expected-warning@+1{{format string is not a string literal}}
375 snprintf(buf, sizeof(buf), unknown_string, unsigned_value);
376 // expected-warning@+1{{format string is not a string literal}}
377 vsnprintf(buf, sizeof(buf), unknown_string, va);
378 }
379
380 // FIXME: below are general format string cases where clang should probably try to warn.
381 {
382 char buf[4];
383 sprintf(buf, "%s", "1234");
384 sprintf(buf, "1%s4", "23");
385 sprintf(buf, "%d", 1234);
386
387 // Similar thoughts for strncpy, etc.
388 }
389}
390
391static void testStdlib() {
392 char path_buffer[PATH_MAX - 1];
393#if 0
394 // expected-error@+2{{ignoring return value of function}}
395#endif
396 // expected-error@+1{{must be NULL or a pointer to a buffer with >= PATH_MAX bytes}}
397 realpath("/", path_buffer);
398#if 0
399 // expected-error@+1{{ignoring return value of function}}
400#endif
401 realpath("/", nullptr);
402
403 // FIXME: This should complain about flipped arguments, instead of objectsize.
404 // expected-error@+1{{must be NULL or a pointer to a buffer with >= PATH_MAX bytes}}
405 realpath(nullptr, path_buffer);
406
407 // expected-error@+1{{flipped arguments?}}
408 realpath(nullptr, nullptr);
409}
410} // namespace compilation_tests
411#endif
412
413FORTIFY_TEST(poll) {
414 int pipe_fds[2];
415 if (pipe(pipe_fds)) err(1, "pipe failed");
416
417 // after this, pipe_fds[0] should always report RDHUP
418 if (close(pipe_fds[1])) err(1, "close failed");
419
420 struct pollfd poll_fd = { pipe_fds[0], POLLRDHUP, 0 };
421 {
422 struct pollfd few_fds[] = { poll_fd, poll_fd };
423 // expected-error@+1{{fd_count is larger than the given buffer}}
424 EXPECT_FORTIFY_DEATH(poll(few_fds, 3, 0));
425 // expected-error@+1{{fd_count is larger than the given buffer}}
426 EXPECT_FORTIFY_DEATH(ppoll(few_fds, 3, 0, 0));
427 // expected-error@+1{{fd_count is larger than the given buffer}}
428 EXPECT_FORTIFY_DEATH(ppoll64(few_fds, 3, 0, nullptr));
429 }
430
431 {
432 struct {
433 struct pollfd few[2];
434 struct pollfd extra[1];
435 } fds = { { poll_fd, poll_fd }, { poll_fd } };
436 static_assert(sizeof(fds) >= sizeof(struct pollfd) * 3, "");
437
438#if _FORTIFY_SOURCE > 1
439 // expected-error@+2{{fd_count is larger than the given buffer}}
440#endif
441 EXPECT_FORTIFY_DEATH_STRUCT(poll(fds.few, 3, 0));
442
443 struct timespec timeout = {};
444#if _FORTIFY_SOURCE > 1
445 // expected-error@+2{{fd_count is larger than the given buffer}}
446#endif
447 EXPECT_FORTIFY_DEATH_STRUCT(ppoll(fds.few, 3, &timeout, 0));
448
449#if _FORTIFY_SOURCE > 1
450 // expected-error@+2{{fd_count is larger than the given buffer}}
451#endif
452 EXPECT_FORTIFY_DEATH_STRUCT(ppoll64(fds.few, 3, 0, nullptr));
453 }
454}
455
456FORTIFY_TEST(socket) {
457 {
458 char small_buffer[8];
459 // expected-error@+1{{size bigger than buffer}}
460 EXPECT_FORTIFY_DEATH(recv(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0));
461 // expected-error@+1{{size bigger than buffer}}
462 EXPECT_FORTIFY_DEATH(recvfrom(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0, 0, 0));
463
464 // expected-error@+1{{size bigger than buffer}}
465 EXPECT_FORTIFY_DEATH(send(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0));
466 // expected-error@+1{{size bigger than buffer}}
467 EXPECT_FORTIFY_DEATH(sendto(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0, 0, 0));
468 }
469
470 {
471 struct {
472 char tiny_buffer[4];
473 char tiny_buffer2;
474 } split = {};
475
476 EXPECT_NO_DEATH(recv(kBogusFD, split.tiny_buffer, sizeof(split), 0));
477 EXPECT_NO_DEATH(recvfrom(kBogusFD, split.tiny_buffer, sizeof(split), 0, 0, 0));
478 }
479}
480
481FORTIFY_TEST(sys_stat) {
482 // expected-error@+1{{'umask' called with invalid mode}}
483 EXPECT_FORTIFY_DEATH(umask(01777));
484}
485
486FORTIFY_TEST(stdio) {
487 char small_buffer[8] = {};
488 {
489#if 0
490 // expected-error@+1{{may overflow the destination buffer}}
491#endif
492 EXPECT_FORTIFY_DEATH(snprintf(small_buffer, sizeof(small_buffer) + 1, ""));
493
494 va_list va;
495#if 0
496 // expected-error@+1{{may overflow the destination buffer}}
497#endif
498 // expected-warning@+1{{format string is empty}}
499 EXPECT_FORTIFY_DEATH(vsnprintf(small_buffer, sizeof(small_buffer) + 1, "", va));
500 }
501
502 // expected-error@+1{{size should not be negative}}
503 EXPECT_FORTIFY_DEATH(fgets(small_buffer, -1, stdin));
504 // expected-error@+1{{size is larger than the destination buffer}}
505 EXPECT_FORTIFY_DEATH(fgets(small_buffer, sizeof(small_buffer) + 1, stdin));
506
507 // expected-error@+1{{size * count overflows}}
508 EXPECT_NO_DEATH(fread(small_buffer, 2, (size_t)-1, stdin));
509 // expected-error@+1{{size * count is too large for the given buffer}}
510 EXPECT_FORTIFY_DEATH(fread(small_buffer, 1, sizeof(small_buffer) + 1, stdin));
511
512 // expected-error@+1{{size * count overflows}}
513 EXPECT_NO_DEATH(fwrite(small_buffer, 2, (size_t)-1, stdout));
514 // expected-error@+1{{size * count is too large for the given buffer}}
515 EXPECT_FORTIFY_DEATH(fwrite(small_buffer, 1, sizeof(small_buffer) + 1, stdout));
516}
517
518FORTIFY_TEST(unistd) {
519 char small_buffer[8];
520
521 // Return value warnings are (sort of) a part of FORTIFY, so we don't ignore them.
522#if 0
523 // expected-error@+2{{ignoring return value of function}}
524#endif
525 // expected-error@+1{{bytes overflows the given object}}
526 EXPECT_FORTIFY_DEATH(read(kBogusFD, small_buffer, sizeof(small_buffer) + 1));
527#if 0
528 // expected-error@+2{{ignoring return value of function}}
529#endif
530 // expected-error@+1{{bytes overflows the given object}}
531 EXPECT_FORTIFY_DEATH(pread(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0));
532#if 0
533 // expected-error@+2{{ignoring return value of function}}
534#endif
535 // expected-error@+1{{bytes overflows the given object}}
536 EXPECT_FORTIFY_DEATH(pread64(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0));
537#if 0
538 // expected-error@+2{{ignoring return value of function}}
539#endif
540 // expected-error@+1{{bytes overflows the given object}}
541 EXPECT_FORTIFY_DEATH(write(kBogusFD, small_buffer, sizeof(small_buffer) + 1));
542#if 0
543 // expected-error@+2{{ignoring return value of function}}
544#endif
545 // expected-error@+1{{bytes overflows the given object}}
546 EXPECT_FORTIFY_DEATH(pwrite(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0));
547#if 0
548 // expected-error@+2{{ignoring return value of function}}
549#endif
550 // expected-error@+1{{bytes overflows the given object}}
551 EXPECT_FORTIFY_DEATH(pwrite64(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0));
552#if 0
553 // expected-error@+2{{ignoring return value of function}}
554#endif
555 // expected-error@+1{{bytes overflows the given object}}
556 EXPECT_FORTIFY_DEATH(readlink("/", small_buffer, sizeof(small_buffer) + 1));
557#if 0
558 // expected-error@+2{{ignoring return value of function}}
559#endif
560 // expected-error@+1{{bytes overflows the given object}}
561 EXPECT_FORTIFY_DEATH(getcwd(small_buffer, sizeof(small_buffer) + 1));
562
563 // getcwd allocates and returns a buffer if you pass null to getcwd
564 EXPECT_NO_DEATH(getcwd(nullptr, 0));
565 EXPECT_NO_DEATH(getcwd(nullptr, 4096));
566
567 struct {
568 char tiny_buffer[4];
569 char tiny_buffer2[4];
570 } split;
571
572 EXPECT_NO_DEATH(read(kBogusFD, split.tiny_buffer, sizeof(split)));
573 EXPECT_NO_DEATH(pread(kBogusFD, split.tiny_buffer, sizeof(split), 0));
574 EXPECT_NO_DEATH(pread64(kBogusFD, split.tiny_buffer, sizeof(split), 0));
575 EXPECT_NO_DEATH(write(kBogusFD, split.tiny_buffer, sizeof(split)));
576 EXPECT_NO_DEATH(pwrite(kBogusFD, split.tiny_buffer, sizeof(split), 0));
577 EXPECT_NO_DEATH(pwrite64(kBogusFD, split.tiny_buffer, sizeof(split), 0));
578
579#if _FORTIFY_SOURCE > 1
580 // expected-error@+2{{bytes overflows the given object}}
581#endif
582 EXPECT_FORTIFY_DEATH_STRUCT(readlink("/", split.tiny_buffer, sizeof(split)));
583#if _FORTIFY_SOURCE > 1
584 // expected-error@+2{{bytes overflows the given object}}
585#endif
586 EXPECT_FORTIFY_DEATH_STRUCT(getcwd(split.tiny_buffer, sizeof(split)));
587
588 {
589 // FIXME: These should all die in FORTIFY. Headers are bugged.
590#ifdef COMPILATION_TESTS
591 char* volatile unknown = small_buffer;
592 const size_t count = static_cast<size_t>(SSIZE_MAX) + 1;
593 // expected-error@+1{{'count' must be <= SSIZE_MAX}}
594 EXPECT_FORTIFY_DEATH(read(kBogusFD, unknown, count));
595 // expected-error@+1{{'count' must be <= SSIZE_MAX}}
596 EXPECT_FORTIFY_DEATH(pread(kBogusFD, unknown, count, 0));
597 // expected-error@+1{{'count' must be <= SSIZE_MAX}}
598 EXPECT_FORTIFY_DEATH(pread64(kBogusFD, unknown, count, 0));
599 // expected-error@+1{{'count' must be <= SSIZE_MAX}}
600 EXPECT_FORTIFY_DEATH(write(kBogusFD, unknown, count));
601 // expected-error@+1{{'count' must be <= SSIZE_MAX}}
602 EXPECT_FORTIFY_DEATH(pwrite(kBogusFD, unknown, count, 0));
603 // expected-error@+1{{'count' must be <= SSIZE_MAX}}
604 EXPECT_FORTIFY_DEATH(pwrite64(kBogusFD, unknown, count, 0));
605#endif
606 }
607}
608
609#endif // defined(__BIONIC__)