diff options
| author | Zach Shaftel | 2025-06-11 15:37:31 -0400 |
|---|---|---|
| committer | Eli Zaretskii | 2025-06-14 17:54:06 +0300 |
| commit | 009cdc8ae09ef060c030feac06578a4ba37cd8c5 (patch) | |
| tree | 29624799ada8ee27f508fd29a395dd0a0de0f9b6 | |
| parent | e1d0ee1b6b545c82937755dd19787c3d7059eca3 (diff) | |
| download | emacs-009cdc8ae09ef060c030feac06578a4ba37cd8c5.tar.gz emacs-009cdc8ae09ef060c030feac06578a4ba37cd8c5.zip | |
Fix segfault in profiler-cpu-log and profiler-memory-log (bug#78763)
* src/profiler.c (export_log): Check if a log has been allocated first,
and return nil if it hasn't.
(Fprofiler_cpu_log, Fprofiler_memory_log): Doc fix.
* test/src/profiler-tests.el (profiler-tests-cpu-profiler)
(profiler-tests-memory-profiler): New tests.
| -rw-r--r-- | src/profiler.c | 13 | ||||
| -rw-r--r-- | test/src/profiler-tests.el | 58 |
2 files changed, 69 insertions, 2 deletions
diff --git a/src/profiler.c b/src/profiler.c index 12d75012c79..f421eb52b31 100644 --- a/src/profiler.c +++ b/src/profiler.c | |||
| @@ -534,7 +534,11 @@ DEFUN ("profiler-cpu-log", Fprofiler_cpu_log, Sprofiler_cpu_log, | |||
| 534 | The log is a hash-table mapping backtraces to counters which represent | 534 | The log is a hash-table mapping backtraces to counters which represent |
| 535 | the amount of time spent at those points. Every backtrace is a vector | 535 | the amount of time spent at those points. Every backtrace is a vector |
| 536 | of functions, where the last few elements may be nil. | 536 | of functions, where the last few elements may be nil. |
| 537 | Before returning, a new log is allocated for future samples. */) | 537 | |
| 538 | If the profiler has not run since the last invocation of | ||
| 539 | `profiler-cpu-log' (or was never run at all), return nil. If the | ||
| 540 | profiler is currently running, allocate a new log for future samples | ||
| 541 | before returning. */) | ||
| 538 | (void) | 542 | (void) |
| 539 | { | 543 | { |
| 540 | /* Temporarily stop profiling to avoid it interfering with our data | 544 | /* Temporarily stop profiling to avoid it interfering with our data |
| @@ -556,6 +560,7 @@ Before returning, a new log is allocated for future samples. */) | |||
| 556 | static Lisp_Object | 560 | static Lisp_Object |
| 557 | export_log (struct profiler_log *plog) | 561 | export_log (struct profiler_log *plog) |
| 558 | { | 562 | { |
| 563 | if (!plog->log) return Qnil; | ||
| 559 | log_t *log = plog->log; | 564 | log_t *log = plog->log; |
| 560 | /* The returned hash table uses `equal' as key equivalence predicate | 565 | /* The returned hash table uses `equal' as key equivalence predicate |
| 561 | which is more discriminating than the `function-equal' used by | 566 | which is more discriminating than the `function-equal' used by |
| @@ -639,7 +644,11 @@ DEFUN ("profiler-memory-log", | |||
| 639 | The log is a hash-table mapping backtraces to counters which represent | 644 | The log is a hash-table mapping backtraces to counters which represent |
| 640 | the amount of memory allocated at those points. Every backtrace is a vector | 645 | the amount of memory allocated at those points. Every backtrace is a vector |
| 641 | of functions, where the last few elements may be nil. | 646 | of functions, where the last few elements may be nil. |
| 642 | Before returning, a new log is allocated for future samples. */) | 647 | |
| 648 | If the profiler has not run since the last invocation of | ||
| 649 | `profiler-memory-log' (or was never run at all), return nil. If the | ||
| 650 | profiler is currently running, allocate a new log for future samples | ||
| 651 | before returning. */) | ||
| 643 | (void) | 652 | (void) |
| 644 | { | 653 | { |
| 645 | bool prof_mem = profiler_memory_running; | 654 | bool prof_mem = profiler_memory_running; |
diff --git a/test/src/profiler-tests.el b/test/src/profiler-tests.el new file mode 100644 index 00000000000..fe50de713e2 --- /dev/null +++ b/test/src/profiler-tests.el | |||
| @@ -0,0 +1,58 @@ | |||
| 1 | ;;; profiler-tests.el --- tests for src/profiler.c -*- lexical-binding:t -*- | ||
| 2 | |||
| 3 | ;; Copyright (C) 2025 Free Software Foundation, Inc. | ||
| 4 | |||
| 5 | ;; This file is part of GNU Emacs. | ||
| 6 | |||
| 7 | ;; GNU Emacs is free software: you can redistribute it and/or modify | ||
| 8 | ;; it under the terms of the GNU General Public License as published by | ||
| 9 | ;; the Free Software Foundation, either version 3 of the License, or | ||
| 10 | ;; (at your option) any later version. | ||
| 11 | |||
| 12 | ;; GNU Emacs is distributed in the hope that it will be useful, | ||
| 13 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 15 | ;; GNU General Public License for more details. | ||
| 16 | |||
| 17 | ;; You should have received a copy of the GNU General Public License | ||
| 18 | ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. | ||
| 19 | |||
| 20 | ;;; Commentary: | ||
| 21 | |||
| 22 | ;;; Code: | ||
| 23 | |||
| 24 | (require 'ert) | ||
| 25 | |||
| 26 | (ert-deftest profiler-tests-memory-profiler () | ||
| 27 | (let ((was-running (profiler-memory-running-p))) | ||
| 28 | ;; do this first in case the profiler was already running | ||
| 29 | (should (eq was-running (profiler-memory-stop))) | ||
| 30 | (profiler-memory-start) | ||
| 31 | (should-error (profiler-memory-start)) | ||
| 32 | (should (profiler-memory-running-p)) | ||
| 33 | (should (hash-table-p (profiler-memory-log))) | ||
| 34 | ;; `profiler-memory-log' shouldn't terminate profiling. | ||
| 35 | (should (profiler-memory-running-p)) | ||
| 36 | (profiler-memory-stop) | ||
| 37 | (profiler-memory-log) ;flush the log | ||
| 38 | (should-not (profiler-memory-log)) | ||
| 39 | (when was-running (profiler-memory-start)))) | ||
| 40 | |||
| 41 | (defconst profiler-tests-cpu-sampling-interval 1000000) | ||
| 42 | |||
| 43 | (ert-deftest profiler-tests-cpu-profiler () | ||
| 44 | (skip-unless (fboundp 'profiler-cpu-start)) | ||
| 45 | (let ((was-running (profiler-cpu-running-p))) | ||
| 46 | (should (eq was-running (profiler-cpu-stop))) | ||
| 47 | (profiler-cpu-start profiler-tests-cpu-sampling-interval) | ||
| 48 | (should-error (profiler-cpu-start profiler-tests-cpu-sampling-interval)) | ||
| 49 | (should (hash-table-p (profiler-cpu-log))) | ||
| 50 | (should (profiler-cpu-running-p)) | ||
| 51 | (profiler-cpu-stop) | ||
| 52 | (profiler-cpu-log) | ||
| 53 | (should-not (profiler-cpu-log)) | ||
| 54 | (when was-running | ||
| 55 | (profiler-cpu-start (or (bound-and-true-p profiler-sampling-interval) | ||
| 56 | profiler-tests-cpu-sampling-interval))))) | ||
| 57 | |||
| 58 | ;;; profiler-tests.el ends here | ||