Bug 114250

Summary: context record in signal handler contains incorrect values
Product: [Developer tools] valgrind Reporter: Dave Nomura <dcnomura>
Component: generalAssignee: Julian Seward <jseward>
Status: REPORTED ---    
Severity: normal CC: gsmith, info, tom
Priority: NOR    
Version First Reported In: 2.4   
Target Milestone: ---   
Platform: RedHat Enterprise Linux   
OS: Linux   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:

Description Dave Nomura 2005-10-12 01:37:15 UTC
I have created a signal handler for INT 4 ("into" instr) using sigaction but
when I get in the handler I find that the values in the context record for some
of the registers is different when running valgrind than when running the x86
program.

The source to the test program (int4.c) is included below.

The output when running the program:
windfall-apex% int4
in handler
info->si_signo = 11
info->si_errno = 0
info->si_code = 128
context.uc_mcontext.gregs[12] = 4
context.uc_mcontext.gregs[14] = 0x80485c1

Reg 12 is REG_TRAPNO and used by the signal handler for identifying the overflow
condition.  Reg 14 is REG_EIP and points to the instruction after the offending
instruction.

Under valgrind 2.4.0 I get:
windfall-apex% valgrind int4
==5482== Memcheck, a memory error detector for x86-linux.
==5482== Copyright (C) 2002-2005, and GNU GPL'd, by Julian Seward et al.
==5482== Using valgrind-2.4.0, a program supervision framework for x86-linux.
==5482== Copyright (C) 2000-2005, and GNU GPL'd, by Julian Seward et al.
==5482== For more details, rerun with: -v
==5482== 
in handler
info->si_signo = 11
info->si_errno = 0
info->si_code = 128
context.uc_mcontext.gregs[12] = 0
context.uc_mcontext.gregs[14] = 0x80485c0
==5482== 
==5482== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 13 from 1)
==5482== malloc/free: in use at exit: 0 bytes in 0 blocks.
==5482== malloc/free: 0 allocs, 0 frees, 0 bytes allocated.
==5482== For counts of detected errors, rerun with: -v
==5482== No malloc'd blocks -- no leaks are possible.

The bug is that REG_TRAPNO contains 0 instead of 4, and REG_EIP points at the
"into" instruction rather than at the following instruction.

----------------------- int4.c ---------------------------
#include <signal.h>
#include <sys/signal.h>
#include <ucontext.h>
#include <stdio.h>

char *p = 0;

void
handler(sig, info, context)
int sig;
siginfo_t *info;
ucontext_t *context;
{
        int i;
        printf("in handler\n");
        printf("info->si_signo = %d\n", info->si_signo);
        printf("info->si_errno = %d\n", info->si_errno);
        printf("info->si_code = %d\n", info->si_code);

        /* register 12 is REG_TRAPNO */
        /* register 14 is REG_EIP */
        printf("context.uc_mcontext.gregs[12] = %d\n", 
                context->uc_mcontext.gregs[12]);
        printf("context.uc_mcontext.gregs[14] = 0x%x\n", 
                context->uc_mcontext.gregs[14]);
        exit(0);
}

int
main()
{
        int x = 0x7fffffff;
        struct sigaction act, oact;
        int i;

        bzero(&act, sizeof(struct sigaction));
        bzero(&oact, sizeof(struct sigaction));
        act.sa_sigaction = &handler;
        act.sa_flags = SA_SIGINFO;
        sigaction(SIGSEGV, &act, &oact);
        x = x*x;        /* force overflow flag to be set */
        asm("into");    /* signal 4 if overflow bit is set */
        printf("returned from handler\n");
}
Comment 1 T I Z E N 2025-02-17 19:10:39 UTC
If the origin Members still active on this or have an Ticket, MR or PR (as in Invent or on the Kanban), can you insert it to the Bug Report?
Thank you for the bug report. Unfortunately we were not able to get to it yet. Can we ask you to please check if this is still an issue with either Plasma 5.27 (the current LTS version) or Plasma 6.3 (the latest released version).

Regards,

[Tizen](https://invent.kde.org/tizen)
Comment 2 Tom Hughes 2025-02-17 19:13:41 UTC
Valgrind is a guest in the KDE bug tracker but it does not use KDE development processes so please ignore that last comment.