1 /*
2 * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
45 import java.io.IOException;
46 import java.nio.ByteBuffer;
47 import java.nio.ByteOrder;
48 import java.nio.channels.FileChannel;
49 import java.nio.file.Files;
50 import java.nio.file.Path;
51 import java.nio.file.Paths;
52 import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
53 import java.nio.file.StandardOpenOption;
54 import static java.nio.file.StandardOpenOption.READ;
55 import static java.nio.file.StandardOpenOption.WRITE;
56 import java.util.ArrayList;
57 import java.util.HashSet;
58 import java.util.List;
59 import java.util.Random;
60 import sun.hotspot.WhiteBox;
61
62 public class SharedArchiveConsistency {
63 public static WhiteBox wb;
64 public static int offset_magic; // FileMapHeader::_magic
65 public static int sp_offset_crc; // FileMapHeader::space_info::_crc
66 public static int file_header_size = -1;// total size of header, variant, need calculation
67 public static int space_info_size; // size of space_info
68 public static int sp_offset; // offset of FileMapHeader::space_info
69 public static int sp_used_offset; // offset of space_info::_used
70 public static int size_t_size; // size of size_t
71
72 public static File jsa; // will be updated during test
73 public static File orgJsaFile; // kept the original file not touched.
74 public static String[] shared_region_name = {"MiscCode", "ReadWrite", "ReadOnly", "MiscData"};
75 public static int num_regions = shared_region_name.length;
76 public static String[] matchMessages = {
77 "Unable to use shared archive",
78 "An error has occurred while processing the shared archive file.",
79 "Checksum verification failed.",
80 "The shared archive file has been truncated."
81 };
82
83 public static void getFileOffsetInfo() throws Exception {
84 wb = WhiteBox.getWhiteBox();
85 offset_magic = wb.getOffsetForName("FileMapHeader::_magic");
86 sp_offset_crc = wb.getOffsetForName("space_info::_crc");
87 try {
88 int nonExistOffset = wb.getOffsetForName("FileMapHeader::_non_exist_offset");
89 System.exit(-1); // should fail
90 } catch (Exception e) {
91 // success
92 }
93
94 sp_offset = wb.getOffsetForName("FileMapHeader::_space[0]") - offset_magic;
95 sp_used_offset = wb.getOffsetForName("space_info::_used") - sp_offset_crc;
96 size_t_size = wb.getOffsetForName("size_t_size");
97 space_info_size = wb.getOffsetForName("space_info_size");
98 }
99
100 public static int getFileHeaderSize(FileChannel fc) throws Exception {
101 if (file_header_size != -1) {
102 return file_header_size;
103 }
104 // this is not real header size, it is struct size
105 file_header_size = wb.getOffsetForName("file_header_size");
106 int offset_path_misc_info = wb.getOffsetForName("FileMapHeader::_paths_misc_info_size") -
107 offset_magic;
108 int path_misc_info_size = (int)readInt(fc, offset_path_misc_info, size_t_size);
109 file_header_size += path_misc_info_size; //readInt(fc, offset_path_misc_info, size_t_size);
110 System.out.println("offset_path_misc_info = " + offset_path_misc_info);
111 System.out.println("path_misc_info_size = " + path_misc_info_size);
112 System.out.println("file_header_size = " + file_header_size);
113 file_header_size = (int)align_up_page(file_header_size);
114 System.out.println("file_header_size (aligned to page) = " + file_header_size);
115 return file_header_size;
116 }
117
149 }
150
151 public static FileChannel getFileChannel() throws Exception {
152 List<StandardOpenOption> arry = new ArrayList<StandardOpenOption>();
153 arry.add(READ);
154 arry.add(WRITE);
155 return FileChannel.open(jsa.toPath(), new HashSet<StandardOpenOption>(arry));
156 }
157
158 public static void modifyJsaContentRandomly() throws Exception {
159 FileChannel fc = getFileChannel();
160 // corrupt random area in the data areas (MiscCode, ReadWrite, ReadOnly, MiscData)
161 long[] used = new long[num_regions]; // record used bytes
162 long start0, start, end, off;
163 int used_offset, path_info_size;
164
165 int bufSize;
166 System.out.printf("%-12s%-12s%-12s%-12s%-12s\n", "Space Name", "Offset", "Used bytes", "Reg Start", "Random Offset");
167 start0 = getFileHeaderSize(fc);
168 for (int i = 0; i < num_regions; i++) {
169 used_offset = sp_offset + space_info_size * i + sp_used_offset;
170 // read 'used'
171 used[i] = readInt(fc, used_offset, size_t_size);
172 start = start0;
173 for (int j = 0; j < i; j++) {
174 start += align_up_page(used[j]);
175 }
176 end = start + used[i];
177 off = getRandomBetween(start, end);
178 System.out.printf("%-12s%-12d%-12d%-12d%-12d\n", shared_region_name[i], used_offset, used[i], start, off);
179 if (end - off < 1024) {
180 bufSize = (int)(end - off + 1);
181 } else {
182 bufSize = 1024;
183 }
184 ByteBuffer bbuf = ByteBuffer.wrap(new byte[bufSize]);
185 writeData(fc, off, bbuf);
186 }
187 if (fc.isOpen()) {
188 fc.close();
189 }
190 }
191
192 public static void modifyJsaContent() throws Exception {
193 FileChannel fc = getFileChannel();
194 byte[] buf = new byte[4096];
195 ByteBuffer bbuf = ByteBuffer.wrap(buf);
196
197 long total = 0L;
198 long used_offset = 0L;
199 long[] used = new long[num_regions];
200 System.out.printf("%-12s%-12s\n", "Space name", "Used bytes");
201 for (int i = 0; i < num_regions; i++) {
202 used_offset = sp_offset + space_info_size* i + sp_used_offset;
203 // read 'used'
204 used[i] = readInt(fc, used_offset, size_t_size);
205 System.out.printf("%-12s%-12d\n", shared_region_name[i], used[i]);
206 total += used[i];
207 }
208 System.out.printf("%-12s%-12d\n", "Total: ", total);
209 long corrupt_used_offset = getFileHeaderSize(fc);
210 System.out.println("Corrupt RO section, offset = " + corrupt_used_offset);
211 while (used_offset < used[0]) {
212 writeData(fc, corrupt_used_offset, bbuf);
213 bbuf.clear();
214 used_offset += 4096;
215 }
216 fc.force(true);
217 if (fc.isOpen()) {
218 fc.close();
219 }
220 }
221
222 public static void modifyJsaHeader() throws Exception {
|
1 /*
2 * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
45 import java.io.IOException;
46 import java.nio.ByteBuffer;
47 import java.nio.ByteOrder;
48 import java.nio.channels.FileChannel;
49 import java.nio.file.Files;
50 import java.nio.file.Path;
51 import java.nio.file.Paths;
52 import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
53 import java.nio.file.StandardOpenOption;
54 import static java.nio.file.StandardOpenOption.READ;
55 import static java.nio.file.StandardOpenOption.WRITE;
56 import java.util.ArrayList;
57 import java.util.HashSet;
58 import java.util.List;
59 import java.util.Random;
60 import sun.hotspot.WhiteBox;
61
62 public class SharedArchiveConsistency {
63 public static WhiteBox wb;
64 public static int offset_magic; // FileMapHeader::_magic
65 public static int sp_offset_crc; // CDSFileMapRegion::_crc
66 public static int file_header_size = -1;// total size of header, variant, need calculation
67 public static int CDSFileMapRegion_size; // size of CDSFileMapRegion
68 public static int sp_offset; // offset of CDSFileMapRegion
69 public static int sp_used_offset; // offset of CDSFileMapRegion::_used
70 public static int size_t_size; // size of size_t
71
72 public static File jsa; // will be updated during test
73 public static File orgJsaFile; // kept the original file not touched.
74 public static String[] shared_region_name = {"MiscCode", "ReadWrite", "ReadOnly", "MiscData"};
75 public static int num_regions = shared_region_name.length;
76 public static String[] matchMessages = {
77 "Unable to use shared archive",
78 "An error has occurred while processing the shared archive file.",
79 "Checksum verification failed.",
80 "The shared archive file has been truncated."
81 };
82
83 public static void getFileOffsetInfo() throws Exception {
84 wb = WhiteBox.getWhiteBox();
85 offset_magic = wb.getOffsetForName("FileMapHeader::_magic");
86 sp_offset_crc = wb.getOffsetForName("CDSFileMapRegion::_crc");
87 try {
88 int nonExistOffset = wb.getOffsetForName("FileMapHeader::_non_exist_offset");
89 System.exit(-1); // should fail
90 } catch (Exception e) {
91 // success
92 }
93
94 sp_offset = wb.getOffsetForName("FileMapHeader::_space[0]") - offset_magic;
95 sp_used_offset = wb.getOffsetForName("CDSFileMapRegion::_used") - sp_offset_crc;
96 size_t_size = wb.getOffsetForName("size_t_size");
97 CDSFileMapRegion_size = wb.getOffsetForName("CDSFileMapRegion_size");
98 }
99
100 public static int getFileHeaderSize(FileChannel fc) throws Exception {
101 if (file_header_size != -1) {
102 return file_header_size;
103 }
104 // this is not real header size, it is struct size
105 file_header_size = wb.getOffsetForName("file_header_size");
106 int offset_path_misc_info = wb.getOffsetForName("FileMapHeader::_paths_misc_info_size") -
107 offset_magic;
108 int path_misc_info_size = (int)readInt(fc, offset_path_misc_info, size_t_size);
109 file_header_size += path_misc_info_size; //readInt(fc, offset_path_misc_info, size_t_size);
110 System.out.println("offset_path_misc_info = " + offset_path_misc_info);
111 System.out.println("path_misc_info_size = " + path_misc_info_size);
112 System.out.println("file_header_size = " + file_header_size);
113 file_header_size = (int)align_up_page(file_header_size);
114 System.out.println("file_header_size (aligned to page) = " + file_header_size);
115 return file_header_size;
116 }
117
149 }
150
151 public static FileChannel getFileChannel() throws Exception {
152 List<StandardOpenOption> arry = new ArrayList<StandardOpenOption>();
153 arry.add(READ);
154 arry.add(WRITE);
155 return FileChannel.open(jsa.toPath(), new HashSet<StandardOpenOption>(arry));
156 }
157
158 public static void modifyJsaContentRandomly() throws Exception {
159 FileChannel fc = getFileChannel();
160 // corrupt random area in the data areas (MiscCode, ReadWrite, ReadOnly, MiscData)
161 long[] used = new long[num_regions]; // record used bytes
162 long start0, start, end, off;
163 int used_offset, path_info_size;
164
165 int bufSize;
166 System.out.printf("%-12s%-12s%-12s%-12s%-12s\n", "Space Name", "Offset", "Used bytes", "Reg Start", "Random Offset");
167 start0 = getFileHeaderSize(fc);
168 for (int i = 0; i < num_regions; i++) {
169 used_offset = sp_offset + CDSFileMapRegion_size * i + sp_used_offset;
170 // read 'used'
171 used[i] = readInt(fc, used_offset, size_t_size);
172 start = start0;
173 for (int j = 0; j < i; j++) {
174 start += align_up_page(used[j]);
175 }
176 end = start + used[i];
177 off = getRandomBetween(start, end);
178 System.out.printf("%-12s%-12d%-12d%-12d%-12d\n", shared_region_name[i], used_offset, used[i], start, off);
179 if (end - off < 1024) {
180 bufSize = (int)(end - off + 1);
181 } else {
182 bufSize = 1024;
183 }
184 ByteBuffer bbuf = ByteBuffer.wrap(new byte[bufSize]);
185 writeData(fc, off, bbuf);
186 }
187 if (fc.isOpen()) {
188 fc.close();
189 }
190 }
191
192 public static void modifyJsaContent() throws Exception {
193 FileChannel fc = getFileChannel();
194 byte[] buf = new byte[4096];
195 ByteBuffer bbuf = ByteBuffer.wrap(buf);
196
197 long total = 0L;
198 long used_offset = 0L;
199 long[] used = new long[num_regions];
200 System.out.printf("%-12s%-12s\n", "Space name", "Used bytes");
201 for (int i = 0; i < num_regions; i++) {
202 used_offset = sp_offset + CDSFileMapRegion_size* i + sp_used_offset;
203 // read 'used'
204 used[i] = readInt(fc, used_offset, size_t_size);
205 System.out.printf("%-12s%-12d\n", shared_region_name[i], used[i]);
206 total += used[i];
207 }
208 System.out.printf("%-12s%-12d\n", "Total: ", total);
209 long corrupt_used_offset = getFileHeaderSize(fc);
210 System.out.println("Corrupt RO section, offset = " + corrupt_used_offset);
211 while (used_offset < used[0]) {
212 writeData(fc, corrupt_used_offset, bbuf);
213 bbuf.clear();
214 used_offset += 4096;
215 }
216 fc.force(true);
217 if (fc.isOpen()) {
218 fc.close();
219 }
220 }
221
222 public static void modifyJsaHeader() throws Exception {
|