void testEndpoint () { testcase ("Endpoint"); { std::pair <Endpoint, bool> result ( Endpoint::from_string_checked ("1.2.3.4")); expect (result.second); if (expect (result.first.address().is_v4 ())) { expect (result.first.address().to_v4() == AddressV4 (1, 2, 3, 4)); expect (result.first.port() == 0); expect (to_string (result.first) == "1.2.3.4"); } } { std::pair <Endpoint, bool> result ( Endpoint::from_string_checked ("1.2.3.4:5")); expect (result.second); if (expect (result.first.address().is_v4 ())) { expect (result.first.address().to_v4() == AddressV4 (1, 2, 3, 4)); expect (result.first.port() == 5); expect (to_string (result.first) == "1.2.3.4:5"); } } Endpoint ep; ep = Endpoint (AddressV4 (127,0,0,1), 80); expect (! is_unspecified (ep)); expect (! is_public (ep)); expect ( is_private (ep)); expect (! is_multicast (ep)); expect ( is_loopback (ep)); expect (to_string (ep) == "127.0.0.1:80"); ep = Endpoint (AddressV4 (10,0,0,1)); expect (AddressV4::get_class (ep.to_v4()) == 'A'); expect (! is_unspecified (ep)); expect (! is_public (ep)); expect ( is_private (ep)); expect (! is_multicast (ep)); expect (! is_loopback (ep)); expect (to_string (ep) == "10.0.0.1"); ep = Endpoint (AddressV4 (166,78,151,147)); expect (! is_unspecified (ep)); expect ( is_public (ep)); expect (! is_private (ep)); expect (! is_multicast (ep)); expect (! is_loopback (ep)); expect (to_string (ep) == "166.78.151.147"); }
void Field::check_access_by(InstanceClass* sender_class, InstanceClass* static_receiver_class, FailureMode fail_mode JVM_TRAPS) { InstanceClass* field_class = ic(); if (is_public()) { return; } if (field_class->equals(sender_class)) { return; } if (!is_private()) { if (field_class->is_same_class_package(sender_class)) { return; } if (is_protected()) { if (sender_class->is_subclass_of(field_class)) { if (static_receiver_class->equals(sender_class) || static_receiver_class->is_subclass_of(sender_class) || sender_class->is_subclass_of(static_receiver_class)) { return; } } } } Throw::illegal_access(fail_mode JVM_NO_CHECK_AT_BOTTOM); }
/** * The callee contains an invoke to a virtual method we either do not know * or it's not public. Given the caller may not be in the same * hierarchy/package we cannot inline it unless we make the method public. * But we need to make all methods public across the hierarchy and for methods * we don't know we have no idea whether the method was public or not anyway. */ bool MultiMethodInliner::unknown_virtual(DexInstruction* insn, DexMethod* context) { if (insn->opcode() == OPCODE_INVOKE_VIRTUAL || insn->opcode() == OPCODE_INVOKE_VIRTUAL_RANGE) { auto method = static_cast<DexOpcodeMethod*>(insn)->get_method(); auto res_method = resolver(method, MethodSearch::Virtual); if (res_method == nullptr) { // if it's not known to redex but it's a common java/android API method if (method_ok(method->get_class(), method)) { return false; } auto type = method->get_class(); if (type_ok(type)) return false; // the method ref is bound to a type known to redex but the method does // not exist in the hierarchy known to redex. Essentially the method // is from an external type i.e. A.equals(Object) auto cls = type_class(type); while (cls != nullptr) { type = cls->get_super_class(); cls = type_class(type); } if (type_ok(type)) return false; if (method_ok(type, method)) return false; assert(track(method)); info.escaped_virtual++; return true; } if (res_method->is_external() && !is_public(res_method)) { info.non_pub_virtual++; return true; } } return false; }
// @Override virtual void debug_print(int indent) override { print_space(indent); if (is_public()) printf("public "); else if (is_protected()) printf("protected "); else if (is_private()) printf("private "); if (is_static()) printf("static "); if (is_final()) printf("final "); }
void AccessFlags::print_on(outputStream* st) const { if (is_public ()) st->print("public " ); if (is_private ()) st->print("private " ); if (is_protected ()) st->print("protected " ); if (is_static ()) st->print("static " ); if (is_final ()) st->print("final " ); if (is_synchronized()) st->print("synchronized "); if (is_volatile ()) st->print("volatile " ); if (is_transient ()) st->print("transient " ); if (is_native ()) st->print("native " ); if (is_interface ()) st->print("interface " ); if (is_abstract ()) st->print("abstract " ); if (is_strict ()) st->print("strict " ); if (is_synthetic ()) st->print("synthetic " ); if (is_old ()) st->print("{old} " ); if (is_obsolete ()) st->print("{obsolete} " ); }
/** * The callee contains a *get/put instruction to an unknown field. * Given the caller may not be in the same hierarchy/package we cannot inline * it unless we make the field public. * But we need to make all fields public across the hierarchy and for fields * we don't know we have no idea whether the field was public or not anyway. */ bool MultiMethodInliner::unknown_field(DexInstruction* insn, DexMethod* context) { if (is_ifield_op(insn->opcode()) || is_sfield_op(insn->opcode())) { auto fop = static_cast<DexOpcodeField*>(insn); auto field = fop->field(); field = resolve_field(field, is_sfield_op(insn->opcode()) ? FieldSearch::Static : FieldSearch::Instance); if (field == nullptr) { info.escaped_field++; return true; } if (!field->is_concrete() && !is_public(field)) { info.non_pub_field++; return true; } } return false; }
// ------------------------------------------------------------------ // ciFlags::print_klass_flags void ciFlags::print_klass_flags(outputStream* st) { if (is_public()) { st->print("public"); } else { st->print("DEFAULT_ACCESS"); } if (is_final()) { st->print(",final"); } if (is_super()) { st->print(",super"); } if (is_interface()) { st->print(",interface"); } if (is_abstract()) { st->print(",abstract"); } }
/** * Check if a visibility/accessibility change would turn a method referenced * in a callee to virtual methods as they are inlined into the caller. * That is, once a callee is inlined we need to ensure that everything that was * referenced by a callee is visible and accessible in the caller context. * This step would not be needed if we changed all private instance to static. */ bool MultiMethodInliner::create_vmethod(DexInstruction* insn) { auto opcode = insn->opcode(); if (opcode == OPCODE_INVOKE_DIRECT || opcode == OPCODE_INVOKE_DIRECT_RANGE) { auto method = static_cast<DexOpcodeMethod*>(insn)->get_method(); method = resolver(method, MethodSearch::Direct); if (method == nullptr) { info.need_vmethod++; return true; } always_assert(method->is_def()); if (is_init(method)) { if (!method->is_concrete() && !is_public(method)) { info.non_pub_ctor++; return true; } // concrete ctors we can handle because they stay invoke_direct return false; } info.need_vmethod++; return true; } return false; }
// ------------------------------------------------------------------ // ciFlags::print_member_flags void ciFlags::print_member_flags(outputStream* st) { if (is_public()) { st->print("public"); } else if (is_private()) { st->print("private"); } else if (is_protected()) { st->print("protected"); } else { st->print("DEFAULT_ACCESS"); } if (is_static()) { st->print(",static"); } if (is_final()) { st->print(",final"); } if (is_synchronized()) { st->print(",synchronized"); } if (is_volatile()) { st->print(",volatile"); } if (is_transient()) { st->print(",transient"); } if (is_native()) { st->print(",native"); } if (is_abstract()) { st->print(",abstract"); } if (is_strict()) { st->print(",strict"); } }
// Check that visibility / accessibility changes to the current method // won't need to change a referenced method into a virtual or static one. bool gather_invoked_methods_that_prevent_relocation( const DexMethod* method, std::unordered_set<DexMethodRef*>* methods_preventing_relocation) { auto code = method->get_code(); always_assert(code); bool can_relocate = true; for (const auto& mie : InstructionIterable(code)) { auto insn = mie.insn; auto opcode = insn->opcode(); if (is_invoke(opcode)) { auto meth = resolve_method(insn->get_method(), opcode_to_search(insn)); if (!meth && opcode == OPCODE_INVOKE_VIRTUAL && unknown_virtuals::is_method_known_to_be_public(insn->get_method())) { continue; } if (meth) { always_assert(meth->is_def()); if (meth->is_external() && !is_public(meth)) { meth = nullptr; } else if (opcode == OPCODE_INVOKE_DIRECT && !is_init(meth)) { meth = nullptr; } } if (!meth) { can_relocate = false; if (!methods_preventing_relocation) { break; } methods_preventing_relocation->emplace(insn->get_method()); } } } return can_relocate; }
void testEndpoint () { testcase ("Endpoint"); { std::pair <Endpoint, bool> result ( Endpoint::from_string_checked ("1.2.3.4")); expect (result.second); if (expect (result.first.address().is_v4 ())) { expect (result.first.address().to_v4() == AddressV4 (1, 2, 3, 4)); expect (result.first.port() == 0); expect (to_string (result.first) == "1.2.3.4"); } } { std::pair <Endpoint, bool> result ( Endpoint::from_string_checked ("1.2.3.4:5")); expect (result.second); if (expect (result.first.address().is_v4 ())) { expect (result.first.address().to_v4() == AddressV4 (1, 2, 3, 4)); expect (result.first.port() == 5); expect (to_string (result.first) == "1.2.3.4:5"); } } Endpoint ep; ep = Endpoint (AddressV4 (127,0,0,1), 80); expect (! is_unspecified (ep)); expect (! is_public (ep)); expect ( is_private (ep)); expect (! is_multicast (ep)); expect ( is_loopback (ep)); expect (to_string (ep) == "127.0.0.1:80"); ep = Endpoint (AddressV4 (10,0,0,1)); expect (AddressV4::get_class (ep.to_v4()) == 'A'); expect (! is_unspecified (ep)); expect (! is_public (ep)); expect ( is_private (ep)); expect (! is_multicast (ep)); expect (! is_loopback (ep)); expect (to_string (ep) == "10.0.0.1"); ep = Endpoint (AddressV4 (166,78,151,147)); expect (! is_unspecified (ep)); expect ( is_public (ep)); expect (! is_private (ep)); expect (! is_multicast (ep)); expect (! is_loopback (ep)); expect (to_string (ep) == "166.78.151.147"); { ep = Endpoint::from_string ("192.0.2.112"); expect (! is_unspecified (ep)); expect (ep == Endpoint::from_string_altform ("192.0.2.112")); auto const ep1 = Endpoint::from_string ("192.0.2.112:2016"); expect (! is_unspecified (ep1)); expect (ep.address() == ep1.address()); expect (ep1.port() == 2016); auto const ep2 = Endpoint::from_string_altform ("192.0.2.112:2016"); expect (! is_unspecified (ep2)); expect (ep.address() == ep2.address()); expect (ep2.port() == 2016); expect (ep1 == ep2); auto const ep3 = Endpoint::from_string_altform ("192.0.2.112 2016"); expect (! is_unspecified (ep3)); expect (ep.address() == ep3.address()); expect (ep3.port() == 2016); expect (ep2 == ep3); auto const ep4 = Endpoint::from_string_altform ("192.0.2.112 2016"); expect (! is_unspecified (ep4)); expect (ep.address() == ep4.address()); expect (ep4.port() == 2016); expect (ep3 == ep4); expect (to_string(ep1) == to_string(ep2)); expect (to_string(ep1) == to_string(ep3)); expect (to_string(ep1) == to_string(ep4)); } // Failures: expect (is_unspecified ( Endpoint::from_string ("192.0.2.112:port"))); expect (is_unspecified ( Endpoint::from_string_altform ("192.0.2.112:port"))); expect (is_unspecified ( Endpoint::from_string_altform ("192.0.2.112 port"))); expect (is_unspecified ( Endpoint::from_string ("ip:port"))); expect (is_unspecified ( Endpoint::from_string_altform ("ip:port"))); expect (is_unspecified ( Endpoint::from_string_altform ("ip port"))); expect (is_unspecified ( Endpoint::from_string(""))); expect (is_unspecified ( Endpoint::from_string_altform(""))); expect (is_unspecified ( Endpoint::from_string("255"))); expect (is_unspecified ( Endpoint::from_string_altform("255"))); expect (is_unspecified ( Endpoint::from_string("512"))); expect (is_unspecified ( Endpoint::from_string_altform("512"))); expect (is_unspecified ( Endpoint::from_string("1.2.3.256"))); expect (is_unspecified ( Endpoint::from_string_altform("1.2.3.256"))); expect (is_unspecified ( Endpoint::from_string("1.2.3:80"))); expect (is_unspecified ( Endpoint::from_string_altform("1.2.3:80"))); expect (is_unspecified ( Endpoint::from_string_altform("1.2.3 80"))); expect (is_unspecified ( Endpoint::from_string("1.2.3.4:65536"))); expect (is_unspecified ( Endpoint::from_string_altform("1.2.3:65536"))); expect (is_unspecified ( Endpoint::from_string_altform("1.2.3 65536"))); expect (is_unspecified ( Endpoint::from_string("1.2.3.4:89119"))); expect (is_unspecified ( Endpoint::from_string_altform("1.2.3:89119"))); expect (is_unspecified ( Endpoint::from_string_altform("1.2.3 89119"))); }