开发者

memcpy segmentation fault on linux but not os x

I'm working on implementing a log based file system for a file as a class project. I have a good amount of it working on my 64 bit OS X laptop, but when I try to run the code on the CS department's 32 bit linux machines, I get a seg fault.

The API we're given allows writing DISK_SECTOR_SIZE (512) bytes at a time. Our log record consists of the 512 bytes the user wants to write as well as some metadata (which sector he wants to write to, the type of operation, etc).

All in all, the size of the "record" object is 528 bytes, which means each log record spans 2 sectors on the disk.

The first record writes 0-512 on sector 0, and 0-15 on sector 1. The second record writes 16-512 on sector 1, and 0-31 on sector 2. The third record writes 32-512 on sector 2, and 0-47 on sector 3. ETC.

So what I do is read the two sectors I'll be modifying into 2 freshly allocated buffers, copy starting at record into buf1+the calculated offset for 512-offset bytes. This works correctly on both machines.

However, the second memcpy fails. Specifically, "record+DISK_SECTOR_SIZE-offset" in the below code segfaults, but only on the linux machine. Running some random tests, it gets more curious. The linux machine reports sizeof(Record) to be 528. Therefore, if I tried to memcpy from record+500 into buf for 1 byte, it shouldn't have a problem.

In fact, the biggest offset I can get from record is 254. That is, memcpy(buf1, record+254, 1) works, but memcpy(buf1, record+255, 1) segfaults.

Does anyone know what I'm missing?

Record *record = malloc(sizeof(Record));
record->tid = tid;
record->opType = OP_WRITE;
record->opArg = sector;
int i;
for (i = 0; i < DISK_SECTOR_SIZE; i++) {
  record->data[i] = buf[i]; // *buf is passed into this function
}

char* buf1 = malloc(DISK_SECTOR_SIZE);
char* buf2 = malloc(DISK_SECTOR_SIZE);

d_read(ad->disk, ad->curLogSector, buf1);  // API to read a specified sector into a buffer
d_read(ad->disk, ad->curLogSector+1, buf2);开发者_JAVA技巧

memcpy(buf1+offset, record, DISK_SECTOR_SIZE-offset);
memcpy(buf2, record+DISK_SECTOR_SIZE-offset, offset+sizeof(Record)-sizeof(record->data));


When you add 1 to a pointer p, you aren't adding 1 byte, you're adding sizeof(p) bytes.

So in this case, you need to cast record to a char* before adding to it. Right now record+500 actually points 500*528 = 264,000 bytes away from record.

Of course, this doesn't explain why memcpy(buf, record+254, 1) doesn't segfault. Just "lucky", I guess.


record+BLAH means, when translated into machine code: add BLAH*sizeof(Record) to the address in record.

So record+500 is not 500 bytes from record; it is 500*528 = 264000 bytes away from record.


Try running your Linux code under valgrind - that should take you straight to the root cause of the problem.


record+DISK_SECTOR_SIZE-offset is your problem. record+1 doesnt give and address of record+1.

It gives you and address of record+1*sizeof(record). (when you increment or decrement a pointer it steps in multiples of the data type your using. Just typecast your record pointer like so: (byte*)record+DISK_SECTOR_SIZE-offset. That would explain the segmentation fault since record is at least DISK_SECTOR_SIZE long.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜