UPGRADE YOUR BROWSER

We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Participant futuristic
Participant
1,688 Views
Registered: ‎11-02-2017

@E Simulation failed: SIGSEGV.

Jump to solution

Hello, 

 

Ok, @E Simulation failed: SIGSEGV error is driving me crazy. I am having trouble pin pointing what caused it. Please have a look a the code below. 

int *Mem;
int *Mem1;
int *Mem2;

void allocate_mem()
{
	int kSize = (k) * sizeof(int);
        int pSize = (p) * sizeof(int);
	int tSize = kSize + pSize;

	Mem = (int*)malloc(tSize);
	Mem1 = (int*)Mem;
	Mem2 = (int*)(Mem + kSize);
}
int main()
{
    int a = x*sizeof(int);
    int b = y*sizeof(int);
    int* addrA = (int*)malloc(x*sizeof(int));
    int* addrB = (int*)malloc(y*sizeof(int));

    FILE* filePtr;
    filePtr = fopen("~/input.bin", "rb");
    fread(addrB, sizeof(int), b, filePtr);
    fclose(filePtr);
    memcpy(Mem2, addrB, b);  

    return 0;
}

When debugging the segmentation error comes form the below line. 

 

memcpy(Mem2, addrB, b);  
memcpy(Mem1, addrB, b); // Works fine but the Mem1 block has been loaded its own input from a different file

 I have checked for Mem2 and addrB not returning NULL. Also checked the number of elements from fread(...). Everything seems ok for me but HLS is still throwing the @E Simulation failed: SIGSEGV error. This error won't happen if I select "Build Only".

 

Here is also the message from the debugging: 

 

Breakpoint 1, main () at ../../../topmain.cpp:107
107		memcpy(Mem2, addrB, b);

Program received signal SIGSEGV, Segmentation fault.
0x0040167d in main () at ../../../topmain.cpp:107
107		memcpy(Mem2, addrB, b);

Program received signal SIGSEGV, Segmentation fault.
0x0040167d in main () at ../../../topmain.cpp:107
107		memcpy(Mem2, addrB, b);
[Thread 10808.0x1838 exited with code 3221225477]
[Inferior 1 (process 10808) exited with code 030000000005]
Quit (expect signal SIGINT when the program is resumed)

 

Any comment is appreciated. 

 

Cheers!

0 Kudos
1 Solution

Accepted Solutions
Scholar u4223374
Scholar
2,252 Views
Registered: ‎04-26-2015

Re: @E Simulation failed: SIGSEGV.

Jump to solution

Ah, I think I have it!

 

int kSize = (k) * sizeof(int);

Mem = (int*)malloc(tSize);
Mem2 = (int*)(Mem + kSize);

When you add a value to a pointer, C shifts that many elements through memory, not that many bytes. If you do this:

 

Mem2 = Mem + N;

then (assuming Mem is an int* and sizeof(int) = 4) Mem2 will end up N elements further through RAM, which is 4N bytes.

 

You're adding kSize, so the number of bytes shifted is actually kSize * sizeof(int) = k * sizeof(int) * sizeof(int)...

 

The fix is easy:

 

Mem2 = (int*)(Mem + k);
7 Replies
Scholar hbucher
Scholar
1,684 Views
Registered: ‎03-22-2016

Re: @E Simulation failed: SIGSEGV.

Jump to solution

@futuristic  What are the values of x and y?

Print/check the pointers you get from malloc. It might be too big and failing, returning NULL.

vitorian.com --- We do this for fun. Always give kudos. Accept as solution if your question was answered.
I will not answer to personal messages - use the forums instead.
0 Kudos
Participant futuristic
Participant
1,673 Views
Registered: ‎11-02-2017

Re: @E Simulation failed: SIGSEGV.

Jump to solution
x(25050) and y(784).

Bytes allocated: 102280B (Mem1) + 3136B (Mem2)
Memory region: 14524392 to 14946056

And Mem is pointing to 14524392.
0 Kudos
Scholar u4223374
Scholar
1,641 Views
Registered: ‎04-26-2015

Re: @E Simulation failed: SIGSEGV.

Jump to solution

The obvious issue I can see in your code is that you never actually call allocate_mem(), so Mem2 will not be initialized.

 

 

This shouldn't be causing the problem, but are you sure your array sizes are right?

 

You start with y = 784, then you multiply that by sizeof(int) (ie 4) to get b = 3136, and then you malloc b*sizeof(int) bytes (ie y * sizeof(int) * sizeof(int) bytes). You also ask fread to read (y * sizeof(int)) elements, each with size sizeof(int), again reading y * sizeof(int) * sizeof(int) bytes. However, when you're copying into mem2, you only copy y * sizeof(int) bytes.

 

That may be completely intentional, it just looks a bit strange.

 

 

 

 

0 Kudos
Participant futuristic
Participant
1,637 Views
Registered: ‎11-02-2017

Re: @E Simulation failed: SIGSEGV.

Jump to solution
Yeah that is because I customized my code to post it here. I call the allocate_mem() actually.

You are right on the array size. I have edited it.
0 Kudos
Participant futuristic
Participant
1,635 Views
Registered: ‎11-02-2017

Re: @E Simulation failed: SIGSEGV.

Jump to solution
But why is it not having segmentation issue when memcpy(Mem1, addrB, b); or memcpy(Mem, addrB, b); (even though that is not what I wanted to do)

HLS throws the error only when asking to copy to Mem2. memcpy(Mem2, addrB, b);
0 Kudos
Scholar u4223374
Scholar
2,253 Views
Registered: ‎04-26-2015

Re: @E Simulation failed: SIGSEGV.

Jump to solution

Ah, I think I have it!

 

int kSize = (k) * sizeof(int);

Mem = (int*)malloc(tSize);
Mem2 = (int*)(Mem + kSize);

When you add a value to a pointer, C shifts that many elements through memory, not that many bytes. If you do this:

 

Mem2 = Mem + N;

then (assuming Mem is an int* and sizeof(int) = 4) Mem2 will end up N elements further through RAM, which is 4N bytes.

 

You're adding kSize, so the number of bytes shifted is actually kSize * sizeof(int) = k * sizeof(int) * sizeof(int)...

 

The fix is easy:

 

Mem2 = (int*)(Mem + k);
Participant futuristic
Participant
1,596 Views
Registered: ‎11-02-2017

Re: @E Simulation failed: SIGSEGV.

Jump to solution

That is exactly right. Solved it! Thanks! 

0 Kudos