6
6
import dev .xdark .ssvm .mirror .type .InstanceClass ;
7
7
import dev .xdark .ssvm .operation .VMOperations ;
8
8
import dev .xdark .ssvm .value .ArrayValue ;
9
+ import dev .xdark .ssvm .value .ObjectValue ;
9
10
10
11
import java .util .Map ;
11
12
12
13
/**
13
14
* Sets up native bindings for jdk/internal/util/SystemProps$Raw,
14
15
* integrating both VM and platform properties, including support
15
- * for propDefault() retrieval. This ensures that when SystemProps
16
- * requests default platform properties, a pre-populated array is
17
- * returned to simulate the native environment.
16
+ * for propDefault() retrieval. If the index is out of range, we
17
+ * return an empty string.
18
18
*/
19
19
public final class SystemProps$RawNatives {
20
20
// Index constants as defined in the provided code
@@ -59,6 +59,9 @@ public final class SystemProps$RawNatives {
59
59
private static final int _user_name_NDX = 38 ;
60
60
private static final int FIXED_LENGTH = 39 ;
61
61
62
+ // Cache the platform properties so we can access them in propDefault
63
+ private static ArrayValue platformArrayCache ;
64
+
62
65
public static void init (VirtualMachine vm ) {
63
66
InstanceClass jc = (InstanceClass ) vm .findBootstrapClass ("jdk/internal/util/SystemProps$Raw" );
64
67
if (jc != null ) {
@@ -67,50 +70,52 @@ public static void init(VirtualMachine vm) {
67
70
68
71
// Provide platform properties for propDefault
69
72
vmi .setInvoker (jc , "platformProperties" , "()[Ljava/lang/String;" , ctx -> {
70
- ArrayValue platformArray = ops .allocateArray (vm .getSymbols ().java_lang_String (), FIXED_LENGTH );
73
+ if (platformArrayCache == null ) {
74
+ platformArrayCache = ops .allocateArray (vm .getSymbols ().java_lang_String (), FIXED_LENGTH );
71
75
72
- // Provide meaningful defaults - adjust to suit your environment
73
- platformArray .setReference (_display_country_NDX , ops .newUtf8 (vm .getProperties ().getOrDefault ("user.country" , "US" )));
74
- platformArray .setReference (_display_language_NDX , ops .newUtf8 (vm .getProperties ().getOrDefault ("user.language" , "en" )));
75
- platformArray .setReference (_display_script_NDX , ops .newUtf8 (vm .getProperties ().getOrDefault ("user.script" , "" )));
76
- platformArray .setReference (_display_variant_NDX , ops .newUtf8 (vm .getProperties ().getOrDefault ("user.variant" , "" )));
77
- platformArray .setReference (_file_encoding_NDX , ops .newUtf8 ("UTF-8" ));
78
- platformArray .setReference (_file_separator_NDX , ops .newUtf8 (vm .getProperties ().getOrDefault ("file.separator" , "/" )));
79
- platformArray .setReference (_format_country_NDX , ops .newUtf8 ("US" ));
80
- platformArray .setReference (_format_language_NDX , ops .newUtf8 ("en" ));
81
- platformArray .setReference (_format_script_NDX , ops .newUtf8 ("" ));
82
- platformArray .setReference (_format_variant_NDX , ops .newUtf8 ("" ));
83
- platformArray .setReference (_ftp_nonProxyHosts_NDX , ops .newUtf8 ("" ));
84
- platformArray .setReference (_ftp_proxyHost_NDX , ops .newUtf8 ("" ));
85
- platformArray .setReference (_ftp_proxyPort_NDX , ops .newUtf8 ("" ));
86
- platformArray .setReference (_http_nonProxyHosts_NDX , ops .newUtf8 ("" ));
87
- platformArray .setReference (_http_proxyHost_NDX , ops .newUtf8 ("" ));
88
- platformArray .setReference (_http_proxyPort_NDX , ops .newUtf8 ("" ));
89
- platformArray .setReference (_https_proxyHost_NDX , ops .newUtf8 ("" ));
90
- platformArray .setReference (_https_proxyPort_NDX , ops .newUtf8 ("" ));
91
- platformArray .setReference (_java_io_tmpdir_NDX , ops .newUtf8 (vm .getProperties ().getOrDefault ("java.io.tmpdir" , "/tmp" )));
92
- platformArray .setReference (_line_separator_NDX , ops .newUtf8 (System .lineSeparator ()));
93
- platformArray .setReference (_os_arch_NDX , ops .newUtf8 (vm .getProperties ().getOrDefault ("os.arch" , "amd64" )));
94
- platformArray .setReference (_os_name_NDX , ops .newUtf8 (vm .getProperties ().getOrDefault ("os.name" , "Linux" )));
95
- platformArray .setReference (_os_version_NDX , ops .newUtf8 (vm .getProperties ().getOrDefault ("os.version" , "5.11.0-0" )));
96
- platformArray .setReference (_path_separator_NDX , ops .newUtf8 (vm .getProperties ().getOrDefault ("path.separator" , ":" )));
97
- platformArray .setReference (_socksNonProxyHosts_NDX , ops .newUtf8 ("" ));
98
- platformArray .setReference (_socksProxyHost_NDX , ops .newUtf8 ("" ));
99
- platformArray .setReference (_socksProxyPort_NDX , ops .newUtf8 ("" ));
100
- platformArray .setReference (_sun_arch_abi_NDX , ops .newUtf8 ("" ));
101
- platformArray .setReference (_sun_arch_data_model_NDX , ops .newUtf8 ("64" ));
102
- platformArray .setReference (_sun_cpu_endian_NDX , ops .newUtf8 ("little" ));
103
- platformArray .setReference (_sun_cpu_isalist_NDX , ops .newUtf8 ("" ));
104
- platformArray .setReference (_sun_io_unicode_encoding_NDX , ops .newUtf8 ("" ));
105
- platformArray .setReference (_sun_jnu_encoding_NDX , ops .newUtf8 ("UTF-8" ));
106
- platformArray .setReference (_sun_os_patch_level_NDX , ops .newUtf8 ("" ));
107
- platformArray .setReference (_sun_stderr_encoding_NDX , ops .newUtf8 ("UTF-8" ));
108
- platformArray .setReference (_sun_stdout_encoding_NDX , ops .newUtf8 ("UTF-8" ));
109
- platformArray .setReference (_user_dir_NDX , ops .newUtf8 (vm .getProperties ().getOrDefault ("user.dir" , "/home/user" )));
110
- platformArray .setReference (_user_home_NDX , ops .newUtf8 (vm .getProperties ().getOrDefault ("user.home" , "/home/user" )));
111
- platformArray .setReference (_user_name_NDX , ops .newUtf8 (vm .getProperties ().getOrDefault ("user.name" , "user" )));
76
+ // Provide meaningful defaults - adjust to suit your environment
77
+ platformArrayCache .setReference (_display_country_NDX , ops .newUtf8 (vm .getProperties ().getOrDefault ("user.country" , "US" )));
78
+ platformArrayCache .setReference (_display_language_NDX , ops .newUtf8 (vm .getProperties ().getOrDefault ("user.language" , "en" )));
79
+ platformArrayCache .setReference (_display_script_NDX , ops .newUtf8 (vm .getProperties ().getOrDefault ("user.script" , "" )));
80
+ platformArrayCache .setReference (_display_variant_NDX , ops .newUtf8 (vm .getProperties ().getOrDefault ("user.variant" , "" )));
81
+ platformArrayCache .setReference (_file_encoding_NDX , ops .newUtf8 ("UTF-8" ));
82
+ platformArrayCache .setReference (_file_separator_NDX , ops .newUtf8 (vm .getProperties ().getOrDefault ("file.separator" , "/" )));
83
+ platformArrayCache .setReference (_format_country_NDX , ops .newUtf8 ("US" ));
84
+ platformArrayCache .setReference (_format_language_NDX , ops .newUtf8 ("en" ));
85
+ platformArrayCache .setReference (_format_script_NDX , ops .newUtf8 ("" ));
86
+ platformArrayCache .setReference (_format_variant_NDX , ops .newUtf8 ("" ));
87
+ platformArrayCache .setReference (_ftp_nonProxyHosts_NDX , ops .newUtf8 ("" ));
88
+ platformArrayCache .setReference (_ftp_proxyHost_NDX , ops .newUtf8 ("" ));
89
+ platformArrayCache .setReference (_ftp_proxyPort_NDX , ops .newUtf8 ("" ));
90
+ platformArrayCache .setReference (_http_nonProxyHosts_NDX , ops .newUtf8 ("" ));
91
+ platformArrayCache .setReference (_http_proxyHost_NDX , ops .newUtf8 ("" ));
92
+ platformArrayCache .setReference (_http_proxyPort_NDX , ops .newUtf8 ("" ));
93
+ platformArrayCache .setReference (_https_proxyHost_NDX , ops .newUtf8 ("" ));
94
+ platformArrayCache .setReference (_https_proxyPort_NDX , ops .newUtf8 ("" ));
95
+ platformArrayCache .setReference (_java_io_tmpdir_NDX , ops .newUtf8 (vm .getProperties ().getOrDefault ("java.io.tmpdir" , "/tmp" )));
96
+ platformArrayCache .setReference (_line_separator_NDX , ops .newUtf8 (System .lineSeparator ()));
97
+ platformArrayCache .setReference (_os_arch_NDX , ops .newUtf8 (vm .getProperties ().getOrDefault ("os.arch" , "amd64" )));
98
+ platformArrayCache .setReference (_os_name_NDX , ops .newUtf8 (vm .getProperties ().getOrDefault ("os.name" , "Linux" )));
99
+ platformArrayCache .setReference (_os_version_NDX , ops .newUtf8 (vm .getProperties ().getOrDefault ("os.version" , "5.11.0-0" )));
100
+ platformArrayCache .setReference (_path_separator_NDX , ops .newUtf8 (vm .getProperties ().getOrDefault ("path.separator" , ":" )));
101
+ platformArrayCache .setReference (_socksNonProxyHosts_NDX , ops .newUtf8 ("" ));
102
+ platformArrayCache .setReference (_socksProxyHost_NDX , ops .newUtf8 ("" ));
103
+ platformArrayCache .setReference (_socksProxyPort_NDX , ops .newUtf8 ("" ));
104
+ platformArrayCache .setReference (_sun_arch_abi_NDX , ops .newUtf8 ("" ));
105
+ platformArrayCache .setReference (_sun_arch_data_model_NDX , ops .newUtf8 ("64" ));
106
+ platformArrayCache .setReference (_sun_cpu_endian_NDX , ops .newUtf8 ("little" ));
107
+ platformArrayCache .setReference (_sun_cpu_isalist_NDX , ops .newUtf8 ("" ));
108
+ platformArrayCache .setReference (_sun_io_unicode_encoding_NDX , ops .newUtf8 ("" ));
109
+ platformArrayCache .setReference (_sun_jnu_encoding_NDX , ops .newUtf8 ("UTF-8" ));
110
+ platformArrayCache .setReference (_sun_os_patch_level_NDX , ops .newUtf8 ("" ));
111
+ platformArrayCache .setReference (_sun_stderr_encoding_NDX , ops .newUtf8 ("UTF-8" ));
112
+ platformArrayCache .setReference (_sun_stdout_encoding_NDX , ops .newUtf8 ("UTF-8" ));
113
+ platformArrayCache .setReference (_user_dir_NDX , ops .newUtf8 (vm .getProperties ().getOrDefault ("user.dir" , "/home/user" )));
114
+ platformArrayCache .setReference (_user_home_NDX , ops .newUtf8 (vm .getProperties ().getOrDefault ("user.home" , "/home/user" )));
115
+ platformArrayCache .setReference (_user_name_NDX , ops .newUtf8 (vm .getProperties ().getOrDefault ("user.name" , "user" )));
116
+ }
112
117
113
- ctx .setResult (platformArray );
118
+ ctx .setResult (platformArrayCache );
114
119
return Result .ABORT ;
115
120
});
116
121
@@ -127,6 +132,34 @@ public static void init(VirtualMachine vm) {
127
132
ctx .setResult (array );
128
133
return Result .ABORT ;
129
134
});
135
+
136
+ // Override propDefault(int) to return empty if index is not found or null.
137
+ // Signature: propDefault(I)Ljava/lang/String;
138
+ vmi .setInvoker (jc , "propDefault" , "(I)Ljava/lang/String;" , ctx -> {
139
+ int index = ctx .getLocals ().loadInt (1 );
140
+ VMOperations vops = vm .getOperations ();
141
+
142
+ // Ensure platformArrayCache is initialized
143
+ if (platformArrayCache == null ) {
144
+ // If for some reason platformProperties hasn't been called yet, call it now.
145
+ // This will populate platformArrayCache.
146
+ platformArrayCache = vops .allocateArray (vm .getSymbols ().java_lang_String (), FIXED_LENGTH );
147
+ // You may reinitialize it here or ensure platformProperties is called beforehand.
148
+ // For brevity, assume defaults again or call ctx.getInvoker() with platformProperties if accessible.
149
+ }
150
+
151
+ if (index < 0 || index >= FIXED_LENGTH ) {
152
+ ctx .setResult (vops .newUtf8 ("" ));
153
+ } else {
154
+ ObjectValue val = platformArrayCache .getReference (index );
155
+ if (val .isNull ()) {
156
+ ctx .setResult (vops .newUtf8 ("" ));
157
+ } else {
158
+ ctx .setResult (val );
159
+ }
160
+ }
161
+ return Result .ABORT ;
162
+ });
130
163
}
131
164
}
132
165
0 commit comments