author | rriggs |
Fri, 09 Nov 2018 13:28:16 -0500 | |
changeset 52478 | b915bd68d907 |
parent 52095 | c459186b9584 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
49440 | 2 |
* Copyright (c) 1994, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
#include <stdlib.h> |
|
27 |
#include <string.h> |
|
1773
1be34caf9662
6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents:
1769
diff
changeset
|
28 |
#include <stddef.h> |
2 | 29 |
|
30 |
#include "jni.h" |
|
31 |
#include "jni_util.h" |
|
32 |
#include "jvm.h" |
|
33 |
#include "io_util.h" |
|
34 |
#include "io_util_md.h" |
|
35 |
||
36 |
/* IO helper functions */ |
|
37 |
||
1773
1be34caf9662
6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents:
1769
diff
changeset
|
38 |
jint |
2 | 39 |
readSingle(JNIEnv *env, jobject this, jfieldID fid) { |
1773
1be34caf9662
6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents:
1769
diff
changeset
|
40 |
jint nread; |
2 | 41 |
char ret; |
42 |
FD fd = GET_FD(this, fid); |
|
43 |
if (fd == -1) { |
|
44
9291315d799d
6632696: Writing to closed output files (writeBytes) leaks native memory (unix)
martin
parents:
2
diff
changeset
|
44 |
JNU_ThrowIOException(env, "Stream Closed"); |
2 | 45 |
return -1; |
46 |
} |
|
11116
3e486ce0019d
7030624: size_t usages in src/windows/native/java/io/io_util_md.c need to be re-visited
alanb
parents:
9035
diff
changeset
|
47 |
nread = IO_Read(fd, &ret, 1); |
2 | 48 |
if (nread == 0) { /* EOF */ |
49 |
return -1; |
|
16476
e269be167fae
8001334: Remove use of JVM_* functions from java.io code
dxu
parents:
11116
diff
changeset
|
50 |
} else if (nread == -1) { /* error */ |
2 | 51 |
JNU_ThrowIOExceptionWithLastError(env, "Read error"); |
52 |
} |
|
53 |
return ret & 0xFF; |
|
54 |
} |
|
55 |
||
56 |
/* The maximum size of a stack-allocated buffer. |
|
57 |
*/ |
|
58 |
#define BUF_SIZE 8192 |
|
59 |
||
1768
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
715
diff
changeset
|
60 |
/* |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
715
diff
changeset
|
61 |
* Returns true if the array slice defined by the given offset and length |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
715
diff
changeset
|
62 |
* is out of bounds. |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
715
diff
changeset
|
63 |
*/ |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
715
diff
changeset
|
64 |
static int |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
715
diff
changeset
|
65 |
outOfBounds(JNIEnv *env, jint off, jint len, jbyteArray array) { |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
715
diff
changeset
|
66 |
return ((off < 0) || |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
715
diff
changeset
|
67 |
(len < 0) || |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
715
diff
changeset
|
68 |
// We are very careful to avoid signed integer overflow, |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
715
diff
changeset
|
69 |
// the result of which is undefined in C. |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
715
diff
changeset
|
70 |
((*env)->GetArrayLength(env, array) - off < len)); |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
715
diff
changeset
|
71 |
} |
2 | 72 |
|
1773
1be34caf9662
6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents:
1769
diff
changeset
|
73 |
jint |
2 | 74 |
readBytes(JNIEnv *env, jobject this, jbyteArray bytes, |
75 |
jint off, jint len, jfieldID fid) |
|
76 |
{ |
|
1773
1be34caf9662
6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents:
1769
diff
changeset
|
77 |
jint nread; |
2 | 78 |
char stackBuf[BUF_SIZE]; |
1773
1be34caf9662
6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents:
1769
diff
changeset
|
79 |
char *buf = NULL; |
2 | 80 |
FD fd; |
81 |
||
82 |
if (IS_NULL(bytes)) { |
|
1773
1be34caf9662
6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents:
1769
diff
changeset
|
83 |
JNU_ThrowNullPointerException(env, NULL); |
2 | 84 |
return -1; |
85 |
} |
|
86 |
||
1768
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
715
diff
changeset
|
87 |
if (outOfBounds(env, off, len, bytes)) { |
1773
1be34caf9662
6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents:
1769
diff
changeset
|
88 |
JNU_ThrowByName(env, "java/lang/IndexOutOfBoundsException", NULL); |
2 | 89 |
return -1; |
90 |
} |
|
91 |
||
92 |
if (len == 0) { |
|
93 |
return 0; |
|
94 |
} else if (len > BUF_SIZE) { |
|
95 |
buf = malloc(len); |
|
1773
1be34caf9662
6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents:
1769
diff
changeset
|
96 |
if (buf == NULL) { |
1be34caf9662
6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents:
1769
diff
changeset
|
97 |
JNU_ThrowOutOfMemoryError(env, NULL); |
2 | 98 |
return 0; |
99 |
} |
|
100 |
} else { |
|
101 |
buf = stackBuf; |
|
102 |
} |
|
103 |
||
104 |
fd = GET_FD(this, fid); |
|
105 |
if (fd == -1) { |
|
44
9291315d799d
6632696: Writing to closed output files (writeBytes) leaks native memory (unix)
martin
parents:
2
diff
changeset
|
106 |
JNU_ThrowIOException(env, "Stream Closed"); |
2 | 107 |
nread = -1; |
1769
27a33d1b16ec
6791458: FileInputStream/RandomAccessFile.read leaks memory if invoked on closed stream with len > 8k
martin
parents:
1768
diff
changeset
|
108 |
} else { |
11116
3e486ce0019d
7030624: size_t usages in src/windows/native/java/io/io_util_md.c need to be re-visited
alanb
parents:
9035
diff
changeset
|
109 |
nread = IO_Read(fd, buf, len); |
1769
27a33d1b16ec
6791458: FileInputStream/RandomAccessFile.read leaks memory if invoked on closed stream with len > 8k
martin
parents:
1768
diff
changeset
|
110 |
if (nread > 0) { |
27a33d1b16ec
6791458: FileInputStream/RandomAccessFile.read leaks memory if invoked on closed stream with len > 8k
martin
parents:
1768
diff
changeset
|
111 |
(*env)->SetByteArrayRegion(env, bytes, off, nread, (jbyte *)buf); |
16476
e269be167fae
8001334: Remove use of JVM_* functions from java.io code
dxu
parents:
11116
diff
changeset
|
112 |
} else if (nread == -1) { |
1769
27a33d1b16ec
6791458: FileInputStream/RandomAccessFile.read leaks memory if invoked on closed stream with len > 8k
martin
parents:
1768
diff
changeset
|
113 |
JNU_ThrowIOExceptionWithLastError(env, "Read error"); |
27a33d1b16ec
6791458: FileInputStream/RandomAccessFile.read leaks memory if invoked on closed stream with len > 8k
martin
parents:
1768
diff
changeset
|
114 |
} else { /* EOF */ |
27a33d1b16ec
6791458: FileInputStream/RandomAccessFile.read leaks memory if invoked on closed stream with len > 8k
martin
parents:
1768
diff
changeset
|
115 |
nread = -1; |
27a33d1b16ec
6791458: FileInputStream/RandomAccessFile.read leaks memory if invoked on closed stream with len > 8k
martin
parents:
1768
diff
changeset
|
116 |
} |
2 | 117 |
} |
118 |
||
119 |
if (buf != stackBuf) { |
|
120 |
free(buf); |
|
121 |
} |
|
122 |
return nread; |
|
123 |
} |
|
124 |
||
125 |
void |
|
7515
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
126 |
writeSingle(JNIEnv *env, jobject this, jint byte, jboolean append, jfieldID fid) { |
1773
1be34caf9662
6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents:
1769
diff
changeset
|
127 |
// Discard the 24 high-order bits of byte. See OutputStream#write(int) |
1be34caf9662
6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents:
1769
diff
changeset
|
128 |
char c = (char) byte; |
1be34caf9662
6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents:
1769
diff
changeset
|
129 |
jint n; |
2 | 130 |
FD fd = GET_FD(this, fid); |
131 |
if (fd == -1) { |
|
44
9291315d799d
6632696: Writing to closed output files (writeBytes) leaks native memory (unix)
martin
parents:
2
diff
changeset
|
132 |
JNU_ThrowIOException(env, "Stream Closed"); |
2 | 133 |
return; |
134 |
} |
|
7515
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
135 |
if (append == JNI_TRUE) { |
11116
3e486ce0019d
7030624: size_t usages in src/windows/native/java/io/io_util_md.c need to be re-visited
alanb
parents:
9035
diff
changeset
|
136 |
n = IO_Append(fd, &c, 1); |
7515
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
137 |
} else { |
11116
3e486ce0019d
7030624: size_t usages in src/windows/native/java/io/io_util_md.c need to be re-visited
alanb
parents:
9035
diff
changeset
|
138 |
n = IO_Write(fd, &c, 1); |
7515
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
139 |
} |
16476
e269be167fae
8001334: Remove use of JVM_* functions from java.io code
dxu
parents:
11116
diff
changeset
|
140 |
if (n == -1) { |
2 | 141 |
JNU_ThrowIOExceptionWithLastError(env, "Write error"); |
142 |
} |
|
143 |
} |
|
144 |
||
145 |
void |
|
146 |
writeBytes(JNIEnv *env, jobject this, jbyteArray bytes, |
|
7515
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
147 |
jint off, jint len, jboolean append, jfieldID fid) |
2 | 148 |
{ |
1773
1be34caf9662
6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents:
1769
diff
changeset
|
149 |
jint n; |
2 | 150 |
char stackBuf[BUF_SIZE]; |
1773
1be34caf9662
6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents:
1769
diff
changeset
|
151 |
char *buf = NULL; |
2 | 152 |
FD fd; |
153 |
||
154 |
if (IS_NULL(bytes)) { |
|
1773
1be34caf9662
6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents:
1769
diff
changeset
|
155 |
JNU_ThrowNullPointerException(env, NULL); |
2 | 156 |
return; |
157 |
} |
|
158 |
||
1768
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
715
diff
changeset
|
159 |
if (outOfBounds(env, off, len, bytes)) { |
1773
1be34caf9662
6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents:
1769
diff
changeset
|
160 |
JNU_ThrowByName(env, "java/lang/IndexOutOfBoundsException", NULL); |
2 | 161 |
return; |
162 |
} |
|
163 |
||
164 |
if (len == 0) { |
|
165 |
return; |
|
166 |
} else if (len > BUF_SIZE) { |
|
167 |
buf = malloc(len); |
|
1773
1be34caf9662
6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents:
1769
diff
changeset
|
168 |
if (buf == NULL) { |
1be34caf9662
6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents:
1769
diff
changeset
|
169 |
JNU_ThrowOutOfMemoryError(env, NULL); |
2 | 170 |
return; |
171 |
} |
|
172 |
} else { |
|
173 |
buf = stackBuf; |
|
174 |
} |
|
175 |
||
176 |
(*env)->GetByteArrayRegion(env, bytes, off, len, (jbyte *)buf); |
|
177 |
||
178 |
if (!(*env)->ExceptionOccurred(env)) { |
|
179 |
off = 0; |
|
180 |
while (len > 0) { |
|
181 |
fd = GET_FD(this, fid); |
|
182 |
if (fd == -1) { |
|
44
9291315d799d
6632696: Writing to closed output files (writeBytes) leaks native memory (unix)
martin
parents:
2
diff
changeset
|
183 |
JNU_ThrowIOException(env, "Stream Closed"); |
9291315d799d
6632696: Writing to closed output files (writeBytes) leaks native memory (unix)
martin
parents:
2
diff
changeset
|
184 |
break; |
2 | 185 |
} |
7515
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
186 |
if (append == JNI_TRUE) { |
11116
3e486ce0019d
7030624: size_t usages in src/windows/native/java/io/io_util_md.c need to be re-visited
alanb
parents:
9035
diff
changeset
|
187 |
n = IO_Append(fd, buf+off, len); |
7515
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
188 |
} else { |
11116
3e486ce0019d
7030624: size_t usages in src/windows/native/java/io/io_util_md.c need to be re-visited
alanb
parents:
9035
diff
changeset
|
189 |
n = IO_Write(fd, buf+off, len); |
7515
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5506
diff
changeset
|
190 |
} |
16476
e269be167fae
8001334: Remove use of JVM_* functions from java.io code
dxu
parents:
11116
diff
changeset
|
191 |
if (n == -1) { |
2 | 192 |
JNU_ThrowIOExceptionWithLastError(env, "Write error"); |
193 |
break; |
|
194 |
} |
|
195 |
off += n; |
|
196 |
len -= n; |
|
197 |
} |
|
198 |
} |
|
199 |
if (buf != stackBuf) { |
|
200 |
free(buf); |
|
201 |
} |
|
202 |
} |
|
203 |
||
52095
c459186b9584
8212001: Verify exported symbols in java.base (libjava)
prappo
parents:
51671
diff
changeset
|
204 |
void |
2 | 205 |
throwFileNotFoundException(JNIEnv *env, jstring path) |
206 |
{ |
|
207 |
char buf[256]; |
|
16476
e269be167fae
8001334: Remove use of JVM_* functions from java.io code
dxu
parents:
11116
diff
changeset
|
208 |
size_t n; |
2 | 209 |
jobject x; |
210 |
jstring why = NULL; |
|
211 |
||
16476
e269be167fae
8001334: Remove use of JVM_* functions from java.io code
dxu
parents:
11116
diff
changeset
|
212 |
n = getLastErrorString(buf, sizeof(buf)); |
2 | 213 |
if (n > 0) { |
1773
1be34caf9662
6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents:
1769
diff
changeset
|
214 |
why = JNU_NewStringPlatform(env, buf); |
22641
e47f8892133e
8033372: Check jdk/src/share/native/java/lang for JNI pending exception issues
alanb
parents:
19032
diff
changeset
|
215 |
CHECK_NULL(why); |
2 | 216 |
} |
217 |
x = JNU_NewObjectByName(env, |
|
1773
1be34caf9662
6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents:
1769
diff
changeset
|
218 |
"java/io/FileNotFoundException", |
1be34caf9662
6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents:
1769
diff
changeset
|
219 |
"(Ljava/lang/String;Ljava/lang/String;)V", |
1be34caf9662
6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents:
1769
diff
changeset
|
220 |
path, why); |
2 | 221 |
if (x != NULL) { |
1773
1be34caf9662
6792066: src/share/native/java/io/io_util.c clean-ups
martin
parents:
1769
diff
changeset
|
222 |
(*env)->Throw(env, x); |
2 | 223 |
} |
224 |
} |