Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions ext/ldap/JitLdapLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use PHPLLVM\Builder;
use PHPLLVM\Value;

/** LLVM lowering for ldap_bind() / ldap_bind_ext() / ldap_sasl_bind() / ldap_unbind() / ldap_close() / ldap_set/get_option() / ldap_start_tls() / ldap_set_rebind_proc() (#32001, #32002, #32107, #32109, #32146, #32147, #32148). */
/** LLVM lowering for ldap_bind() / ldap_bind_ext() / ldap_sasl_bind() / ldap_unbind() / ldap_close() / ldap_set/get_option() / ldap_start_tls() / ldap_set_rebind_proc() (#32001, #32002, #32107, #32109, #32146, #32147, #32148, #32172). */
final class JitLdapLink
{
/** @param list<JITVariable> $args */
Expand Down Expand Up @@ -186,13 +186,16 @@ public static function invokeBindExt(Context $context, array $args): Value
BasicBlockHelper::restoreInsertBlock($context, $savedInsert);
}

return $context->builder->call(
$context->lookupFunction('__compiler_ldap_bind_ext'),
$handle,
$dn,
$password,
$hasDn,
$hasPassword
return JitLdapResult::registerReturnedResult(
$context,
$context->builder->call(
$context->lookupFunction('__compiler_ldap_bind_ext'),
$handle,
$dn,
$password,
$hasDn,
$hasPassword
)
);
}

Expand Down
146 changes: 136 additions & 10 deletions ext/ldap/JitLdapResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
use PHPCompiler\JIT\JitStringBuiltinArg;
use PHPCompiler\JIT\JitValueBox;
use PHPCompiler\JIT\Variable as JITVariable;
use PHPCompiler\VM\Variable;
use PHPLLVM\Builder;
use PHPLLVM\Value;

/** LLVM lowering for ldap_compare() (#32121). */
/** LLVM lowering for ldap_compare() / ldap_count_entries() (#32121, #32172). */
final class JitLdapResult
{
/** @param list<JITVariable> $args */
Expand Down Expand Up @@ -48,6 +50,112 @@ public static function invokeCompare(Context $context, array $args): Value
);
}

/** @param list<JITVariable> $args */
public static function invokeCountEntries(Context $context, array $args): Value
{
$argc = \count($args);
if (2 !== $argc) {
throw new \ArgumentCountError(\sprintf(
'ldap_count_entries() expects exactly 2 arguments, %d given',
$argc
));
}

$conn = self::lowerConnectionHandle($context, $args[0], 'ldap_count_entries');
$result = self::lowerResultHandle($context, $args[1], 'ldap_count_entries');

$savedInsert = BasicBlockHelper::tryGetInsertBlock($context);
LdapRuntime::ensureLinked($context);
if (null !== $savedInsert) {
BasicBlockHelper::restoreInsertBlock($context, $savedInsert);
}

$count = $context->builder->call(
$context->lookupFunction('__compiler_ldap_count_entries'),
$conn,
$result
);

return self::longFromI64($context, $count);
}

/**
* Map a NestedJIT LDAP\Result return value to its VM result id (#32172).
*
* Peer: ldap_connect() register in {@see ldap_connect::call}.
*/
public static function registerReturnedResult(Context $context, Value $result): Value
{
$typeField = $context->structFieldMap['__value__']['type'];
$typeByte = $context->builder->load(
$context->builder->structGep($result, $typeField)
);
$i8 = $context->getTypeFromString('int8');
$kind = $context->builder->and($typeByte, $i8->constInt(0x7f, false));
$isObject = $context->builder->icmp(
Builder::INT_EQ,
$kind,
$i8->constInt(Variable::TYPE_OBJECT & 0x7f, false)
);
$regBb = BasicBlockHelper::append($context, 'ldap_result_register');
$doneBb = BasicBlockHelper::append($context, 'ldap_result_done');
$context->builder->branchIf($isObject, $regBb, $doneBb);

$context->builder->positionAtEnd($regBb);
$obj = $context->builder->call(
$context->lookupFunction('__value__readObject'),
$result
);
$voidp = $context->getTypeFromString('void')->pointerType(0);
$i64 = $context->getTypeFromString('int64');
$objAddr = $context->builder->ptrToInt(
$context->builder->pointerCast($obj, $voidp),
$i64
);
$context->builder->call(
$context->lookupFunction('__compiler_ldap_result_register'),
$objAddr
);
$context->builder->branch($doneBb);
$context->builder->positionAtEnd($doneBb);

return $result;
}

private static function longFromI64(Context $context, Value $value): Value
{
$slot = JitValueBox::alloc($context);
$ptr = JitValueBox::pointer($context, $slot);
JitValueBox::writeLong($context, $slot, $value);

return $ptr;
}

private static function lowerResultHandle(Context $context, JITVariable $arg, string $function): Value
{
if (JITVariable::TYPE_OBJECT === $arg->type) {
return JitGetObjectId::invoke($context, $arg, $function);
}
if (JITVariable::TYPE_VALUE === $arg->type) {
$loaded = JitValueBox::valuePtrFromVariable($context, $arg);
$obj = $context->builder->call(
$context->lookupFunction('__value__readObject'),
$loaded
);
$voidp = $context->getTypeFromString('void')->pointerType(0);
$i64 = $context->getTypeFromString('int64');

return $context->builder->ptrToInt(
$context->builder->pointerCast($obj, $voidp),
$i64
);
}

self::emitTypeErrorAndAbort($context, self::resultScalarTypeError($function, $arg->type));

return $context->getTypeFromString('int64')->constInt(0, false);
}

private static function lowerConnectionHandle(Context $context, JITVariable $arg, string $function): Value
{
if (JITVariable::TYPE_OBJECT === $arg->type) {
Expand Down Expand Up @@ -82,28 +190,46 @@ private static function emitTypeErrorAndAbort(Context $context, string $message)
}

private static function scalarTypeError(string $function, int $type): string
{
return self::typeErrorMessage($function, 1, 'ldap', 'LDAP\\Connection', self::typeLabel($type));
}

private static function resultScalarTypeError(string $function, int $type): string
{
return self::typeErrorMessage($function, 2, 'result', 'LDAP\\Result', self::typeLabel($type));
}

private static function typeLabel(int $type): string
{
switch ($type) {
case JITVariable::TYPE_NATIVE_LONG:
return self::typeErrorMessage($function, 'int');
return 'int';
case JITVariable::TYPE_NATIVE_DOUBLE:
return self::typeErrorMessage($function, 'float');
return 'float';
case JITVariable::TYPE_NATIVE_BOOL:
return self::typeErrorMessage($function, 'bool');
return 'bool';
case JITVariable::TYPE_STRING:
return self::typeErrorMessage($function, 'string');
return 'string';
case JITVariable::TYPE_NULL:
return self::typeErrorMessage($function, 'null');
return 'null';
default:
return self::typeErrorMessage($function, 'mixed');
return 'mixed';
}
}

private static function typeErrorMessage(string $function, string $given): string
{
private static function typeErrorMessage(
string $function,
int $argNum,
string $param,
string $expected,
string $given
): string {
return \sprintf(
'%s(): Argument #1 ($ldap) must be of type LDAP\\Connection, %s given',
'%s(): Argument #%d ($%s) must be of type %s, %s given',
$function,
$argNum,
$param,
$expected,
$given
);
}
Expand Down
1 change: 1 addition & 0 deletions ext/ldap/LdapLinkJitHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public static function bindExtArgv(int $handle, ?string $dn, ?string $password,

return $out;
}
VmLdapResult::enqueuePendingJitHandle($result->toObject()->id);

return $result;
}
Expand Down
39 changes: 36 additions & 3 deletions ext/ldap/LdapResultJitHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@
use PHPCompiler\VM\Variable;

/**
* ldap_compare() for compiled JIT/AOT modules (#32121).
* ldap_compare() / ldap_count_entries() for compiled JIT/AOT modules (#32121, #32172).
*
* SSOT: {@see VmLdapCore::compare}
* php-src: ext/ldap/ldap.c — PHP_FUNCTION(ldap_compare)
* SSOT: {@see VmLdapCore::compare} / {@see VmLdapNative::countEntries}
* php-src: ext/ldap/ldap.c — PHP_FUNCTION(ldap_compare) / ldap_count_entries
*/
final class LdapResultJitHelper
{
public static function registerHandleArgv(int $handle): void
{
VmLdapResult::claimPendingJitHandle($handle);
}

public static function compareArgv(int $handle, string $dn, string $attribute, string $value): Variable
{
$conn = self::requireConnection($handle, 'ldap_compare');
Expand All @@ -29,6 +34,17 @@ public static function compareArgv(int $handle, string $dn, string $attribute, s
return $out;
}

public static function countEntriesArgv(int $connHandle, int $resultHandle): int
{
$conn = self::requireConnection($connHandle, 'ldap_count_entries');
$result = self::requireResult($resultHandle, 'ldap_count_entries');

return VmLdapNative::countEntries(
VmLdapConnection::native($conn),
VmLdapResult::resultNative($result)
);
}

private static function requireConnection(int $handle, string $function): ObjectEntry
{
if (VmLdapConnection::isClosedLookupKey($handle)) {
Expand All @@ -45,4 +61,21 @@ private static function requireConnection(int $handle, string $function): Object

return $conn;
}

private static function requireResult(int $handle, string $function): ObjectEntry
{
if (VmLdapResult::isFreedLookupKey($handle)) {
throw new \TypeError(
$function.'(): supplied LDAP\\Result is not a valid ldap result resource'
);
}
$result = VmLdapResult::resultForLookupKey($handle);
if (null === $result) {
throw new \TypeError(
$function.'(): Argument #2 ($result) must be of type LDAP\\Result, mixed given'
);
}

return $result;
}
}
54 changes: 53 additions & 1 deletion ext/ldap/VmLdapResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@ final class VmLdapResult

public const ENTRY_CLASS_NAME = 'LDAP\\ResultEntry';

/** @var array<int, array{native: \FFI\CData, freed: bool, connection_id: int}> */
/** @var array<int, array{native: \FFI\CData, freed: bool, connection_id: int, object: ObjectEntry}> */
private static array $results = [];

/** @var array<int, array{native: \FFI\CData, connection_id: int, result_id: int, ber: ?\FFI\CData}> */
private static array $entries = [];

/** @var list<int> object ids from wrapResult awaiting JIT handle registration (#32172) */
private static array $pendingJitHandleIds = [];

/** @var array<int, int> JIT object address (ptrToInt) => object id */
private static array $jitHandleToId = [];

public static function registerClasses(Context $ctx): void
{
if (!isset($ctx->classes[self::RESULT_CLASS_LC])) {
Expand All @@ -51,13 +57,59 @@ public static function wrapResult(\FFI\CData $native, Context $ctx, ObjectEntry
'native' => $native,
'freed' => false,
'connection_id' => $connection->id,
'object' => $object,
];
$var = new Variable(Variable::TYPE_OBJECT);
$var->object($object);

return $var;
}

/** Enqueue object id after ldap_bind_ext() NestedJIT helper wrap (#32172). */
public static function enqueuePendingJitHandle(int $objectId): void
{
self::$pendingJitHandleIds[] = $objectId;
}

/** Map compiled __object__* address to VM result state after Result wrap JIT (#32172). */
public static function claimPendingJitHandle(int $handle): void
{
if ($handle <= 0 || [] === self::$pendingJitHandleIds) {
return;
}
self::$jitHandleToId[$handle] = (int) \array_shift(self::$pendingJitHandleIds);
}

public static function resultForLookupKey(int $handle): ?ObjectEntry
{
if ($handle <= 0) {
return null;
}
$id = self::$jitHandleToId[$handle] ?? null;
if (null === $id || !isset(self::$results[$id])) {
return null;
}
$object = self::$results[$id]['object'];
if (!self::isLiveResult($object)) {
return null;
}

return $object;
}

public static function isFreedLookupKey(int $handle): bool
{
if ($handle <= 0) {
return false;
}
$id = self::$jitHandleToId[$handle] ?? null;
if (null === $id || !isset(self::$results[$id])) {
return false;
}

return (bool) self::$results[$id]['freed'];
}

public static function wrapEntry(\FFI\CData $native, Context $ctx, ObjectEntry $connection, int $resultId): Variable
{
self::registerClasses($ctx);
Expand Down
2 changes: 1 addition & 1 deletion ext/ldap/ldap_search_builtins.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function execute(Frame $frame): void

public function call(Context $context, JITVariable ...$args): Value
{
throw new \LogicException('ldap_count_entries() is not implemented for JIT in this compiler build (issue #3369)');
return JitLdapResult::invokeCountEntries($context, $args);
}
}

Expand Down
Loading
Loading