Skip to content

Commit a800ca8

Browse files
authored
Merge pull request #1741 from dbwiddis/fix-kstatctl-mapping
Map KstatCtl.kc_chain as a pointer
2 parents d036ad9 + d62bc2f commit a800ca8

2 files changed

Lines changed: 30 additions & 7 deletions

File tree

‎CHANGES.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ Features
1010

1111
Bug Fixes
1212
---------
13+
* [#1740](https://github.com/java-native-access/jna/issues/1740): Fix `c.s.j.p.unix.solaris.LibKstat.KstatCtl` mapping `kc_chain` as an inline `Kstat` instead of a pointer, which made every call taking a `KstatCtl` write past the end of the structure `kstat_open()` allocated, and map the `Kstat` function pointers `ks_update` and `ks_snapshot` as `Pointer` instead of `int` - [@dbwiddis](https://github.com/dbwiddis).
1314

15+
Breaking Changes
16+
----------------
17+
* `c.s.j.p.unix.solaris.LibKstat.KstatCtl#kc_chain` is now a `Pointer` instead of a `Kstat`. Use `KstatCtl#chain()` to get the head of the kstat chain as a `Kstat`. The previous mapping never returned usable data: the inline `Kstat` decoded the native chain pointer as its `ks_crtime` timestamp, and every following field from bytes past the end of the native structure, so code that read this field could not have relied on it.
18+
* `c.s.j.p.unix.solaris.LibKstat.Kstat#ks_update` and `Kstat#ks_snapshot` are now `Pointer` instead of `int`, matching the native function pointers. The structure layout is unchanged, but the previous mapping held only half of a kernel-only address, which has no meaning in user space.
1419

1520
Release 5.19.1
1621
==============

‎contrib/platform/src/com/sun/jna/platform/unix/solaris/LibKstat.java‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,22 +132,28 @@ class Kstat extends Structure {
132132
public long ks_snaptime; // time of last data snapshot
133133

134134
// Fields relevant to kernel only
135-
public int ks_update; // dynamic update function
135+
public Pointer ks_update; // dynamic update function
136136

137137
public Pointer ks_private; // provider-private data
138138

139-
public int ks_snapshot; // snapshot function
139+
public Pointer ks_snapshot; // snapshot function
140140

141141
public Pointer ks_lock; // protects this kstat's data
142142

143+
public Kstat() {
144+
super();
145+
}
146+
147+
public Kstat(Pointer p) {
148+
super(p);
149+
read();
150+
}
151+
143152
public Kstat next() {
144153
if (ks_next == null) {
145154
return null;
146155
}
147-
Kstat n = new Kstat();
148-
n.useMemory(ks_next);
149-
n.read();
150-
return n;
156+
return new Kstat(ks_next);
151157
}
152158
}
153159

@@ -350,16 +356,28 @@ public KstatIO(Pointer p) {
350356
/**
351357
* A kstat control structure. Only one thread may actively use a KstatCtl
352358
* value at any time. Synchronization is left to the application.
359+
* <p>
360+
* Oracle Solaris 11.4 appends a fourth field, {@code void **kc_private},
361+
* which is private to libkstat. It is deliberately not mapped: this
362+
* structure only ever wraps the pointer returned by {@code kstat_open()},
363+
* so mapping the three public fields reads and writes a prefix of the
364+
* native structure on every release, and leaves the private field alone.
353365
*/
354366
@FieldOrder({"kc_chain_id", "kc_chain", "kc_kd"})
355367
class KstatCtl extends Structure {
356368

357369
public int kc_chain_id; // current kstat chain ID
358370

359-
public Kstat kc_chain; // pointer to kstat chain
371+
public Pointer kc_chain; // pointer to kstat chain
360372

361373
public int kc_kd; // /dev/kstat descriptor - not public interface
362374

375+
public Kstat chain() {
376+
if (kc_chain == null) {
377+
return null;
378+
}
379+
return new Kstat(kc_chain);
380+
}
363381
}
364382

365383
/**

0 commit comments

Comments
 (0)