Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(267)

Side by Side Diff: src/trusted/service_runtime/fs/obj_proxy.c

Issue 10512008: Remove some unused code that fails to compile with -Wundef (Closed) Base URL: svn://svn.chromium.org/native_client/trunk/src/native_client
Patch Set: Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2011 The Native Client Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
7 /*
8 * NaCl Service Runtime Object Proxy. Used for the NaCl file system
9 * and as a generic object proxy mechanism.
10 */
11
12 #include "native_client/src/include/portability.h"
13
14 #include <string.h>
15
16 #include "native_client/src/shared/platform/nacl_log.h"
17 #include "native_client/src/trusted/service_runtime/fs/obj_proxy.h"
18
19 #define PARANOID 1
20 /* #define DEBUG 1 */
21
22 /* private */
23 struct NaClObjProxyEntry {
24 void *obj;
25 uint8_t name[1]; /* NB: actual allocated size is larger */
26 };
27
28
29 /*
30 * The hash functor class for object->name mapping is the trivial
31 * subclass of NaClHashFunctor: the virtual functions do not need
32 * access to any instance data.
33 */
34
35 static void NaClObjProxyFunctorDtor(struct NaClCmpFunctor *vself) {
36 UNREFERENCED_PARAMETER(vself);
37 return;
38 }
39
40
41 static int NaClObjProxyObjCmp(struct NaClCmpFunctor *vself,
42 void *vlhs,
43 void *vrhs) {
44 struct NaClObjProxyEntry *lhs = (struct NaClObjProxyEntry *) vlhs;
45 struct NaClObjProxyEntry *rhs = (struct NaClObjProxyEntry *) vrhs;
46
47 UNREFERENCED_PARAMETER(vself);
48 return lhs->obj != rhs->obj;
49 }
50
51
52 static uintptr_t NaClObjProxyObjHash(struct NaClHashFunctor *vself,
53 void *ventry) {
54 struct NaClObjProxyEntry *entry = (struct NaClObjProxyEntry *) ventry;
55
56 UNREFERENCED_PARAMETER(vself);
57 return (uintptr_t) entry->obj;
58 }
59
60
61 static struct NaClHashFunctorVtbl const kNaClObjProxyObjHashFunctorVtbl = {
62 {
63 NaClObjProxyFunctorDtor, /* .base.Dtor */
64 NaClObjProxyObjCmp, /* .base.OrderCmp */
65 },
66 NaClObjProxyObjHash, /* .Hash */
67 };
68
69
70 static int NaClObjProxyObjFunctorCtor(struct NaClHashFunctor *self) {
71 self->vtbl = &kNaClObjProxyObjHashFunctorVtbl;
72 return 1;
73 }
74
75 /*
76 * The hash functor for the name->object mapping requires instance
77 * data: the virtual functions need to know the name length.
78 */
79
80 struct NaClObjProxyNameFunctor {
81 struct NaClHashFunctor base;
82 struct NaClObjProxy *info;
83 };
84
85
86 static int NaClObjProxyNameCmp(struct NaClCmpFunctor *vself,
87 void *vlhs, void *vrhs) {
88 struct NaClObjProxyNameFunctor *self
89 = (struct NaClObjProxyNameFunctor *) vself;
90
91 struct NaClObjProxyEntry *lhs = (struct NaClObjProxyEntry *) vlhs;
92 struct NaClObjProxyEntry *rhs = (struct NaClObjProxyEntry *) vrhs;
93
94 return memcmp(lhs->name, rhs->name, self->info->name_bytes);
95 }
96
97 static uintptr_t NaClObjProxyNameHash(struct NaClHashFunctor *vself,
98 void *ventry) {
99 struct NaClObjProxyNameFunctor *self
100 = (struct NaClObjProxyNameFunctor *) vself;
101 struct NaClObjProxyEntry *entry = (struct NaClObjProxyEntry *) ventry;
102
103 uintptr_t h;
104 int i;
105
106 for (i = self->info->name_bytes, h = 0; --i >= 0; ) {
107 h = 17 * h + 3 * entry->name[i]; /* need better mixing? */
108 }
109 return h;
110 }
111
112
113 struct NaClHashFunctorVtbl const kNaClObjProxyNameHashFunctorVtbl = {
114 {
115 NaClObjProxyFunctorDtor, /* .base.Dtor */
116 NaClObjProxyNameCmp, /* .base.OrderCmp */
117 },
118 NaClObjProxyNameHash, /* .Hash */
119 };
120
121
122 static int NaClObjProxyNameFunctorCtor(struct NaClObjProxyNameFunctor *self,
123 struct NaClObjProxy *info) {
124 self->base.vtbl = &kNaClObjProxyNameHashFunctorVtbl;
125 self->info = info;
126 return 1;
127 }
128
129
130 int NaClObjProxyCtor(struct NaClObjProxy *self,
131 struct NaClSecureRngIf *rng,
132 int name_bytes) {
133 int rv = 0;
134 struct NaClHashFunctor *obj2name_functor = NULL;
135 struct NaClObjProxyNameFunctor *name2obj_functor = NULL;
136 struct NaClContainerHashTbl *o2n = NULL;
137 struct NaClContainerHashTbl *n2o = NULL;
138 struct NaClObjProxyEntry *key = NULL;
139
140 self->obj_to_name = NULL;
141 self->name_to_obj = NULL;
142 self->obj_to_name_functor = NULL;
143 self->name_to_obj_functor = NULL;
144
145 if (!NaClMutexCtor(&self->mu)) {
146 goto cleanup;
147 }
148
149 obj2name_functor = malloc(sizeof *obj2name_functor);
150 if (NULL == obj2name_functor) {
151 goto cleanup;
152 }
153 if (!NaClObjProxyObjFunctorCtor(obj2name_functor)) {
154 goto cleanup;
155 }
156 self->obj_to_name_functor = (struct NaClHashFunctor *) obj2name_functor;
157 obj2name_functor = NULL;
158
159 name2obj_functor = malloc(sizeof *name2obj_functor);
160 if (NULL == name2obj_functor) {
161 goto cleanup;
162 }
163 if (!NaClObjProxyNameFunctorCtor(name2obj_functor, self)) {
164 goto cleanup;
165 }
166 self->name_to_obj_functor = (struct NaClHashFunctor *) name2obj_functor;
167 name2obj_functor = NULL;
168
169 o2n = malloc(sizeof *o2n);
170 if (NULL == o2n) {
171 goto cleanup;
172 }
173 n2o = malloc(sizeof *n2o);
174 if (NULL == n2o) {
175 goto cleanup;
176 }
177 key = malloc(sizeof *key + name_bytes);
178 if (!NaClContainerHashTblCtor(o2n, self->obj_to_name_functor, 257)) {
179 goto cleanup;
180 }
181 self->obj_to_name = (struct NaClContainer *) o2n;
182 o2n = NULL;
183 if (!NaClContainerHashTblCtor(n2o, self->name_to_obj_functor, 257)) {
184 goto cleanup;
185 }
186 self->name_to_obj = (struct NaClContainer *) n2o;
187 n2o = NULL;
188 self->name_bytes = name_bytes;
189 self->key = key;
190 self->rng = rng;
191 rv = 1;
192 cleanup:
193 if (!rv) {
194 free(obj2name_functor);
195 if (NULL != self->obj_to_name_functor) {
196 ((*self->obj_to_name_functor->vtbl->base.Dtor)
197 ((struct NaClCmpFunctor *) self->obj_to_name_functor));
198 free(self->obj_to_name_functor);
199 self->obj_to_name_functor = NULL;
200 }
201 free(name2obj_functor);
202 if (NULL != self->name_to_obj_functor) {
203 ((*self->name_to_obj_functor->vtbl->base.Dtor)
204 ((struct NaClCmpFunctor *) self->name_to_obj_functor));
205 self->name_to_obj_functor = NULL;
206 }
207 free(o2n);
208 if (NULL != self->obj_to_name) {
209 (*self->obj_to_name->vtbl->Dtor)(self->obj_to_name);
210 free(self->obj_to_name);
211 self->obj_to_name = NULL;
212 }
213 free(n2o);
214 if (NULL != self->name_to_obj) {
215 (*self->name_to_obj->vtbl->Dtor)(self->name_to_obj);
216 free(self->name_to_obj);
217 self->name_to_obj = NULL;
218 }
219 free(key);
220 NaClMutexDtor(&self->mu);
221 }
222 return rv;
223 }
224
225
226 void NaClObjProxyDtor(struct NaClObjProxy *self) {
227 ((*self->obj_to_name_functor->vtbl->base.Dtor)
228 ((struct NaClCmpFunctor *) self->obj_to_name_functor));
229 ((*self->name_to_obj_functor->vtbl->base.Dtor)
230 ((struct NaClCmpFunctor *) self->name_to_obj_functor));
231 (*self->name_to_obj->vtbl->Dtor)(self->name_to_obj);
232 free(self->name_to_obj);
233 (*self->obj_to_name->vtbl->Dtor)(self->obj_to_name);
234 free(self->obj_to_name);
235 free(self->key);
236 NaClMutexDtor(&self->mu);
237 }
238
239
240 uint8_t const *NaClObjProxyFindNameByObj(struct NaClObjProxy *self,
241 void *obj) {
242 struct NaClObjProxyEntry *found = NULL;
243 struct NaClContainerHashTblIter iter;
244 uint8_t const *rv = NULL;
245
246 NaClXMutexLock(&self->mu);
247 self->key->obj = obj;
248 (*self->obj_to_name->vtbl->Find)(self->obj_to_name,
249 self->key,
250 (struct NaClContainerIter *) &iter);
251 if (!(*iter.base.vtbl->AtEnd)((struct NaClContainerIter *) &iter)) {
252 found = ((struct NaClObjProxyEntry *)
253 (*iter.base.vtbl->Star)((struct NaClContainerIter *) &iter));
254 rv = found->name;
255 }
256 NaClXMutexUnlock(&self->mu);
257
258 return rv;
259 }
260
261
262 int NaClObjProxyFindObjByName(struct NaClObjProxy *self,
263 uint8_t const *name,
264 void **out) {
265 struct NaClObjProxyEntry *found = NULL;
266 struct NaClContainerHashTblIter iter;
267 int rv = 0;
268
269 NaClXMutexLock(&self->mu);
270 memcpy(self->key->name, name, self->name_bytes);
271 (*self->name_to_obj->vtbl->Find)(self->name_to_obj,
272 self->key,
273 (struct NaClContainerIter *) &iter);
274 if (!(*iter.base.vtbl->AtEnd)((struct NaClContainerIter *) &iter)) {
275 found = ((struct NaClObjProxyEntry *)
276 (*iter.base.vtbl->Star)((struct NaClContainerIter *) &iter));
277 *out = found->obj;
278 #if DEBUG
279 printf("ObjByName %d: %02x%02x%02x...\n", (int) found->obj,
280 self->key->name[0], self->key->name[1], self->key->name[2]);
281 #endif
282 rv = 1;
283 }
284 NaClXMutexUnlock(&self->mu);
285 return rv;
286 }
287
288
289 uint8_t const *NaClObjProxyInsert(struct NaClObjProxy *self, void *obj) {
290 struct NaClObjProxyEntry *entry = NULL;
291 struct NaClObjProxyEntry *entry2 = NULL;
292 uint8_t const *rv = NULL;
293 #if PARANOID
294 void *out;
295
296 if (NULL != NaClObjProxyFindNameByObj(self, obj)) {
297 return rv;
298 }
299 #endif
300
301 entry = malloc(sizeof *entry + self->name_bytes);
302 if (NULL == entry) {
303 goto cleanup;
304 }
305 entry2 = malloc(sizeof *entry2 + self->name_bytes);
306 if (NULL == entry2) {
307 free(entry);
308 goto cleanup;
309 }
310 entry->obj = obj;
311 #if PARANOID
312 do {
313 #endif
314 (*self->rng->vtbl->GenBytes)(self->rng,
315 entry->name, self->name_bytes);
316 #if PARANOID
317 } while (NaClObjProxyFindObjByName(self, entry->name, &out));
318 #endif
319 memcpy(entry2, entry, (sizeof *entry) + self->name_bytes);
320 #if DEBUG
321 printf("entry: %d: %02x%02x%02x...\n", (int) entry->obj,
322 entry->name[0], entry->name[1], entry->name[2]);
323 printf("entry2: %d: %02x%02x%02x...\n", (int) entry2->obj,
324 entry2->name[0], entry2->name[1], entry2->name[2]);
325 #endif
326
327 NaClXMutexLock(&self->mu);
328 (*self->obj_to_name->vtbl->Insert)(self->obj_to_name, entry);
329 (*self->name_to_obj->vtbl->Insert)(self->name_to_obj, entry2);
330 NaClXMutexUnlock(&self->mu);
331
332 rv = entry->name;
333 cleanup:
334 return rv;
335 }
336
337
338 int NaClObjProxyRemove(struct NaClObjProxy *self, void *obj) {
339 /*
340 * must first find the entry so we know its randomly assigned name,
341 * then delete from name_to_obj before deleting from obj_to_name.
342 */
343 struct NaClContainerHashTblIter o2niter;
344 struct NaClObjProxyEntry *entry;
345 struct NaClContainerHashTblIter n2oiter;
346
347 self->key->obj = obj;
348
349 (*self->obj_to_name->vtbl->Find)(self->obj_to_name,
350 self->key,
351 (struct NaClContainerIter *) &o2niter);
352 if ((*o2niter.base.vtbl->AtEnd)((struct NaClContainerIter *) &o2niter)) {
353 return 0; /* not found */
354 }
355 entry = ((struct NaClObjProxyEntry *)
356 (*o2niter.base.vtbl->Star)((struct NaClContainerIter *) &o2niter));
357 (*self->name_to_obj->vtbl->Find)(self->name_to_obj,
358 entry,
359 (struct NaClContainerIter *) &n2oiter);
360 if ((*n2oiter.base.vtbl->AtEnd)((struct NaClContainerIter *) &n2oiter)) {
361 NaClLog(LOG_FATAL, "object %08"NACL_PRIxPTR
362 " found in o2n tbl, but not in n2o\n",
363 (uintptr_t) obj);
364 return 0; /* internal error! */
365 }
366 (*o2niter.base.vtbl->Erase)((struct NaClContainerIter *) &o2niter);
367 (*n2oiter.base.vtbl->Erase)((struct NaClContainerIter *) &n2oiter);
368
369 return 1;
370 }
OLDNEW
« no previous file with comments | « src/trusted/service_runtime/fs/obj_proxy.h ('k') | src/trusted/service_runtime/fs/obj_proxy_test.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698