sisoputnfrba / foro

Foro de consultas para el trabajo práctico
148 stars 7 forks source link

Proceso finaliza antes solo cuando uso Valgrind #3870

Closed Lisandrogq closed 4 months ago

Lisandrogq commented 4 months ago

Buenas, tengo un problema al utilizar Valgrind: al correr el proceso cpu con valgrind, finaliza antes de lo esperado (si lo corro desde vscode no finaliza en ese punto). Solo para dar contexto adicional por si sirve: Comenze a utilizar Valgrind para encontrar algun mal manejo de memoría, ya que tengo comportamientos raros en la funcion mov_in de cpu.

Hay una parte del log de valgrind que dice lo siguiente, no encontre razon para que finalizara

==82062== Process terminating with default action of signal 11 (SIGSEGV)
==82062==  Access not within mapped region at address 0x8
==82062==    at 0x10A6C8: ejecutar_cliclos (main.c:24)
==82062==    by 0x10BB11: main (main.c:441)
==82062==  If you believe this happened as a result of a stack
==82062==  overflow in your program's main thread (unlikely but
==82062==  possible), you can try to increase the size of the
==82062==  main thread stack using the --main-stacksize= flag.
==82062==  The main thread stack size used in this run was 8388608.

Las partes de codigo mencionadas son las siguientes:


void ejecutar_cliclos()
{
    while (!terminar_modulo)
    {
        int status = STATUS_OK;
        sem_wait(&hay_proceso);
        while (status != STATUS_DESALOJADO)
        {
            log_error(logger,"%i",pcb_exec->registros->PC);
            t_strings_instruccion *instruccion = fetch(pcb_exec->registros->PC); /////////LINEA 24

            decode(instruccion); 

            status = execute(instruccion);
            if (status == STATUS_OK) 

            {                        
                check_intr(&status); 
            }
        }
    }
}

int main(int argc, char const *argv[])
{
    sem_init(&hay_proceso, 0, 0);
    sem_init(&desalojar, 0, 0);
    dic_p_registros = dictionary_create();
    dic_tam_registros = dictionary_create();
    inicializar_diccionario_tams();

    // dictionary = inicializar_diccionario(contexto); esto ya no hace falta, se inicializa al recibir pcb

    t_log *logger;
    t_config *config;
    logger = iniciar_logger();
    logger = log_create("cpu.log", "Cpu_MateLavado", 1, LOG_LEVEL_DEBUG);
    config = iniciar_config();
    config = config_create("cpu.config");

    iniciar_thread_dispatch();
    iniciar_thread_interrupt();
    socket_memoria = iniciar_conexion_memoria(config, logger);

    ejecutar_cliclos(); /////////LINEA 441

    pthread_join(tid[0], NULL);
    pthread_join(tid[1], NULL);
    return 0;
}

El log de Valgrind completo:


valgrind --leak-check=full --track-origins=yes ./bin/cpu 
==82062== Memcheck, a memory error detector
==82062== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==82062== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==82062== Command: ./bin/cpu
==82062== 

El thread servidor-cpu-dispatch inició su ejecución

El thread servidor-cpu-interrupt inició su ejecución
[INFO] 00:25:54:205 Cpu_MateLavado/(82062:82062): cpu
==82062== Invalid read of size 4
==82062==    at 0x10D7E2: crear_conexion (utils_generales.c:79)
==82062==    by 0x10B8A4: iniciar_conexion_memoria (main.c:396)
==82062==    by 0x10BB01: main (main.c:439)
==82062==  Address 0x4aa9f50 is 16 bytes inside a block of size 64 free'd
==82062==    at 0x484B27F: free (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==82062==    by 0x4981F3F: freeaddrinfo (getaddrinfo.c:2640)
==82062==    by 0x10D7DD: crear_conexion (utils_generales.c:78)
==82062==    by 0x10B8A4: iniciar_conexion_memoria (main.c:396)
==82062==    by 0x10BB01: main (main.c:439)
==82062==  Block was alloc'd at
==82062==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==82062==    by 0x49807E9: generate_addrinfo (getaddrinfo.c:1075)
==82062==    by 0x49807E9: gaih_inet (getaddrinfo.c:1193)
==82062==    by 0x49807E9: getaddrinfo (getaddrinfo.c:2397)
==82062==    by 0x10D777: crear_conexion (utils_generales.c:66)
==82062==    by 0x10B8A4: iniciar_conexion_memoria (main.c:396)
==82062==    by 0x10BB01: main (main.c:439)
==82062== 
==82062== Invalid read of size 8
==82062==    at 0x10D7E9: crear_conexion (utils_generales.c:79)
==82062==    by 0x10B8A4: iniciar_conexion_memoria (main.c:396)
==82062==    by 0x10BB01: main (main.c:439)
==82062==  Address 0x4aa9f58 is 24 bytes inside a block of size 64 free'd
==82062==    at 0x484B27F: free (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==82062==    by 0x4981F3F: freeaddrinfo (getaddrinfo.c:2640)
==82062==    by 0x10D7DD: crear_conexion (utils_generales.c:78)
==82062==    by 0x10B8A4: iniciar_conexion_memoria (main.c:396)
==82062==    by 0x10BB01: main (main.c:439)
==82062==  Block was alloc'd at
==82062==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==82062==    by 0x49807E9: generate_addrinfo (getaddrinfo.c:1075)
==82062==    by 0x49807E9: gaih_inet (getaddrinfo.c:1193)
==82062==    by 0x49807E9: getaddrinfo (getaddrinfo.c:2397)
==82062==    by 0x10D777: crear_conexion (utils_generales.c:66)
==82062==    by 0x10B8A4: iniciar_conexion_memoria (main.c:396)
==82062==    by 0x10BB01: main (main.c:439)
==82062== 
==82062== Syscall param socketcall.connect(serv_addr.sa_family) points to unaddressable byte(s)
==82062==    at 0x499C52B: connect (connect.c:26)
==82062==    by 0x10D7F9: crear_conexion (utils_generales.c:79)
==82062==    by 0x10B8A4: iniciar_conexion_memoria (main.c:396)
==82062==    by 0x10BB01: main (main.c:439)
==82062==  Address 0x4aa9f70 is 48 bytes inside a block of size 64 free'd
==82062==    at 0x484B27F: free (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==82062==    by 0x4981F3F: freeaddrinfo (getaddrinfo.c:2640)
==82062==    by 0x10D7DD: crear_conexion (utils_generales.c:78)
==82062==    by 0x10B8A4: iniciar_conexion_memoria (main.c:396)
==82062==    by 0x10BB01: main (main.c:439)
==82062==  Block was alloc'd at
==82062==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==82062==    by 0x49807E9: generate_addrinfo (getaddrinfo.c:1075)
==82062==    by 0x49807E9: gaih_inet (getaddrinfo.c:1193)
==82062==    by 0x49807E9: getaddrinfo (getaddrinfo.c:2397)
==82062==    by 0x10D777: crear_conexion (utils_generales.c:66)
==82062==    by 0x10B8A4: iniciar_conexion_memoria (main.c:396)
==82062==    by 0x10BB01: main (main.c:439)
==82062== 
==82062== Syscall param socketcall.connect(serv_addr.sin_port) points to unaddressable byte(s)
==82062==    at 0x499C52B: connect (connect.c:26)
==82062==    by 0x10D7F9: crear_conexion (utils_generales.c:79)
==82062==    by 0x10B8A4: iniciar_conexion_memoria (main.c:396)
==82062==    by 0x10BB01: main (main.c:439)
==82062==  Address 0x4aa9f72 is 50 bytes inside a block of size 64 free'd
==82062==    at 0x484B27F: free (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==82062==    by 0x4981F3F: freeaddrinfo (getaddrinfo.c:2640)
==82062==    by 0x10D7DD: crear_conexion (utils_generales.c:78)
==82062==    by 0x10B8A4: iniciar_conexion_memoria (main.c:396)
==82062==    by 0x10BB01: main (main.c:439)
==82062==  Block was alloc'd at
==82062==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==82062==    by 0x49807E9: generate_addrinfo (getaddrinfo.c:1075)
==82062==    by 0x49807E9: gaih_inet (getaddrinfo.c:1193)
==82062==    by 0x49807E9: getaddrinfo (getaddrinfo.c:2397)
==82062==    by 0x10D777: crear_conexion (utils_generales.c:66)
==82062==    by 0x10B8A4: iniciar_conexion_memoria (main.c:396)
==82062==    by 0x10BB01: main (main.c:439)
==82062== 
==82062== Syscall param socketcall.connect(serv_addr.sin_addr) points to unaddressable byte(s)
==82062==    at 0x499C52B: connect (connect.c:26)
==82062==    by 0x10D7F9: crear_conexion (utils_generales.c:79)
==82062==    by 0x10B8A4: iniciar_conexion_memoria (main.c:396)
==82062==    by 0x10BB01: main (main.c:439)
==82062==  Address 0x4aa9f74 is 52 bytes inside a block of size 64 free'd
==82062==    at 0x484B27F: free (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==82062==    by 0x4981F3F: freeaddrinfo (getaddrinfo.c:2640)
==82062==    by 0x10D7DD: crear_conexion (utils_generales.c:78)
==82062==    by 0x10B8A4: iniciar_conexion_memoria (main.c:396)
==82062==    by 0x10BB01: main (main.c:439)
==82062==  Block was alloc'd at
==82062==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==82062==    by 0x49807E9: generate_addrinfo (getaddrinfo.c:1075)
==82062==    by 0x49807E9: gaih_inet (getaddrinfo.c:1193)
==82062==    by 0x49807E9: getaddrinfo (getaddrinfo.c:2397)
==82062==    by 0x10D777: crear_conexion (utils_generales.c:66)
==82062==    by 0x10B8A4: iniciar_conexion_memoria (main.c:396)
==82062==    by 0x10BB01: main (main.c:439)
==82062== 
[INFO] 00:25:54:198 Servidor/(82062:82064): Cpu-interruptlisto para recibir
[INFO] 00:25:53:968 Servidor/(82062:82063): Cpu-dipatch listo para recibir
[INFO] 00:26:04:982 Servidor/(82062:82063): se conecto el modulo kernel(dispatch)
[INFO] 00:26:04:984 Servidor/(82062:82063): Me llego la operacion uno, la informacion enviada fue: kernel
[INFO] 00:26:05:027 Servidor/(82062:82063): Me llego el mensaje SOY EL CLIENTE DISPATCH
[DEBUG] 00:26:16:413 Servidor/(82062:82063): Se recibio el proceso a ejecutar por dispatch
[INFO] 00:26:16:416 Servidor/(82062:82063): Quantum: 6000
==82062== Invalid read of size 8
==82062==    at 0x10A6C8: ejecutar_cliclos (main.c:24)
==82062==    by 0x10BB11: main (main.c:441)
==82062==  Address 0x8 is not stack'd, malloc'd or (recently) free'd
==82062== 
==82062== 
==82062== Process terminating with default action of signal 11 (SIGSEGV)
==82062==  Access not within mapped region at address 0x8
==82062==    at 0x10A6C8: ejecutar_cliclos (main.c:24)
==82062==    by 0x10BB11: main (main.c:441)
==82062==  If you believe this happened as a result of a stack
==82062==  overflow in your program's main thread (unlikely but
==82062==  possible), you can try to increase the size of the
==82062==  main thread stack using the --main-stacksize= flag.
==82062==  The main thread stack size used in this run was 8388608.
==82062== 
==82062== HEAP SUMMARY:
==82062==     in use at exit: 16,598 bytes in 59 blocks
==82062==   total heap usage: 209 allocs, 150 frees, 29,908 bytes allocated
==82062== 
==82062== 41 (32 direct, 9 indirect) bytes in 1 blocks are definitely lost in loss record 40 of 53
==82062==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==82062==    by 0x486DE2C: log_create (log.c:54)
==82062==    by 0x10B637: servidor_dispatch (main.c:329)
==82062==    by 0x4909AC2: start_thread (pthread_create.c:442)
==82062==    by 0x499AA03: clone (clone.S:100)
==82062== 
==82062== 272 bytes in 1 blocks are possibly lost in loss record 45 of 53
==82062==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==82062==    by 0x40147D9: calloc (rtld-malloc.h:44)
==82062==    by 0x40147D9: allocate_dtv (dl-tls.c:375)
==82062==    by 0x40147D9: _dl_allocate_tls (dl-tls.c:634)
==82062==    by 0x490A7B4: allocate_stack (allocatestack.c:430)
==82062==    by 0x490A7B4: pthread_create@@GLIBC_2.34 (pthread_create.c:647)
==82062==    by 0x10B76A: iniciar_thread_dispatch (main.c:365)
==82062==    by 0x10BAE4: main (main.c:437)
==82062== 
==82062== 272 bytes in 1 blocks are possibly lost in loss record 46 of 53
==82062==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==82062==    by 0x40147D9: calloc (rtld-malloc.h:44)
==82062==    by 0x40147D9: allocate_dtv (dl-tls.c:375)
==82062==    by 0x40147D9: _dl_allocate_tls (dl-tls.c:634)
==82062==    by 0x490A7B4: allocate_stack (allocatestack.c:430)
==82062==    by 0x490A7B4: pthread_create@@GLIBC_2.34 (pthread_create.c:647)
==82062==    by 0x10B7D6: iniciar_thread_interrupt (main.c:376)
==82062==    by 0x10BAEE: main (main.c:438)
==82062== 
==82062== LEAK SUMMARY:
==82062==    definitely lost: 32 bytes in 1 blocks
==82062==    indirectly lost: 9 bytes in 1 blocks
==82062==      possibly lost: 544 bytes in 2 blocks
==82062==    still reachable: 16,013 bytes in 55 blocks
==82062==         suppressed: 0 bytes in 0 blocks
==82062== Reachable blocks (those to which a pointer was found) are not shown.
==82062== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==82062== 
==82062== For lists of detected and suppressed errors, rerun with: -s
==82062== ERROR SUMMARY: 9 errors from 9 contexts (suppressed: 0 from 0)
make: *** [makefile:57: memcheck] Segmentation fault (core dumped)```
mefederico commented 4 months ago

Hola buenas!

Lo que te esta indicando valgrind es que estas leyendo una variable que no asignaste previamente o no hicieron el malloc correspondiente. La pregunta es, donde asignan pcb_exec? se crea bien?

Tengo mis dudas de si sin valgrind no les rompe también, de cualquier forma la variable no la están manejando de una forma correcta

Lisandrogq commented 4 months ago

Buenass, efectivamente estabamos haciendo post al semaforo una linea antes de inicializar la variable. Lo raro es que eso nunca nos trajo(ni nos trae) problemas al correr el proceso normalmente con vscode(con Valgrind sucede siempre). Mas allá de esta curiosidad, muchas gracias por la ayuda.