Search code examples
c++linuxubunturuntime-error

I don't know what cause segmentation fault(core dumped)


void hexdump(void* ptr, const int buflen)
{
unsigned char* buf = (unsigned char*)ptr;
int i, j, d, hex = 0;
short* ins;
string op;
for (i = 0; i < buflen; i += 16) {
    for (j = 0; j < 16; j += 4) { 
        if (i + j < buflen) {
            cout << buflen << endl;
            cout << "inst " << (i+j) / 4 << ": ";

I was using linux ubuntu server. My purpose for programing is to read mechine code binary file and get the assembly code and print it out. However, above code is where failure is printed. Until cout << "inst " << (i+j) / 4 << ": "; it works, and buflen(which is 24) is printed but after that segmentation fault(core dumped) comes out and my execution stops. These are the rest of the code. (find,work functions are not yet made or used)

#include <fstream>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

string find(char op[7]);
void work(string inst, short* ins);

void hexdump(void* ptr, const int buflen)
{
unsigned char* buf = (unsigned char*)ptr;
int i, j, d, hex = 0;
short* ins;
string op;
for (i = 0; i < buflen; i += 16) {
    for (j = 0; j < 16; j += 4) { 
        if (i + j < buflen) {
            cout << buflen << endl;
            cout << "inst " << (i+j) / 4 << ": ";
            for (int a = 0; a < 32; a += 8) {
                d = buf[i + j + a / 8];

                for (int k = 0; k < 8; k++) {
                    if (d % 2 != 0) {
                        ins[k + a] = 1;
                    }
                    else {
                        ins[k + a] = 0;
                    }
                    
                    d = d / 2;
                }
            }
            for (int i = 31; i >= 0; i -= 4) {
                hex = hex + ins[i] * 8;
                hex = hex + ins[i - 1] * 4;
                hex = hex + ins[i - 2] * 2;
                hex = hex + ins[i - 3] * 1;
                if (hex == 10)
                    printf("a");
                else if (hex == 11)
                    printf("b");
                else if (hex == 12)
                    printf("c");
                else if (hex == 13)
                    printf("d");
                else if (hex == 14)
                    printf("e");
                else if (hex == 15)
                    printf("f");
                else
                    printf("%d", hex);
                hex = 0;
            }
            for (int i = 6; i >=0; i--) {
                if (ins[i] == 1)
                    op.append("1");
                else if (ins[i] == 0)
                    op.append("0");
            }
            cout << endl << op << endl;
            //work(find(op), ins);
            printf("\n");
        }
    }
}
}

int main(int argc, char* argv[])
{
ifstream in;

in.open(argv[1], ios::in | ios::binary);
if (in.is_open())
{
    // get the starting position
    streampos start = in.tellg();

    // go to the end
    in.seekg(0, std::ios::end);

    // get the ending position
    streampos end = in.tellg();

    // go back to the start
    in.seekg(0, std::ios::beg);

    // create a vector to hold the data that
    // is resized to the total size of the file    
    std::vector<char> contents;
    contents.resize(static_cast<size_t>(end - start));

    // read it in
    in.read(&contents[0], contents.size());

    // print it out (for clarity)
    hexdump(contents.data(), contents.size());
}
in.close();
return 0;
}
string find(char op[7]) {
   string inst("unknown instruction");
   if(op=="")
   return inst;
}
void work(string inst, short* ins);

Solution

  • tldr: The variable ins is pointing to a random memory because the code never assigns it to anything valid. Hence, you have undefined behavior (crashing being the most likely outcome) when dereferencing this pointer and writing to it's address.

    short* ins;   // THIS POINTER NEVER GETS ALLOCATED OR ASSIGNED TO VALID MEMORY
    string op;
    for (i = 0; i < buflen; i += 16) {
        for (j = 0; j < 16; j += 4) { 
            if (i + j < buflen) {
                cout << buflen << endl;
                cout << "inst " << (i+j) / 4 << ": ";
                for (int a = 0; a < 32; a += 8) {
                    d = buf[i + j + a / 8];
    
                    for (int k = 0; k < 8; k++) {
                        if (d % 2 != 0) {
                            ins[k + a] = 1;   // THIS IS UNDEFINED BEHAVIOR, IT PROBABLY CRASHES