valgrind错误和ucontext.为什么“使用大小为8的未初始化值"? [英] valgrind error and ucontext. Why "Use of uninitialised value of size 8"?

查看:125
本文介绍了valgrind错误和ucontext.为什么“使用大小为8的未初始化值"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图理解为什么valgrind对于使用ucontexts的小型测试程序抱怨使用大小为8的未初始化值".它基本上是一个程序,它创建"n_ucs" ucontext,并在"max_switch"时间内切换它们.

I have been trying to understand why valgrind complains about "Use of uninitialised value of size 8" for this small test program that uses ucontexts. It is basically a program that creates "n_ucs" ucontexts and switches over them for "max_switch" times.

我了解警告:客户端切换堆栈?"(这基本上是程序的全部内容),但是对于所有使用大小为8的未初始化值"来说,我都没有任何意义

I understand the "Warning: client switching stacks?" (which is basically what the program all about), but I can't really make sense to all the "Use of uninitialised value of size 8"

如果Valgrind错误为假阳性,或者该程序有根本错误,我想获得一些帮助.(我在使用相同机制的大型程序中看到了很多,但我将其精简到了最低限度,可以在此处发布.)

I would like to get some help understanding if Valgrind errors are false positive or if this program has something fundamentally wrong. (I see a lot of them on a much larger program that uses the same mechanisms, but I have distilled it to the minimum to post here).

感谢您的帮助.

谢谢

杰克

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <ucontext.h>

#define STACK_SIZE   (8*1024)

int n_ucs = 1;
int max_switchs = 10;
int n_switchs = 0;
int tid = 0;

ucontext_t *ucs;
static ucontext_t engine_uc;

static void func(int arg)
{
    while (n_switchs < max_switchs) {
        int c_tid = tid;
        int n_tid = (tid + 1) % n_ucs;
        n_switchs++;
        tid = n_tid;
        swapcontext(&ucs[c_tid], &ucs[n_tid]);
    }
}

int main(int argc, char **argv)
{
    if (argc > 1)
        n_ucs = atoi(argv[1]);
    if (argc > 2)
        max_switchs = atoi(argv[2]);

    ucs = malloc(sizeof(ucontext_t) * n_ucs);
    int i;
    for (i = 0; i < n_ucs; i++) {
        /* Create initial ucontext_t, including stack */
        getcontext(&ucs[i]);
        ucs[i].uc_stack.ss_sp = malloc(STACK_SIZE);
        ucs[i].uc_stack.ss_size = STACK_SIZE;
        ucs[i].uc_stack.ss_flags = 0;
        ucs[i].uc_link = &engine_uc;
        makecontext(&ucs[i], (void (*)())func, 1, i);
    }

    /* jump to the first uc */
    swapcontext(&engine_uc, &ucs[tid]);

    /* destroy stacks */
    for (i = 0; i < n_ucs; i++)
        free(ucs[i].uc_stack.ss_sp);
    free(ucs);
    return 0;
}

使用 gcc main.c 进行编译,并使用 ./a.out 2 2

compile with gcc main.c and run with ./a.out 2 2

gcc -v

gcc -v

使用内置规格.COLLECT_GCC = gccCOLLECT_LTO_WRAPPER =/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper目标:x86_64-linux-gnu配置为:../src/configure -v--with-pkgversion ='Ubuntu 4.8.2-19ubuntu1'--with-bugurl = file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages = c,c ++,java,go,d,fortran,objc,obj-c ++ --prefix =/usr --program-suffix = -4.8 --enable-shared --enable-linker-build-id --libexecdir =/usr/lib --without-included-gettext --enable-threads = posix --with-gxx-include-dir =/usr/include/c ++/4.8 --libdir =/usr/lib --enable-nls --with-sysroot =/--enable-clocale = gnu --enable-libstdcxx-debug --enable-libstdcxx-time = yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib-disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir =/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir =/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory = amd64 --with-ecj-jar =/usr/share/java/eclipse-ecj.jar --enable-objc-gc--enable-multiarch --disable-werror --with-arch-32 = i686 --with-abi = m64 --with-multilib-list = m32,m64,mx32 --with-tune = generic --enable-checking = release --build = x86_64-linux-gnu --host = x86_64-linux-gnu --target = x86_64-linux-gnu线程模型:posix gcc版本4.8.2(Ubuntu 4.8.2-19ubuntu1)

Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.2-19ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1)

ldd --version

ldd --version

ldd(Ubuntu EGLIBC 2.19-0ubuntu6.3)2.19版权所有(C)2014 Free Software Foundation,Inc..这是免费软件;看到复制条件的来源.没有保修;甚至没有特定目的的适销性或适用性.罗兰(Roland)撰写McGrath和Ulrich Drepper.

ldd (Ubuntu EGLIBC 2.19-0ubuntu6.3) 2.19 Copyright (C) 2014 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Written by Roland McGrath and Ulrich Drepper.

valgrind --track-origins=yes ./a.out 2 2
==21949== Memcheck, a memory error detector
==21949== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==21949== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==21949== Command: ./a.out 2 2
==21949==
==21949== Warning: client switching stacks?  SP change: 0xffefffdd8 --> 0x51ff7b8
==21949==          to suppress, use: --max-stackframe=68616717856 or greater
==21949== Use of uninitialised value of size 8
==21949==    at 0x400738: func (main.c:25)
==21949==    by 0x4E58EC4: (below main) (libc-start.c:287)
==21949==  Uninitialised value was created by a stack allocation
==21949==    at 0x4E7E445: swapcontext (swapcontext.S:92)
==21949==
==21949== Conditional jump or move depends on uninitialised value(s)
==21949==    at 0x4E807A7: __start_context (__start_context.S:37)
==21949==    by 0x4E58EC4: (below main) (libc-start.c:287)
==21949==  Uninitialised value was created by a stack allocation
==21949==    at 0x4E7E445: swapcontext (swapcontext.S:92)
==21949==
==21949== Syscall param rt_sigprocmask(set) contains uninitialised byte(s)
==21949==    at 0x4E7E0EC: setcontext (setcontext.S:47)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==  Uninitialised value was created by a stack allocation
==21949==    at 0x4E7E445: swapcontext (swapcontext.S:92)
==21949==
==21949== Use of uninitialised value of size 8
==21949==    at 0x4E7E0F5: setcontext (setcontext.S:54)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==  Uninitialised value was created by a stack allocation
==21949==    at 0x4E7E445: swapcontext (swapcontext.S:92)
==21949==
==21949== Use of uninitialised value of size 8
==21949==    at 0x4E7E0FE: setcontext (setcontext.S:56)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==    by 0x4E807AD: __start_context (__start_context.S:39)
==21949==  Uninitialised value was created by a stack allocation
==21949==    at 0x4E7E445: swapcontext (swapcontext.S:92)
==21949==
==21949== Warning: client switching stacks?  SP change: 0x51ff7c0 --> 0xffefffde0
==21949==          to suppress, use: --max-stackframe=68616717856 or greater
==21949==
==21949== HEAP SUMMARY:
==21949==     in use at exit: 0 bytes in 0 blocks
==21949==   total heap usage: 3 allocs, 3 frees, 18,256 bytes allocated
==21949==
==21949== All heap blocks were freed -- no leaks are possible
==21949==
==21949== For counts of detected and suppressed errors, rerun with: -v
==21949== ERROR SUMMARY: 5 errors from 5 contexts (suppressed: 0 from 0)

推荐答案

您必须将有关堆栈更改的信息通知valgrind.在此处阅读示例 https://github.com/lu-zero/valgrind/blob/master/memcheck/tests/linux/stack_changes.c

You must notify valgrind about the stack's change. Read an example here https://github.com/lu-zero/valgrind/blob/master/memcheck/tests/linux/stack_changes.c

这是正确的代码:

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <ucontext.h>
#include <valgrind/valgrind.h>

#define STACK_SIZE   (8*1024)

int n_ucs = 1;
int max_switchs = 10;
int n_switchs = 0;
int tid = 0;

ucontext_t *ucs;
static ucontext_t engine_uc;

 void func(int arg)
{
    while (n_switchs < max_switchs) {
        int c_tid = tid;
        int n_tid = (tid + 1) % n_ucs;
        n_switchs++;
        tid = n_tid;
        swapcontext(&ucs[c_tid], &ucs[n_tid]);

    }
}

int main(int argc, char **argv)
{
    if (argc > 1)
        n_ucs = atoi(argv[1]);
    if (argc > 2)
        max_switchs = atoi(argv[2]);

    ucs = malloc(sizeof(ucontext_t) * n_ucs);

    //store the VALGRIND_STACK_REGISTER return values
    int* valgrind_ret = malloc(n_ucs*sizeof(int));

    int i;
    for (i = 0; i < n_ucs; i++) {
        /* Create initial ucontext_t, including stack */
        getcontext(&ucs[i]);

        //pass stack to valgrind
        void* mystack = malloc(STACK_SIZE);
        VALGRIND_STACK_REGISTER(mystack, mystack + STACK_SIZE);

        ucs[i].uc_stack.ss_sp = mystack;
        ucs[i].uc_stack.ss_size = STACK_SIZE;
        ucs[i].uc_stack.ss_flags = 0;
        ucs[i].uc_link = &engine_uc;
        makecontext(&ucs[i], (void (*)())func, 1, i);
    }

    /* jump to the first uc */
    swapcontext(&engine_uc, &ucs[tid]);

    /* destroy stacks */
    for (i = 0; i < n_ucs; i++) {
        //valgrind stack deregister 
        VALGRIND_STACK_DEREGISTER(valgrind_ret[i]);

        free(ucs[i].uc_stack.ss_sp);
    }
    free(ucs);
    return 0;
}

这篇关于valgrind错误和ucontext.为什么“使用大小为8的未初始化值"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆