1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
|
/** cifframe.m - Wrapper/Objective-C interface for ffi function interface
Copyright (C) 1999-2015 Free Software Foundation, Inc.
Written by: Adam Fedor <fedor@gnu.org>
Date: Dec 1999, rewritten Apr 2002
This file is part of the GNUstep Base Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110 USA.
*/
#import "common.h"
#if !defined (__GNU_LIBOBJC__)
# include <objc/encoding.h>
#endif
#ifdef HAVE_MALLOC_H
#if !defined(__OpenBSD__)
#include <malloc.h>
#endif
#endif
#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif
#include "cifframe.h"
#import "Foundation/NSException.h"
#import "Foundation/NSData.h"
#import "GSInvocation.h"
#import "GSPrivate.h"
/* ffi defines types in a very odd way that doesn't map to the
normal objective-c type (see ffi.h). Here we make up for that */
#if GS_SIZEOF_SHORT == 2
#define gsffi_type_ushort ffi_type_uint16
#define gsffi_type_sshort ffi_type_sint16
#elif GS_SIZEOF_SHORT == 4
#define gsffi_type_ushort ffi_type_uint32
#define gsffi_type_sshort ffi_type_sint32
#else
#error FFI Sizeof SHORT case not handled
#endif
#if GS_SIZEOF_INT == 2
#define gsffi_type_uint ffi_type_uint16
#define gsffi_type_sint ffi_type_sint16
#elif GS_SIZEOF_INT == 4
#define gsffi_type_uint ffi_type_uint32
#define gsffi_type_sint ffi_type_sint32
#elif GS_SIZEOF_INT == 8
#define gsffi_type_uint ffi_type_uint64
#define gsffi_type_sint ffi_type_sint64
#else
#error FFI Sizeof INT case not handled
#endif
#if GS_SIZEOF_LONG == 2
#define gsffi_type_ulong ffi_type_uint16
#define gsffi_type_slong ffi_type_sint16
#elif GS_SIZEOF_LONG == 4
#define gsffi_type_ulong ffi_type_uint32
#define gsffi_type_slong ffi_type_sint32
#elif GS_SIZEOF_LONG == 8
#define gsffi_type_ulong ffi_type_uint64
#define gsffi_type_slong ffi_type_sint64
#else
#error FFI Sizeof LONG case not handled
#endif
#ifdef _C_LNG_LNG
#if GS_SIZEOF_LONG_LONG == 8
#define gsffi_type_ulong_long ffi_type_uint64
#define gsffi_type_slong_long ffi_type_sint64
#else
#error FFI Sizeof LONG LONG case not handled
#endif
#endif
ffi_type *cifframe_type(const char *typePtr, const char **advance);
/* Best guess at the space needed for a structure, since we don't know
for sure until it's calculated in ffi_prep_cif, which is too late */
int
cifframe_guess_struct_size(ffi_type *stype)
{
int i, size;
unsigned align = __alignof(double);
if (stype->elements == NULL)
return stype->size;
size = 0;
i = 0;
while (stype->elements[i])
{
if (stype->elements[i]->elements)
size += cifframe_guess_struct_size(stype->elements[i]);
else
size += stype->elements[i]->size;
if (size % align != 0)
{
size += (align - size % align);
}
i++;
}
return size;
}
NSMutableData *
cifframe_from_signature (NSMethodSignature *info)
{
unsigned size = sizeof(cifframe_t);
unsigned align = __alignof(double);
unsigned type_offset = 0;
unsigned offset = 0;
NSMutableData *result;
void *buf;
int i;
int numargs = [info numberOfArguments];
ffi_type *rtype;
ffi_type *arg_types[numargs];
cifframe_t *cframe;
/* FIXME: in cifframe_type, return values/arguments that are structures
have custom ffi_types with are allocated separately. We should allocate
them in our cifframe so we don't leak memory. Or maybe we could
cache structure types? */
rtype = cifframe_type([info methodReturnType], NULL);
for (i = 0; i < numargs; i++)
{
arg_types[i] = cifframe_type([info getArgumentTypeAtIndex: i], NULL);
}
if (numargs > 0)
{
if (size % align != 0)
{
size += align - (size % align);
}
type_offset = size;
/* Make room to copy the arg_types */
size += sizeof(ffi_type *) * numargs;
if (size % align != 0)
{
size += align - (size % align);
}
offset = size;
size += numargs * sizeof(void*);
if (size % align != 0)
{
size += (align - (size % align));
}
for (i = 0; i < numargs; i++)
{
if (arg_types[i]->elements)
size += cifframe_guess_struct_size(arg_types[i]);
else
size += arg_types[i]->size;
if (size % align != 0)
{
size += (align - size % align);
}
}
}
result = [NSMutableData dataWithCapacity: size];
[result setLength: size];
cframe = buf = [result mutableBytes];
if (cframe)
{
cframe->nargs = numargs;
cframe->arg_types = buf + type_offset;
memcpy(cframe->arg_types, arg_types, sizeof(ffi_type *) * numargs);
cframe->values = buf + offset;
if (ffi_prep_cif (&cframe->cif, FFI_DEFAULT_ABI, numargs,
rtype, cframe->arg_types) != FFI_OK)
{
cframe = NULL;
result = NULL;
}
else
{
/* Set values locations. This must be done after ffi_prep_cif so
that any structure sizes get calculated first. */
offset += numargs * sizeof(void*);
if (offset % align != 0)
{
offset += align - (offset % align);
}
for (i = 0; i < numargs; i++)
{
cframe->values[i] = buf + offset;
offset += arg_types[i]->size;
if (offset % align != 0)
{
offset += (align - offset % align);
}
}
}
}
return result;
}
void
cifframe_set_arg(cifframe_t *cframe, int index, void *buffer, int size)
{
if (index < 0 || index >= cframe->nargs)
return;
memcpy(cframe->values[index], buffer, size);
}
void
cifframe_get_arg(cifframe_t *cframe, int index, void *buffer, int size)
{
if (index < 0 || index >= cframe->nargs)
return;
memcpy(buffer, cframe->values[index], size);
}
void *
cifframe_arg_addr(cifframe_t *cframe, int index)
{
if (index < 0 || index >= cframe->nargs)
return NULL;
return cframe->values[index];
}
/*
* Get the ffi_type for this type
*/
ffi_type *
cifframe_type(const char *typePtr, const char **advance)
{
static ffi_type stypeNSPoint = { 0 };
static ffi_type stypeNSRange = { 0 };
static ffi_type stypeNSRect = { 0 };
static ffi_type stypeNSSize = { 0 };
const char *type;
ffi_type *ftype = 0;
typePtr = objc_skip_type_qualifiers (typePtr);
type = typePtr;
/*
* Scan for size and alignment information.
*/
switch (*typePtr++)
{
case _C_ID: ftype = &ffi_type_pointer;
break;
case _C_CLASS: ftype = &ffi_type_pointer;
break;
case _C_SEL: ftype = &ffi_type_pointer;
break;
case _C_CHR: ftype = &ffi_type_schar;
break;
case _C_UCHR: ftype = &ffi_type_uchar;
break;
case _C_SHT: ftype = &gsffi_type_sshort;
break;
case _C_USHT: ftype = &gsffi_type_ushort;
break;
case _C_INT: ftype = &gsffi_type_sint;
break;
case _C_UINT: ftype = &gsffi_type_uint;
break;
case _C_LNG: ftype = &gsffi_type_slong;
break;
case _C_ULNG: ftype = &gsffi_type_ulong;
break;
#ifdef _C_LNG_LNG
case _C_LNG_LNG: ftype = &gsffi_type_slong_long;
break;
case _C_ULNG_LNG: ftype = &gsffi_type_ulong_long;
break;
#endif
case _C_FLT: ftype = &ffi_type_float;
break;
case _C_DBL: ftype = &ffi_type_double;
break;
case _C_PTR:
ftype = &ffi_type_pointer;
if (*typePtr == '?')
{
typePtr++;
}
else
{
const char *adv;
cifframe_type(typePtr, &adv);
typePtr = adv;
}
break;
case _C_ATOM:
case _C_CHARPTR:
ftype = &ffi_type_pointer;
break;
case _C_ARY_B:
{
const char *adv;
ftype = &ffi_type_pointer;
while (isdigit(*typePtr))
{
typePtr++;
}
cifframe_type(typePtr, &adv);
typePtr = adv;
typePtr++; /* Skip end-of-array */
}
break;
case _C_STRUCT_B:
{
int types, maxtypes, size;
ffi_type *local;
const char *adv;
unsigned align = __alignof(double);
/* Standard structures can be handled using cached type information.
Since the switch statement has already skipped the _C_STRUCT_B
character, we must use typePtr-1 below to successfully match the
type encoding with one of the standard type encodings. The same
holds for skipping past the whole structure type's encoding with
objc_skip_typespec.
*/
if (GSSelectorTypesMatch(typePtr - 1, @encode(NSRange)))
{
ftype = &stypeNSRange;
if (ftype->type == 0)
{
static ffi_type *elems[3];
if (*@encode(NSUInteger) == _C_ULNG)
{
elems[0] = &gsffi_type_ulong;
}
#ifdef _C_LNG_LNG
else if (*@encode(NSUInteger) == _C_ULNG_LNG)
{
elems[0] = &gsffi_type_ulong_long;
}
#endif
else
{
elems[0] = &gsffi_type_uint;
}
elems[1] = elems[0];
elems[2] = 0;
ftype->elements = elems;
ftype->type = FFI_TYPE_STRUCT;
}
typePtr = objc_skip_typespec (typePtr - 1);
break;
}
else if (GSSelectorTypesMatch(typePtr - 1, @encode(NSPoint)))
{
ftype = &stypeNSPoint;
if (ftype->type == 0)
{
static ffi_type *elems[3];
if (*@encode(CGFloat) == _C_DBL)
{
elems[0] = &ffi_type_double;
}
else
{
elems[0] = &ffi_type_float;
}
elems[1] = elems[0];
elems[2] = 0;
ftype->elements = elems;
ftype->type = FFI_TYPE_STRUCT;
}
typePtr = objc_skip_typespec (typePtr - 1);
break;
}
else if (GSSelectorTypesMatch(typePtr - 1, @encode(NSSize)))
{
ftype = &stypeNSSize;
if (ftype->type == 0)
{
static ffi_type *elems[3];
if (*@encode(CGFloat) == _C_DBL)
{
elems[0] = &ffi_type_double;
}
else
{
elems[0] = &ffi_type_float;
}
elems[1] = elems[0];
elems[2] = 0;
ftype->elements = elems;
ftype->type = FFI_TYPE_STRUCT;
}
typePtr = objc_skip_typespec (typePtr - 1);
break;
}
else if (GSSelectorTypesMatch(typePtr - 1, @encode(NSRect)))
{
ftype = &stypeNSRect;
if (ftype->type == 0)
{
static ffi_type *elems[3];
/* An NSRect is an NSPoint and an NSSize, but those
* two structures are actually identical.
*/
elems[0] = cifframe_type(@encode(NSSize), NULL);
elems[1] = cifframe_type(@encode(NSPoint), NULL);
elems[2] = 0;
ftype->elements = elems;
ftype->type = FFI_TYPE_STRUCT;
}
typePtr = objc_skip_typespec (typePtr - 1);
break;
}
/*
* Skip "<name>=" stuff.
*/
while (*typePtr != _C_STRUCT_E)
{
if (*typePtr++ == '=')
{
break;
}
}
types = 0;
maxtypes = 4;
size = sizeof(ffi_type);
if (size % align != 0)
{
size += (align - (size % align));
}
ftype = malloc(size + (maxtypes+1)*sizeof(ffi_type));
ftype->size = 0;
ftype->alignment = 0;
ftype->type = FFI_TYPE_STRUCT;
ftype->elements = (void*)ftype + size;
/*
* Continue accumulating structure size.
*/
while (*typePtr != _C_STRUCT_E)
{
local = cifframe_type(typePtr, &adv);
typePtr = adv;
NSCAssert(typePtr, @"End of signature while parsing");
ftype->elements[types++] = local;
if (types >= maxtypes)
{
maxtypes *=2;
ftype = realloc(ftype,
size + (maxtypes+1)*sizeof(ffi_type));
ftype->elements = (void*)ftype + size;
}
}
ftype->elements[types] = NULL;
typePtr++; /* Skip end-of-struct */
}
break;
case _C_UNION_B:
{
const char *adv;
int max_align = 0;
/*
* Skip "<name>=" stuff.
*/
while (*typePtr != _C_UNION_E)
{
if (*typePtr++ == '=')
{
break;
}
}
ftype = NULL;
while (*typePtr != _C_UNION_E)
{
ffi_type *local;
int align = objc_alignof_type(typePtr);
local = cifframe_type(typePtr, &adv);
typePtr = adv;
NSCAssert(typePtr, @"End of signature while parsing");
if (align > max_align)
{
if (ftype && ftype->type == FFI_TYPE_STRUCT
&& ftype != &stypeNSPoint
&& ftype != &stypeNSRange
&& ftype != &stypeNSRect
&& ftype != &stypeNSSize)
{
free(ftype);
}
ftype = local;
max_align = align;
}
}
typePtr++; /* Skip end-of-union */
}
break;
case _C_VOID: ftype = &ffi_type_void;
break;
#if __GNUC__ > 2 && defined(_C_BOOL)
case _C_BOOL: ftype = &ffi_type_uchar;
break;
#endif
default:
ftype = &ffi_type_void;
NSCAssert(0, @"Unknown type in sig");
}
/* Skip past any offset information, if there is any */
if (*type != _C_PTR || *type == '?')
{
if (*typePtr == '+')
typePtr++;
if (*typePtr == '-')
typePtr++;
while (isdigit(*typePtr))
typePtr++;
}
if (advance)
*advance = typePtr;
return ftype;
}
GSCodeBuffer*
cifframe_closure (NSMethodSignature *sig, void (*cb)())
{
NSMutableData *frame;
cifframe_t *cframe;
ffi_closure *cclosure;
void *executable;
GSCodeBuffer *memory;
/* Construct the frame (stored in an NSMutableDate object) and sety it
* in a new closure.
*/
frame = cifframe_from_signature(sig);
cframe = [frame mutableBytes];
memory = [GSCodeBuffer memoryWithSize: sizeof(ffi_closure)];
[memory setFrame: frame];
cclosure = [memory buffer];
executable = [memory executable];
if (cframe == NULL || cclosure == NULL)
{
[NSException raise: NSMallocException format: @"Allocating closure"];
}
#if HAVE_FFI_PREP_CLOSURE_LOC
if (ffi_prep_closure_loc(cclosure, &(cframe->cif),
cb, frame, executable) != FFI_OK)
{
[NSException raise: NSGenericException format: @"Preping closure"];
}
#else
executable = (void*)cclosure;
if (ffi_prep_closure(cclosure, &(cframe->cif),
cb, frame) != FFI_OK)
{
[NSException raise: NSGenericException format: @"Preping closure"];
}
#endif
[memory protect];
return memory;
}
/*-------------------------------------------------------------------------*/
/* Functions for handling sending and receiving messages accross a
connection
*/
/* Some return types actually get coded differently. We need to convert
back to the expected return type */
BOOL
cifframe_decode_arg (const char *type, void* buffer)
{
type = objc_skip_type_qualifiers (type);
switch (*type)
{
case _C_CHR:
*(signed char*)buffer = (signed char)(*((ffi_sarg *)buffer));
break;
case _C_UCHR:
*(unsigned char*)buffer = (unsigned char)(*((ffi_arg *)buffer));
break;
case _C_SHT:
*(signed short*)buffer = (signed short)(*((ffi_sarg *)buffer));
break;
case _C_USHT:
*(unsigned short*)buffer = (unsigned short)(*((ffi_arg *)buffer));
break;
case _C_INT:
*(signed int*)buffer = (signed int)(*((ffi_sarg *)buffer));
break;
case _C_UINT:
*(unsigned int*)buffer = (unsigned int)(*((ffi_arg *)buffer));
break;
case _C_LNG:
if (sizeof(signed long) < sizeof(ffi_sarg))
*(signed long*)buffer = (signed long)(*((ffi_sarg *)buffer));
break;
case _C_ULNG:
if (sizeof(unsigned long) < sizeof(ffi_arg))
*(unsigned long*)buffer = (unsigned long)(*((ffi_arg *)buffer));
break;
default:
return NO;
}
return YES;
}
BOOL
cifframe_encode_arg (const char *type, void* buffer)
{
type = objc_skip_type_qualifiers (type);
switch (*type)
{
case _C_CHR:
*(ffi_sarg *)buffer = (ffi_sarg)(*((signed char *)buffer));
break;
case _C_UCHR:
*(ffi_arg *)buffer = (ffi_arg)(*((unsigned char *)buffer));
break;
case _C_SHT:
*(ffi_sarg *)buffer = (ffi_sarg)(*((signed short *)buffer));
break;
case _C_USHT:
*(ffi_arg *)buffer = (ffi_arg)(*((unsigned short *)buffer));
break;
case _C_INT:
*(ffi_sarg *)buffer = (ffi_sarg)(*((signed int *)buffer));
break;
case _C_UINT:
*(ffi_arg *)buffer = (ffi_arg)(*((unsigned int *)buffer));
break;
case _C_LNG:
if (sizeof(signed long) < sizeof(ffi_sarg))
*(ffi_sarg *)buffer = (ffi_sarg)(*((signed long *)buffer));
break;
case _C_ULNG:
if (sizeof(unsigned long) < sizeof(ffi_arg))
*(ffi_arg *)buffer = (ffi_arg)(*((unsigned int *)buffer));
break;
default:
return NO;
}
return YES;
}
|