1 /*
2  * Copyright (C) 2006-2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #undef LOG_TAG
18 #define LOG_TAG "CursorWindow"
19 
20 #include <androidfw/CursorWindow.h>
21 #include <binder/Parcel.h>
22 #include <utils/Log.h>
23 
24 #include <cutils/ashmem.h>
25 #include <sys/mman.h>
26 
27 #include <assert.h>
28 #include <string.h>
29 #include <stdlib.h>
30 
31 namespace android {
32 
CursorWindow(const String8 & name,int ashmemFd,void * data,size_t size,bool readOnly)33 CursorWindow::CursorWindow(const String8& name, int ashmemFd,
34         void* data, size_t size, bool readOnly) :
35         mName(name), mAshmemFd(ashmemFd), mData(data), mSize(size), mReadOnly(readOnly) {
36     mHeader = static_cast<Header*>(mData);
37 }
38 
~CursorWindow()39 CursorWindow::~CursorWindow() {
40     ::munmap(mData, mSize);
41     ::close(mAshmemFd);
42 }
43 
create(const String8 & name,size_t size,CursorWindow ** outCursorWindow)44 status_t CursorWindow::create(const String8& name, size_t size, CursorWindow** outCursorWindow) {
45     String8 ashmemName("CursorWindow: ");
46     ashmemName.append(name);
47 
48     status_t result;
49     int ashmemFd = ashmem_create_region(ashmemName.string(), size);
50     if (ashmemFd < 0) {
51         result = -errno;
52         ALOGE("CursorWindow: ashmem_create_region() failed: errno=%d.", errno);
53     } else {
54         result = ashmem_set_prot_region(ashmemFd, PROT_READ | PROT_WRITE);
55         if (result < 0) {
56             ALOGE("CursorWindow: ashmem_set_prot_region() failed: errno=%d",errno);
57         } else {
58             void* data = ::mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ashmemFd, 0);
59             if (data == MAP_FAILED) {
60                 result = -errno;
61                 ALOGE("CursorWindow: mmap() failed: errno=%d.", errno);
62             } else {
63                 result = ashmem_set_prot_region(ashmemFd, PROT_READ);
64                 if (result < 0) {
65                     ALOGE("CursorWindow: ashmem_set_prot_region() failed: errno=%d.", errno);
66                 } else {
67                     CursorWindow* window = new CursorWindow(name, ashmemFd,
68                             data, size, false /*readOnly*/);
69                     result = window->clear();
70                     if (!result) {
71                         LOG_WINDOW("Created new CursorWindow: freeOffset=%d, "
72                                 "numRows=%d, numColumns=%d, mSize=%d, mData=%p",
73                                 window->mHeader->freeOffset,
74                                 window->mHeader->numRows,
75                                 window->mHeader->numColumns,
76                                 window->mSize, window->mData);
77                         *outCursorWindow = window;
78                         return OK;
79                     }
80                     delete window;
81                 }
82             }
83             ::munmap(data, size);
84         }
85         ::close(ashmemFd);
86     }
87     *outCursorWindow = NULL;
88     return result;
89 }
90 
createFromParcel(Parcel * parcel,CursorWindow ** outCursorWindow)91 status_t CursorWindow::createFromParcel(Parcel* parcel, CursorWindow** outCursorWindow) {
92     String8 name = parcel->readString8();
93 
94     status_t result;
95     int actualSize;
96     int ashmemFd = parcel->readFileDescriptor();
97     if (ashmemFd == int(BAD_TYPE)) {
98         result = BAD_TYPE;
99         ALOGE("CursorWindow: readFileDescriptor() failed");
100     } else {
101         ssize_t size = ashmem_get_size_region(ashmemFd);
102         if (size < 0) {
103             result = UNKNOWN_ERROR;
104             ALOGE("CursorWindow: ashmem_get_size_region() failed: errno=%d.", errno);
105         } else {
106             int dupAshmemFd = ::fcntl(ashmemFd, F_DUPFD_CLOEXEC, 0);
107             if (dupAshmemFd < 0) {
108                 result = -errno;
109                 ALOGE("CursorWindow: fcntl() failed: errno=%d.", errno);
110             } else {
111                 // the size of the ashmem descriptor can be modified between ashmem_get_size_region
112                 // call and mmap, so we'll check again immediately after memory is mapped
113                 void* data = ::mmap(NULL, size, PROT_READ, MAP_SHARED, dupAshmemFd, 0);
114                 if (data == MAP_FAILED) {
115                     result = -errno;
116                     ALOGE("CursorWindow: mmap() failed: errno=%d.", errno);
117                 } else if ((actualSize = ashmem_get_size_region(dupAshmemFd)) != size) {
118                     ::munmap(data, size);
119                     result = BAD_VALUE;
120                     ALOGE("CursorWindow: ashmem_get_size_region() returned %d, expected %d"
121                             " errno=%d",
122                             actualSize, (int) size, errno);
123                 } else {
124                     CursorWindow* window = new CursorWindow(name, dupAshmemFd,
125                             data, size, true /*readOnly*/);
126                     LOG_WINDOW("Created CursorWindow from parcel: freeOffset=%d, "
127                             "numRows=%d, numColumns=%d, mSize=%d, mData=%p",
128                             window->mHeader->freeOffset,
129                             window->mHeader->numRows,
130                             window->mHeader->numColumns,
131                             window->mSize, window->mData);
132                     *outCursorWindow = window;
133                     return OK;
134                 }
135                 ::close(dupAshmemFd);
136             }
137         }
138     }
139     *outCursorWindow = NULL;
140     return result;
141 }
142 
writeToParcel(Parcel * parcel)143 status_t CursorWindow::writeToParcel(Parcel* parcel) {
144     status_t status = parcel->writeString8(mName);
145     if (!status) {
146         status = parcel->writeDupFileDescriptor(mAshmemFd);
147     }
148     return status;
149 }
150 
clear()151 status_t CursorWindow::clear() {
152     if (mReadOnly) {
153         return INVALID_OPERATION;
154     }
155 
156     mHeader->freeOffset = sizeof(Header) + sizeof(RowSlotChunk);
157     mHeader->firstChunkOffset = sizeof(Header);
158     mHeader->numRows = 0;
159     mHeader->numColumns = 0;
160 
161     RowSlotChunk* firstChunk = static_cast<RowSlotChunk*>(offsetToPtr(mHeader->firstChunkOffset));
162     firstChunk->nextChunkOffset = 0;
163     return OK;
164 }
165 
setNumColumns(uint32_t numColumns)166 status_t CursorWindow::setNumColumns(uint32_t numColumns) {
167     if (mReadOnly) {
168         return INVALID_OPERATION;
169     }
170 
171     uint32_t cur = mHeader->numColumns;
172     if ((cur > 0 || mHeader->numRows > 0) && cur != numColumns) {
173         ALOGE("Trying to go from %d columns to %d", cur, numColumns);
174         return INVALID_OPERATION;
175     }
176     mHeader->numColumns = numColumns;
177     return OK;
178 }
179 
allocRow()180 status_t CursorWindow::allocRow() {
181     if (mReadOnly) {
182         return INVALID_OPERATION;
183     }
184 
185     // Fill in the row slot
186     RowSlot* rowSlot = allocRowSlot();
187     if (rowSlot == NULL) {
188         return NO_MEMORY;
189     }
190 
191     // Allocate the slots for the field directory
192     size_t fieldDirSize = mHeader->numColumns * sizeof(FieldSlot);
193     uint32_t fieldDirOffset = alloc(fieldDirSize, true /*aligned*/);
194     if (!fieldDirOffset) {
195         mHeader->numRows--;
196         LOG_WINDOW("The row failed, so back out the new row accounting "
197                 "from allocRowSlot %d", mHeader->numRows);
198         return NO_MEMORY;
199     }
200     FieldSlot* fieldDir = static_cast<FieldSlot*>(offsetToPtr(fieldDirOffset));
201     memset(fieldDir, 0, fieldDirSize);
202 
203     LOG_WINDOW("Allocated row %u, rowSlot is at offset %u, fieldDir is %d bytes at offset %u\n",
204             mHeader->numRows - 1, offsetFromPtr(rowSlot), fieldDirSize, fieldDirOffset);
205     rowSlot->offset = fieldDirOffset;
206     return OK;
207 }
208 
freeLastRow()209 status_t CursorWindow::freeLastRow() {
210     if (mReadOnly) {
211         return INVALID_OPERATION;
212     }
213 
214     if (mHeader->numRows > 0) {
215         mHeader->numRows--;
216     }
217     return OK;
218 }
219 
alloc(size_t size,bool aligned)220 uint32_t CursorWindow::alloc(size_t size, bool aligned) {
221     uint32_t padding;
222     if (aligned) {
223         // 4 byte alignment
224         padding = (~mHeader->freeOffset + 1) & 3;
225     } else {
226         padding = 0;
227     }
228 
229     uint32_t offset = mHeader->freeOffset + padding;
230     uint32_t nextFreeOffset = offset + size;
231     if (nextFreeOffset > mSize) {
232         ALOGW("Window is full: requested allocation %zu bytes, "
233                 "free space %zu bytes, window size %zu bytes",
234                 size, freeSpace(), mSize);
235         return 0;
236     }
237 
238     mHeader->freeOffset = nextFreeOffset;
239     return offset;
240 }
241 
getRowSlot(uint32_t row)242 CursorWindow::RowSlot* CursorWindow::getRowSlot(uint32_t row) {
243     uint32_t chunkPos = row;
244     RowSlotChunk* chunk = static_cast<RowSlotChunk*>(
245             offsetToPtr(mHeader->firstChunkOffset));
246     while (chunkPos >= ROW_SLOT_CHUNK_NUM_ROWS) {
247         chunk = static_cast<RowSlotChunk*>(offsetToPtr(chunk->nextChunkOffset));
248         chunkPos -= ROW_SLOT_CHUNK_NUM_ROWS;
249     }
250     return &chunk->slots[chunkPos];
251 }
252 
allocRowSlot()253 CursorWindow::RowSlot* CursorWindow::allocRowSlot() {
254     uint32_t chunkPos = mHeader->numRows;
255     RowSlotChunk* chunk = static_cast<RowSlotChunk*>(
256             offsetToPtr(mHeader->firstChunkOffset));
257     while (chunkPos > ROW_SLOT_CHUNK_NUM_ROWS) {
258         chunk = static_cast<RowSlotChunk*>(offsetToPtr(chunk->nextChunkOffset));
259         chunkPos -= ROW_SLOT_CHUNK_NUM_ROWS;
260     }
261     if (chunkPos == ROW_SLOT_CHUNK_NUM_ROWS) {
262         if (!chunk->nextChunkOffset) {
263             chunk->nextChunkOffset = alloc(sizeof(RowSlotChunk), true /*aligned*/);
264             if (!chunk->nextChunkOffset) {
265                 return NULL;
266             }
267         }
268         chunk = static_cast<RowSlotChunk*>(offsetToPtr(chunk->nextChunkOffset));
269         chunk->nextChunkOffset = 0;
270         chunkPos = 0;
271     }
272     mHeader->numRows += 1;
273     return &chunk->slots[chunkPos];
274 }
275 
getFieldSlot(uint32_t row,uint32_t column)276 CursorWindow::FieldSlot* CursorWindow::getFieldSlot(uint32_t row, uint32_t column) {
277     if (row >= mHeader->numRows || column >= mHeader->numColumns) {
278         ALOGE("Failed to read row %d, column %d from a CursorWindow which "
279                 "has %d rows, %d columns.",
280                 row, column, mHeader->numRows, mHeader->numColumns);
281         return NULL;
282     }
283     RowSlot* rowSlot = getRowSlot(row);
284     if (!rowSlot) {
285         ALOGE("Failed to find rowSlot for row %d.", row);
286         return NULL;
287     }
288     FieldSlot* fieldDir = static_cast<FieldSlot*>(offsetToPtr(rowSlot->offset));
289     return &fieldDir[column];
290 }
291 
putBlob(uint32_t row,uint32_t column,const void * value,size_t size)292 status_t CursorWindow::putBlob(uint32_t row, uint32_t column, const void* value, size_t size) {
293     return putBlobOrString(row, column, value, size, FIELD_TYPE_BLOB);
294 }
295 
putString(uint32_t row,uint32_t column,const char * value,size_t sizeIncludingNull)296 status_t CursorWindow::putString(uint32_t row, uint32_t column, const char* value,
297         size_t sizeIncludingNull) {
298     return putBlobOrString(row, column, value, sizeIncludingNull, FIELD_TYPE_STRING);
299 }
300 
putBlobOrString(uint32_t row,uint32_t column,const void * value,size_t size,int32_t type)301 status_t CursorWindow::putBlobOrString(uint32_t row, uint32_t column,
302         const void* value, size_t size, int32_t type) {
303     if (mReadOnly) {
304         return INVALID_OPERATION;
305     }
306 
307     FieldSlot* fieldSlot = getFieldSlot(row, column);
308     if (!fieldSlot) {
309         return BAD_VALUE;
310     }
311 
312     uint32_t offset = alloc(size);
313     if (!offset) {
314         return NO_MEMORY;
315     }
316 
317     memcpy(offsetToPtr(offset), value, size);
318 
319     fieldSlot->type = type;
320     fieldSlot->data.buffer.offset = offset;
321     fieldSlot->data.buffer.size = size;
322     return OK;
323 }
324 
putLong(uint32_t row,uint32_t column,int64_t value)325 status_t CursorWindow::putLong(uint32_t row, uint32_t column, int64_t value) {
326     if (mReadOnly) {
327         return INVALID_OPERATION;
328     }
329 
330     FieldSlot* fieldSlot = getFieldSlot(row, column);
331     if (!fieldSlot) {
332         return BAD_VALUE;
333     }
334 
335     fieldSlot->type = FIELD_TYPE_INTEGER;
336     fieldSlot->data.l = value;
337     return OK;
338 }
339 
putDouble(uint32_t row,uint32_t column,double value)340 status_t CursorWindow::putDouble(uint32_t row, uint32_t column, double value) {
341     if (mReadOnly) {
342         return INVALID_OPERATION;
343     }
344 
345     FieldSlot* fieldSlot = getFieldSlot(row, column);
346     if (!fieldSlot) {
347         return BAD_VALUE;
348     }
349 
350     fieldSlot->type = FIELD_TYPE_FLOAT;
351     fieldSlot->data.d = value;
352     return OK;
353 }
354 
putNull(uint32_t row,uint32_t column)355 status_t CursorWindow::putNull(uint32_t row, uint32_t column) {
356     if (mReadOnly) {
357         return INVALID_OPERATION;
358     }
359 
360     FieldSlot* fieldSlot = getFieldSlot(row, column);
361     if (!fieldSlot) {
362         return BAD_VALUE;
363     }
364 
365     fieldSlot->type = FIELD_TYPE_NULL;
366     fieldSlot->data.buffer.offset = 0;
367     fieldSlot->data.buffer.size = 0;
368     return OK;
369 }
370 
371 }; // namespace android
372