Categories
Programming

Invalid array pointers in C

I’ve been working with the MEX API. It basically allows you to run C/Fortran code from MATLAB. This can be very useful, since you can do some performance critical steps in C/Fortran and the rest of the code in Matlab. But often enough, when you try to combine two things with complimentary features, you end up with the worst of both worlds. This is not entirely the case for CMEX, but it does involve much more thought to implement efficient code (since MATLAB’s JIT compiler has been becoming better over time).

But back to the subject. I had recently, amongst other changes, replaced the unsigned ints for ints. The code would run fine, I could manipulate the output, plot it, mesh it, etc. However, as soon as I cleared it, I would get an ugly crash with the not-very-helpful message:

free(): invalid pointer: 0xsomememoryaddress

Oopsie! What in the world does that mean? I investigated the bug further using gdb, but the address I got from the crash message was not part of my variables. After some fussing around, I found out that the problem was that one of our ints was initialized with UINT_MAX, which is 2^{32}-1 for unsigned ints, but is -1 for signed ints (at least for x86 processors). So, it turns out I was writing to the negative indexes of one of the arrays (the exact one that would crash when cleared).

My guess is that array[-1] contains some kind of control code for the array. Maybe a pointer or identifier, so that free() knows what/how much memory to free.

An example code of the issue can be found below:

#include <stdio.h>
#include <stdlib.h>

int main()
{
        double * array = (double*) malloc(100*sizeof(double));

        printf("Array[-1] = 0\n");
        array[-1] = 0;

        printf("Freeing array\n");
        free(array);
        return 0;
}

Which outputs:

*** Error in `./negarr': free(): invalid pointer: 0x0000000000828010 ***

I hope this is helpful for someone.

Categories
Sys Admin

SSH Login using keys

I’ve been using SSH for many reasons lately (X11 Forwarding, managing my server remotely, port forwarding, etc). One thing I’ve learned recently is how to use SSH Keys instead of passwords. There are two main advantages for me to use keys:

  • It eliminates the need to type in my password all of the time (although you can set a password, then you’d need both the KEY and password combination to login)
  • It’s generally safer than using a password (if you disable pure password authentication altogether)

Let’s get down to it, first you create the key. I’m using ECDSA, since it’s as safe as RSA or DSA, but requires less computation (not that it matters much since it’s used only during authentication). (Don’t use ecdsa, it’s believed to have a backdoor. For now we are better off with a 4096-bit RSA key, this post was modified accordingly) I heard that Putty and some older versions of OpenSSH don’t support ECDSA, so bear this in mind. Here we go:

ssh-keygen -t rsa -b 4096

It will ask you to create a passphrase (aka password). If you don’t, then you will be able to login without a password. Now we need to transfer the key to the remote machine (if you used DSA or RSA, the filename will be id_dsa and id_rsa respectively):

ssh-copy-id -i ~/.ssh/id_rsa.pub username@remotemachine

Where username is your user name and remotemachine is the hostname or ip address of the remote machine. Now we log in to the remote machine:

ssh username@remotemachine

If everything went well, you should be able to login without using a password. (:

If you want to disable password authentication, you can edit /etc/ssh/sshd_config:

PasswordAuthentication no
ChallengeResponseAuthentication no

Some of the information was taken from: ArchWiki – SSH Keys

Update: A very good article about securing your SSH is given here: https://stribika.github.io/2015/01/04/secure-secure-shell.html

Categories
Programming

Writing code using cuBLAS and cuSPARSE

When starting to use a new API or library, I’m usually find myself overwhelmed by the whole process. The main issue, I believe, is that the only way to learn how to program is by… well, programming (and I don’t mean it as a joke, but if you must insist…).

Often enough, I find the examples terribly hard to follow. Usually, the problem lies in that there is a single example that tries to show all the features in the program. That approach is a very good way to quickly refresh your memory on how to use a library you already know how to use, but not necessarily useful for a first-timer. In other words, it’s only easy after you know how to do it.

Thinking about this, I’ve created a really really simple CUBLAS example. The code is available here: CUBLAS Sample.zip

For those familiar with BLAS/LAPACK and CUDA, there’s not a whole lot of difference, but I think you’ve gotta drink milk before you eat solid food.

For those who are interested, you can check out a presentation I’ve given at MSU about cuBLAS and cuSPARSE on Oct. 22 2013: cuBLAS and cuSPARSE presentation.pdf

The code for the 2D Jacobi Iteration that I’ve used is here with the approval of Prof. Christlieb: 2DJacobiBlasSparse.zip

Categories
Uncategorized

Hello world

Hi,

welcome to my blog. I hope you find something interesting here. (;

fsjal