-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwp-wamr.php
executable file
·321 lines (277 loc) · 11.3 KB
/
wp-wamr.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
/*
* Plugin Name: WAMR for Wordpress
* Description: WebAssembly Micro Runtime (WAMR) for Wordpress
* Version: 0.2
* Author: AsmNext Team
* Author URI: https://asmnext.com
*/
define( 'WP_WAMR_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
function wp_wamr_exec($atts = array(), $content = null, $tag = '') {
$result = "";
$_atts = shortcode_atts(
array(
'packagename' => '',
'filename' => 'test',
'function' => '',
'stacksize' => 0,
'heapsize' => 0,
'repl' => 'false',
'env' => '', // query string (e.g. 'key1=value1&key2=value2')
'dir' => '', // comma sperated (e.g. '/mnt/wasi1,/mmt/wasi2')
'args' => '',
'benchmark' => 'false'
), $atts, $tag
);
// Path of 'iwasm' runtime
// Source code: https://github.com/bytecodealliance/wasm-micro-runtime
$wamr_bin_path = WP_WAMR_PLUGIN_DIR . 'bin/iwasm';
// Build a command line
$cmd = array();
$is_tmpfile = false;
if (file_exists($wamr_bin_path)) {
array_push($cmd, $wamr_bin_path);
if($_atts['stacksize'] > 0) {
array_push($cmd, '--stack-size=' . $_atts['stacksize']);
}
if($_atts['heapsize'] > 0) {
array_push($cmd, '--heap-size=' . $_atts['heapsize']);
}
if($_atts['repl'] == 'true') {
array_push($cmd, '--repl');
}
if(!empty($_atts['env'])) {
$_envs = explode('&', $_atts['env']);
foreach($_envs as $_env) {
array_push($cmd, '--env="' . addslashes($_env) . '"');
}
}
if(!empty($_atts['dir'])) {
$_dirs = explode(',', $_atts['dir']);
foreach($_dirs as $_dir) {
array_push($cmd, '--dir="' . addslashes($_dir) . '"');
}
}
if(!empty($_atts['function'])) {
array_push($cmd, '--function');
array_push($cmd, $_atts['function']);
}
// Path of WASM binary
$filepath = wp_wamr_load_media($_atts['filename'], $_atts['packagename']);
if (empty($filepath)) {
$filepath = WP_WAMR_PLUGIN_DIR . 'wasm-bin/' . $_atts['filename'] . '.wasm';
}
array_push($cmd, $filepath);
// Add arguments
if(!empty($_atts['args'])) {
array_push($cmd, $_atts['args']);
}
// Build a command line
$_cmd = implode(' ', $cmd);
// Get stdout
if($_atts['benchmark'] == 'true') {
$ms = microtime(true);
shell_exec($_cmd);
$_ms = microtime(true);
$result .= "[Benchmark] " . sprintf('%.8fs', ($_ms - $ms)) . " (WASM/Shell)";
} else {
$result .= shell_exec($_cmd);
}
} else {
$result .= "[Error] WAMR not found!";
}
// Remove WASM file
$sys_tmpdir = sys_get_temp_dir();
$targets = glob($sys_tmpdir . "/wp-wamr-*");
foreach($targets as $target) {
wp_wamr_clean($target);
}
return $result;
}
function wp_wamr_clean($target) {
$_targets = glob("$target/*");
foreach($_targets as $_target) {
if (is_dir($_target)) {
wp_wamr_clean($_target);
rmdir($_target);
} else {
unlink($_target);
}
}
rmdir($target);
}
function wp_wamr_benchmark() {
$result = "";
include_once(WP_WAMR_PLUGIN_DIR . 'benchmark/tower_of_hanoi.php');
// Tower of Hanoi - 4 disks
$result .= "<br>Tower of Hanoi - 4 disks";
$ms = microtime(true);
hanoi_move(4, 'A', 'B', 'C');
$_ms = microtime(true);
$result .= "<br>" . sprintf('%.8fs', ($_ms - $ms)) . " (PHP/Native)";
$ms = microtime(true);
shell_exec('php ' . WP_WAMR_PLUGIN_DIR . 'benchmark/tower_of_hanoi_4disks');
$_ms = microtime(true);
$result .= "<br>" . sprintf('%.8fs', ($_ms - $ms)) . " (PHP/Shell)";
$result .= "<br>" . wp_wamr_exec(array('filename' => 'tower_of_hanoi_4disks.wasm', 'benchmark' => 'true'));
$result .= "<br>";
// Tower of Hanoi - 8 disks
$result .= "<br>Tower of Hanoi - 8 disks";
$ms = microtime(true);
hanoi_move(8, 'A', 'B', 'C');
$_ms = microtime(true);
$result .= "<br>" . sprintf('%.8fs', ($_ms - $ms)) . " (PHP/Native)";
$ms = microtime(true);
shell_exec('php ' . WP_WAMR_PLUGIN_DIR . 'benchmark/tower_of_hanoi_8disks');
$_ms = microtime(true);
$result .= "<br>" . sprintf('%.8fs', ($_ms - $ms)) . " (PHP/Shell)";
$result .= "<br>" . wp_wamr_exec(array('filename' => 'tower_of_hanoi_8disks', 'benchmark' => 'true'));
$result .= "<br>";
// Tower of Hanoi - 16 disks
$result .= "<br>Tower of Hanoi - 16 disks";
$ms = microtime(true);
hanoi_move(16, 'A', 'B', 'C');
$_ms = microtime(true);
$result .= "<br>" . sprintf('%.8fs', ($_ms - $ms)) . " (PHP/Native)";
$ms = microtime(true);
shell_exec('php ' . WP_WAMR_PLUGIN_DIR . 'benchmark/tower_of_hanoi_16disks.php');
$_ms = microtime(true);
$result .= "<br>" . sprintf('%.8fs', ($_ms - $ms)) . " (PHP/Shell)";
$result .= "<br>" . wp_wamr_exec(array('filename' => 'tower_of_hanoi_16disks', 'benchmark' => 'true'));
$result .= "<br>";
// Tower of Hanoi - 20 disks
$result .= "<br>Tower of Hanoi - 20 disks";
$ms = microtime(true);
hanoi_move(20, 'A', 'B', 'C');
$_ms = microtime(true);
$result .= "<br>" . sprintf('%.8fs', ($_ms - $ms)) . " (PHP/Native)";
$ms = microtime(true);
shell_exec('php ' . WP_WAMR_PLUGIN_DIR . 'benchmark/tower_of_hanoi_20disks.php');
$_ms = microtime(true);
$result .= "<br>" . sprintf('%.8fs', ($_ms - $ms)) . " (PHP/Shell)";
$result .= "<br>" . wp_wamr_exec(array('filename' => 'tower_of_hanoi_20disks', 'benchmark' => 'true'));
$result .= "<br>";
// Tower of Hanoi - 24 disks
$result .= "<br>Tower of Hanoi - 24 disks";
$ms = microtime(true);
hanoi_move(24, 'A', 'B', 'C');
$_ms = microtime(true);
$result .= "<br>" . sprintf('%.8fs', ($_ms - $ms)) . " (PHP/Native)";
$ms = microtime(true);
shell_exec('php ' . WP_WAMR_PLUGIN_DIR . 'benchmark/tower_of_hanoi_24disks.php');
$_ms = microtime(true);
$result .= "<br>" . sprintf('%.8fs', ($_ms - $ms)) . " (PHP/Shell)";
$result .= "<br>" . wp_wamr_exec(array('filename' => 'tower_of_hanoi_24disks', 'benchmark' => 'true'));
$result .= "<br>";
// Tower of Hanoi - 28 disks
$result .= "<br>Tower of Hanoi - 28 disks";
$ms = microtime(true);
hanoi_move(28, 'A', 'B', 'C');
$_ms = microtime(true);
$result .= "<br>" . sprintf('%.8fs', ($_ms - $ms)) . " (PHP/Native)";
$ms = microtime(true);
shell_exec('php ' . WP_WAMR_PLUGIN_DIR . 'benchmark/tower_of_hanoi_28disks.php');
$_ms = microtime(true);
$result .= "<br>" . sprintf('%.8fs', ($_ms - $ms)) . " (PHP/Shell)";
$result .= "<br>" . wp_wamr_exec(array('filename' => 'tower_of_hanoi_28disks', 'benchmark' => 'true'));
$result .= "<br>";
return $result;
}
function wp_wamr_verify_checksum($basedir, $filename) {
// Check exists target file
$filepath = $basedir . '/' . $filename . '.wasm';
if (!file_exists($filepath)) {
echo "[Error] Does not exists target file";
}
// Check exists MD5SUM file
$checksum_index = -1;
$checksum_file = '';
$checksum_possibles = array(
$basedir . '/MD5SUM',
$basedir . '/md5sum',
$basedir . '/SHA1SUM',
$basedir . '/sha1sum'
);
for ($i = 0; $i < count($checksum_possibles); $i++) {
if (file_exists($checksum_possibles[$i])) {
$checksum_index = $i;
$checksum_file = $checksum_possibles[$checksum_index];
break;
}
}
// If could not find
if ($hashinfo_index < 0) {
echo "[Error] Does not exists the checksum file";
return false;
}
// Open the MD5SUM file
$contents = fread(fopen($checksum_file, 'r'), filesize($checksum_file));
// Parse MD5SUM contents
$segments = preg_split('/[\s]+/', $contents);
// Calcuate file hash
$checksum = '';
if (strpos(strtoupper($checksum_file), "MD5SUM") !== false) {
$checksum = md5_file($filepath);
} else if (strpos(strtoupper($checksum_file), "SHA1SUM") !== false) {
$checksum = sha1_file($filepath);
}
// Is it verified?
return !empty($checksum) ? in_array($checksum, $segments) : false;
}
function wp_wamr_load_media($filename, $packagename) {
global $wpdb;
$old_filepath = "";
$filepath = "";
// The following code is used to retrieve and store the site URL in the variable $site_url.
// This URL is the root URL of the WordPress site, which is essential for building paths or links relative to the site's base.
//
// REAL PATH: /var/www/html/wordpress/wp-content/plugins
// The directory path provided here represents the actual file system path where the WordPress plugins are stored.
//
// It's important to understand that the real path may differ from the site's URL. The real path refers to the location on the server's filesystem,
// while the URL (provided by site_url()) is what users access via their web browsers.
//
// To ensure consistency between the URL and the server's file structure, the following line of code retrieves the document root directory of the server,
// appends '/wordpress' to it, and assigns it to the $docroot variable.
//
// $docroot will store the full absolute path to the root of the WordPress installation on the server,
// which is necessary for performing file operations or linking resources correctly.
//
// Note: The realpath() function is used to resolve any symbolic links, '..' or '.' in the path, ensuring that $docroot contains the correct and absolute path.
$site_url = site_url();
$docroot = realpath($_SERVER["DOCUMENT_ROOT"]) . '/wordpress'; // ends with a slash (/) + '/wordpress'
$sys_tmpdir = sys_get_temp_dir();
$tmpdir = $sys_tmpdir . '/wp-wamr-' . substr(md5(mt_rand()), 0, 7);
if(mkdir($tmpdir)) {
$results = $wpdb->get_results( "select guid from {$wpdb->prefix}posts
where post_type = 'attachment' and post_title = '{$packagename}'
order by post_date desc limit 1" , OBJECT );
foreach($results as $attachment) {
if ($site_url == substr($attachment->guid, 0, strlen($site_url))) {
$old_filepath = $docroot . substr($attachment->guid, strlen($site_url));
}
}
// For security reason, the media file must be compressed like '*.zip'
if (!empty($old_filepath) && file_exists($old_filepath)) {
$zip = new ZipArchive();
$result = $zip->open($old_filepath);
if ($result === TRUE) {
$zip->extractTo($tmpdir . '/');
$zip->close();
$_filepath = $tmpdir . '/' . $filename . '.wasm';
if(file_exists($_filepath) && wp_wamr_verify_checksum($tmpdir, $filename)) {
$filepath = $_filepath;
} else {
echo "[Error] Failed to verify the checksum of WASM file";
}
} else {
echo "[Error] Invaild package file";
}
} else {
echo "[Error] No exists package file in Media Library";
}
}
return $filepath;
}
add_shortcode('wamr_exec', 'wp_wamr_exec');
add_shortcode('wamr_benchmark', 'wp_wamr_benchmark');