OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/process_util.h" | 5 #include "base/process_util.h" |
6 | 6 |
7 #include <ctype.h> | 7 #include <ctype.h> |
8 #include <dirent.h> | 8 #include <dirent.h> |
9 #include <dlfcn.h> | 9 #include <dlfcn.h> |
10 #include <errno.h> | 10 #include <errno.h> |
(...skipping 495 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
506 CHECK(false) << "Out of memory."; | 506 CHECK(false) << "Out of memory."; |
507 } | 507 } |
508 | 508 |
509 void OnNoMemory() { | 509 void OnNoMemory() { |
510 OnNoMemorySize(0); | 510 OnNoMemorySize(0); |
511 } | 511 } |
512 | 512 |
513 } // namespace | 513 } // namespace |
514 | 514 |
515 extern "C" { | 515 extern "C" { |
516 | 516 #if !defined(USE_TCMALLOC) |
517 #if !defined(LINUX_USE_TCMALLOC) | |
518 | 517 |
519 extern "C" { | 518 extern "C" { |
520 void* __libc_malloc(size_t size); | 519 void* __libc_malloc(size_t size); |
521 void* __libc_realloc(void* ptr, size_t size); | 520 void* __libc_realloc(void* ptr, size_t size); |
522 void* __libc_calloc(size_t nmemb, size_t size); | 521 void* __libc_calloc(size_t nmemb, size_t size); |
523 void* __libc_valloc(size_t size); | 522 void* __libc_valloc(size_t size); |
524 void* __libc_pvalloc(size_t size); | 523 void* __libc_pvalloc(size_t size); |
525 void* __libc_memalign(size_t alignment, size_t size); | 524 void* __libc_memalign(size_t alignment, size_t size); |
526 } // extern "C" | 525 } // extern "C" |
527 | 526 |
528 // Overriding the system memory allocation functions: | 527 // Overriding the system memory allocation functions: |
529 // | 528 // |
530 // For security reasons, we want malloc failures to be fatal. Too much code | 529 // For security reasons, we want malloc failures to be fatal. Too much code |
531 // doesn't check for a NULL return value from malloc and unconditionally uses | 530 // doesn't check for a NULL return value from malloc and unconditionally uses |
532 // the resulting pointer. If the first offset that they try to access is | 531 // the resulting pointer. If the first offset that they try to access is |
533 // attacker controlled, then the attacker can direct the code to access any | 532 // attacker controlled, then the attacker can direct the code to access any |
534 // part of memory. | 533 // part of memory. |
535 // | 534 // |
536 // Thus, we define all the standard malloc functions here and mark them as | 535 // Thus, we define all the standard malloc functions here and mark them as |
537 // visibility 'default'. This means that they replace the malloc functions for | 536 // visibility 'default'. This means that they replace the malloc functions for |
538 // all Chromium code and also for all code in shared libraries. There are tests | 537 // all Chromium code and also for all code in shared libraries. There are tests |
539 // for this in process_util_unittest.cc. | 538 // for this in process_util_unittest.cc. |
540 // | 539 // |
541 // If we are using tcmalloc, then the problem is moot since tcmalloc handles | 540 // If we are using tcmalloc, then the problem is moot since tcmalloc handles |
542 // this for us. Thus this code is in a !defined(LINUX_USE_TCMALLOC) block. | 541 // this for us. Thus this code is in a !defined(USE_TCMALLOC) block. |
543 // | 542 // |
544 // We call the real libc functions in this code by using __libc_malloc etc. | 543 // We call the real libc functions in this code by using __libc_malloc etc. |
545 // Previously we tried using dlsym(RTLD_NEXT, ...) but that failed depending on | 544 // Previously we tried using dlsym(RTLD_NEXT, ...) but that failed depending on |
546 // the link order. Since ld.so needs calloc during symbol resolution, it | 545 // the link order. Since ld.so needs calloc during symbol resolution, it |
547 // defines its own versions of several of these functions in dl-minimal.c. | 546 // defines its own versions of several of these functions in dl-minimal.c. |
548 // Depending on the runtime library order, dlsym ended up giving us those | 547 // Depending on the runtime library order, dlsym ended up giving us those |
549 // functions and bad things happened. See crbug.com/31809 | 548 // functions and bad things happened. See crbug.com/31809 |
550 // | 549 // |
551 // This means that any code which calls __libc_* gets the raw libc versions of | 550 // This means that any code which calls __libc_* gets the raw libc versions of |
552 // these functions. | 551 // these functions. |
(...skipping 30 matching lines...) Expand all Loading... |
583 // posix_memalign has a unique signature and doesn't have a __libc_ variant. | 582 // posix_memalign has a unique signature and doesn't have a __libc_ variant. |
584 int posix_memalign(void** ptr, size_t alignment, size_t size) | 583 int posix_memalign(void** ptr, size_t alignment, size_t size) |
585 __attribute__ ((visibility("default"))); | 584 __attribute__ ((visibility("default"))); |
586 | 585 |
587 int posix_memalign(void** ptr, size_t alignment, size_t size) { | 586 int posix_memalign(void** ptr, size_t alignment, size_t size) { |
588 // This will use the safe version of memalign, above. | 587 // This will use the safe version of memalign, above. |
589 *ptr = memalign(alignment, size); | 588 *ptr = memalign(alignment, size); |
590 return 0; | 589 return 0; |
591 } | 590 } |
592 | 591 |
593 #endif // !defined(LINUX_USE_TCMALLOC) | 592 #endif // !defined(USE_TCMALLOC) |
594 } // extern C | 593 } // extern C |
595 | 594 |
596 void EnableTerminationOnOutOfMemory() { | 595 void EnableTerminationOnOutOfMemory() { |
597 // Set the new-out of memory handler. | 596 // Set the new-out of memory handler. |
598 std::set_new_handler(&OnNoMemory); | 597 std::set_new_handler(&OnNoMemory); |
599 // If we're using glibc's allocator, the above functions will override | 598 // If we're using glibc's allocator, the above functions will override |
600 // malloc and friends and make them die on out of memory. | 599 // malloc and friends and make them die on out of memory. |
601 } | 600 } |
602 | 601 |
603 bool AdjustOOMScore(ProcessId process, int score) { | 602 bool AdjustOOMScore(ProcessId process, int score) { |
604 if (score < 0 || score > 15) | 603 if (score < 0 || score > 15) |
605 return false; | 604 return false; |
606 | 605 |
607 FilePath oom_adj("/proc"); | 606 FilePath oom_adj("/proc"); |
608 oom_adj = oom_adj.Append(Int64ToString(process)); | 607 oom_adj = oom_adj.Append(Int64ToString(process)); |
609 oom_adj = oom_adj.AppendASCII("oom_adj"); | 608 oom_adj = oom_adj.AppendASCII("oom_adj"); |
610 | 609 |
611 if (!file_util::PathExists(oom_adj)) | 610 if (!file_util::PathExists(oom_adj)) |
612 return false; | 611 return false; |
613 | 612 |
614 std::string score_str = IntToString(score); | 613 std::string score_str = IntToString(score); |
615 return (static_cast<int>(score_str.length()) == | 614 return (static_cast<int>(score_str.length()) == |
616 file_util::WriteFile(oom_adj, score_str.c_str(), score_str.length())); | 615 file_util::WriteFile(oom_adj, score_str.c_str(), score_str.length())); |
617 } | 616 } |
618 | 617 |
619 } // namespace base | 618 } // namespace base |
OLD | NEW |