fold/third_party/ghidra-decompiler/cpp/pcoderaw.cc
trvon 5152d0c9f7
feat(binary): vendor Ghidra 12.1 decompiler + validate P-Code lifting (Phase 0-1)
Foundation for replacing fold's dead/abstract binary encoder with a real
SSA P-Code frontend. Verified root-cause gap: serializeBinaryIR() emits only
data-discovery nodes (sections/strings/imports/exports); the control-flow
BinaryExprKind values (BasicBlock/CallSite/...) are never constructed, so the
Z3-BB encoder's encodable filter is always empty and encodeBinaryBBs never
runs — hence 0 binary Z3 findings. The P-Code pipeline supersedes that path.

Phase 0 — docs/internal/ghidra-pcode-integration-research.md: P-Code opcode
core, the decompiler C++ embedding API (LoadImage/SleighArchitecture/Funcdata),
the CORE+DECCORE+SLEIGH source sets, no-bison finding, and pypcode as build
reference.

Phase 1 — vendored Ghidra decompiler at pinned commit (Ghidra_12.1_build,
7e89d94e34...) under third_party/ghidra-decompiler/ (cpp + AArch64 specs +
Apache-2.0 LICENSE + VENDOR.md + .gitignore for build artifacts). Validated by
hand on this clang-21/macOS toolchain:
  - full SSA engine (CORE+DECCORE+SLEIGH, 80 files incl. heritage/funcdata/
    action) compiles clean, -std=c++11 -O2, NO BFD, NO bison (generated
    parsers are committed upstream; ruleparse isn't in the lib set);
  - sleigh compiler builds; AArch64 .slaspec -> .sla compiles;
  - tools/pcode_dump.cpp (custom LoadImage, no BFD) lifts the real arm64
    sym._div_by_zero_direct to correct P-Code: `mov w9,#0 -> COPY(const 0)`,
    `sdiv -> INT_SDIV(w8,w9)` — divisor zero-ness explicit in the data flow,
    so the Phase-3 encoder proves div-by-zero trivially, multi-arch.

Not yet meson-wired (next step) and no fold-side IR/encoder yet (Phase 2-3);
this commit is the validated vendor + smoke tool.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-05-31 17:32:47 -04:00

124 lines
4.1 KiB
C++

/* ###
* IP: GHIDRA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "pcoderaw.hh"
#include "translate.hh"
namespace ghidra {
/// Build this VarnodeData from an \<addr>, \<register>, or \<varnode> element.
/// \param decoder is the stream decoder
void VarnodeData::decode(Decoder &decoder)
{
uint4 elemId = decoder.openElement();
decodeFromAttributes(decoder);
decoder.closeElement(elemId);
}
/// Collect attributes for the VarnodeData possibly from amidst other attributes
/// \param decoder is the stream decoder
void VarnodeData::decodeFromAttributes(Decoder &decoder)
{
space = (AddrSpace *)0;
size = 0;
for(;;) {
uint4 attribId = decoder.getNextAttributeId();
if (attribId == 0)
break; // Its possible to have no attributes in an <addr/> tag
if (attribId == ATTRIB_SPACE) {
space = decoder.readSpace();
decoder.rewindAttributes();
offset = space->decodeAttributes(decoder,size);
break;
}
else if (attribId == ATTRIB_NAME) {
const Translate *trans = decoder.getAddrSpaceManager()->getDefaultCodeSpace()->getTrans();
const VarnodeData &point(trans->getRegister(decoder.readString()));
*this = point;
break;
}
}
}
/// Return \b true, if \b this, as an address range, contains the other address range
/// \param op2 is the other VarnodeData to test for containment
/// \return \b true if \b this contains the other
bool VarnodeData::contains(const VarnodeData &op2) const
{
if (space != op2.space) return false;
if (op2.offset < offset) return false;
if ((offset + (size-1)) < (op2.offset + (op2.size-1))) return false;
return true;
}
/// If \b this and \b lo form a contiguous range of bytes, where \b this makes up the most significant
/// bytes and \b lo makes up the least significant bytes, return \b true.
/// \param lo is the given VarnodeData to compare with
/// \return \b true if the two byte ranges are contiguous and in order
bool VarnodeData::isContiguous(const VarnodeData &lo) const
{
if (space != lo.space) return false;
if (space->isBigEndian()) {
uintb nextoff = space->wrapOffset(offset+size);
if (nextoff == lo.offset) return true;
}
else {
uintb nextoff = space->wrapOffset(lo.offset+lo.size);
if (nextoff == offset) return true;
}
return false;
}
/// This assumes the \<op> element is already open.
/// Decode info suitable for call to PcodeEmit::dump. The output pointer is changed to null if there
/// is no output for this op, otherwise the existing pointer is used to store the output.
/// \param decoder is the stream decoder
/// \param isize is the (preparsed) number of input parameters for the p-code op
/// \param invar is an array of storage for the input Varnodes
/// \param outvar is a (handle) to the storage for the output Varnode
/// \return the p-code op OpCode
OpCode PcodeOpRaw::decode(Decoder &decoder,int4 isize,VarnodeData *invar,VarnodeData **outvar)
{
OpCode opcode = (OpCode)decoder.readSignedInteger(ATTRIB_CODE);
uint4 subId = decoder.peekElement();
if (subId == ELEM_VOID) {
decoder.openElement();
decoder.closeElement(subId);
*outvar = (VarnodeData *)0;
}
else {
(*outvar)->decode(decoder);
}
for(int4 i=0;i<isize;++i) {
subId = decoder.peekElement();
if (subId == ELEM_SPACEID) {
decoder.openElement();
invar[i].space = decoder.getAddrSpaceManager()->getConstantSpace();
invar[i].offset = (uintb)(uintp)decoder.readSpace(ATTRIB_NAME);
invar[i].size = sizeof(void *);
decoder.closeElement(subId);
}
else
invar[i].decode(decoder);
}
return opcode;
}
} // End namespace ghidra