/* code to create classes C and D */ static void setup_classes() { #ifdef TAGGING pyobj zero = inject_int(0); pyobj one = inject_int(1); #else pyobj zero = create_int(0); pyobj one = create_int(1); #endif pyobj list0 = create_list(zero); pyobj list1 = create_list(one); // class C: // def m(self): // return self.f C = create_class(list0); // Add method m to the class. set_attr(C, "m", create_closure(C_m, list0)); // class D(C): // def m(self): // return self.f + 1 set_subscript(list1, zero, C); // list1[0] = C D = create_class(list1); pyobj D_m_closure = create_closure(D_m, list0); set_attr(D, "m", create_closure(D_m, list0)); pyobj D_n_closure = create_closure(D_n, list0); set_attr(D, "n", create_closure(D_n, list0)); }
DexClass* create_merger_class(const DexType* type, const DexType* super_type, const std::vector<DexField*>& merger_fields, const TypeSet& interfaces, bool add_type_tag_field, bool with_default_ctor /* false */) { always_assert(type && super_type); std::vector<DexField*> fields; if (add_type_tag_field) { auto type_tag_field = static_cast<DexField*>(DexField::make_field( type, DexString::make_string(INTERNAL_TYPE_TAG_FIELD_NAME), get_int_type())); type_tag_field->make_concrete(ACC_PUBLIC | ACC_FINAL); fields.push_back(type_tag_field); } for (auto f : merger_fields) { fields.push_back(f); } // Put merger class in the same package as super_type. auto pkg_name = get_merger_package_name(super_type); auto cls = create_class(type, super_type, pkg_name, fields, interfaces, with_default_ctor); TRACE(TERA, 3, " created merger class w/ fields %s \n", SHOW(cls)); return cls; }
ObjectPtr<Class> get_float_class() { static Value* root = NULL; if (!root) { ObjectPtr<Class> cls = create_class(snow::sym("Float"), get_numeric_class()); root = gc_create_root(cls); } return *root; }
void create_scope() { auto obj_t = DexType::make_type("Ljava/lang/Object;"); auto int_t = DexType::make_type("I"); auto string_t = DexType::make_type("Ljava/lang/String;"); auto a = DexType::make_type("A"); auto b = DexType::make_type("B"); auto c = DexType::make_type("C"); auto d = DexType::make_type("D"); auto u = DexType::make_type("U"); auto e = DexType::make_type("E"); auto cls_A = create_class(a, obj_t, {make_field_def(a, "f1", int_t, ACC_PUBLIC, true)}, ACC_PUBLIC, true); auto cls_B = create_class(b, a, {make_field_def(b, "f2", string_t, ACC_PUBLIC | ACC_STATIC)}); auto cls_C = create_class(c, b, {}); auto cls_D = create_class(d, obj_t, {make_field_def(d, "f", a)}); auto cls_E = create_class(e, obj_t, {}); }
ObjectPtr<Class> get_integer_class() { static Value* root = NULL; if (!root) { ObjectPtr<Class> cls = create_class(snow::sym("Integer"), get_numeric_class()); SN_DEFINE_METHOD(cls, "%", integer_modulo); SN_DEFINE_METHOD(cls, "~", integer_complement); root = gc_create_root(cls); } return *root; }
TEST(RenameMembers, rename) { g_redex = new RedexContext(); auto obj_t = DexType::make_type("Ljava/lang/Object;"); auto int_t = DexType::make_type("I"); auto a = DexType::make_type("A"); auto field = make_field_def(a, "wombat", int_t, ACC_PUBLIC, true); auto cls_A = create_class(a, obj_t, {field}, ACC_PUBLIC, true); std::string name_before = field->get_name()->c_str(); ASSERT_EQ("wombat", name_before); DexFieldSpec spec; spec.name = DexString::make_string("numbat"); field->change(spec); std::string name_after = field->get_name()->c_str(); ASSERT_EQ("numbat", name_after); }
ObjectPtr<Class> get_numeric_class() { static Value* root = NULL; if (!root) { ObjectPtr<Class> cls = create_class(snow::sym("Numeric"), NULL); SN_DEFINE_METHOD(cls, "+", numeric_add); SN_DEFINE_METHOD(cls, "-", numeric_subtract); SN_DEFINE_METHOD(cls, "*", numeric_multiply); SN_DEFINE_METHOD(cls, "/", numeric_divide); SN_DEFINE_METHOD(cls, "<", numeric_less_than); SN_DEFINE_METHOD(cls, "<=", numeric_less_than_or_equal); SN_DEFINE_METHOD(cls, ">", numeric_greater_than); SN_DEFINE_METHOD(cls, ">=", numeric_greater_than_or_equal); SN_DEFINE_METHOD(cls, "inspect", numeric_inspect); SN_DEFINE_METHOD(cls, "to_string", numeric_inspect); root = gc_create_root(cls); } return *root; }
int main (int argc, char *argv[]) { big_pyobj *A, *B, *C; big_pyobj *A_parents, *B_parents, *C_parents; big_pyobj *list; int i; /** * test1: simple class with no superclasses * class A: */ pymem_init(); A_parents = create_list(inject_int(0)); incref(A_parents); A = create_class(inject_big(A_parents)); incref(A); assert (A_parents->ref_ctr == 2); decref(A); assert (A_parents->ref_ctr == 1); decref(A_parents); pymem_print_stats(); pymem_shutdown(); /** * test2: class with one superclass * class A: * class B(A): * B -> B_parents -> A -> A_parents */ pymem_init(); A_parents = create_list(inject_int(0)); incref(A_parents); A = create_class(inject_big(A_parents)); incref(A); assert (A_parents->ref_ctr == 2); B_parents = create_list(inject_int(1)); incref(B_parents); set_subscript(inject_big(B_parents), inject_int(0), inject_big(A)); assert (A->ref_ctr == 2); B = create_class(inject_big(B_parents)); incref(B); assert (B_parents->ref_ctr == 2); decref(A_parents); assert (A_parents->ref_ctr == 1); decref(A); assert (A->ref_ctr == 1); decref(B_parents); assert (B_parents->ref_ctr == 1); pymem_print_stats(); decref(B); // everything should go away at this point. pymem_print_stats(); pymem_shutdown(); /** * test3: simple class with attribute. * list = [] * class A: * a = list */ pymem_init(); list = create_list(inject_int(0)); incref(list); A_parents = create_list(inject_int(0)); incref(A_parents); A = create_class(inject_big(A_parents)); incref(A); assert (A_parents->ref_ctr == 2); set_attr(inject_big(A), "a", inject_big(list)); decref(list); // the list should not go away here. assert (list->ref_ctr == 1); decref(A_parents); assert (A_parents->ref_ctr == 1); pymem_print_stats(); decref(A); // now everything should be de-allocated. pymem_print_stats(); pymem_shutdown(); }
int main (int argc, char *argv[]) { big_pyobj *A_parents; big_pyobj *A; big_pyobj *freevars; big_pyobj *f; big_pyobj *ubmethod; int i; /** * test1: retrieve unbound method from simple class * class A: * f = lambda x: x * dependencies: * f -> freevars * A -> A_parents * A -> f (via A.f) * ubmethod -> f * ubmethod -> A */ pymem_init(); freevars = create_list(inject_int(0)); incref(freevars); f = create_closure(dummy_function, inject_big(freevars)); incref(f); A_parents = create_list(inject_int(0)); incref(A_parents); A = create_class(inject_big(A_parents)); incref(A); assert (A_parents->ref_ctr == 2); set_attr(inject_big(A), "f", inject_big(f)); assert (f->ref_ctr == 2); ubmethod = project_big(get_attr(inject_big(A), "f")); incref(ubmethod); assert (A->ref_ctr == 2); assert (f->ref_ctr == 3); decref(freevars); assert (freevars->ref_ctr == 1); decref(f); assert (f->ref_ctr == 2); decref(A_parents); assert(A_parents->ref_ctr == 1); decref(A); assert(A->ref_ctr == 1); // at this point only the unbound method is keeping stuff alive. pymem_print_stats(); decref(ubmethod); pymem_print_stats(); pymem_shutdown(); }