The code was mangled by posting in HTML. Instead, use plain text (or an
attachment,
if the mailing list allows them.) I [attempt to] attach the code I used:
-rw-rw-r--. 1 jreiser jreiser 3125 Aug 14 09:30 shmtest.c
On my system, /usr/include/linux/shm.h says:
-----
#define SHMMAX 0x2000000 /* max shared seg size (bytes) */
#define SHMMIN 1 /* min shared seg size (bytes) */
#define SHMMNI 4096 /* max num of segs system wide */
#define SHMALL (SHMMAX/getpagesize()*(SHMMNI/16))
#define SHMSEG SHMMNI /* max shared segs per process */
-----
In particular, SHMMAX is only 32MiB, so several of the attempted shared segments
are too big. My current actual parameters in
$ cat /proc/sys/kernel/shmall
$ cat /proc/sys/kernel/shmmax
$ cat /proc/sys/kernel/shmmni
agree with the defaults that are in shm.h.
My test program works for me under valgrind-3.8.0 on Fedora 17 x86_64 in 64-bit
mode.
Linux host.domain 3.5.0-2.fc17.x86_64 #1 SMP Mon Jul 30 14:48:59 UTC 2012
x86_64 x86_64 x86_64 GNU/Linux
gcc (GCC) 4.7.0 20120507 (Red Hat 4.7.0-5)
I [attempt to] attach the output that I get:
-rw-rw-r--. 1 jreiser jreiser 9321 Aug 14 09:31 shmtest.out
To see the system calls that valgrind sees:
valgrind --trace-syscalls=yes ...
Of course you can run that under strace, too, in order to verify
that valgrind actually does what it says it is doing:
strace valgrind --trace-syscalls=yes ...
but it is tedious to read the output.
--
#include <sys/types.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
struct shm_details
{
key_t key;
size_t size;
int shmid;
void *shm;
};
main(int argc,char **argv)
{
int i;
/* default SHMMAX is 32MB */
struct shm_details test_shm[] = {
{0xe300626d, 2147456, -1, NULL },
{0xe300626c, 144271608, -1, NULL}, /* LARGE */
{0xe300626e, 115604, -1, NULL},
{0xe300626f, 118376704, -1, NULL}, /* LARGE */
{0xe300653d, 574592, -1, NULL},
{0xe300653c, 22578504, -1, NULL},
{0xe300653e, 18092, -1, NULL},
{0xe300653f, 208416960, -1, NULL}, /* LARGE */
{0xe30066b9, 2147456, -1, NULL},
{0xe30066b8, 120870984, -1, NULL}, /* LARGE */
{0xe30066ba, 484260, -1, NULL},
{0xe30066bb, 9793648960, -1, NULL}, /* LARGE */
{0xe30068ad, 8438912, -1, NULL},
{0xe30068ac, 553973472, -1, NULL} /* LARGE */
};
int n_err = 0;
int list = (sizeof(test_shm)/sizeof(test_shm[0]));
int shmflg;
int delete=0;
if (argc != 2) {
printf("Usage: ./test_shm -c|-g|-d\n -c for create shared memory\n -g for get shared memory\n -d delete the shared memory\n");
return(0);
}
if (strcmp(argv[1],"-c") == 0) {
shmflg = IPC_CREAT | 0666;
}else if (strcmp(argv[1],"-g") == 0) {
shmflg = 0666;
} else if (strcmp(argv[1],"-d") == 0) {
shmflg = 0666;
delete = 1;
} else {
printf("Usage: ./test_shm -c|-g|-d\n -c for create\n -g for get shared memory\n -d delete the shared memory\n");
return(0);
}
n_err=0;
for (i=0;i < sizeof(test_shm)/sizeof(test_shm[0]); ++i) {
test_shm[i].shmid = shmget(test_shm[i].key, test_shm[i].size, shmflg);
printf("\tid=%#8.8x = shmget(key=%#8.8x, size=%#8.8lx, flag=%#o)\n",
test_shm[i].shmid, test_shm[i].key, test_shm[i].size, shmflg);
if (test_shm[i].shmid == -1) {
perror("shmget");
printf("ERROR: shmget failed for shmkey=0x%)x\n", test_shm[i].key);
++n_err;
}
}
/*delete*/
n_err=0;
if (delete == 1) {
for (i=0;i < sizeof(test_shm)/sizeof(test_shm[0]); ++i) {
struct shmid_ds buf;
int const r = shmctl(test_shm[i].shmid, IPC_RMID, &buf);
printf("\tr=%d = shmctl(id=%#8.8x, op=%x)\n", r, test_shm[i].shmid, IPC_RMID);
if (r!=0) {
perror("shmctl");
printf("ERROR: shmctl failed for shmkey=0x%x)\n", test_shm[i].key);
++n_err;
}
}
printf("INFO: All shared memories are deleted\n");
return n_err;
}
/* Now we attach the segment to our data space. */
n_err=0;
for (i=0;i < sizeof(test_shm)/sizeof(test_shm[0]); ++i) {
test_shm[i].shm = shmat(test_shm[i].shmid, 0, 0);
printf("\t%p = shmat(key=%#8.8x, 0, 0)\n", test_shm[i].shm, test_shm[i].shmid);
if (test_shm[i].shm == (char *) -1) {
perror("shmat");
printf("ERROR: shmat failed for shmkey=0x%x\n", test_shm[i].key);
++n_err;
}
}
n_err=0;
for (i=0;i < sizeof(test_shm)/sizeof(test_shm[0]); ++i) {
int const r = shmdt(test_shm[i].shm);
printf("\t%#x = shmdt(addr=%p)\n", r, test_shm[i].shm);
if (r !=0 ) {
perror("shmat");
printf("ERROR: shmdt failed for shmkey=0x%x\n", test_shm[i].key);
++n_err;
}
return n_err;
}
printf("INFO: exit success\n");
return(0);
}
$ /usr/local/valgrind-3.8.0/bin/valgrind --tool=memcheck --leak-check=full
--track-origins=yes ./shmtest -c
==3629== Memcheck, a memory error detector
==3629== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==3629== Using Valgrind-3.8.0 and LibVEX; rerun with -h for copyright info
==3629== Command: ./shmtest -c
==3629==
id=0x0035000e = shmget(key=0xe300626d, size=0x0020c480, flag=01666)
id=0xffffffff = shmget(key=0xe300626c, size=0x089968f8, flag=01666)
shmget: Invalid argument
ERROR: shmget failed for shmkey=0x%)x
id=0x00358010 = shmget(key=0xe300626e, size=0x0001c394, flag=01666)
id=0xffffffff = shmget(key=0xe300626f, size=0x070e4900, flag=01666)
shmget: Invalid argument
ERROR: shmget failed for shmkey=0x%)x
id=0x00360011 = shmget(key=0xe300653d, size=0x0008c480, flag=01666)
id=0x00368013 = shmget(key=0xe300653c, size=0x01588548, flag=01666)
id=0x00370014 = shmget(key=0xe300653e, size=0x000046ac, flag=01666)
id=0xffffffff = shmget(key=0xe300653f, size=0x0c6c30c0, flag=01666)
shmget: Invalid argument
ERROR: shmget failed for shmkey=0x%)x
id=0x00378015 = shmget(key=0xe30066b9, size=0x0020c480, flag=01666)
id=0xffffffff = shmget(key=0xe30066b8, size=0x07345848, flag=01666)
shmget: Invalid argument
ERROR: shmget failed for shmkey=0x%)x
id=0x00380016 = shmget(key=0xe30066ba, size=0x000763a4, flag=01666)
id=0xffffffff = shmget(key=0xe30066bb, size=0x247bf3940, flag=01666)
shmget: Invalid argument
ERROR: shmget failed for shmkey=0x%)x
id=0x00388017 = shmget(key=0xe30068ad, size=0x0080c480, flag=01666)
id=0xffffffff = shmget(key=0xe30068ac, size=0x2104f6e0, flag=01666)
shmget: Invalid argument
ERROR: shmget failed for shmkey=0x%)x
0x5028000 = shmat(key=0x0035000e, 0, 0)
0xffffffffffffffff = shmat(key=0xffffffff, 0, 0)
shmat: Invalid argument
ERROR: shmat failed for shmkey=0xe300626c
0x5235000 = shmat(key=0x00358010, 0, 0)
0xffffffffffffffff = shmat(key=0xffffffff, 0, 0)
shmat: Invalid argument
ERROR: shmat failed for shmkey=0xe300626f
0x5252000 = shmat(key=0x00360011, 0, 0)
0x52df000 = shmat(key=0x00368013, 0, 0)
0x4c0f000 = shmat(key=0x00370014, 0, 0)
0xffffffffffffffff = shmat(key=0xffffffff, 0, 0)
shmat: Invalid argument
ERROR: shmat failed for shmkey=0xe300653f
0x6868000 = shmat(key=0x00378015, 0, 0)
0xffffffffffffffff = shmat(key=0xffffffff, 0, 0)
shmat: Invalid argument
ERROR: shmat failed for shmkey=0xe30066b8
0x6a75000 = shmat(key=0x00380016, 0, 0)
0xffffffffffffffff = shmat(key=0xffffffff, 0, 0)
shmat: Invalid argument
ERROR: shmat failed for shmkey=0xe30066bb
0x6aec000 = shmat(key=0x00388017, 0, 0)
0xffffffffffffffff = shmat(key=0xffffffff, 0, 0)
shmat: Invalid argument
ERROR: shmat failed for shmkey=0xe30068ac
0 = shmdt(addr=0x5028000)
==3629==
==3629== HEAP SUMMARY:
==3629== in use at exit: 0 bytes in 0 blocks
==3629== total heap usage: 12 allocs, 12 frees, 6,816 bytes allocated
==3629==
==3629== All heap blocks were freed -- no leaks are possible
==3629==
==3629== For counts of detected and suppressed errors, rerun with: -v
==3629== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
$ /usr/local/valgrind-3.8.0/bin/valgrind --tool=memcheck --leak-check=full
--track-origins=yes ./shmtest -g
==3630== Memcheck, a memory error detector
==3630== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==3630== Using Valgrind-3.8.0 and LibVEX; rerun with -h for copyright info
==3630== Command: ./shmtest -g
==3630==
id=0x0035000e = shmget(key=0xe300626d, size=0x0020c480, flag=0666)
id=0xffffffff = shmget(key=0xe300626c, size=0x089968f8, flag=0666)
shmget: No such file or directory
ERROR: shmget failed for shmkey=0x%)x
id=0x00358010 = shmget(key=0xe300626e, size=0x0001c394, flag=0666)
id=0xffffffff = shmget(key=0xe300626f, size=0x070e4900, flag=0666)
shmget: No such file or directory
ERROR: shmget failed for shmkey=0x%)x
id=0x00360011 = shmget(key=0xe300653d, size=0x0008c480, flag=0666)
id=0x00368013 = shmget(key=0xe300653c, size=0x01588548, flag=0666)
id=0x00370014 = shmget(key=0xe300653e, size=0x000046ac, flag=0666)
id=0xffffffff = shmget(key=0xe300653f, size=0x0c6c30c0, flag=0666)
shmget: No such file or directory
ERROR: shmget failed for shmkey=0x%)x
id=0x00378015 = shmget(key=0xe30066b9, size=0x0020c480, flag=0666)
id=0xffffffff = shmget(key=0xe30066b8, size=0x07345848, flag=0666)
shmget: No such file or directory
ERROR: shmget failed for shmkey=0x%)x
id=0x00380016 = shmget(key=0xe30066ba, size=0x000763a4, flag=0666)
id=0xffffffff = shmget(key=0xe30066bb, size=0x247bf3940, flag=0666)
shmget: No such file or directory
ERROR: shmget failed for shmkey=0x%)x
id=0x00388017 = shmget(key=0xe30068ad, size=0x0080c480, flag=0666)
id=0xffffffff = shmget(key=0xe30068ac, size=0x2104f6e0, flag=0666)
shmget: No such file or directory
ERROR: shmget failed for shmkey=0x%)x
0x5028000 = shmat(key=0x0035000e, 0, 0)
0xffffffffffffffff = shmat(key=0xffffffff, 0, 0)
shmat: Invalid argument
ERROR: shmat failed for shmkey=0xe300626c
0x5235000 = shmat(key=0x00358010, 0, 0)
0xffffffffffffffff = shmat(key=0xffffffff, 0, 0)
shmat: Invalid argument
ERROR: shmat failed for shmkey=0xe300626f
0x5252000 = shmat(key=0x00360011, 0, 0)
0x52df000 = shmat(key=0x00368013, 0, 0)
0x4c0f000 = shmat(key=0x00370014, 0, 0)
0xffffffffffffffff = shmat(key=0xffffffff, 0, 0)
shmat: Invalid argument
ERROR: shmat failed for shmkey=0xe300653f
0x6868000 = shmat(key=0x00378015, 0, 0)
0xffffffffffffffff = shmat(key=0xffffffff, 0, 0)
shmat: Invalid argument
ERROR: shmat failed for shmkey=0xe30066b8
0x6a75000 = shmat(key=0x00380016, 0, 0)
0xffffffffffffffff = shmat(key=0xffffffff, 0, 0)
shmat: Invalid argument
ERROR: shmat failed for shmkey=0xe30066bb
0x6aec000 = shmat(key=0x00388017, 0, 0)
0xffffffffffffffff = shmat(key=0xffffffff, 0, 0)
shmat: Invalid argument
ERROR: shmat failed for shmkey=0xe30068ac
0 = shmdt(addr=0x5028000)
==3630==
==3630== HEAP SUMMARY:
==3630== in use at exit: 0 bytes in 0 blocks
==3630== total heap usage: 12 allocs, 12 frees, 6,816 bytes allocated
==3630==
==3630== All heap blocks were freed -- no leaks are possible
==3630==
==3630== For counts of detected and suppressed errors, rerun with: -v
==3630== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
$ /usr/local/valgrind-3.8.0/bin/valgrind --tool=memcheck --leak-check=full
--track-origins=yes ./shmtest -d
==3631== Memcheck, a memory error detector
==3631== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==3631== Using Valgrind-3.8.0 and LibVEX; rerun with -h for copyright info
==3631== Command: ./shmtest -d
==3631==
id=0x0035000e = shmget(key=0xe300626d, size=0x0020c480, flag=0666)
id=0xffffffff = shmget(key=0xe300626c, size=0x089968f8, flag=0666)
shmget: No such file or directory
ERROR: shmget failed for shmkey=0x%)x
id=0x00358010 = shmget(key=0xe300626e, size=0x0001c394, flag=0666)
id=0xffffffff = shmget(key=0xe300626f, size=0x070e4900, flag=0666)
shmget: No such file or directory
ERROR: shmget failed for shmkey=0x%)x
id=0x00360011 = shmget(key=0xe300653d, size=0x0008c480, flag=0666)
id=0x00368013 = shmget(key=0xe300653c, size=0x01588548, flag=0666)
id=0x00370014 = shmget(key=0xe300653e, size=0x000046ac, flag=0666)
id=0xffffffff = shmget(key=0xe300653f, size=0x0c6c30c0, flag=0666)
shmget: No such file or directory
ERROR: shmget failed for shmkey=0x%)x
id=0x00378015 = shmget(key=0xe30066b9, size=0x0020c480, flag=0666)
id=0xffffffff = shmget(key=0xe30066b8, size=0x07345848, flag=0666)
shmget: No such file or directory
ERROR: shmget failed for shmkey=0x%)x
id=0x00380016 = shmget(key=0xe30066ba, size=0x000763a4, flag=0666)
id=0xffffffff = shmget(key=0xe30066bb, size=0x247bf3940, flag=0666)
shmget: No such file or directory
ERROR: shmget failed for shmkey=0x%)x
id=0x00388017 = shmget(key=0xe30068ad, size=0x0080c480, flag=0666)
id=0xffffffff = shmget(key=0xe30068ac, size=0x2104f6e0, flag=0666)
shmget: No such file or directory
ERROR: shmget failed for shmkey=0x%)x
r=0 = shmctl(id=0x0035000e, op=0)
r=-1 = shmctl(id=0xffffffff, op=0)
shmctl: Invalid argument
ERROR: shmctl failed for shmkey=0xe300626c)
r=0 = shmctl(id=0x00358010, op=0)
r=-1 = shmctl(id=0xffffffff, op=0)
shmctl: Invalid argument
ERROR: shmctl failed for shmkey=0xe300626f)
r=0 = shmctl(id=0x00360011, op=0)
r=0 = shmctl(id=0x00368013, op=0)
r=0 = shmctl(id=0x00370014, op=0)
r=-1 = shmctl(id=0xffffffff, op=0)
shmctl: Invalid argument
ERROR: shmctl failed for shmkey=0xe300653f)
r=0 = shmctl(id=0x00378015, op=0)
r=-1 = shmctl(id=0xffffffff, op=0)
shmctl: Invalid argument
ERROR: shmctl failed for shmkey=0xe30066b8)
r=0 = shmctl(id=0x00380016, op=0)
r=-1 = shmctl(id=0xffffffff, op=0)
shmctl: Invalid argument
ERROR: shmctl failed for shmkey=0xe30066bb)
r=0 = shmctl(id=0x00388017, op=0)
r=-1 = shmctl(id=0xffffffff, op=0)
shmctl: Invalid argument
ERROR: shmctl failed for shmkey=0xe30068ac)
INFO: All shared memories are deleted
==3631==
==3631== HEAP SUMMARY:
==3631== in use at exit: 0 bytes in 0 blocks
==3631== total heap usage: 12 allocs, 12 frees, 6,816 bytes allocated
==3631==
==3631== All heap blocks were freed -- no leaks are possible
==3631==
==3631== For counts of detected and suppressed errors, rerun with: -v
==3631== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
$
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Valgrind-users mailing list
Valgrind-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/valgrind-users