Skip to content

BUG: UnicornResumeTest hangs indefinitely during emulate_until #2674

@dguido

Description

@dguido

Description

The UnicornResumeTest test suite hangs indefinitely when attempting to use Unicorn engine's emulate_until() function. The test times out and must be killed, indicating a deadlock or infinite loop in the Unicorn emulation layer.

Steps to Reproduce

Run any of the affected tests:

pytest tests/native/test_unicorn_concrete.py::UnicornResumeTest::test_integration_resume -xvs

The test will hang and require manual interruption (Ctrl+C or timeout).

Current Behavior

  1. ResumeUnicornPlugin.will_run_callback() is invoked
  2. Calls state.cpu.emulate_until(0x402180) (MAIN address)
  3. Emulation never completes - hangs indefinitely
  4. No output or progress indicators
  5. Test must be killed manually or times out in CI

Expected Behavior

  • Emulation should complete and reach the target address
  • Hooks should be triggered appropriately
  • Test should complete within reasonable time (<10 seconds)

Root Cause Analysis

The hang occurs in the Unicorn emulation layer, likely due to one of:

  1. Missing Memory Mappings: Required memory regions not mapped for Unicorn
  2. Infinite Loop: Emulation stuck in a loop without reaching target
  3. Unsupported Instructions: Unicorn encountering instructions it can't handle
  4. Initialization Issues: Linux loader code not properly handled by Unicorn

Investigation Findings

Test Structure

class ResumeUnicornPlugin(Plugin):
    def will_run_callback(self, ready_states):
        for state in ready_states:
            state.cpu.emulate_until(0x402180)  # <-- Hangs here

Binary Information

  • Binary: tests/native/binaries/rusticorn
  • Type: ELF 64-bit LSB executable, x86-64, dynamically linked
  • Target addresses:
    • MAIN: 0x402180
    • PRE_LOOP: 0x4022EE
    • POST_LOOP: 0x402346

Debug Observations

  • No output is produced during emulation
  • Process appears to be in busy-wait or deadlock
  • CPU usage remains high during hang
  • No exceptions or errors are raised

Suggested Investigation Steps

  1. Add Timeout and Logging:

    • Wrap emulate_until with timeout
    • Add progress callbacks to track emulation
    • Log memory access patterns
  2. Memory Analysis:

    • Verify all required memory regions are mapped
    • Check permissions on mapped regions
    • Ensure stack and heap are properly initialized
  3. Instruction Tracing:

    • Enable Unicorn instruction tracing
    • Identify last successfully executed instruction
    • Check for problematic instruction patterns
  4. Alternative Start Points:

    • Try emulating from different addresses
    • Skip problematic initialization code
    • Use concrete execution for loader portions

Potential Fixes

Option 1: Timeout with Fallback

Add timeout to emulate_until and fall back to regular execution on timeout.

Option 2: Selective Emulation

Only use Unicorn for specific code regions, avoiding problematic areas.

Option 3: Fix Memory Mapping

Ensure all required memory is properly mapped before emulation.

Test Information

  • Tests: All tests in UnicornResumeTest class
  • File: tests/native/test_unicorn_concrete.py
  • Lines: 122-174
  • Binary: tests/native/binaries/rusticorn

Impact

  • Three test failures in CI
  • Blocks Unicorn integration testing
  • May affect performance optimization features
  • Causes CI timeout issues

Environment

  • Python 3.13.3
  • Unicorn 2.1.3
  • Linux 6.14.0-23-generic
  • x86-64 architecture

Additional Context

This issue was discovered during CI failure investigation. The Unicorn API was recently updated to fix parameter passing issues (aux1 as keyword argument), but this hang issue persists. Tests have been temporarily disabled pending resolution.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions