PostHeaderIcon PIC18F24K40: Memory Execution Prevention Is a Different Kind of Limit

Protection bits are usually about who may look. The K40 generation introduces a control that is about where things may happen: a configuration that prevents the core from fetching instructions out of regions that should only ever hold data.

This is not confidentiality and it does not stop a read. It is a structural constraint, and it matters to anyone rebuilding firmware because a rebuilt image that puts code where the original did not will not run — and the failure will look like a corrupted program rather than a configuration mismatch.

What the control does

With the control enabled, execution is confined to the regions the device considers executable. An attempt to fetch an instruction from a non-executable region produces a reset rather than a wild branch.

The practical benefit to a vendor is robustness: a stack overflow or a corrupted function pointer cannot turn into arbitrary code execution from a buffer. The practical cost to a reader is that the memory map acquires a meaning it did not have before — some regions hold code, others must not.

Region Executable when enabled Typical contents
Program array, normal range Yes Application code
Boot block Yes Bootloader, vectors
Data EEPROM No Settings, calibration
Stack and heap in RAM No Runtime data
Configuration area No Device settings

Why this changes a rebuild

A rebuild written from behaviour will naturally place code wherever the compiler puts it, and a compiler targeting this device places code in the executable range. So far, no problem.

The problem appears when a rebuild needs to place a routine outside the normal range — a small loader in the boot block, or a routine that must run from a specific address. If the original did that and the rebuild does not, the behaviour differs; if the rebuild does it and the device has prevention enabled, the device resets.

Diagnosing that from a reset is hard, because a reset looks like a watchdog, a brown-out, or a crash. The distinguishing evidence is that the reset occurs immediately after a call into the affected region, reproducibly, with no other symptom.

Vectors and the relocated table

This generation also allows the interrupt vector table to be relocated, which interacts with execution prevention: the table must live in an executable region, and moving it changes the addresses the hardware fetches from.

For analysis, a relocated vector table is a trap. A reader who assumes the table is at its default address will find data there, decode it as branches, and produce a nonsensical call graph. The tell is that the addresses found do not correspond to plausible code entry points.

Finding the real table requires knowing the configuration, which brings the analysis back to the configuration read — the same first step as on every part discussed here.

What protection still does

None of the above replaces the ordinary protection bits, which remain present and independent:

  • code protection, per block, gating reads;
  • write protection, per block, gating modification;
  • boot block protection, gating both for the small region at the bottom.

Execution prevention is a fourth, orthogonal setting. A device can have execution prevention enabled and no read protection at all — a configuration that is entirely readable and still refuses to run code from a buffer.

Reading a K40 device

The array behaves like the earlier families: block-granular read protection, configuration readable through the programmer, data EEPROM separately protected.

Two differences are worth expecting. The configuration space is larger, with more fields, so a tool’s abbreviated display may hide the field you need. And the device identification area carries more information than on earlier parts, including a unique identifier — useful for distinguishing units but not for recovering code.

Limits and honest expectations

  • Execution prevention does not impede reading. It is not a confidentiality feature.
  • A rebuild must respect the original’s region assignment, and finding that assignment requires the configuration.
  • The failure mode of getting it wrong is a reset, which is easily mistaken for a hardware fault.

Related reading

FAQ

Does execution prevention stop a read? No. It constrains where the core may fetch instructions from.

What happens if code runs from a non-executable region? The device resets. There is no fault handler for it in the usual sense.

Can it be disabled? It is a configuration setting, changeable by rewriting configuration — which requires an erase.

Does it affect the boot block? The boot block is executable; the setting constrains regions that are not intended to hold code.

Why does a rebuild reset immediately on this part? Usually because it placed a routine where the original did not, and prevention rejected the fetch.

Conclusion

The K40 generation adds a constraint on structure rather than on secrecy: code may only execute where code is allowed. That changes what a rebuilt image must satisfy, and it introduces a failure mode — an immediate, reproducible reset — that looks like hardware and is actually configuration. Reading the configuration first resolves it, as it does on every part in this family.

Comments are closed.