A computer-implemented method of control flow enforcement, comprising the steps: analysing a portion of code of a program: to generate a reference control flow in respect of the portion of code, said portion of code comprising a plurality of instructions, wherein the reference control flow indicates the expected execution of the program under normal operating conditions; and to identify at least one possible attack mechanism for a security attack against the portion of code; analysing the reference control flow to derive a plurality of control flow rules for said instructions of said portion of code, wherein the control flow rules define run-time behaviour which is expected according to the reference control flow; testing the plurality of control flow rules against the reference control flow and recording the number of said attack mechanisms eliminated by each control flow rule; identifying a subset of the plurality of control flow rules to provide a reduced control flow rule set comprising said subset of control flow rules, wherein the subset of control flow rules is identified to maximise the number of eliminated attack mechanisms; evaluating at least one of the control flow rules of said reduced control flow rule set during execution of the portion of code to facilitate detection of a deviation from the reference control flow, wherein said deviation is indicative of a possible security attack against the portion of code; generating a response upon detection of a deviation from the expected control flow. analysing a portion of code of a program: to generate a reference control flow in respect of the portion of code, said portion of code comprising a plurality of instructions, wherein the reference control flow indicates the expected execution of the program under normal operating conditions; and to identify at least one possible attack mechanism for a security attack against the portion of code; to generate a reference control flow in respect of the portion of code, said portion of code comprising a plurality of instructions, wherein the reference control flow indicates the expected execution of the program under normal operating conditions; and to identify at least one possible attack mechanism for a security attack against the portion of code; analysing the reference control flow to derive a plurality of control flow rules for said instructions of said portion of code, wherein the control flow rules define run-time behaviour which is expected according to the reference control flow; testing the plurality of control flow rules against the reference control flow and recording the number of said attack mechanisms eliminated by each control flow rule; identifying a subset of the plurality of control flow rules to provide a reduced control flow rule set comprising said subset of control flow rules, wherein the subset of control flow rules is identified to maximise the number of eliminated attack mechanisms; evaluating at least one of the control flow rules of said reduced control flow rule set during execution of the portion of code to facilitate detection of a deviation from the reference control flow, wherein said deviation is indicative of a possible security attack against the portion of code; generating a response upon detection of a deviation from the expected control flow.