简体   繁体   English

C ++ std :: map std :: bitset segfault

[英]C++ std::map std::bitset segfault

I have this code: 我有以下代码:

static void XMLCALL
hackHandler(void *data, const XML_Char *name, const XML_Char **attr)
{
SetPointers* sets = static_cast<SetPointers*>(data);
if (strcmp(name, "instruction") == 0 || strcmp(name, "load") == 0 ||
    strcmp(name, "modify") == 0||strcmp(name, "store") == 0) {
    long address(0);
    long page(0);
    int offset(0);
    long size(0);
    int i(0);
    for (i = 0; attr[i]; i += 2) {
        if (strcmp(attr[i], "address") == 0) {
            address = strtol(attr[i+1], NULL, 16);
            page = address >> 12;
            offset = address & 0xFFF;
            continue;
        }
        if (strcmp(attr[i], "size") == 0) {
            size = strtol(attr[i + 1], NULL, 16);
        }
    }
    map<long, bitset<4096> >::iterator itLocal;

    itLocal = sets->lCount->find(page);
    if (itLocal == sets->lCount->end()) {
        sets->lCount->insert(pair<long, bitset<4096> >
            (page, bitset<4096>()));
        itLocal = sets->lCount->find(page);
    }
    //now mark the bitmap
    for (i = 0; i < size; i++) {
        (itLocal->second)[i + offset] = 1;
    }

    if (strcmp(name, "instruction") == 0) {
        itLocal = sets->lCode->find(page);
        if (itLocal == sets->lCode->end()) {
            sets->lCode->insert(pair<long, bitset<4096> >
                (page, bitset<4096>()));
            itLocal = sets->lCode->find(page);
        }
        for (i = 0; i < size; i++) {
            (itLocal->second)[i + offset] = 1;
        }
    } else {
        itLocal = sets->lMemory->find(page);
        if (itLocal == sets->lMemory->end()) {
            sets->lMemory->insert(pair<long, bitset<4096> >
                (page, bitset<4096>()));
            itLocal = sets->lMemory->find(page);
        }
        for (i = 0; i < size; i++) {
            (itLocal->second)[i + offset] = 1;
        }
    }
}
}

This aims to mark a bitset, 4096 bits long, with a 1 when that byte of a page is accessed. 目的是在访问页面的字节时用1标记一个长度为4096位的位集。

This code works well on my test machine, when I use about 1GB of XML to test. 当我使用大约1GB的XML进行测试时,此代码在我的测试机上运行良好。 But when I run it on the full thing (220GB of XML) it gives a segmentation fault on: 但是,当我在全部内容(220GB的XML)上运行它时,会在以下方面产生分段错误:

 sets->lCode->insert(pair<long, bitset<4096> >
            (page, bitset<4096>()));

But it does this very early on in the run, so it's difficult to think this is a product of the size of the data. 但这是在运行的早期完成的,因此很难认为这是数据大小的产物。 In any case I am have had no problem in analysing this larger data set using some very similar code (check my github repo at https://github.com/mcmenaminadrian - this project is memsize, but pagestat uses very siumilar code). 无论如何,我都可以使用一些非常相似的代码来分析这个更大的数据集(在https://github.com/mcmenaminadrian上查看我的github存储库-这个项目是memsize,但是pagestat使用非常相似的代码)没有问题。 The only differentiating factor with this code seems to be the use of bitset. 此代码的唯一区别因素似乎是使用位集。

Can someone spot the error which has eluded me so far? 到目前为止,有人可以发现我所无法企及的错误吗?

(The code is multithreaded - is bitset thread safe? Could this be a library issue - my test system is Mac OSX, but the "production" system is Linux - Ubuntu 12.04 LTS?) (代码是多线程的-位集线程安全吗?这可能是库问题吗?我的测试系统是Mac OSX,而​​“生产”系统是Linux-Ubuntu 12.04 LTS?)

There are no checks to make sure that i + offset is less than 4096 . 没有检查来确保i + offset小于4096 That could be the source of the problem. 这可能是问题的根源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM