ddc-dec 0.1.12

DEX → Java decompiler front-end over jdc-core
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
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
//! The DEX front-end's implementation of `jdc_core::Ctx`.
//!
//! Every method here answers a *semantic* question about class metadata
//! using the pooled DEX class table; generics are absent (erased), so all
//! generic-aware queries answer "unknown" and the core degrades gracefully.

use jdc_core::ir::expr::{Expr, TypeRef};
use jdc_core::types::{
    ClassAccessFlags, FieldAccessFlags, GenericType, JavaType, MethodAccessFlags, MethodDescriptor,
};
use jdc_core::{Ctx, Family, NestedClass, NestedKind};

use crate::access::*;
use crate::{desc_type, DexPool, PoolClass};

/// Front-end context handed to the core's structurer and printer.
pub struct DexCtx<'a> {
    pub pool: &'a DexPool,
    pub class: &'a PoolClass,
    /// Field display names visible in this class's lexical scope by
    /// INHERITANCE or ENCLOSURE (supers/interfaces of the class and of
    /// every `$`-outer, transitively). `declares_field` only saw own
    /// fields: an inherited int `h` captured the class ref `h.e`
    /// (无法取消引用int, weixin v2/j family) because the shadow check
    /// missed it and the simple render stayed unqualified.
    inherited_fields: jdc_core::FxHashSet<String>,
}

impl<'a> DexCtx<'a> {
    pub fn new(pool: &'a DexPool, class: &'a PoolClass) -> Self {
        let inherited_fields = {
            let mut set: jdc_core::FxHashSet<String> = jdc_core::FxHashSet::default();
            let mut level = class.name.clone();
            loop {
                let mut queue = vec![level.clone()];
                let mut seen: jdc_core::FxHashSet<String> =
                    jdc_core::FxHashSet::default();
                let mut hops = 0u32;
                while let Some(c) = queue.pop() {
                    if hops >= 64 || !seen.insert(c.clone()) {
                        continue;
                    }
                    hops += 1;
                    let Some(pc) = pool.get(&c) else { continue };
                    if c != class.name {
                        for f in pc.static_fields.iter().chain(pc.instance_fields.iter()) {
                            set.insert(crate::classdec::java_ident(&f.name).into_owned());
                        }
                    }
                    if let Some(sup) = &pc.super_name {
                        queue.push(sup.clone());
                    }
                    queue.extend(pc.interfaces.iter().cloned());
                }
                let Some(i) = level.rfind('$') else { break };
                level.truncate(i);
            }
            set
        };
        DexCtx { pool, class, inherited_fields }
    }

    fn find_class(&self, internal: &str) -> Option<&PoolClass> {
        self.pool.get(internal)
    }

    /// The javac-convention enclosing field: named `this$0` AND typed
    /// as the outer. The pure type match over-fires on STATIC nested
    /// classes that legitimately hold an outer-typed field (wcdb
    /// CancellationSignal$Transport's mCancellationSignal — rendered as
    /// an inner class, `new Transport(null)` from a static method is
    /// "需要包含...的封闭实例"). An obfuscated-renamed this$0 falls to
    /// the static render, which stays compilable: the ctor keeps its
    /// outer param and every call site passes it explicitly.
    fn holds_this0(&self, internal: &str, pc: &PoolClass) -> bool {
        let Some(outer) = self.find_outer(internal) else {
            return false;
        };
        pc.instance_fields.iter().any(|f| {
            f.name == "this$0"
                && f
                    .desc
                    .strip_prefix('L')
                    .and_then(|d| d.strip_suffix(';'))
                    .is_some_and(|ty| ty == outer)
        })
    }

    /// Nesting evidence (see `find_outer_name`).
    fn outer_of(&self, internal: &str) -> Option<String> {
        crate::find_outer_name(self.pool, internal)
    }

    fn class_access_flags(&self, internal: &str) -> ClassAccessFlags {
        let mut f = ClassAccessFlags::empty();
        let Some(pc) = self.find_class(internal) else {
            return f;
        };
        let a = pc.access;
        if a & ACC_PUBLIC != 0 {
            f |= ClassAccessFlags::PUBLIC;
        }
        if a & ACC_FINAL != 0 {
            f |= ClassAccessFlags::FINAL;
        }
        if a & ACC_INTERFACE != 0 {
            f |= ClassAccessFlags::INTERFACE;
        }
        if a & ACC_ABSTRACT != 0 {
            f |= ClassAccessFlags::ABSTRACT;
        }
        if a & ACC_SYNTHETIC != 0 {
            f |= ClassAccessFlags::SYNTHETIC;
        }
        if a & ACC_ANNOTATION != 0 {
            f |= ClassAccessFlags::ANNOTATION;
        }
        if a & ACC_ENUM != 0 {
            f |= ClassAccessFlags::ENUM;
        }
        f
    }
}

impl<'a> Ctx for DexCtx<'a> {
    fn pool_id(&self) -> u64 {
        // Printer::shorten memo cache key: every DexCtx in a run shares
        // one DexPool, and shorten's ctx queries (has_class/find_outer/
        // class_bases) are pool-level — one cache entry set per pool.
        self.pool as *const DexPool as u64
    }

    fn obscured_simple(&self, internal: &str) -> Option<String> {
        crate::classdec::obscured_render_pub(internal)
    }

    fn class_name(&self) -> &str {
        &self.class.name
    }

    fn source_level(&self) -> u16 {
        52
    }

    fn find_outer(&self, internal: &str) -> Option<String> {
        if internal == self.class.name {
            return self.outer_of(internal);
        }
        self.outer_of(internal)
    }

    fn family(&self, root: &str) -> Family {
        let mut fam = Family {
            root: root.to_string(),
            ..Default::default()
        };
        // Cached child index: BFS over the `$` chain from `root`.
        let mut queue: std::collections::VecDeque<String> =
            self.pool.children_of(root).iter().cloned().collect();
        let mut seen: jdc_core::FxHashSet<String> = queue.iter().cloned().collect();
        while let Some(name) = queue.pop_front() {
            for c in self.pool.children_of(&name) {
                if seen.insert(c.clone()) {
                    queue.push_back(c.clone());
                }
            }
            let Some(_) = self.find_class(&name) else {
                continue;
            };
            let root_prefix = format!("{}$", root);
            let rest: String = if name.starts_with(&root_prefix) {
                name[root_prefix.len()..].to_string()
            } else {
                name.rsplit('$').next().unwrap_or(&name).to_string()
            };
            let simple = rest.rsplit('$').next().unwrap_or(&rest).to_string();
            let kind = classify_nested(&rest);
            let access = self.class_access_flags(&name);
            fam.nested.insert(
                name.clone(),
                NestedClass {
                    name: name.clone(),
                    simple,
                    kind,
                    access,
                    sig_header: None,
                },
            );
            match kind {
                NestedKind::Anonymous => {
                    fam.anonymous.insert(name.clone());
                }
                NestedKind::Local => {
                    fam.locals.insert(name.clone());
                }
                NestedKind::Lambda => {
                    fam.lambdas.insert(name.clone());
                }
                NestedKind::Member => {}
            }
        }
        fam
    }

    fn nested_is_static(&self, internal: &str) -> bool {
        match self.find_class(internal) {
            // ACC_STATIC (annotation evidence) OR the structural signal:
            // no instance field typed as the outer class. javac ALWAYS
            // gives a non-static inner class an enclosing-instance field
            // (this$0), and the field TYPE survives obfuscation that
            // renames the field itself. Plain d8 output carries no
            // nesting annotations at all — without the structural
            // fallback every static nested class rendered as an inner
            // one (`str.new Report(...)` swallowing the first ctor arg).
            Some(pc) => pc.is_static_nested() || !self.holds_this0(internal, pc),
            None => true,
        }
    }

    fn class_has_this0(&self, internal: &str) -> bool {
        // Inner classes without ACC_STATIC carry an enclosing instance.
        match self.find_class(internal) {
            Some(pc) => !pc.is_static_nested() && self.holds_this0(internal, pc),
            None => false,
        }
    }

    fn is_subtype_of(&self, sub: &JavaType, sup: &str) -> bool {
        if let JavaType::Object(n) = sub {
            self.pool.is_subtype(n, sup)
        } else {
            false
        }
    }

    fn is_interface(&self, internal: &str) -> bool {
        self.find_class(internal)
            .map(|pc| pc.is_interface())
            .unwrap_or(false)
    }

    fn is_sealed(&self, _internal: &str) -> bool {
        false
    }

    fn super_name(&self, internal: &str) -> Option<String> {
        self.find_class(internal)
            .and_then(|pc| pc.super_name.clone())
    }

    fn has_class(&self, internal: &str) -> bool {
        self.pool.get(internal).is_some() || internal == self.class.name
    }

    fn class_supers_args(
        &self,
        internal: &str,
        _args: &[GenericType],
    ) -> Vec<(String, Vec<GenericType>)> {
        let mut out = Vec::new();
        let mut cur = self.find_class(internal).map(|pc| pc.name.clone());
        let mut hops = 0;
        while let Some(name) = cur {
            hops += 1;
            if hops > 64 {
                break;
            }
            if name == "java/lang/Object" {
                break;
            }
            out.push((name.clone(), Vec::new()));
            cur = self.find_class(&name).and_then(|pc| pc.super_name.clone());
        }
        out
    }

    fn class_bases(&self, internal: &str) -> Option<(Vec<String>, Option<String>)> {
        let pc = self.find_class(internal)?;
        Some((pc.interfaces.clone(), pc.super_name.clone()))
    }

    fn field_flags(&self, internal: &str, name: &str) -> Option<FieldAccessFlags> {
        let pc = self.find_class(internal)?;
        let raw = pc
            .static_fields
            .iter()
            .chain(pc.instance_fields.iter())
            .find(|f| f.name == name)?
            .access;
        Some(raw_field_flags(raw))
    }

    fn method_flags(&self, internal: &str, name: &str, desc: &str) -> Option<MethodAccessFlags> {
        let pc = self.find_class(internal)?;
        let m = pc.find_method(name, desc)?;
        Some(raw_method_flags(m.access))
    }

    fn declares_field(&self, internal: &str, name: &str) -> bool {
        if internal == self.class.name {
            return self.inherited_fields.contains(name)
                || self.class.field_flags_of(name).is_some();
        }
        self.find_class(internal)
            .map(|pc| pc.field_flags_of(name).is_some())
            .unwrap_or(false)
    }

    fn declares_method_named(&self, internal: &str, name: &str) -> bool {
        self.find_class(internal)
            .map(|pc| pc.all_methods().any(|m| &*m.name == name))
            .unwrap_or(false)
    }

    fn is_generic_call(&self, _e: &Expr) -> bool {
        false
    }

    fn generic_call_formals(&self, _e: &Expr) -> Option<(Vec<GenericType>, Vec<String>)> {
        None
    }

    fn polymorphic_ret_cast(
        &self,
        _cls: &str,
        _name: &str,
        _desc: &MethodDescriptor,
    ) -> Option<JavaType> {
        None
    }

    fn ctor_formals_by_arity(&self, internal: &str, arity: usize) -> Option<Vec<GenericType>> {
        let pc = self.find_class(internal)?;
        let ctors = pc.ctors_by_arity(arity);
        let m = ctors.first()?;
        let md = m.parsed_desc()?;
        Some(md.args.iter().map(java_type_to_generic).collect())
    }

    fn ctor_param_types(
        &self,
        internal: &str,
        skip: usize,
        n: usize,
        args: &[Expr],
    ) -> Option<Vec<JavaType>> {
        let _ = args;
        let pc = self.find_class(internal)?;
        let ctors = pc.ctors_by_arity(skip + n);
        let m = ctors.first()?;
        let md = m.parsed_desc()?;
        Some(md.args.iter().skip(skip).cloned().collect())
    }

    fn outer_param_via_super(&self, _internal: &str) -> bool {
        false
    }

    fn nested_method(
        &self,
        _l: &jdc_core::ir::expr::LambdaExpr,
        _outer_vt: &jdc_core::var::VarTable,
    ) -> Option<jdc_core::MethodBody> {
        None
    }
}

/// `$`-suffix classification (javac conventions d8 preserves).
fn classify_nested(rest: &str) -> NestedKind {
    // rest is the text after `Outer$`.
    let tail = rest.rsplit('$').next().unwrap_or(rest);
    if rest.starts_with("-$$Lambda$") || tail.starts_with("-$$Lambda$") {
        return NestedKind::Lambda;
    }
    if !tail.is_empty() && tail.chars().all(|c| c.is_ascii_digit()) {
        return NestedKind::Anonymous;
    }
    if tail.starts_with(|c: char| c.is_ascii_digit()) {
        return NestedKind::Local;
    }
    NestedKind::Member
}

fn raw_field_flags(raw: u32) -> FieldAccessFlags {
    let mut f = FieldAccessFlags::empty();
    if raw & ACC_PUBLIC != 0 {
        f |= FieldAccessFlags::PUBLIC;
    }
    if raw & ACC_PRIVATE != 0 {
        f |= FieldAccessFlags::PRIVATE;
    }
    if raw & ACC_PROTECTED != 0 {
        f |= FieldAccessFlags::PROTECTED;
    }
    if raw & ACC_STATIC != 0 {
        f |= FieldAccessFlags::STATIC;
    }
    if raw & ACC_FINAL != 0 {
        f |= FieldAccessFlags::FINAL;
    }
    if raw & ACC_SYNTHETIC != 0 {
        f |= FieldAccessFlags::SYNTHETIC;
    }
    if raw & ACC_ENUM != 0 {
        f |= FieldAccessFlags::ENUM;
    }
    f
}

fn raw_method_flags(raw: u32) -> MethodAccessFlags {
    let mut f = MethodAccessFlags::empty();
    if raw & ACC_PUBLIC != 0 {
        f |= MethodAccessFlags::PUBLIC;
    }
    if raw & ACC_PRIVATE != 0 {
        f |= MethodAccessFlags::PRIVATE;
    }
    if raw & ACC_PROTECTED != 0 {
        f |= MethodAccessFlags::PROTECTED;
    }
    if raw & ACC_STATIC != 0 {
        f |= MethodAccessFlags::STATIC;
    }
    if raw & ACC_FINAL != 0 {
        f |= MethodAccessFlags::FINAL;
    }
    if raw & ACC_SYNCHRONIZED != 0 || raw & ACC_DECLARED_SYNCHRONIZED != 0 {
        f |= MethodAccessFlags::SYNCHRONIZED;
    }
    if raw & ACC_BRIDGE != 0 {
        f |= MethodAccessFlags::BRIDGE;
    }
    if raw & ACC_VARARGS != 0 {
        f |= MethodAccessFlags::VARARGS;
    }
    if raw & ACC_NATIVE != 0 {
        f |= MethodAccessFlags::NATIVE;
    }
    if raw & ACC_ABSTRACT != 0 {
        f |= MethodAccessFlags::ABSTRACT;
    }
    if raw & ACC_STRICT != 0 {
        f |= MethodAccessFlags::STRICT;
    }
    if raw & ACC_SYNTHETIC != 0 {
        f |= MethodAccessFlags::SYNTHETIC;
    }
    f
}

/// Erased JavaType → generic type view (no signature data in DEX).
pub fn java_type_to_generic(t: &JavaType) -> GenericType {
    match t {
        JavaType::Object(n) => GenericType::Class(class_sig_of(n)),
        JavaType::Array(inner) => GenericType::Array(Box::new(java_type_to_generic(inner))),
        other => GenericType::Primitive(other.primitive_char().unwrap_or('V')),
    }
}

fn class_sig_of(internal: &str) -> jdc_core::types::ClassSig {
    let (package, name) = match internal.rfind('/') {
        Some(i) => (internal[..i].to_string(), internal[i + 1..].to_string()),
        None => (String::new(), internal.to_string()),
    };
    jdc_core::types::ClassSig {
        package,
        parts: vec![jdc_core::types::ClassSigPart { name, args: vec![] }],
    }
}

/// Render a type reference for signatures where the printer is not used.
pub fn type_ref_of(desc: &str) -> TypeRef {
    TypeRef::J(desc_type(desc))
}