fold/third_party/ghidra-decompiler/cpp/pcodeparse.hh
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

101 lines
3.4 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.
*/
#ifndef __PCODEPARSE_HH__
#define __PCODEPARSE_HH__
#include "pcodecompile.hh"
#include "sleighbase.hh"
namespace ghidra {
// Classes for compiling a standalone snippet of pcode, given an existing sleigh language
struct IdentRec {
const char *nm;
int4 id;
};
class PcodeLexer {
public:
enum { // Lexer states
start,
special2, // Middle of special 2 character operator
special3, // First character of special 3 character operator
special32, // Second character of special 3 character operator
comment, // Middle of an endofline comment
punctuation, // Punctuation character
identifier, // Middle of an identifier
hexstring, // Middle of a hexidecimal number
decstring, // Middle of a decimal number
endstream, // Reached end of stream
illegal // Scanned an illegal character
};
private:
static const IdentRec idents[];
int4 curstate;
char curchar,lookahead1,lookahead2;
char curtoken[256];
int4 tokpos;
bool endofstream;
bool endofstreamsent;
istream *s;
string curidentifier;
uintb curnum;
void starttoken(void) { curtoken[0] = curchar; tokpos = 1; }
void advancetoken(void) { curtoken[tokpos++] = curchar; }
bool isIdent(char c) const { return (isalnum(c)||(c=='_')||(c=='.')); }
bool isHex(char c) const { return isxdigit(c); }
bool isDec(char c) const { return isdigit(c); }
int4 findIdentifier(const string &str) const;
int4 moveState(void);
public:
PcodeLexer(void) { s = (istream *)0; }
void initialize(istream *t);
int4 getNextToken(void);
const string &getIdentifier(void) const { return curidentifier; }
uintb getNumber(void) const { return curnum; }
};
class PcodeSnippet : public PcodeCompile {
PcodeLexer lexer;
const SleighBase *sleigh; // Language from which we get symbols
SymbolTree tree; // Symbols in the local scope of the snippet (temporaries)
uint4 tempbase;
int4 errorcount;
string firsterror;
ConstructTpl *result;
virtual uint4 allocateTemp(void);
virtual void addSymbol(SleighSymbol *sym);
public:
PcodeSnippet(const SleighBase *slgh);
void setResult(ConstructTpl *res) { result = res; }
ConstructTpl *releaseResult(void) { ConstructTpl *res = result; result = (ConstructTpl *)0; return res; }
virtual ~PcodeSnippet(void);
virtual const Location *getLocation(SleighSymbol *sym) const { return (const Location *)0; }
virtual void reportError(const Location *loc, const string &msg);
virtual void reportWarning(const Location *loc, const string &msg) {}
bool hasErrors(void) const { return (errorcount != 0); }
const string getErrorMessage(void) const { return firsterror; }
void setUniqueBase(uint4 val) { tempbase = val; }
uint4 getUniqueBase(void) const { return tempbase; }
void clear(void);
int lex(void);
bool parseStream(istream& s);
void addOperand(const string &name,int4 index);
};
} // End namespace ghidra
#endif