aegis_vm 0.2.52

Advanced Rust code virtualization and obfuscation framework
Documentation
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! String Operations Tests

use aegis_vm::{execute, build_config::opcodes::{stack, string, exec}};

/// Test STR_NEW and STR_LEN
#[test]
fn test_str_new_and_len() {
    let bytecode = [
        // Create string with capacity 100
        stack::PUSH_IMM8, 100,
        string::STR_NEW,

        // Length should be 0
        string::STR_LEN,
        exec::HALT,
    ];

    let result = execute(&bytecode, &[]).unwrap();
    assert_eq!(result, 0, "New string should have length 0");
}

/// Test STR_PUSH and STR_LEN
#[test]
fn test_str_push() {
    let bytecode = [
        stack::PUSH_IMM8, 100,
        string::STR_NEW,

        // Push 'H' (0x48)
        stack::DUP,
        stack::PUSH_IMM8, 0x48,
        string::STR_PUSH,

        // Push 'i' (0x69)
        stack::DUP,
        stack::PUSH_IMM8, 0x69,
        string::STR_PUSH,

        // Length should be 2
        string::STR_LEN,
        exec::HALT,
    ];

    let result = execute(&bytecode, &[]).unwrap();
    assert_eq!(result, 2, "String 'Hi' should have length 2");
}

/// Test STR_GET - get byte at index
#[test]
fn test_str_get() {
    let bytecode = [
        stack::PUSH_IMM8, 100,
        string::STR_NEW,

        // Build "abc" = [0x61, 0x62, 0x63]
        stack::DUP, stack::PUSH_IMM8, 0x61, string::STR_PUSH,  // 'a'
        stack::DUP, stack::PUSH_IMM8, 0x62, string::STR_PUSH,  // 'b'
        stack::DUP, stack::PUSH_IMM8, 0x63, string::STR_PUSH,  // 'c'

        // Get str[1] = 'b' = 0x62
        stack::DUP,
        stack::PUSH_IMM8, 1,
        string::STR_GET,

        stack::SWAP,
        stack::DROP,
        exec::HALT,
    ];

    let result = execute(&bytecode, &[]).unwrap();
    assert_eq!(result, 0x62, "str[1] should be 'b' (0x62)");
}

/// Test STR_SET
#[test]
fn test_str_set() {
    let bytecode = [
        stack::PUSH_IMM8, 100,
        string::STR_NEW,

        // Build "abc"
        stack::DUP, stack::PUSH_IMM8, 0x61, string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, 0x62, string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, 0x63, string::STR_PUSH,

        // Set str[1] = 'X' (0x58)
        stack::DUP,
        stack::PUSH_IMM8, 1,
        stack::PUSH_IMM8, 0x58,
        string::STR_SET,

        // Verify str[1] = 'X'
        stack::DUP,
        stack::PUSH_IMM8, 1,
        string::STR_GET,

        stack::SWAP,
        stack::DROP,
        exec::HALT,
    ];

    let result = execute(&bytecode, &[]).unwrap();
    assert_eq!(result, 0x58, "str[1] should be 'X' (0x58) after set");
}

/// Test STR_EQ - string equality
#[test]
fn test_str_eq_equal() {
    let bytecode = [
        // Create string 1: "ab"
        stack::PUSH_IMM8, 10,
        string::STR_NEW,
        stack::DUP, stack::PUSH_IMM8, 0x61, string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, 0x62, string::STR_PUSH,

        // Create string 2: "ab"
        stack::PUSH_IMM8, 10,
        string::STR_NEW,
        stack::DUP, stack::PUSH_IMM8, 0x61, string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, 0x62, string::STR_PUSH,

        // Compare: should be equal (1)
        string::STR_EQ,
        exec::HALT,
    ];

    let result = execute(&bytecode, &[]).unwrap();
    assert_eq!(result, 1, "Equal strings should return 1");
}

/// Test STR_EQ - string inequality
#[test]
fn test_str_eq_not_equal() {
    let bytecode = [
        // Create string 1: "ab"
        stack::PUSH_IMM8, 10,
        string::STR_NEW,
        stack::DUP, stack::PUSH_IMM8, 0x61, string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, 0x62, string::STR_PUSH,

        // Create string 2: "ac"
        stack::PUSH_IMM8, 10,
        string::STR_NEW,
        stack::DUP, stack::PUSH_IMM8, 0x61, string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, 0x63, string::STR_PUSH,  // 'c' not 'b'

        // Compare: should not be equal (0)
        string::STR_EQ,
        exec::HALT,
    ];

    let result = execute(&bytecode, &[]).unwrap();
    assert_eq!(result, 0, "Different strings should return 0");
}

/// Test STR_EQ - different lengths
#[test]
fn test_str_eq_different_lengths() {
    let bytecode = [
        // Create string 1: "ab"
        stack::PUSH_IMM8, 10,
        string::STR_NEW,
        stack::DUP, stack::PUSH_IMM8, 0x61, string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, 0x62, string::STR_PUSH,

        // Create string 2: "abc" (longer)
        stack::PUSH_IMM8, 10,
        string::STR_NEW,
        stack::DUP, stack::PUSH_IMM8, 0x61, string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, 0x62, string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, 0x63, string::STR_PUSH,

        string::STR_EQ,
        exec::HALT,
    ];

    let result = execute(&bytecode, &[]).unwrap();
    assert_eq!(result, 0, "Different length strings should return 0");
}

/// Test STR_CMP - less than
#[test]
fn test_str_cmp_less() {
    let bytecode = [
        // Create "aa"
        stack::PUSH_IMM8, 10,
        string::STR_NEW,
        stack::DUP, stack::PUSH_IMM8, 0x61, string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, 0x61, string::STR_PUSH,

        // Create "ab"
        stack::PUSH_IMM8, 10,
        string::STR_NEW,
        stack::DUP, stack::PUSH_IMM8, 0x61, string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, 0x62, string::STR_PUSH,

        // Compare "aa" < "ab" -> should return -1 (u64::MAX)
        string::STR_CMP,
        exec::HALT,
    ];

    let result = execute(&bytecode, &[]).unwrap();
    assert_eq!(result, u64::MAX, "\"aa\" < \"ab\" should return -1 (u64::MAX)");
}

/// Test STR_CMP - greater than
#[test]
fn test_str_cmp_greater() {
    let bytecode = [
        // Create "b"
        stack::PUSH_IMM8, 10,
        string::STR_NEW,
        stack::DUP, stack::PUSH_IMM8, 0x62, string::STR_PUSH,

        // Create "a"
        stack::PUSH_IMM8, 10,
        string::STR_NEW,
        stack::DUP, stack::PUSH_IMM8, 0x61, string::STR_PUSH,

        // Compare "b" > "a" -> should return 1
        string::STR_CMP,
        exec::HALT,
    ];

    let result = execute(&bytecode, &[]).unwrap();
    assert_eq!(result, 1, "\"b\" > \"a\" should return 1");
}

/// Test STR_CMP - equal
#[test]
fn test_str_cmp_equal() {
    let bytecode = [
        // Create "test"
        stack::PUSH_IMM8, 10,
        string::STR_NEW,
        stack::DUP, stack::PUSH_IMM8, b't', string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, b'e', string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, b's', string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, b't', string::STR_PUSH,

        // Create "test"
        stack::PUSH_IMM8, 10,
        string::STR_NEW,
        stack::DUP, stack::PUSH_IMM8, b't', string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, b'e', string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, b's', string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, b't', string::STR_PUSH,

        string::STR_CMP,
        exec::HALT,
    ];

    let result = execute(&bytecode, &[]).unwrap();
    assert_eq!(result, 0, "Equal strings should return 0");
}

/// Test STR_HASH
#[test]
fn test_str_hash() {
    let bytecode = [
        // Create "test"
        stack::PUSH_IMM8, 10,
        string::STR_NEW,
        stack::DUP, stack::PUSH_IMM8, b't', string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, b'e', string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, b's', string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, b't', string::STR_PUSH,

        string::STR_HASH,
        exec::HALT,
    ];

    let result = execute(&bytecode, &[]).unwrap();
    // Hash should be non-zero and consistent
    assert_ne!(result, 0, "Hash should not be 0");
}

/// Test STR_HASH - same string should produce same hash
#[test]
fn test_str_hash_consistency() {
    // Create first hash
    let bytecode1 = [
        stack::PUSH_IMM8, 10,
        string::STR_NEW,
        stack::DUP, stack::PUSH_IMM8, b'a', string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, b'b', string::STR_PUSH,
        string::STR_HASH,
        exec::HALT,
    ];

    // Create second hash (same string)
    let bytecode2 = [
        stack::PUSH_IMM8, 10,
        string::STR_NEW,
        stack::DUP, stack::PUSH_IMM8, b'a', string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, b'b', string::STR_PUSH,
        string::STR_HASH,
        exec::HALT,
    ];

    let hash1 = execute(&bytecode1, &[]).unwrap();
    let hash2 = execute(&bytecode2, &[]).unwrap();
    assert_eq!(hash1, hash2, "Same string should produce same hash");
}

/// Test STR_CONCAT
#[test]
fn test_str_concat() {
    let bytecode = [
        // Create "Hello"
        stack::PUSH_IMM8, 10,
        string::STR_NEW,
        stack::DUP, stack::PUSH_IMM8, b'H', string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, b'e', string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, b'l', string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, b'l', string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, b'o', string::STR_PUSH,

        // Create "World"
        stack::PUSH_IMM8, 10,
        string::STR_NEW,
        stack::DUP, stack::PUSH_IMM8, b'W', string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, b'o', string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, b'r', string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, b'l', string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, b'd', string::STR_PUSH,

        // Concat -> "HelloWorld"
        string::STR_CONCAT,

        // Length should be 10
        string::STR_LEN,
        exec::HALT,
    ];

    let result = execute(&bytecode, &[]).unwrap();
    assert_eq!(result, 10, "\"HelloWorld\" should have length 10");
}

/// Test STR_CONCAT - verify concatenated content
#[test]
fn test_str_concat_content() {
    let bytecode = [
        // Create "AB"
        stack::PUSH_IMM8, 10,
        string::STR_NEW,
        stack::DUP, stack::PUSH_IMM8, b'A', string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, b'B', string::STR_PUSH,

        // Create "CD"
        stack::PUSH_IMM8, 10,
        string::STR_NEW,
        stack::DUP, stack::PUSH_IMM8, b'C', string::STR_PUSH,
        stack::DUP, stack::PUSH_IMM8, b'D', string::STR_PUSH,

        // Concat -> "ABCD"
        string::STR_CONCAT,

        // Get str[2] = 'C'
        stack::DUP,
        stack::PUSH_IMM8, 2,
        string::STR_GET,

        stack::SWAP,
        stack::DROP,
        exec::HALT,
    ];

    let result = execute(&bytecode, &[]).unwrap();
    assert_eq!(result, b'C' as u64, "\"ABCD\"[2] should be 'C'");
}

/// Test empty string operations
#[test]
fn test_empty_string() {
    let bytecode = [
        // Create empty string
        stack::PUSH_IMM8, 10,
        string::STR_NEW,

        // Create another empty string
        stack::PUSH_IMM8, 10,
        string::STR_NEW,

        // They should be equal
        string::STR_EQ,
        exec::HALT,
    ];

    let result = execute(&bytecode, &[]).unwrap();
    assert_eq!(result, 1, "Empty strings should be equal");
}

/// Test bounds check for string
#[test]
fn test_str_bounds_check() {
    let bytecode = [
        stack::PUSH_IMM8, 10,
        string::STR_NEW,

        // Push one byte
        stack::DUP, stack::PUSH_IMM8, b'a', string::STR_PUSH,

        // Try to get str[5] (out of bounds)
        stack::PUSH_IMM8, 5,
        string::STR_GET,

        exec::HALT,
    ];

    let result = execute(&bytecode, &[]);
    assert!(result.is_err(), "Accessing out of bounds should error");
}