WARNING! Access to this system is limited to authorised users only.
Unauthorised users may be subject to prosecution.
Unauthorised access to this system is a criminal offence under Australian law (Federal Crimes Act 1914 Part VIA)
It is a criminal offence to:
(1) Obtain access to data without authority. -Penalty 2 years imprisonment.
(2) Damage, delete, alter or insert data without authority. -Penalty 10 years imprisonment.
User activity is monitored and recorded. Anyone using this system expressly consents to such monitoring and recording.

To protect your data, the CISO officer has suggested users to enable 2FA as soon as possible.
Currently 2.7% of users enabled 2FA.

ncs_tests.c 22 KB
Newer Older
Kunshan Wang's avatar
Kunshan Wang committed
1
#include <stdio.h>
Kunshan Wang's avatar
Kunshan Wang committed
2
#include <string.h>
Kunshan Wang's avatar
Kunshan Wang committed
3
4
5
#include <stdint.h>
#include <inttypes.h>
#include <stdbool.h>
Kunshan Wang's avatar
Kunshan Wang committed
6
#include <stdlib.h>
Kunshan Wang's avatar
Kunshan Wang committed
7

Kunshan Wang's avatar
Kunshan Wang committed
8
9
#include <muapi.h>

Kunshan Wang's avatar
Kunshan Wang committed
10
11
12
13
14
#define MU_ASSERT_EQUALS(a, b, f) if (!(a == b)) { \
    muprintf("%s (%" f ") is not equal to %s (%" f ")\n", #a, a, #b, b); \
    return false; \
}

Kunshan Wang's avatar
Kunshan Wang committed
15
16
17
18
19
#define MU_ASSERT_EQUALS_TRAP(a, b, f) if (!(a == b)) { \
    muprintf("%s (%" f ") is not equal to %s (%" f ")\n", #a, a, #b, b); \
    exit(1); \
}

Kunshan Wang's avatar
Kunshan Wang committed
20
#define ID(name) ctx->id_of(ctx, name)
Kunshan Wang's avatar
Kunshan Wang committed
21
#define NAME(id) ctx->name_of(ctx, id)
Kunshan Wang's avatar
Kunshan Wang committed
22
23
24
25
26
27
28
29
30

#define muprintf(fmt, ...) printf("[C:%d:%s] " fmt, __LINE__, __func__, ## __VA_ARGS__)

bool test_basic(MuVM *mvm, int theID, char *theName) {
    muprintf("header: %p\n", mvm->header);
    muprintf("new_context: %p\n", mvm->new_context);
    muprintf("id_of: %p\n", mvm->id_of);
    muprintf("name_of: %p\n", mvm->name_of);
    muprintf("set_trap_handler: %p\n", mvm->set_trap_handler);
Kunshan Wang's avatar
Kunshan Wang committed
31
32

    int id = mvm->id_of(mvm, theName);
Kunshan Wang's avatar
Kunshan Wang committed
33
    muprintf("id = %d\n", id);
Kunshan Wang's avatar
Kunshan Wang committed
34
    if (id != theID) {
Kunshan Wang's avatar
Kunshan Wang committed
35
36
        muprintf("ID %d is not equal to %d\n", id, theID);
        return false;
Kunshan Wang's avatar
Kunshan Wang committed
37
38
39
    }

    char *name = mvm->name_of(mvm, theID);
Kunshan Wang's avatar
Kunshan Wang committed
40
    muprintf("name = %s\n", name);
Kunshan Wang's avatar
Kunshan Wang committed
41
    if (strcmp(name, theName) != 0) {
Kunshan Wang's avatar
Kunshan Wang committed
42
43
        muprintf("name %s is not equal to %s\n", name, theName);
        return false;
Kunshan Wang's avatar
Kunshan Wang committed
44
45
    }

Kunshan Wang's avatar
Kunshan Wang committed
46
    return true;
Kunshan Wang's avatar
Kunshan Wang committed
47
48
}

Kunshan Wang's avatar
Kunshan Wang committed
49
bool test_with_ctx(MuVM *mvm, int theID, char *theName) {
Kunshan Wang's avatar
Kunshan Wang committed
50
51
52
    MuCtx *ctx = mvm->new_context(mvm);

    int id = ctx->id_of(ctx, theName);
Kunshan Wang's avatar
Kunshan Wang committed
53
    muprintf("id = %d\n", id);
Kunshan Wang's avatar
Kunshan Wang committed
54
    if (id != theID) {
Kunshan Wang's avatar
Kunshan Wang committed
55
56
        muprintf("ID %d is not equal to %d\n", id, theID);
        return false;
Kunshan Wang's avatar
Kunshan Wang committed
57
58
59
    }

    char *name = ctx->name_of(ctx, theID);
Kunshan Wang's avatar
Kunshan Wang committed
60
    muprintf("name = %s\n", name);
Kunshan Wang's avatar
Kunshan Wang committed
61
    if (strcmp(name, theName) != 0) {
Kunshan Wang's avatar
Kunshan Wang committed
62
63
        muprintf("name %s is not equal to %s\n", name, theName);
        return false;
Kunshan Wang's avatar
Kunshan Wang committed
64
65
    }

Kunshan Wang's avatar
Kunshan Wang committed
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
    ctx->close_context(ctx);

    return true;
}

bool test_basic_conv(MuVM *mvm) {
    MuCtx *ctx = mvm->new_context(mvm);

    int8_t   p8  =  0x7f;
    int8_t   n8  = -0x80;
    uint8_t  u8  =  0xffU;
    uint8_t  r8  =  0x5aU;
    int16_t  p16 =  0x7fff;
    int16_t  n16 = -0x8000;
    uint16_t u16 =  0xffffU;
    uint16_t r16 =  0x55aaU;
    int32_t  p32 =  0x7fffffff;
    int32_t  n32 = -0x80000000;
    uint32_t u32 =  0xffffffffU;
    uint32_t r32 =  0x5555aaaaU;
    int64_t  p64 =  0x7fffffffffffffffL;
    int64_t  n64 = -0x8000000000000000L;
    uint64_t u64 =  0xffffffffffffffffUL;
    uint64_t r64 =  0x55555555aaaaaaaaUL;

    MuIntValue vp8  = ctx->handle_from_sint8 (ctx, p8 , 8 );
    MuIntValue vn8  = ctx->handle_from_sint8 (ctx, n8 , 8 );
    MuIntValue vu8  = ctx->handle_from_uint8 (ctx, u8 , 8 );
    MuIntValue vr8  = ctx->handle_from_uint8 (ctx, r8 , 8 );
    MuIntValue vp16 = ctx->handle_from_sint16(ctx, p16, 16);
    MuIntValue vn16 = ctx->handle_from_sint16(ctx, n16, 16);
    MuIntValue vu16 = ctx->handle_from_uint16(ctx, u16, 16);
    MuIntValue vr16 = ctx->handle_from_uint16(ctx, r16, 16);
    MuIntValue vp32 = ctx->handle_from_sint32(ctx, p32, 32);
    MuIntValue vn32 = ctx->handle_from_sint32(ctx, n32, 32);
    MuIntValue vu32 = ctx->handle_from_uint32(ctx, u32, 32);
    MuIntValue vr32 = ctx->handle_from_uint32(ctx, r32, 32);
    MuIntValue vp64 = ctx->handle_from_sint64(ctx, p64, 64);
    MuIntValue vn64 = ctx->handle_from_sint64(ctx, n64, 64);
    MuIntValue vu64 = ctx->handle_from_uint64(ctx, u64, 64);
    MuIntValue vr64 = ctx->handle_from_uint64(ctx, r64, 64);

    int8_t   bp8  = ctx->handle_to_sint8 (ctx, vp8 );
    int8_t   bn8  = ctx->handle_to_sint8 (ctx, vn8 );
    uint8_t  bu8  = ctx->handle_to_uint8 (ctx, vu8 );
    uint8_t  br8  = ctx->handle_to_uint8 (ctx, vr8 );
    int16_t  bp16 = ctx->handle_to_sint16(ctx, vp16);
    int16_t  bn16 = ctx->handle_to_sint16(ctx, vn16);
    uint16_t bu16 = ctx->handle_to_uint16(ctx, vu16);
    uint16_t br16 = ctx->handle_to_uint16(ctx, vr16);
    int32_t  bp32 = ctx->handle_to_sint32(ctx, vp32);
    int32_t  bn32 = ctx->handle_to_sint32(ctx, vn32);
    uint32_t bu32 = ctx->handle_to_uint32(ctx, vu32);
    uint32_t br32 = ctx->handle_to_uint32(ctx, vr32);
    int64_t  bp64 = ctx->handle_to_sint64(ctx, vp64);
    int64_t  bn64 = ctx->handle_to_sint64(ctx, vn64);
    uint64_t bu64 = ctx->handle_to_uint64(ctx, vu64);
    uint64_t br64 = ctx->handle_to_uint64(ctx, vr64);

    MU_ASSERT_EQUALS(p8 , bp8 , PRIi8 )
    MU_ASSERT_EQUALS(n8 , bn8 , PRIi8 )
    MU_ASSERT_EQUALS(u8 , bu8 , PRIu8 )
    MU_ASSERT_EQUALS(r8 , br8 , PRIu8 )
    MU_ASSERT_EQUALS(p16, bp16, PRIi16)
    MU_ASSERT_EQUALS(n16, bn16, PRIi16)
    MU_ASSERT_EQUALS(u16, bu16, PRIu16)
    MU_ASSERT_EQUALS(r16, br16, PRIu16)
    MU_ASSERT_EQUALS(p32, bp32, PRIi32)
    MU_ASSERT_EQUALS(n32, bn32, PRIi32)
    MU_ASSERT_EQUALS(u32, bu32, PRIu32)
    MU_ASSERT_EQUALS(r32, br32, PRIu32)
    MU_ASSERT_EQUALS(p64, bp64, PRIi64)
    MU_ASSERT_EQUALS(n64, bn64, PRIi64)
    MU_ASSERT_EQUALS(u64, bu64, PRIu64)
    MU_ASSERT_EQUALS(r64, br64, PRIu64)

    MuIntValue sep8t64 = ctx->handle_from_sint8(ctx, p8, 64);
    MuIntValue sen8t64 = ctx->handle_from_sint8(ctx, n8, 64);
    MuIntValue zeu8t64 = ctx->handle_from_uint8(ctx, u8, 64);
    MuIntValue zer8t64 = ctx->handle_from_uint8(ctx, r8, 64);

    uint64_t bsep8t64 = ctx->handle_to_uint64(ctx, sep8t64);
    uint64_t bsen8t64 = ctx->handle_to_uint64(ctx, sen8t64);
    uint64_t bzeu8t64 = ctx->handle_to_uint64(ctx, zeu8t64);
    uint64_t bzer8t64 = ctx->handle_to_uint64(ctx, zer8t64);

    MU_ASSERT_EQUALS(bsep8t64, 0x000000000000007fULL, PRIx64);
    MU_ASSERT_EQUALS(bsen8t64, 0xffffffffffffff80ULL, PRIx64);
    MU_ASSERT_EQUALS(bzeu8t64, 0x00000000000000ffULL, PRIx64);
    MU_ASSERT_EQUALS(bzer8t64, 0x000000000000005aULL, PRIx64);

    float  f = 3.14f;
    double d = 6.28;
        
    MuFloatValue  hf = ctx->handle_from_float (ctx, f);
    MuDoubleValue hd = ctx->handle_from_double(ctx, d);

    float  bf = ctx->handle_to_float (ctx, hf);
    double bd = ctx->handle_to_double(ctx, hd);

    MU_ASSERT_EQUALS(bf, 3.14f, "f" );
    MU_ASSERT_EQUALS(bd, 6.28 , "lf");

    MuCPtr p = (MuCPtr)&f;
    MuCFP fp = (MuCFP)test_basic_conv;

    MuID id_ptrvoid = ctx->id_of(ctx, "@ptrvoid");
    MuID id_fpv_v   = ID("@fpv_v");

    MuUPtrValue hp = ctx->handle_from_ptr(ctx, id_ptrvoid, p );
    MuUFPValue hfp = ctx->handle_from_fp (ctx, id_fpv_v,   fp);

    MuCPtr bp = ctx->handle_to_ptr(ctx, hp );
    MuCFP bfp = ctx->handle_to_fp (ctx, hfp);

    MU_ASSERT_EQUALS(bp , p , "p");
    MU_ASSERT_EQUALS(bfp, fp, "p");

    ctx->delete_value(ctx, zer8t64);

    ctx->close_context(ctx);

    return true;
}

bool test_global_vars(MuVM *mvm, int64_t(*the_plus_one_fp)(int64_t)) {
    MuCtx *ctx = mvm->new_context(mvm);

    MuIntValue  hI64_7 = ctx->handle_from_const(ctx, ID("@I64_7"));
    int64_t I64_7 = ctx->handle_to_sint64(ctx, hI64_7);

    MU_ASSERT_EQUALS(I64_7, 7LL, PRId64);

    MuIRefValue     hg_i64           = ctx->handle_from_global(ctx, ID("@g_i64"));
    MuFuncRefValue  hplus_one        = ctx->handle_from_func  (ctx, ID("@plus_one"));
    MuUFPValue      hplus_one_native = ctx->handle_from_expose(ctx, ID("@plus_one_native"));

    int64_t (*plus_one_fp)(int64_t) = (int64_t(*)(int64_t))(ctx->handle_to_fp(ctx, hplus_one_native));

    MU_ASSERT_EQUALS(plus_one_fp, the_plus_one_fp, "p");

    // NOTE: cannot call it now. In this refimpl, Mu must call native first.
    // This test case is run by ScalaTest, not Mu IR.

    //int64_t result = plus_one_fp(42LL);

    //MU_ASSERT_EQUALS(result, 43LL, PRId64);
Kunshan Wang's avatar
Kunshan Wang committed
213
214
215

    ctx->close_context(ctx);

Kunshan Wang's avatar
Kunshan Wang committed
216
    return true;
Kunshan Wang's avatar
Kunshan Wang committed
217
}
Kunshan Wang's avatar
Kunshan Wang committed
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

struct simple_context {
    int magic;
};

void malloc_freer(MuValue *values, MuCPtr freerdata) {
    free(values);
}

void nop_freer(MuValue *values, MuCPtr freerdata) {
}

void simple_trap_handler(MuCtx *ctx, MuThreadRefValue thread,
        MuStackRefValue stack, int wpid, MuTrapHandlerResult *result,
        MuStackRefValue *new_stack, MuValue **values, int *nvalues,
        MuValuesFreer *freer, MuCPtr *freerdata, MuRefValue *exception,
        MuCPtr userdata) {

    muprintf("Hi! I am the native trap handler!\n");

    struct simple_context *userctx = (struct simple_context*)userdata;
    int magic = userctx->magic;
    muprintf("My magic is %d\n", magic);
    MU_ASSERT_EQUALS_TRAP(magic, 42, "d");

    muprintf("I am going to introspect the stack.\n");

    MuFCRefValue *cursor = ctx->new_cursor(ctx, stack);
    MuID fid  = ctx->cur_func(ctx, cursor);
    muprintf("Function: %d: %s\n", fid, NAME(fid));
    MuID fvid = ctx->cur_func_ver(ctx, cursor);
    muprintf("Version: %d: %s\n", fvid, NAME(fvid));
    MuID iid  = ctx->cur_inst(ctx, cursor);
    muprintf("Instruction: %d: %s\n", iid, NAME(iid));

    MuID trap1_id = ID("@trapper.v1.entry.trap1");
    MuID trap2_id = ID("@trapper.v1.entry.trap2");

    MU_ASSERT_EQUALS_TRAP(fid , ID("@trapper"), "d");
    MU_ASSERT_EQUALS_TRAP(fvid, ID("@trapper.v1"), "d");

    muprintf("Selecting branch according to the instruction ID: %d\n", iid);

    if (iid == trap1_id) {
        muprintf("It is %%trap1\n");
        MuValue kas[1];
        muprintf("Dumping keep-alives...\n");
        ctx->dump_keepalives(ctx, cursor, kas);
        muprintf("Dumped\n");
        ctx->close_cursor(ctx, cursor);

        int64_t oldKa = ctx->handle_to_sint64(ctx, kas[0]);
        muprintf("The keep-alive variable is %" PRId64 "\n", oldKa);
        MU_ASSERT_EQUALS_TRAP(oldKa, 42LL, PRId64);

        int64_t newKa = oldKa + 1;

        muprintf("Prepare to return\n");
        muprintf("Writing result at %p\n", result);
        *result = MU_REBIND_PASS_VALUES;
        muprintf("Writing new_stack at %p\n", new_stack);
        *new_stack = stack;
        muprintf("Allocating values array, writing at %p\n", values);
        *values = (MuValue*)malloc(8);
        muprintf("Setting the only value. Addr: %p\n", &(*values)[0]);
        (*values)[0] = ctx->handle_from_sint64(ctx, newKa, 64);
        muprintf("Writing nvalues at %p\n", nvalues);
        *nvalues = 1;
        muprintf("Writing freer at %p\n", freer);
        *freer = malloc_freer;
        muprintf("Writing freerdata at %p\n", freerdata);
        *freerdata = NULL;
        muprintf("Bye!\n");
        return;
    } else if (iid == trap2_id) {
        muprintf("It is %%trap2\n");
        MuValue kas[1];
        ctx->dump_keepalives(ctx, cursor, kas);
        ctx->close_cursor(ctx, cursor);

        muprintf("Introspect the KAs...\n");
        int64_t v1 = ctx->handle_to_sint64(ctx, kas[0]);
        muprintf("KA value %%v1 is %" PRId64 "\n", v1);
        MU_ASSERT_EQUALS_TRAP(v1, 43LL, PRId64);

        muprintf("Prepare to return from trap handler...\n");
        *result = MU_REBIND_PASS_VALUES;
        *new_stack = stack;
        *values = NULL;
        *nvalues = 0;
        *freer = NULL;
        *freerdata = NULL;
        muprintf("Bye!\n");
        return;
    } else {
        muprintf("Unknown trap. ID: %d\n", iid);
        MuName trapName = NAME(iid);
        muprintf("name: %s\n", trapName);
        exit(1);
    }

    return; // Unreachable, but the C compiler may not be smart enough.
}

bool test_traps(MuVM *mvm) {
    struct simple_context userctx = { 42 };
    mvm->set_trap_handler(mvm, simple_trap_handler, &userctx);

    MuCtx *ctx = mvm->new_context(mvm);

    MuValue args[1];
    args[0] = ctx->handle_from_sint64(ctx, 42LL, 64);

    MuFuncRefValue func = ctx->handle_from_func(ctx, ID("@trapper"));
    MuStackRefValue stack = ctx->new_stack(ctx, func);
    MuThreadRefValue thread = ctx->new_thread(ctx, stack, MU_REBIND_PASS_VALUES,
            args, 1, NULL);

    mvm->execute(mvm);

    ctx->close_context(ctx);

    return true;
}
Kunshan Wang's avatar
Kunshan Wang committed
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541

void load_bundle_trap_handler(MuCtx *ctx, MuThreadRefValue thread,
        MuStackRefValue stack, int wpid, MuTrapHandlerResult *result,
        MuStackRefValue *new_stack, MuValue **values, int *nvalues,
        MuValuesFreer *freer, MuCPtr *freerdata, MuRefValue *exception,
        MuCPtr userdata) {

    muprintf("Hi! I am the native trap handler!\n");

    MuFCRefValue *cursor = ctx->new_cursor(ctx, stack);
    MuID fid  = ctx->cur_func(ctx, cursor);
    MuID fvid = ctx->cur_func_ver(ctx, cursor);
    MuID iid  = ctx->cur_inst(ctx, cursor);

    MuID trap_id = ID("@made_in_c.v1.entry.trap");

    MU_ASSERT_EQUALS_TRAP(fid , ID("@made_in_c"), "d");
    MU_ASSERT_EQUALS_TRAP(fvid, ID("@made_in_c.v1"), "d");
    MU_ASSERT_EQUALS_TRAP(iid , ID("@made_in_c.v1.entry.trap"), "d");

    MuValue kas[1];
    ctx->dump_keepalives(ctx, cursor, kas);
    ctx->close_cursor(ctx, cursor);

    int64_t n = ctx->handle_to_sint64(ctx, kas[0]);
    muprintf("KA value %%n is %" PRId64 "\n", n);
    MU_ASSERT_EQUALS_TRAP(n, 53LL, PRId64);

    MuIRefValue hg_cmagic_iref = ctx->handle_from_global(ctx, ID("@g_cmagic"));
    MuIntValue  hg_cmagic_value = ctx->load(ctx, MU_NOT_ATOMIC, hg_cmagic_iref);
    int64_t g_cmagic = ctx->handle_to_sint64(ctx, hg_cmagic_value);
    muprintf("Global value @g_cmagic is %" PRId64 "\n", g_cmagic);
    MU_ASSERT_EQUALS_TRAP(g_cmagic, 52LL, PRId64);

    muprintf("Prepare to return from trap handler...\n");
    *result = MU_REBIND_PASS_VALUES;
    *new_stack = stack;
    *values = NULL;
    *nvalues = 0;
    *freer = NULL;
    *freerdata = NULL;
    muprintf("Bye!\n");

    return;
}

bool test_load_bundle(MuVM *mvm) {
    mvm->set_trap_handler(mvm, load_bundle_trap_handler, NULL);

    MuCtx *ctx = mvm->new_context(mvm);

    char ir[] = ".funcdef @made_in_c VERSION %v1 <@i_i> {\n"
        "  %entry(<@i64> %n):\n"
        "    [%trap] TRAP <> KEEPALIVE (%n)\n"
        "    COMMINST @uvm.thread_exit\n"
        "}\n"
        ".global @g_cmagic <@i64>\n"
        ;

    char hail[] = ".init @g_cmagic = 52\n";

    muprintf("Loading bundle...\n");
    ctx->load_bundle(ctx, ir, strlen(ir));
    muprintf("Loading HAIL...\n");
    ctx->load_hail(ctx, hail, strlen(hail));
    muprintf("All loaded.\n");

    MuValue args[1];
    args[0] = ctx->handle_from_sint64(ctx, 53LL, 64);

    MuFuncRefValue func = ctx->handle_from_func(ctx, ID("@made_in_c"));
    MuStackRefValue stack = ctx->new_stack(ctx, func);
    MuThreadRefValue thread = ctx->new_thread(ctx, stack, MU_REBIND_PASS_VALUES,
            args, 1, NULL);

    mvm->execute(mvm);

    ctx->close_context(ctx);

    return true;
}

bool test_comp_types(MuVM *mvm) {
    MuCtx *ctx = mvm->new_context(mvm);

    MuStructValue s1  = ctx->handle_from_const(ctx, ID("@S1"));
    MuIntValue    s10 = ctx->extract_value(ctx, s1, 0);
    int64_t s10_v = ctx->handle_to_sint64(ctx, s10);
    MU_ASSERT_EQUALS(s10_v, 6LL, PRId64);

    MuDoubleValue s11 = ctx->extract_value(ctx, s1, 1);
    double s11_v = ctx->handle_to_double(ctx, s11);
    MU_ASSERT_EQUALS(s11_v, 7.0, "lf");

    MuIntValue    I64_20 = ctx->handle_from_const(ctx, ID("@I64_20"));
    MuStructValue s1m    = ctx->insert_value(ctx, s1, 0, I64_20);
    MuIntValue    s10m   = ctx->extract_value(ctx, s1m, 0);
    int64_t       s10m_v = ctx->handle_to_sint64(ctx, s10m);
    MU_ASSERT_EQUALS(s10m_v, 20LL, PRId64);

    MuArrayValue a1 = ctx->handle_from_const(ctx, ID("@A1"));

    MuID i32 = ID("@i32");

    for (int i=0; i<3; i++) {
        MuIntValue hi  = ctx->handle_from_sint32(ctx, i, 32);
        MuIntValue a1i = ctx->extract_element(ctx, a1, hi);
        int32_t a1i_v  = ctx->handle_to_sint32(ctx, a1i);
        MU_ASSERT_EQUALS(a1i_v, i+2, PRId32);
        ctx->delete_value(ctx, hi);
        ctx->delete_value(ctx, a1i);
    }

    MuIntValue I32_2  = ctx->handle_from_const(ctx, ID("@I32_2"));
    MuIntValue I32_30 = ctx->handle_from_const(ctx, ID("@I32_30"));

    MuArrayValue a1m   = ctx->insert_element (ctx, a1,  I32_2, I32_30);
    MuIntValue   a1m2  = ctx->extract_element(ctx, a1m, I32_2);
    int32_t      a1m2v = ctx->handle_to_sint32(ctx, a1m2);
    MU_ASSERT_EQUALS(a1m2v, 30, PRId32);

    ctx->close_context(ctx);

    return true;
}

bool test_memory_ops(MuVM *mvm) {
    MuCtx *ctx = mvm->new_context(mvm);

    MuRefValue r1 = ctx->new_fixed(ctx, ID("@i64"));
    MuRefValue r2 = ctx->new_fixed(ctx, ID("@i64"));

    MuIntValue I64_3  = ctx->handle_from_const(ctx, ID("@I64_3"));
    MuIntValue I64_4  = ctx->handle_from_const(ctx, ID("@I64_4"));
    MuIntValue I64_10 = ctx->handle_from_const(ctx, ID("@I64_10"));

    MuRefValue rh1 = ctx->new_hybrid(ctx, ID("@hyb"), I64_10);
    MuRefValue rh2 = ctx->new_hybrid(ctx, ID("@hyb"), I64_10);

    int r1r1 = ctx->ref_eq(ctx, r1, r1);
    int r1r2 = ctx->ref_eq(ctx, r1, r2);

    MU_ASSERT_EQUALS(r1r1, 1, "d");
    MU_ASSERT_EQUALS(r1r2, 0, "d");

    MuIRefValue rh1i  = ctx->get_iref(ctx, rh1);
    MuIRefValue rh1f  = ctx->get_field_iref(ctx, rh1i, 0);
    MuIRefValue rh1v  = ctx->get_var_part_iref(ctx, rh1i);
    MuIRefValue rh1v3 = ctx->shift_iref(ctx, rh1v, I64_3);
    MuIRefValue rh1v4 = ctx->shift_iref(ctx, rh1v, I64_4);

    int rh33 = ctx->ref_ult(ctx, rh1v3, rh1v3);
    int rh34 = ctx->ref_ult(ctx, rh1v3, rh1v4);

    MU_ASSERT_EQUALS(rh33, 0, "d");
    MU_ASSERT_EQUALS(rh34, 1, "d");

    MuRefValue  r3   = ctx->new_fixed(ctx, ID("@a1"));
    MuIRefValue r3i  = ctx->get_iref(ctx, r3);
    MuIRefValue r3i3 = ctx->get_elem_iref(ctx, r3i, I64_3);

    MuRefValue r1i = ctx->get_iref(ctx, r1);
    
    ctx->store(ctx, MU_SEQ_CST, r1i, I64_10);
    MuIntValue l = ctx->load(ctx, MU_SEQ_CST, r1i);
    int64_t lv   = ctx->handle_to_sint64(ctx, l);

    MU_ASSERT_EQUALS(lv, 10LL, PRId64);

    int succ1;
    int succ2;

    MuIntValue res1 = ctx->cmpxchg(ctx, MU_SEQ_CST, MU_SEQ_CST, 0,
            r1i, I64_10, I64_4, &succ1);
    MuIntValue res2 = ctx->cmpxchg(ctx, MU_SEQ_CST, MU_SEQ_CST, 0,
            r1i, I64_10, I64_3, &succ2);

    MU_ASSERT_EQUALS(succ1, 1, "d");
    MU_ASSERT_EQUALS(succ2, 0, "d");

    int64_t res1v = ctx->handle_to_sint64(ctx, res1);
    int64_t res2v = ctx->handle_to_sint64(ctx, res2);

    MU_ASSERT_EQUALS(res1v, 10LL, PRId64);
    MU_ASSERT_EQUALS(res2v, 4LL , PRId64);

    MuIntValue res3 = ctx->atomicrmw(ctx, MU_SEQ_CST, MU_ADD, r1i, I64_10);
    MuIntValue res4 = ctx->load(ctx, MU_SEQ_CST, r1i);

    int64_t res3v = ctx->handle_to_sint64(ctx, res3);
    int64_t res4v = ctx->handle_to_sint64(ctx, res4);

    MU_ASSERT_EQUALS(res3v, 4LL , PRId64);
    MU_ASSERT_EQUALS(res4v, 14LL, PRId64);

    ctx->fence(ctx, MU_SEQ_CST);

    ctx->close_context(ctx);

    return true;
Kunshan Wang's avatar
Kunshan Wang committed
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
}

void osr_bundle_trap_handler(MuCtx *ctx, MuThreadRefValue thread,
        MuStackRefValue stack, int wpid, MuTrapHandlerResult *result,
        MuStackRefValue *new_stack, MuValue **values, int *nvalues,
        MuValuesFreer *freer, MuCPtr *freerdata, MuRefValue *exception,
        MuCPtr userdata) {

    muprintf("Hi! I am the native trap handler!\n");

    MuFCRefValue *cursor = ctx->new_cursor(ctx, stack);
    MuID iid  = ctx->cur_inst(ctx, cursor);

    if (iid == ID("@sum_v1.opt.trap_opt")) {
        MuID fid  = ctx->cur_func(ctx, cursor);
        MuID fvid = ctx->cur_func_ver(ctx, cursor);
        MU_ASSERT_EQUALS_TRAP(fid , ID("@sum"), "d");
        MU_ASSERT_EQUALS_TRAP(fvid, ID("@sum_v1"), "d");

        MuValue kas[3];
        ctx->dump_keepalives(ctx, cursor, kas);

        int64_t nv = ctx->handle_to_sint64(ctx, kas[0]);
        int64_t iv = ctx->handle_to_sint64(ctx, kas[1]);
        int64_t sv = ctx->handle_to_sint64(ctx, kas[2]);

        MU_ASSERT_EQUALS_TRAP(nv, 8LL, PRId64);
        MU_ASSERT_EQUALS_TRAP(iv, 5LL, PRId64);
        MU_ASSERT_EQUALS_TRAP(sv, 10LL, PRId64);

        muprintf("Prepare to load new bundle...\n");

        {
            char bundle[4096];

            muprintf("Opening file...\n");
            FILE *fp = fopen("tests/uvm-refimpl-test/osr-tests-part2.uir", "r");
            muprintf("File opened.\n");

            if (fp == NULL) {
                perror("fp is NULL");
                exit(1);
            }

            int already_read = 0;

            while(!feof(fp)) {
                already_read += fread(bundle+already_read, 1, 4096, fp);
            }

            fclose(fp);

            muprintf("Loading bundle...\n");
            ctx->load_bundle(ctx, bundle, already_read);
        }

        muprintf("Loaded. Perform OSR...\n");

        muprintf("Popping...\n");

        ctx->next_frame(ctx, cursor);
        ctx->pop_frames_to(ctx, cursor);
        ctx->close_cursor(ctx, cursor);

        muprintf("Pushing...\n");

        MuFuncRefValue one_shot_func = ctx->handle_from_func(ctx, ID("@sum_osr_oneshot"));
        ctx->push_frame(ctx, stack, one_shot_func);

        MuValue *rebind_values = (MuValue*)malloc(sizeof(MuValue)*3);
        rebind_values[0] = kas[2];
        rebind_values[1] = kas[1];
        rebind_values[2] = kas[0];

        muprintf("Prepare to return from trap handler...\n");
        *result = MU_REBIND_PASS_VALUES;
        *new_stack = stack;
        *values = rebind_values;
        *nvalues = 3;
        *freer = malloc_freer;
        *freerdata = NULL;
        muprintf("Bye!\n");
    } else if (iid == ID("@osr_test_base_v1.entry.trap_base_exit")) {
        MuValue kas[1];
        ctx->dump_keepalives(ctx, cursor, kas);
        ctx->close_cursor(ctx, cursor);

        int64_t rv = ctx->handle_to_sint64(ctx, kas[0]);
        MU_ASSERT_EQUALS_TRAP(rv, 28LL, PRId64);

        muprintf("Prepare to return from trap handler...\n");
        *result = MU_REBIND_PASS_VALUES;
        *new_stack = stack;
        *values = NULL;
        *nvalues = 0;
        *freer = NULL;
        *freerdata = NULL;
        muprintf("Bye!\n");
    } else {
        muprintf("Unknown trap. ID: %d\n", iid);
        MuName trapName = NAME(iid);
        muprintf("name: %s\n", trapName);
        exit(1);
    }
}

bool test_osr(MuVM *mvm) {
    mvm->set_trap_handler(mvm, osr_bundle_trap_handler, NULL);
Kunshan Wang's avatar
Kunshan Wang committed
650

Kunshan Wang's avatar
Kunshan Wang committed
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
    MuCtx *ctx = mvm->new_context(mvm);

    {
        char bundle[4096];

        FILE *fp = fopen("tests/uvm-refimpl-test/osr-tests.uir", "r");
        int already_read = 0;

        while(!feof(fp)) {
            already_read += fread(bundle+already_read, 1, 4096, fp);
        }

        fclose(fp);

        ctx->load_bundle(ctx, bundle, already_read);
    }

    MuValue args[1];
    args[0] = ctx->handle_from_sint64(ctx, 8LL, 64);

    MuFuncRefValue func = ctx->handle_from_func(ctx, ID("@osr_test_base"));
    MuStackRefValue stack = ctx->new_stack(ctx, func);
    MuThreadRefValue thread = ctx->new_thread(ctx, stack, MU_REBIND_PASS_VALUES,
            args, 1, NULL);

    mvm->execute(mvm);

    ctx->close_context(ctx);

    return true;
Kunshan Wang's avatar
Kunshan Wang committed
681
}