Skip to content

Commit 803ce80

Browse files
The non-android ASharedMemory_create has a bug, where if two calls to create memory regions use the same 'name', they will be mapped to the to same /dev/shm file and trip over each other (particularly if they're different sizes).
This CL ensures that each created memory region is unique. PiperOrigin-RevId: 322628204 Change-Id: I13b3b59cd87107844dcdbb26ed86f337c761d94f
1 parent 9b5f67e commit 803ce80

1 file changed

Lines changed: 2 additions & 15 deletions

File tree

‎tensorflow/lite/nnapi/nnapi_implementation.cc‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ limitations under the License.
2020
#include <sys/stat.h>
2121
#include <unistd.h>
2222

23-
#include <algorithm>
2423
#include <cstdlib>
2524

2625
#ifdef __ANDROID__
@@ -65,20 +64,8 @@ void* LoadFunction(void* handle, const char* name, bool optional) {
6564

6665
#ifndef __ANDROID__
6766
// Add /dev/shm implementation of shared memory for non-Android platforms
68-
int ASharedMemory_create(const char* /* name */, size_t size) {
69-
// name should not be used to identify the memory region (hence
70-
// 'anonymous' shared memory). Generate a unique name for every create call.
71-
char _tmpname[L_tmpnam];
72-
if (tmpnam_r(_tmpname) == nullptr) {
73-
return -1;
74-
}
75-
76-
// tmpnam will produce a string containing with slashes, but shm_open
77-
// won't like that.
78-
std::string _name = std::string(_tmpname);
79-
std::replace(_name.begin(), _name.end(), '/', '-');
80-
81-
int fd = shm_open(_name.c_str(), O_RDWR | O_CREAT, 0644);
67+
int ASharedMemory_create(const char* name, size_t size) {
68+
int fd = shm_open(name, O_RDWR | O_CREAT, 0644);
8269
if (fd < 0) {
8370
return fd;
8471
}

0 commit comments

Comments
 (0)