0

I am having the error: corrupted size vs. prev_size: 0x01fe29e8 on my C++ unit test. I have created a very simple unit test to load a kernel module (I²C) into the OS. First I open a file /root/i2c-tests/i2c-stub.ko without errors. Second I do a fstat also without errors. Third I resize a vector and read the content of a file descriptor. Finally, I use init_module to load the kernel module. The functions have error handling and no error is being caught. Actualy I am loading the kernel module into memory and I can list it using lsmod and I also can write and read from it using i2cset and i2cget. However, I am getting this annoying error: *** Error in ./test/sensorTests': corrupted size vs. prev_size: 0x011cd9e8 *** Aborted. I guess I have to have a loop to read the file, like is showing here (How to properly error trap read in c to get byte number from a file descriptor) but I do know how to implement this loop and why I should implement it.

#define _GNU_SOURCE

#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>

#include <gtest/gtest.h>
#include <gmock/gmock.h>

#define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts)
#define delete_module(name, flags) syscall(__NR_delete_module, name, flags)

class I2CKernelModule : public testing::Test {
public:
    I2CKernelModule() {
    }
};

TEST_F(I2CKernelModule, TestAddKernelModule) {
    char *params;
    int fd;
    struct stat fileStat;
    std::vector<char> fileContent;
    params = "chip_addr=0x20";

    // command: sudo insmod /root/i2c-tests/i2c-stub.ko chip_addr=0x20
    if ((fd = open("/root/i2c-tests/i2c-stub.ko", O_RDONLY)) < 0) {
        perror("open");
        GTEST_FAIL();
    }
    if (fstat(fd, &fileStat) < 0) {
        perror("fstat");
        GTEST_FAIL();
    }
    fileContent.resize(fileStat.st_size);
    if (read(fd, fileContent.data(), fileStat.st_size + 1) == -1) {
        perror("read");
        GTEST_FAIL();
    }
    if (close(fd)) {
        perror("close");
        GTEST_FAIL();
    }

    if (init_module(fileContent.data(), fileStat.st_size + 1, params) != 0) {
        perror("init_module");
        GTEST_FAIL();
    }
    GTEST_SUCCESS_("Kernel module loaded.");

    /*
    // sudo rmmod i2c_stub
    if (delete_module("i2c_stub", O_NONBLOCK) != 0) {
        perror("delete_module");
        GTEST_FAIL();
    }
    GTEST_SUCCESS_("Kernel module unloaded.");
    */
}
Felipe
  • 7,013
  • 8
  • 44
  • 102
  • 2
    You're resizing the vector to `fileStat.st_size` bytes but reading in `fileStat.st_size + 1` bytes. – Quentin Oct 25 '18 at 08:21
  • If it weren't for this `std::vector`, I would have swear this is C and not C++... – YSC Oct 25 '18 at 08:25
  • @Quentin if I add `+ 1` on all `fileStat.st_size` or if I remove `+ 1` from all `fileStat.st_size` I get the error `double free or corruption (!prev): 0x01ebd440`. – Felipe Oct 25 '18 at 08:30
  • I transfered all the code to a file outside of the CLion and of my project. Turned out that the error is not happening..... – Felipe Oct 25 '18 at 15:39

0 Answers0