1 : /*
2 : * Copyright 2008 The Native Client Authors. All rights reserved.
3 : * Use of this source code is governed by a BSD-style license that can
4 : * be found in the LICENSE file.
5 : */
6 :
7 : /*
8 : * NaCl Simple/secure ELF loader (NaCl SEL) memory object.
9 : */
10 :
11 : #include <stdlib.h>
12 :
13 : #include "native_client/src/shared/platform/nacl_log.h"
14 : #include "native_client/src/shared/platform/nacl_sync_checked.h"
15 : #include "native_client/src/trusted/service_runtime/nacl_memory_object.h"
16 :
17 :
18 : /*
19 : * Takes ownership of NaClDesc object, so no manipulation of ref count.
20 : */
21 : int NaClMemObjCtor(struct NaClMemObj *nmop,
22 : struct NaClDesc *ndp,
23 : nacl_off64_t nbytes,
24 4 : nacl_off64_t offset) {
25 4 : if (NULL == ndp) {
26 0 : NaClLog(LOG_FATAL, "NaClMemObjCtor: ndp is NULL\n");
27 : }
28 4 : nmop->ndp = ndp;
29 4 : NaClDescRef(ndp);
30 4 : nmop->nbytes = nbytes;
31 4 : nmop->offset = offset;
32 4 : return 1;
33 : }
34 :
35 :
36 : /*
37 : * Placement new copy ctor.
38 : */
39 : int NaClMemObjCopyCtorOff(struct NaClMemObj *nmop,
40 : struct NaClMemObj *src,
41 0 : nacl_off64_t additional) {
42 0 : nmop->ndp = src->ndp;
43 0 : NaClDescRef(nmop->ndp);
44 0 : nmop->nbytes = src->nbytes;
45 0 : nmop->offset = src->offset + additional;
46 0 : return 1;
47 : }
48 :
49 :
50 1 : void NaClMemObjDtor(struct NaClMemObj *nmop) {
51 1 : NaClDescUnref(nmop->ndp);
52 1 : nmop->ndp = NULL;
53 1 : nmop->nbytes = 0;
54 1 : nmop->offset = 0;
55 1 : }
56 :
57 24 : void NaClMemObjSafeDtor(struct NaClMemObj *nmop) {
58 24 : if (NULL == nmop) {
59 23 : return;
60 : }
61 1 : NaClMemObjDtor(nmop);
62 : }
63 :
64 :
65 : struct NaClMemObj *NaClMemObjMake(struct NaClDesc *ndp,
66 : nacl_off64_t nbytes,
67 4 : nacl_off64_t offset) {
68 : struct NaClMemObj *nmop;
69 :
70 4 : if (NULL == ndp) {
71 0 : NaClLog(4, "NaClMemObjMake: invoked with NULL ndp\n");
72 0 : return NULL; /* anonymous paging file backed memory */
73 : }
74 4 : if (NULL == (nmop = malloc(sizeof *nmop))) {
75 0 : NaClLog(LOG_FATAL, ("NaClMemObjMake: out of memory creating object "
76 : "(NaClDesc = 0x%08"NACL_PRIxPTR", "
77 : "offset = 0x%"NACL_PRIx64")\n"),
78 : (uintptr_t) ndp, offset);
79 : }
80 4 : if (!NaClMemObjCtor(nmop, ndp, nbytes, offset)) {
81 0 : NaClLog(LOG_FATAL, "NaClMemObjMake: NaClMemObjCtor failed!\n");
82 : }
83 4 : return nmop;
84 : }
85 :
86 :
87 : struct NaClMemObj *NaClMemObjSplit(struct NaClMemObj *orig,
88 1 : nacl_off64_t additional) {
89 : struct NaClMemObj *nmop;
90 :
91 1 : if (NULL == orig)
92 1 : return NULL;
93 :
94 0 : if (NULL == (nmop = malloc(sizeof *nmop))) {
95 0 : NaClLog(LOG_FATAL, ("NaClMemObjSplit: out of memory creating object"
96 : " (NaClMemObj = 0x%08"NACL_PRIxPTR","
97 : " additional = 0x%"NACL_PRIx64")\n"),
98 : (uintptr_t) orig, additional);
99 : }
100 0 : if (!NaClMemObjCopyCtorOff(nmop, orig, additional)) {
101 0 : NaClLog(LOG_FATAL, "NaClMemObjSplit: NaClMemObjCopyCtorOff failed\n");
102 : }
103 0 : return nmop;
104 : }
105 :
106 :
107 : void NaClMemObjIncOffset(struct NaClMemObj *nmop,
108 2 : nacl_off64_t additional) {
109 2 : if (NULL != nmop) {
110 0 : nmop->offset += additional;
111 : }
112 2 : }
|