+#ifdef LUAJIT_MCODE_TEST
+/* Test wrapper for mcode allocation. DO NOT ENABLE in production! Try:
+** LUAJIT_MCODE_TEST=hhhhhhhhhhhhhhhh luajit -jv main.lua
+** LUAJIT_MCODE_TEST=F luajit -jv main.lua
+*/
+static void *mcode_alloc_at_TEST(jit_State *J, uintptr_t hint, size_t sz, int prot)
+{
+ static int test_ofs = 0;
+ static const char *test_str;
+ if (!test_str) {
+ test_str = getenv("LUAJIT_MCODE_TEST");
+ if (!test_str) test_str = "";
+ }
+ switch (test_str[test_ofs]) {
+ case 'a': /* OK for one allocation. */
+ test_ofs++;
+ /* fallthrough */
+ case '\0': /* EOS: OK for any further allocations. */
+ break;
+ case 'h': /* Ignore one hint. */
+ test_ofs++;
+ /* fallthrough */
+ case 'H': /* Ignore any further hints. */
+ hint = 0u;
+ break;
+ case 'r': /* Randomize one hint. */
+ test_ofs++;
+ /* fallthrough */
+ case 'R': /* Randomize any further hints. */
+ hint = lj_prng_u64(&J2G(J)->prng) & ~(uintptr_t)0xffffu;
+ hint &= ((uintptr_t)1 << (LJ_64 ? 47 : 31)) - 1;
+ break;
+ case 'f': /* Fail one allocation. */
+ test_ofs++;
+ /* fallthrough */
+ default: /* 'F' or unknown: Fail any further allocations. */
+ return NULL;
+ }
+ return mcode_alloc_at(hint, sz, prot);
+}
+#define mcode_alloc_at(hint, sz, prot) mcode_alloc_at_TEST(J, hint, sz, prot)
+#endif
+