Low-level shared memory on iOS

2020-03-26 09:11发布

I'm trying to construct a block of shared memory on iOS. It compiles and links, but shmget() throws a SIGSYS signal on the most innocent parameters:

NSString *p = [[NSBundle mainBundle] pathForResource:@"crash" ofType: nil];
key_t tok = ftok([p UTF8String], 918273);
int mid = shmget(tok, 4096, IPC_CREAT|S_IRUSR|S_IWUSR);

tok is a large positive integer, not -1. Size - tried 1024, same effect.

Is SysV shared memory even supported on iOS? The headers and the libraries are present, or compiler/linker would complain. Same code works on the simulator. What am I doing wrong?

My interest stems from this question.

2条回答
叛逆
2楼-- · 2020-03-26 09:28

The shm_open()/mmap() combo works as advertised, both on simulator and on device (tested on iOS 4) without any explicit permission changes.

Note: shm_open() is weirdly documented as variadic. In reality, you need to specify a third parameter with an access mask - a combination of S_IRUSR-like flags, or an octal chmod value.

EDIT: looks like it broke in iOS 7. shm_open returns -1 with errno=2 (ENOENT) even though O_CREAT flag is specified.

查看更多
Lonely孤独者°
3楼-- · 2020-03-26 09:33

On iOS you cannot use shared memory at all I don't know the exact details but I do know that a) its for security and b) its part of the sandboxing environment

So I know above from myself and my presence in the jailbreak scene however here are a few links describing sandboxing and how they affect shared memory

http://www.trailofbits.com/resources/ios4_security_evaluation_paper.pdf

http://lists.apple.com/archives/cocoa-dev/2012/Apr/msg00535.html

查看更多
登录 后发表回答