1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "napi/native_api.h"
17 #include <cstdio>
18 #include <cstring>
19 #include <fcntl.h>
20 #include <js_native_api.h>
21 #include <js_native_api_types.h>
22 #include <node_api.h>
23 #include <sys/stat.h>
24 #include <sys/uio.h>
25 #include <unistd.h>
26
27 #define PARAM_0 0
28 #define PARAM_1 1
29 #define PARAM_2 2
30 #define PARAM_256 256
31 #define PARAM_UNNORMAL (-1)
32 #define RETURN_0 0
33 #define FAILD (-1)
34 #define ERRON_0 0
35 #define SIZE_10 10
36 #define SIZE_100 100
37 #define SIZE_4096 4096
38 #define SIZE_8192 8192
39 #define TEST_AT_FDCWD (-100)
40 #define TEST_ERROR_AT_FDCWD 100
41
42 #define NO_ERR 0
43 #define SUCCESS 1
44 #define FAIL (-1)
45 #define TEN 10
46 #define TEST_MODE 0666
Readv(napi_env env,napi_callback_info info)47 static napi_value Readv(napi_env env, napi_callback_info info)
48 {
49 size_t argc = 1;
50 napi_value args[1] = {nullptr};
51 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
52
53 int param;
54 napi_get_value_int32(env, args[0], ¶m);
55
56 int ret;
57 if (param == PARAM_UNNORMAL) {
58 ret = readv(PARAM_UNNORMAL, nullptr, PARAM_UNNORMAL);
59 } else {
60 int fd = open("/data/storage/el2/base/files/testReadv.txt", O_RDWR | O_CREAT);
61
62 char buf1[] = "testreadv1";
63 char buf2[] = "testreadv2";
64 struct iovec ios[] = {{buf1, strlen(buf1)}, {buf2, strlen(buf2)}};
65 ssize_t value = writev(fd, ios, sizeof(ios) / sizeof(struct iovec));
66 lseek(fd, PARAM_0, SEEK_SET);
67 memset(buf1, PARAM_0, sizeof(buf1));
68 memset(buf2, PARAM_0, sizeof(buf2));
69 value = readv(fd, ios, sizeof(ios) / sizeof(struct iovec));
70 if (value == strlen(buf1) + strlen(buf2)) {
71 ret = RETURN_0;
72 } else {
73 ret = FAIL;
74 }
75 close(fd);
76 }
77
78 napi_value result = nullptr;
79 napi_create_int32(env, ret, &result);
80 return result;
81 }
82
Writev(napi_env env,napi_callback_info info)83 static napi_value Writev(napi_env env, napi_callback_info info)
84 {
85 size_t argc = 1;
86 napi_value args[1] = {nullptr};
87 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
88 int param;
89 napi_get_value_int32(env, args[0], ¶m);
90 ssize_t resultValue = FAILD;
91 if (param == PARAM_UNNORMAL) {
92 resultValue = writev(PARAM_UNNORMAL, nullptr, PARAM_UNNORMAL);
93 } else {
94 char str0[] = "test ";
95 char str1[] = "writev\n";
96 struct iovec iov[PARAM_2];
97
98 iov[PARAM_0].iov_base = str0;
99 iov[PARAM_0].iov_len = strlen(str0) + PARAM_1;
100 iov[PARAM_1].iov_base = str1;
101 iov[PARAM_1].iov_len = strlen(str1) + PARAM_1;
102
103 ssize_t result = writev(PARAM_1, iov, PARAM_2);
104 if (result == (iov[PARAM_0].iov_len + iov[PARAM_1].iov_len)) {
105 resultValue = RETURN_0;
106 }
107 }
108
109 napi_value result = nullptr;
110 napi_create_int32(env, resultValue, &result);
111
112 return result;
113 }
114
PWriteV(napi_env env,napi_callback_info info)115 static napi_value PWriteV(napi_env env, napi_callback_info info)
116 {
117 char tmpfile[] = "/data/storage/el2/base/files/memfd_cr02100.txt";
118 char buf1[] = "preadv";
119 char buf2[] = "and pwritev";
120 struct iovec iov[2];
121
122 iov[0].iov_base = buf1;
123 iov[0].iov_len = sizeof(buf1) / sizeof(char);
124 iov[1].iov_base = buf2;
125 iov[1].iov_len = sizeof(buf2) / sizeof(char);
126
127 int fd = open(tmpfile, O_RDWR | O_CREAT, TEST_MODE);
128 int count = sizeof(iov) / sizeof(struct iovec);
129 int ret = pwritev(fd, iov, count, PARAM_0);
130 if (ret != PARAM_UNNORMAL) {
131 ret = SUCCESS;
132 } else {
133 ret = FAIL;
134 }
135 close(fd);
136 ret = access(tmpfile, F_OK);
137 if (ret == PARAM_0) {
138 ret = remove(tmpfile);
139 }
140 napi_value result = nullptr;
141 napi_create_int32(env, ret, &result);
142 return result;
143 }
PWriteV64(napi_env env,napi_callback_info info)144 static napi_value PWriteV64(napi_env env, napi_callback_info info)
145 {
146 char tmpfile[] = "/data/storage/el2/base/files/memfd_cr02100.txt";
147 char buf1[] = "preadv";
148 char buf2[] = "and pwritev";
149 struct iovec iov[2];
150
151 iov[0].iov_base = buf1;
152 iov[0].iov_len = sizeof(buf1) / sizeof(char);
153 iov[1].iov_base = buf2;
154 iov[1].iov_len = sizeof(buf2) / sizeof(char);
155
156 int fd = open(tmpfile, O_RDWR | O_CREAT, TEST_MODE);
157 int count = sizeof(iov) / sizeof(struct iovec);
158 int ret = pwritev64(fd, iov, count, PARAM_0);
159 if (ret != PARAM_UNNORMAL) {
160 ret = SUCCESS;
161 } else {
162 ret = FAIL;
163 }
164 close(fd);
165 ret = access(tmpfile, F_OK);
166 if (ret == PARAM_0) {
167 ret = remove(tmpfile);
168 }
169 napi_value result = nullptr;
170 napi_create_int32(env, ret, &result);
171 return result;
172 }
ProcessVmReadV(napi_env env,napi_callback_info info)173 static napi_value ProcessVmReadV(napi_env env, napi_callback_info info)
174 {
175 int ret = PARAM_UNNORMAL;
176 int bufferSize = PARAM_256;
177 char src[256] = "This is process_vm_readv_0100.";
178 char dst[256] = "";
179 struct iovec remote, local;
180 remote.iov_base = src;
181 remote.iov_len = bufferSize;
182
183 local.iov_base = dst;
184 local.iov_len = bufferSize;
185
186 int rev = process_vm_readv(getpid(), &local, PARAM_1, &remote, PARAM_1, PARAM_0);
187 if (rev == sizeof(src)) {
188 ret = RETURN_0;
189 } else {
190 ret = FAIL;
191 }
192 napi_value result = nullptr;
193 napi_create_int32(env, ret, &result);
194 return result;
195 }
ProcessVmWriteV(napi_env env,napi_callback_info info)196 static napi_value ProcessVmWriteV(napi_env env, napi_callback_info info)
197 {
198 int ret = PARAM_UNNORMAL;
199 char src[256] = "This is process_vm_readv_0100.";
200 char dst[256] = "";
201 struct iovec local = {.iov_base = src, .iov_len = sizeof(src)};
202 struct iovec remote = {.iov_base = dst, .iov_len = sizeof(dst)};
203
204 ssize_t rev = process_vm_writev(getpid(), &local, PARAM_1, &remote, PARAM_1, PARAM_0);
205 if (rev == sizeof(src)) {
206 ret = RETURN_0;
207 } else {
208 ret = FAIL;
209 }
210 napi_value result = nullptr;
211 napi_create_int32(env, ret, &result);
212 return result;
213 }
214 EXTERN_C_START
Init(napi_env env,napi_value exports)215 static napi_value Init(napi_env env, napi_value exports)
216 {
217 napi_property_descriptor desc[] = {
218 {"readv", nullptr, Readv, nullptr, nullptr, nullptr, napi_default, nullptr},
219 {"writev", nullptr, Writev, nullptr, nullptr, nullptr, napi_default, nullptr},
220 {"processVmReadV", nullptr, ProcessVmReadV, nullptr, nullptr, nullptr, napi_default, nullptr},
221 {"processVmWriteV", nullptr, ProcessVmWriteV, nullptr, nullptr, nullptr, napi_default, nullptr},
222 {"pWriteV", nullptr, PWriteV, nullptr, nullptr, nullptr, napi_default, nullptr},
223 {"pWriteV64", nullptr, PWriteV64, nullptr, nullptr, nullptr, napi_default, nullptr},
224
225 };
226 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
227 return exports;
228 }
229 EXTERN_C_END
230
231 static napi_module demoModule = {
232 .nm_version = 1,
233 .nm_flags = 0,
234 .nm_filename = nullptr,
235 .nm_register_func = Init,
236 .nm_modname = "uio",
237 .nm_priv = ((void *)0),
238 .reserved = {0},
239 };
240
RegisterModule(void)241 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
242