Repository navigation
Which part of Ghidra's Java and/or C++ code is responsible for transforming array indexing in the C output?
#9689
|
I have code that looks like an implementation of something akin to When Ghidra attempts to access the nth element it currently displays (in the above, datatype I understand this is related somewhat to 16-bit near/far pointers (having to use constructs like If someone could point (oops) me in the right direction, maybe I could try to make it better! |
Replies: 3 comments 60 replies
|
There is a PR that tries to improve Ghidra's array indexing, you can find it below: It might help to get you in the right direction. |
|
it's all in the decompiler C++ side, Ghidra/Features/Decompiler/src/decompile/cpp/ the base + idx*size -> indexing part is mostly in ruleaction.cc. RulePtrArith is the main one, it turns pointer + scaled offset into PTRADD/PTRSUB when the base is a pointer to something of that size. RulePushPtr, RuleStructOffset0 and RulePtraddUndo/RulePtrsubUndo are worth looking at too. then printc.cc (PrintC::opPtradd / opPtrsub) decides if you get p + n or p[n]. the CONCAT22 is kind of a separate problem though. that's a PIECE op, it sees the offset and segment as two 16 bit halves and never figures out they're one far pointer. far pointers normally get handled through the segmentop in the x86 16 bit pspec + RuleSegment (segmentop.cc). if the load doesn't go through the segment userop you're stuck with CONCAT22(seg, off), and RulePtrArith only ever sees the 16 bit offset, which is why you get m_apData + nIdx but no real []. so if you want to hack on it i'd try a rule that folds PIECE(seg, off) back into a segmented pointer when both halves come from the same T *32 field, then the normal PTRADD stuff should just work. the PR DualTachyon linked touches the same code so read it next to RulePtrArith. |
|
Case 2 doesn't match because the high half keeps changing shape while the rules run. Your dump shows This rule does that and rewrites the PIECE as class RulePieceFarPointer : public Rule {
static Varnode *wholeFromLow(Varnode *vn);
static bool isHighOf(Varnode *vn,Varnode *whole);
public:
RulePieceFarPointer(const string &g) : Rule( g, 0, "piecefarpointer") {}
virtual Rule *clone(const ActionGroupList &grouplist) const {
if (!grouplist.contains(getGroup())) return (Rule *)0;
return new RulePieceFarPointer(getGroup());
}
virtual void getOpList(vector<uint4> &oplist) const { oplist.push_back(CPUI_PIECE); }
virtual int4 applyOp(PcodeOp *op,Funcdata &data);
};
Varnode *RulePieceFarPointer::wholeFromLow(Varnode *vn)
{
if (!vn->isWritten()) return (Varnode *)0;
PcodeOp *sub = vn->getDef();
if (sub->code() != CPUI_SUBPIECE) return (Varnode *)0;
if (sub->getIn(1)->getOffset() != 0) return (Varnode *)0;
Varnode *whole = sub->getIn(0);
if (whole->getSize() != 4 || whole->isFree()) return (Varnode *)0;
return whole;
}
bool RulePieceFarPointer::isHighOf(Varnode *vn,Varnode *whole)
{
for(int4 depth=0;depth<4;++depth) {
if (!vn->isWritten()) return false;
PcodeOp *op = vn->getDef();
OpCode opc = op->code();
if (opc == CPUI_INT_AND) {
Varnode *mask = op->getIn(1);
if (!mask->isConstant() || mask->getOffset() != 0xffff) return false;
vn = op->getIn(0);
continue;
}
if (opc == CPUI_INT_ZEXT || opc == CPUI_COPY) {
vn = op->getIn(0);
continue;
}
if (opc != CPUI_SUBPIECE) return false;
Varnode *in = op->getIn(0);
uintb off = op->getIn(1)->getOffset();
if (off == 2)
return (in == whole);
if (off != 0) return false;
if (in == whole) return false; // Low half, not high half
if (in->isWritten()) {
PcodeOp *shift = in->getDef();
if ((shift->code() == CPUI_INT_RIGHT || shift->code() == CPUI_INT_SRIGHT) &&
shift->getIn(0) == whole && shift->getIn(1)->isConstant() && shift->getIn(1)->getOffset() == 16)
return true;
}
vn = in; // SUBPIECE(ZEXT(...),0) and the like: keep looking
}
return false;
}
int4 RulePieceFarPointer::applyOp(PcodeOp *op,Funcdata &data)
{
Varnode *hi = op->getIn(0);
Varnode *lo = op->getIn(1);
if (hi->getSize() != 2 || lo->getSize() != 2) return 0;
Architecture *glb = data.getArch();
SegmentOp *segdef = glb->userops.getSegmentOp(glb->getDefaultDataSpace()->getIndex());
if (segdef == (SegmentOp *)0 || !segdef->hasFarPointerSupport()) return 0;
if (!lo->isWritten()) return 0;
PcodeOp *addop = lo->getDef();
if (addop->code() != CPUI_INT_ADD && addop->code() != CPUI_PTRSUB) return 0;
Varnode *cvn = addop->getIn(1);
if (!cvn->isConstant()) return 0;
Varnode *whole = wholeFromLow(addop->getIn(0));
if (whole == (Varnode *)0) return 0;
if (!isHighOf(hi,whole)) return 0;
data.opSetOpcode(op,CPUI_INT_ADD);
data.opSetInput(op,whole,0);
data.opSetInput(op,data.newConstant(4,cvn->getOffset()),1);
return 1;
}Registered in universalAction (coreaction.cc), after RuleSegment: actprop->addRule( new RulePieceFarPointer("segment") );It only runs when the default data space has a segmentop with farpointer="yes" (16-bit x86), so other languages are unaffected. The carry out of the 16-bit offset is dropped, which is what a far pointer does anyway. Your GetModuleName, rebuilt from the listing, Ghidra 12.2, x86:LE:16:Protected Mode:
Case 1: the COPY changes size, a 4-byte output from a 2-byte input. Use CPUI_INT_ZEXT there instead of CPUI_COPY. |
1088:33d2: stock does the same. ~DumpVocabulary's
bis a 1-byte enum at stack 0x8, so the call reads 1 byte from the slot where SS was pushed. Stack heritage then splits every write to that slot into bytes, incl. the push ss. A pushed arg is 2 bytes; w/bas a 2-byte type:1098:0000 fixed. Full diff vs master 42db038 attached; it replaces the earlier file. Three changes: