Skip to content

Commit 3517c8d

Browse files
chen-hu-97Chen Hu
authored and
Chen Hu
committed
JIT: Add IBT support
Indirect Branch Tracking (IBT) is part of Intel's Control-Flow Enforcement Technology (CET). IBT is hardware based, forward edge Control-Flow-Integrity mechanism where any indirect CALL/JMP must target an ENDBR instruction or suffer #CP. This commit adds IBT support for JIT: 1. Add endbr32/64 instruction in Dynasm. 2. Insert endbr32/64 in indirect branch target for jitted code. gcc support CET since v8.1 and set it to default since gcc 11. With this commit, endbr is inserted in jitted code if PHP is compiled with "gcc -fcf-protection=full/branch". Signed-off-by: Chen, Hu <hu1.chen@intel.com>
1 parent df98edb commit 3517c8d

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

ext/opcache/jit/dynasm/dasm_x86.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,8 @@ local map_op = {
11471147
rep_0 = "F3",
11481148
repe_0 = "F3",
11491149
repz_0 = "F3",
1150+
endbr32_0 = "F30F1EFB",
1151+
endbr64_0 = "F30F1EFA",
11501152
-- F4: *hlt
11511153
cmc_0 = "F5",
11521154
-- F6: test... mb,i; div... mb

ext/opcache/jit/zend_jit_x86.dasc

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,6 +1623,16 @@ static size_t tsrm_tls_offset;
16231623
|| }
16241624
|.endmacro
16251625

1626+
|.macro ENDBR
1627+
||#if defined (__CET__) && (__CET__ & 1) != 0
1628+
| .if X64
1629+
| endbr64
1630+
| .else
1631+
| endbr32
1632+
| .endif
1633+
||#endif
1634+
|.endmacro
1635+
16261636
static bool reuse_ip = 0;
16271637
static bool delayed_call_chain = 0;
16281638
static uint32_t delayed_call_level = 0;
@@ -2322,7 +2332,7 @@ static int zend_jit_hybrid_func_hot_counter_stub(dasm_State **Dst)
23222332
}
23232333

23242334
|->hybrid_func_hot_counter:
2325-
2335+
| ENDBR
23262336
return zend_jit_hybrid_hot_counter_stub(Dst,
23272337
((ZEND_JIT_COUNTER_INIT + JIT_G(hot_func) - 1) / JIT_G(hot_func)));
23282338
}
@@ -2334,7 +2344,6 @@ static int zend_jit_hybrid_loop_hot_counter_stub(dasm_State **Dst)
23342344
}
23352345

23362346
|->hybrid_loop_hot_counter:
2337-
23382347
return zend_jit_hybrid_hot_counter_stub(Dst,
23392348
((ZEND_JIT_COUNTER_INIT + JIT_G(hot_loop) - 1) / JIT_G(hot_loop)));
23402349
}
@@ -2379,7 +2388,7 @@ static int zend_jit_hybrid_func_trace_counter_stub(dasm_State **Dst)
23792388
}
23802389

23812390
|->hybrid_func_trace_counter:
2382-
2391+
| ENDBR
23832392
return zend_jit_hybrid_trace_counter_stub(Dst,
23842393
((ZEND_JIT_COUNTER_INIT + JIT_G(hot_func) - 1) / JIT_G(hot_func)));
23852394
}
@@ -2391,7 +2400,7 @@ static int zend_jit_hybrid_ret_trace_counter_stub(dasm_State **Dst)
23912400
}
23922401

23932402
|->hybrid_ret_trace_counter:
2394-
2403+
| ENDBR
23952404
return zend_jit_hybrid_trace_counter_stub(Dst,
23962405
((ZEND_JIT_COUNTER_INIT + JIT_G(hot_return) - 1) / JIT_G(hot_return)));
23972406
}
@@ -2403,7 +2412,7 @@ static int zend_jit_hybrid_loop_trace_counter_stub(dasm_State **Dst)
24032412
}
24042413

24052414
|->hybrid_loop_trace_counter:
2406-
2415+
| ENDBR
24072416
return zend_jit_hybrid_trace_counter_stub(Dst,
24082417
((ZEND_JIT_COUNTER_INIT + JIT_G(hot_loop) - 1) / JIT_G(hot_loop)));
24092418
}
@@ -3049,6 +3058,7 @@ static int zend_jit_align_func(dasm_State **Dst)
30493058

30503059
static int zend_jit_prologue(dasm_State **Dst)
30513060
{
3061+
| ENDBR
30523062
if (zend_jit_vm_kind == ZEND_VM_KIND_HYBRID) {
30533063
| SUB_HYBRID_SPAD
30543064
} else if (GCC_GLOBAL_REGS) {

0 commit comments

Comments
 (0)