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

165 lines
4.5 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 "compression.hh"
namespace ghidra {
/// The compression \b level ranges from 1-9 from faster/least compression to slower/most compression.
/// Use a \b level of 0 for no compression and -1 for the \e default compression level.
/// \param level is the compression level
Compress::Compress(int4 level)
{
compStream.zalloc = Z_NULL;
compStream.zfree = Z_NULL;
compStream.opaque = Z_NULL;
int4 ret = deflateInit(&compStream, level);
if (ret != Z_OK)
throw LowlevelError("Could not initialize deflate stream state");
}
Compress::~Compress(void)
{
deflateEnd(&compStream);
}
/// Return the number of bytes of output space still available. Output may be limited by the amount
/// of space in the output buffer or the amount of data available in the current input buffer.
/// \param buffer is where compressed bytes are stored
/// \param sz is the size, in bytes, of the buffer
/// \param finish is set to \b true if this is the final buffer to add to the stream
/// \return the number of output bytes still available
int4 Compress::deflate(uint1 *buffer,int4 sz,bool finish)
{
int flush = finish ? Z_FINISH : Z_NO_FLUSH;
compStream.avail_out = sz;
compStream.next_out = buffer;
int ret = ::deflate(&compStream, flush);
if (ret == Z_STREAM_ERROR)
throw LowlevelError("Error compressing stream");
return compStream.avail_out;
}
Decompress::Decompress(void)
{
streamFinished = false;
compStream.zalloc = Z_NULL;
compStream.zfree = Z_NULL;
compStream.opaque = Z_NULL;
compStream.avail_in = 0;
compStream.next_in = Z_NULL;
int ret = inflateInit(&compStream);
if (ret != Z_OK)
throw LowlevelError("Could not initialize inflate stream state");
}
/// Return the number of bytes of output space still available. Output may be limited by the amount
/// of space in the output buffer or the amount of data available in the current input buffer.
/// \param buffer is where uncompressed bytes are stored
/// \param sz is the size, in bytes, of the buffer
/// \return the number of output bytes still available
int4 Decompress::inflate(uint1 *buffer,int4 sz)
{
compStream.avail_out = sz;
compStream.next_out = buffer;
int ret = ::inflate(&compStream, Z_NO_FLUSH);
switch (ret) {
case Z_NEED_DICT:
case Z_DATA_ERROR:
case Z_MEM_ERROR:
case Z_STREAM_ERROR:
throw LowlevelError("Error decompressing stream");
case Z_STREAM_END:
streamFinished = true;
break;
default:
break;
}
return compStream.avail_out;
}
Decompress::~Decompress(void)
{
inflateEnd(&compStream);
}
const int4 CompressBuffer::IN_BUFFER_SIZE = 4096;
const int4 CompressBuffer::OUT_BUFFER_SIZE = 4096;
/// \param s is the backing output stream
/// \param level is the level of compression
CompressBuffer::CompressBuffer(ostream &s,int4 level)
: outStream(s), compressor(level)
{
inBuffer = new uint1[IN_BUFFER_SIZE];
outBuffer = new uint1[OUT_BUFFER_SIZE];
setp((char *)inBuffer,(char *)inBuffer + IN_BUFFER_SIZE-1);
}
CompressBuffer::~CompressBuffer(void)
{
delete [] inBuffer;
delete [] outBuffer;
}
/// The compressor is called repeatedly and its output is written to the backing stream
/// until the compressor can no longer fill the \e output buffer.
/// \param lastBuffer is \b true if this is the final set of bytes to add to the compressed stream
void CompressBuffer::flushInput(bool lastBuffer)
{
int len = pptr() - pbase();
compressor.input((uint1 *)pbase(),len);
int4 outAvail;
do {
outAvail = OUT_BUFFER_SIZE;
outAvail = compressor.deflate(outBuffer,outAvail,lastBuffer);
outStream.write((char *)outBuffer,OUT_BUFFER_SIZE-outAvail);
} while(outAvail == 0);
pbump(-len);
}
/// \param c is the final character filling the buffer
/// \return the written character
int CompressBuffer::overflow(int c)
{
if (c != EOF) {
*pptr() = c;
pbump(1);
}
flushInput(false);
return c;
}
/// \return 0 for success
int CompressBuffer::sync(void)
{
flushInput(true);
return 0;
}
}