LCOV - code coverage report
Current view: top level - core/test - run-console-log-buf-overrun.c (source / functions) Hit Total Coverage
Test: skiboot.info Lines: 29 29 100.0 %
Date: 2024-01-02 21:04:04 Functions: 3 3 100.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
       2                 :            : /*
       3                 :            :  * Copyright 2015-2016 IBM Corp.
       4                 :            :  */
       5                 :            : 
       6                 :            : #include <config.h>
       7                 :            : #include <stdlib.h>
       8                 :            : #include <string.h>
       9                 :            : #include <assert.h>
      10                 :            : #include <stdarg.h>
      11                 :            : #include <compiler.h>
      12                 :            : 
      13                 :            : unsigned long tb_hz = 512000000;
      14                 :            : 
      15                 :            : #define __TEST__
      16                 :            : 
      17                 :            : #define CHECK_BUF_ASSERT(buf, str)                      \
      18                 :            :         assert(memcmp(buf, str, strlen(str)) == 0)
      19                 :            : 
      20                 :            : #define CHECK_ASSERT(str)                               \
      21                 :            :         CHECK_BUF_ASSERT(console_buffer, str)
      22                 :            : 
      23                 :            : int huge_tb;
      24                 :            : 
      25                 :          5 : static inline unsigned long mftb(void)
      26                 :            : {
      27                 :            :         /*
      28                 :            :          * return huge value for TB that overrun tmp[16] buffer defined
      29                 :            :          * in print_itoa().
      30                 :            :          */
      31                 :          5 :         if (huge_tb)
      32                 :          1 :                 return 1223372515963611388;
      33                 :            :         else
      34                 :          4 :                 return 42;
      35                 :            : }
      36                 :            : 
      37                 :            : #include "../../libc/include/stdio.h"
      38                 :            : #include "../console-log.c"
      39                 :            : #include "../../libc/stdio/snprintf.c"
      40                 :            : #include "../../libc/stdio/vsnprintf.c"
      41                 :            : 
      42                 :            : char console_buffer[4096];
      43                 :            : struct debug_descriptor debug_descriptor;
      44                 :            : 
      45                 :            : bool flushed_to_drivers;
      46                 :            : 
      47                 :          5 : ssize_t console_write(bool flush_to_drivers, const void *buf, size_t count)
      48                 :            : {
      49                 :          5 :         flushed_to_drivers = flush_to_drivers;
      50                 :          5 :         memcpy(console_buffer, buf, count);
      51                 :          5 :         return count;
      52                 :            : }
      53                 :            : 
      54                 :          1 : int main(void)
      55                 :            : {
      56                 :          1 :         unsigned long value = 0xffffffffffffffff;
      57                 :          1 :         char *ptr = console_buffer;
      58                 :            : 
      59                 :          1 :         debug_descriptor.console_log_levels = 0x75;
      60                 :            : 
      61                 :            :         /* Test for huge TB value. */
      62                 :          1 :         huge_tb = 1;
      63                 :            : 
      64                 :          1 :         prlog(PR_EMERG, "Hello World");
      65                 :          1 :         CHECK_ASSERT("[2389399445.123611388,0] Hello World");
      66                 :            : 
      67                 :          1 :         memset(console_buffer, 0, sizeof(console_buffer));
      68                 :            : 
      69                 :            :         /* Test for normal TB with huge unsigned long value */
      70                 :          1 :         huge_tb = 0;
      71                 :            : 
      72                 :          1 :         prlog(PR_EMERG, "Hello World %lu", value);
      73                 :          1 :         CHECK_ASSERT("[    0.000000042,0] Hello World 18446744073709551615");
      74                 :            : 
      75                 :          1 :         printf("Hello World %lu", value);
      76                 :          1 :         CHECK_ASSERT("[    0.000000042,5] Hello World 18446744073709551615");
      77                 :            : 
      78                 :            :         /*
      79                 :            :          * Test string of size > 320
      80                 :            :          *
      81                 :            :          * core/console-log.c:vprlog() uses buffer[320] to print message
      82                 :            :          * Try printing more than 320 bytes to test stack corruption.
      83                 :            :          * You would see Segmentation fault on stack corruption.
      84                 :            :          */
      85                 :          1 :         prlog(PR_EMERG, "%330s", "Hello World");
      86                 :            : 
      87                 :          1 :         memset(console_buffer, 0, sizeof(console_buffer));
      88                 :            : 
      89                 :            :         /*
      90                 :            :          * Test boundary condition.
      91                 :            :          *
      92                 :            :          * Print string of exact size 320. We should see string truncated
      93                 :            :          * with console_buffer[319] == '\0'.
      94                 :            :          */
      95                 :          1 :         memset(console_buffer, 0, sizeof(console_buffer));
      96                 :            : 
      97                 :          1 :         prlog(PR_EMERG, "%300s", "Hello World");
      98                 :          1 :         assert(console_buffer[319] == 0);
      99                 :            : 
     100                 :            :         /* compare truncated string */
     101                 :          1 :         ptr += 320 - strlen("Hello World");
     102                 :          1 :         CHECK_BUF_ASSERT(ptr, "Hello Worl");
     103                 :            : 
     104                 :          1 :         return 0;
     105                 :            : }

Generated by: LCOV version 1.14