/usr/share/gnu-smalltalk/kernel/ContextPart.st is in gnu-smalltalk-common 3.2.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | "======================================================================
|
| ContextPart Method Definitions
|
|
======================================================================"
"======================================================================
|
| Copyright 1999, 2000, 2001, 2002, 2003, 2007, 2008
| Free Software Foundation, Inc.
| Written by Paolo Bonzini.
|
| This file is part of the GNU Smalltalk class library.
|
| The GNU Smalltalk class 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.1, or (at
| your option) any later version.
|
| The GNU Smalltalk class 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 the GNU Smalltalk class library; see the file COPYING.LIB.
| If not, write to the Free Software Foundation, 59 Temple Place - Suite
| 330, Boston, MA 02110-1301, USA.
|
======================================================================"
Object subclass: ContextPart [
| parent nativeIP ip sp receiver method |
<shape: #pointer>
<category: 'Language-Implementation'>
<comment: 'My instances represent executing Smalltalk code, which represent the local
environment of executable code. They contain a stack and also provide some
methods that can be used in inspection or debugging.'>
ContextPart class >> spIndex [
<category: 'private'>
^4
]
ContextPart class >> checkPresenceOfJIT [
<category: 'private'>
thisContext isJIT
ifTrue: [Smalltalk addFeature: #JIT]
ifFalse: [Smalltalk removeFeature: #JIT]
]
ContextPart class >> backtrace [
"Print a backtrace from the caller to the bottom of the stack on the
Transcript"
<category: 'exception handling'>
thisContext parentContext backtraceOn: Transcript
]
ContextPart class >> backtraceOn: aStream [
"Print a backtrace from the caller to the bottom of the stack on aStream"
<category: 'exception handling'>
thisContext parentContext backtraceOn: aStream
]
ContextPart class >> thisContext [
"Return the value of the thisContext variable. Called internally
when the variable is accessed."
<category: 'built ins'>
<primitive: VMpr_ContextPart_thisContext>
]
backtrace [
"Print a backtrace from the receiver to the bottom of the stack on the
Transcript."
<category: 'printing'>
self backtraceOn: Transcript
]
backtraceOn: aStream [
"Print a backtrace from the caller to the bottom of the stack on aStream."
<category: 'printing'>
| ctx |
ctx := self.
[ctx isNil or: [ctx isEnvironment]] whileFalse:
[ctx isDisabled
ifFalse:
[ctx printOn: aStream.
aStream nl].
ctx := ctx parentContext]
]
currentLineInFile [
"Answer the 1-based number of the line that is pointed to by the
receiver's instruction pointer, relative to the method's file.
The implementation is slow unless the DebugTools package is loaded."
<category: 'debugging'>
^self currentLine + self method sourceCodeLinesDelta
]
currentFileName [
"Answer the name of the file where the method source code is"
<category: 'accessing'>
| sourceCode |
sourceCode := self method methodSourceCode.
sourceCode isString ifTrue: [ ^'a String' ].
sourceCode isNil ifTrue: [ ^'source not available' ].
^sourceCode printedFileName
]
currentLine [
"Answer the 1-based number of the line that is pointed to by the receiver's
instruction pointer. The DebugTools package caches information,
thus making the implementation faster."
<category: 'debugging'>
^self method sourceCodeMap at: (self ip - 1 max: 1) ifAbsent: [1]
]
debugger [
"Answer the debugger that is attached to the given context. It
is always nil unless the DebugTools package is loaded."
<category: 'debugging'>
^nil
]
debuggerClass [
"Answer which debugger should be used to debug the current
context chain. The class with the highest debugging
priority is picked among those mentioned in the chain."
<category: 'debugging'>
| ctx debuggerClass currentClass last |
ctx := self.
[currentClass := ctx receiver class debuggerClass.
currentClass isNil ifTrue: [^nil].
(debuggerClass isNil
or: [currentClass debuggingPriority > debuggerClass debuggingPriority])
ifTrue: [debuggerClass := currentClass].
ctx parentContext isNil or: [ctx isEnvironment]]
whileFalse: [ctx := ctx parentContext].
^debuggerClass
]
isInternalExceptionHandlingContext [
"Answer whether the receiver is a context that should be hidden to the user
when presenting a backtrace."
<category: 'debugging'>
self subclassResponsibility
]
client [
"Answer the client of this context, that is, the object that sent the
message that created this context. Fail if the receiver has no parent"
<category: 'accessing'>
^self parentContext receiver
]
environment [
"To create a valid execution environment for the interpreter even before
it starts, GST creates a fake context whose selector is nil and which
can be used as a marker for the current execution environment. This
method answers that context.
For processes, it answers the process block itself"
<category: 'accessing'>
| ctx next |
ctx := self.
[next := ctx parentContext.
ctx isEnvironment | next isNil]
whileFalse: [ctx := next].
^ctx
]
initialIP [
"Answer the value of the instruction pointer when execution starts
in the current context"
<category: 'accessing'>
^0
]
isDisabled [
"Answers whether the context is skipped when doing a return. Contexts
are marked as disabled whenever a non-local return is done (either by
returning from the enclosing method of a block, or with the
#continue: method of ContextPart) and there are unwind contexts such
as those created by #ensure:. All non-unwind contexts are then
marked as disabled."
<category: 'accessing'>
self subclassResponsibility
]
isUnwind [
"Answers whether the context must continue execution even after a
non-local return (a return from the enclosing method of a block, or
a call to the #continue: method of ContextPart). Such contexts are
created by #ensure:."
<category: 'accessing'>
self subclassResponsibility
]
isEnvironment [
"To create a valid execution environment for the interpreter even
before it starts, GST creates a fake context which invokes a special
``termination'' method. Such a context can be used as a marker for
the current execution environment. Answer whether the receiver is
that kind of context."
<category: 'accessing'>
self subclassResponsibility
]
isProcess [
"Answer whether the receiver represents a process context, i.e. a context
created by BlockClosure>>#newProcess. Such a context can be recognized
because it has no parent but its flags are different from those of the
contexts created by the VM's prepareExecutionEnvironment function."
<category: 'accessing'>
^self parentContext isNil & self isEnvironment not
]
parentContext [
"Answer the context that called the receiver"
<category: 'accessing'>
^parent
]
parentContext: aContext [
"Set the context to which the receiver will return"
"Fixed typing isn't usually good, but this is too important"
<category: 'accessing'>
aContext class superclass == ContextPart | aContext isNil
ifFalse: [^SystemExceptions.WrongClass signalOn: aContext mustBe: ContextPart].
parent := aContext
]
ip [
"Answer the current instruction pointer into the receiver"
"This funny implementation thwarts the interpreter's optimizing effort"
<category: 'accessing'>
^ip yourself
]
ip: newIP [
"Set the instruction pointer for the receiver"
"Fixed typing isn't usually good, but this is too important"
<category: 'accessing'>
newIP isSmallInteger
ifFalse: [^SystemExceptions.WrongClass signalOn: newIP mustBe: SmallInteger].
ip := newIP
]
size [
"Answer the number of valid fields for the receiver. Any read
access from (self size + 1) to (self basicSize) will give nil."
<category: 'accessing'>
^self sp + 1
]
at: index [
"Answer the index-th slot in the receiver. Any read
access from (self size + 1) to (self basicSize) will give nil."
<category: 'accessing'>
(index > self size and: [ index <= self basicSize ]) ifTrue: [^nil].
^super at: index
]
at: index put: anObject [
"Answer the index-th slot in the receiver. Any write
access from (self size + 1) to (self basicSize) will give an error
unless nil is being written. This is because doing such an access
first, and then updating sp, is racy: the garbage collector may
trigger in the middle and move anObject, but the slot in the
context won't be adjusted."
<category: 'accessing'>
(index > self size and: [ index <= self basicSize ]) ifTrue: [
anObject isNil ifFalse: [
^SystemExceptions.InvalidArgument signalOn: self ] ].
^super at: index put: anObject
]
sp [
"Answer the current stack pointer into the receiver"
"This funny implementation thwarts the interpreter's optimizing effort"
<category: 'accessing'>
^sp yourself
]
validSize [
"Answer how many elements in the receiver should be inspected"
<category: 'accessing'>
^self size
]
numArgs [
"Answer the number of arguments passed to the receiver"
<category: 'accessing'>
^self method numArgs
]
numTemps [
"Answer the number of temporaries used by the receiver"
<category: 'accessing'>
^self method numTemps
]
push: anObject [
"Push an object on the receiver's stack."
"Since the newly accessible slots may have contained garbage, this method
makes them valid *before* they become accessible."
<category: 'accessing'>
self at: sp + 1 put: nil.
sp := sp + 1.
self at: sp put: anObject.
]
sp: newSP [
"Set the stack pointer for the receiver."
"Storing into the stack pointer is a potentially dangerous thing, so
this code tests that sp is effectively a number. Also, since the
newly accessible slots may have contained garbage, this method
stores nil into any cells that become accessible."
<category: 'accessing'>
newSP isSmallInteger
ifFalse: [^SystemExceptions.WrongClass signalOn: newSP mustBe: SmallInteger].
newSP > sp ifTrue: [sp + 1 to: newSP do: [:i | self at: i put: nil]].
sp := newSP
]
method [
"Return the CompiledMethod being executed"
<category: 'accessing'>
^method
]
methodClass [
"Return the class in which the CompiledMethod being executed is defined"
<category: 'accessing'>
^self method methodClass
]
isBlock [
"Answer whether the receiver is a block context"
<category: 'accessing'>
self subclassResponsibility
]
receiver [
"Return the receiver (self) for the method being executed"
"This funny implementation thwarts the interpreter's optimizing effort"
<category: 'accessing'>
^receiver yourself
]
selector [
"Return the selector for the method being executed"
<category: 'accessing'>
^self method selector
]
home [
"Answer the MethodContext to which the receiver refers"
<category: 'accessing'>
self subclassResponsibility
]
isJIT [
<category: 'private'>
^nativeIP ~~ 0
]
deepCopy [
"Answer a copy of the entire stack, but don't copy any
of the other instance variables of the context."
<category: 'copying'>
^self copyStack
]
copyStack [
"Answer a copy of the entire stack."
<category: 'copying'>
| ret ctx |
ret := ctx := self copy.
[ctx parentContext isNil]
whileFalse: [ctx parentContext: (ctx := ctx parentContext copy)].
^ret
]
scanBacktraceForAttribute: selector do: aBlock [
"Scan the backtrace for contexts which have the attribute selector
listed in selectors; if one is found, invoke aBlock passing the
context and the attribute."
<category: 'enumerating'>
| ctx attr |
ctx := self.
[(ctx isBlock not and:
[attr := ctx method attributeAt: selector ifAbsent: [nil].
attr notNil])
ifTrue: [aBlock value: ctx value: attr].
ctx isEnvironment or: [(ctx := ctx parentContext) isNil]] whileFalse
]
scanBacktraceFor: selectors do: aBlock [
"Scan the backtrace for contexts whose selector is among those
listed in selectors; if one is found, invoke aBlock passing the
context."
<category: 'enumerating'>
| ctx |
ctx := self.
[ctx isNil or: [ctx isEnvironment]] whileFalse:
[(ctx isBlock not and: [selectors includes: ctx selector])
ifTrue: [aBlock value: ctx].
ctx := ctx parentContext]
]
securityCheckForName: name [
<category: 'security checks'>
self isUntrusted ifFalse: [^self].
^self
doSecurityCheckForName: name
actions: #()
target: nil
]
securityCheckForName: name action: action [
<category: 'security checks'>
self isUntrusted ifFalse: [^self].
^self
doSecurityCheckForName: name
actions: {action}
target: nil
]
securityCheckForName: name target: target [
<category: 'security checks'>
self isUntrusted ifFalse: [^self].
^self
doSecurityCheckForName: name
actions: #()
target: target
]
securityCheckForName: name actions: actions target: target [
<category: 'security checks'>
self isUntrusted ifFalse: [^self].
^self
doSecurityCheckForName: name
actions: actions
target: target
]
doSecurityCheckForName: name actions: actions target: target [
<category: 'security checks'>
| perm ctx |
perm := (Permission new)
name: name actions: actions;
target: target.
(self checkSecurityFor: perm) ifFalse: [(SecurityError for: perm) signal]
]
checkSecurityFor: perm [
<category: 'security checks'>
"First of all, check against the static permissions for this
context."
| state foundAnnotation |
(self receiver class check: perm) ifFalse: [^false].
"Then, check the dynamic permissions. So:
1) check if a method was specifically denying access,
2) look for a deeper context whose static permissions
denies access, but stop if a method is specifically
granting access."
self method isAnnotated
ifTrue:
[foundAnnotation := false.
self method attributesDo:
[:each |
| newPerm |
each selector = #permission:
ifTrue:
[newPerm := each arguments at: 1.
(newPerm implies: perm)
ifTrue:
["Should we check if the granted permission is statically
available? Of course, you can only grant permissions if you
own them statically, so the real question is, should we
check perm or newPerm? The answer is perm (which has
already been found to be available), hence we can skip
an expensive static permission check. Suppose we have a
method that grants access to all files: it makes more sense
if it means ``grant access to all files allowed by the class
security policy'', rather than ``grant access to all files if
the security policy allows it, else do not grant access to
any file''."
foundAnnotation := true.
state := newPerm isAllowing]]].
foundAnnotation ifTrue: [^state]].
"Nope, no special regulations were found in this method. Look in the
parent context, and grant permission if the bottom is reached."
^self parentContext isNil or: [self parentContext checkSecurityFor: perm]
]
continue: anObject [
"Resume execution from the receiver, faking that the context on
top of it in the execution chain has returned anObject. The
receiver must belong to the same process as the executing context,
otherwise the results are not predictable. All #ensure: (and
possibly #ifCurtailed:) blocks between the currently executing
context and the receiver are evaluated (which is not what
would happen if you directly bashed at the parent context of
thisContext)."
<category: 'built ins'>
<primitive: VMpr_ContextPart_continue>
self badReturnError
]
]
|