1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef MOJO_NACL_MOJO_SYSCALL_INTERNAL_H_
6 #define MOJO_NACL_MOJO_SYSCALL_INTERNAL_H_
8 #include "native_client/src/trusted/service_runtime/nacl_copy.h"
9 #include "native_client/src/trusted/service_runtime/sel_ldr.h"
13 class ScopedCopyLock
{
15 explicit ScopedCopyLock(struct NaClApp
* nap
) : nap_(nap
) {
16 NaClCopyTakeLock(nap_
);
19 NaClCopyDropLock(nap_
);
25 static inline uintptr_t NaClUserToSysAddrArray(
30 // TODO(ncbray): overflow checking
31 size_t range
= count
* size
;
32 return NaClUserToSysAddrRange(nap
, uaddr
, range
);
35 template <typename T
> bool ConvertScalarInput(
40 uintptr_t temp
= NaClUserToSysAddrRange(nap
, user_ptr
, sizeof(T
));
41 if (temp
!= kNaClBadAddress
) {
42 *value
= *reinterpret_cast<T
volatile*>(temp
);
49 template <typename T
> bool ConvertScalarOutput(
52 T
volatile** sys_ptr
) {
54 uintptr_t temp
= NaClUserToSysAddrRange(nap
, user_ptr
, sizeof(T
));
55 if (temp
!= kNaClBadAddress
) {
56 *sys_ptr
= reinterpret_cast<T
volatile*>(temp
);
60 *sys_ptr
= 0; // Paranoia.
64 template <typename T
> bool ConvertScalarInOut(
69 T
volatile** sys_ptr
) {
71 uintptr_t temp
= NaClUserToSysAddrRange(nap
, user_ptr
, sizeof(T
));
72 if (temp
!= kNaClBadAddress
) {
73 T
volatile* converted
= reinterpret_cast<T
volatile*>(temp
);
78 } else if (optional
) {
80 *value
= static_cast<T
>(0); // Paranoia.
83 *sys_ptr
= 0; // Paranoia.
84 *value
= static_cast<T
>(0); // Paranoia.
88 template <typename T
> bool ConvertArray(
96 uintptr_t temp
= NaClUserToSysAddrArray(nap
, user_ptr
, length
,
98 if (temp
!= kNaClBadAddress
) {
99 *sys_ptr
= reinterpret_cast<T
*>(temp
);
102 } else if (optional
) {
109 template <typename T
> bool ConvertBytes(
116 uintptr_t temp
= NaClUserToSysAddrRange(nap
, user_ptr
, length
);
117 if (temp
!= kNaClBadAddress
) {
118 *sys_ptr
= reinterpret_cast<T
*>(temp
);
121 } else if (optional
) {
128 // TODO(ncbray): size validation and complete copy.
129 // TODO(ncbray): ensure non-null / missized structs are covered by a test case.
130 template <typename T
> bool ConvertStruct(
136 uintptr_t temp
= NaClUserToSysAddrRange(nap
, user_ptr
, sizeof(T
));
137 if (temp
!= kNaClBadAddress
) {
138 *sys_ptr
= reinterpret_cast<T
*>(temp
);
141 } else if (optional
) {
150 #endif // MOJO_NACL_MOJO_SYSCALL_INTERNAL_H_